aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main')
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java32
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java42
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java105
3 files changed, 134 insertions, 45 deletions
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java
index 13edea8a6..04397d688 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java
@@ -72,30 +72,29 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable {
@Override
public void init(final String consumerName, final EventHandlerParameters consumerParameters,
- final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
+ final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
this.eventReceiver = incomingEventReceiver;
this.name = consumerName;
// Check and get the REST Properties
if (!(consumerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) {
final String errorMessage = "specified consumer properties are not applicable to REST client consumer ("
- + this.name + ")";
+ + this.name + ")";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
restConsumerProperties = (RestClientCarrierTechnologyParameters) consumerParameters
- .getCarrierTechnologyParameters();
+ .getCarrierTechnologyParameters();
// Check if the HTTP method has been set
if (restConsumerProperties.getHttpMethod() == null) {
- restConsumerProperties.setHttpMethod(RestClientCarrierTechnologyParameters.CONSUMER_HTTP_METHOD);
+ restConsumerProperties.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.GET);
}
- if (!restConsumerProperties.getHttpMethod()
- .equalsIgnoreCase(RestClientCarrierTechnologyParameters.CONSUMER_HTTP_METHOD)) {
+ if (!RestClientCarrierTechnologyParameters.HttpMethod.GET.equals(restConsumerProperties.getHttpMethod())) {
final String errorMessage = "specified HTTP method of \"" + restConsumerProperties.getHttpMethod()
- + "\" is invalid, only HTTP method \"GET\" "
- + "is supported for event reception on REST client consumer (" + this.name + ")";
+ + "\" is invalid, only HTTP method \"GET\" "
+ + "is supported for event reception on REST client consumer (" + this.name + ")";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
@@ -203,13 +202,13 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable {
public void run() {
try {
final Response response = client.target(restConsumerProperties.getUrl()).request("application/json")
- .get();
+ .get();
// Check that the event request worked
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
final String errorMessage = "reception of event from URL \"" + restConsumerProperties.getUrl()
- + "\" failed with status code " + response.getStatus() + " and message \""
- + response.readEntity(String.class) + "\"";
+ + "\" failed with status code " + response.getStatus() + " and message \""
+ + response.readEntity(String.class) + "\"";
throw new ApexEventRuntimeException(errorMessage);
}
@@ -219,7 +218,7 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable {
// Check there is content
if (eventJsonString == null || eventJsonString.trim().length() == 0) {
final String errorMessage = "received an empty event from URL \"" + restConsumerProperties.getUrl()
- + "\"";
+ + "\"";
throw new ApexEventRuntimeException(errorMessage);
}
@@ -230,4 +229,13 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable {
}
}
}
+
+ /**
+ * Hook for unit test mocking of HTTP client.
+ *
+ * @param client the mocked client
+ */
+ protected void setClient(final Client client) {
+ this.client = client;
+ }
}
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java
index 5556d0310..a7e0ddd52 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java
@@ -67,29 +67,29 @@ public class ApexRestClientProducer implements ApexEventProducer {
*/
@Override
public void init(final String producerName, final EventHandlerParameters producerParameters)
- throws ApexEventException {
+ throws ApexEventException {
this.name = producerName;
// Check and get the REST Properties
if (!(producerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) {
- final String errorMessage = "specified consumer properties are not applicable to REST client producer ("
- + this.name + ")";
+ final String errorMessage = "specified producer properties are not applicable to REST client producer ("
+ + this.name + ")";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
restProducerProperties = (RestClientCarrierTechnologyParameters) producerParameters
- .getCarrierTechnologyParameters();
+ .getCarrierTechnologyParameters();
// Check if the HTTP method has been set
if (restProducerProperties.getHttpMethod() == null) {
- restProducerProperties.setHttpMethod(RestClientCarrierTechnologyParameters.DEFAULT_PRODUCER_HTTP_METHOD);
+ restProducerProperties.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
}
- if (!"POST".equalsIgnoreCase(restProducerProperties.getHttpMethod())
- && !"PUT".equalsIgnoreCase(restProducerProperties.getHttpMethod())) {
+ if (!RestClientCarrierTechnologyParameters.HttpMethod.POST.equals(restProducerProperties.getHttpMethod())
+ && !RestClientCarrierTechnologyParameters.HttpMethod.PUT.equals(restProducerProperties.getHttpMethod())) {
final String errorMessage = "specified HTTP method of \"" + restProducerProperties.getHttpMethod()
- + "\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supproted "
- + "for event sending on REST client producer (" + this.name + ")";
+ + "\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supproted "
+ + "for event sending on REST client producer (" + this.name + ")";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
@@ -140,7 +140,7 @@ public class ApexRestClientProducer implements ApexEventProducer {
public void sendEvent(final long executionId, final String eventName, final Object event) {
// Check if this is a synchronized event, if so we have received a reply
final SynchronousEventCache synchronousEventCache = (SynchronousEventCache) peerReferenceMap
- .get(EventHandlerPeeredMode.SYNCHRONOUS);
+ .get(EventHandlerPeeredMode.SYNCHRONOUS);
if (synchronousEventCache != null) {
synchronousEventCache.removeCachedEventToApexIfExists(executionId);
}
@@ -151,16 +151,15 @@ public class ApexRestClientProducer implements ApexEventProducer {
// Check that the request worked
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" using HTTP \""
- + restProducerProperties.getHttpMethod() + "\" failed with status code "
- + response.getStatus() + " and message \"" + response.readEntity(String.class)
- + "\", event:\n" + event;
+ + restProducerProperties.getHttpMethod() + "\" failed with status code " + response.getStatus()
+ + " and message \"" + response.readEntity(String.class) + "\", event:\n" + event;
LOGGER.warn(errorMessage);
throw new ApexEventRuntimeException(errorMessage);
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("event sent from engine using {} to URL {} with HTTP {} : {} and response {} ", this.name,
- restProducerProperties.getUrl(), restProducerProperties.getHttpMethod(), event, response);
+ restProducerProperties.getUrl(), restProducerProperties.getHttpMethod(), event, response);
}
}
@@ -179,14 +178,23 @@ public class ApexRestClientProducer implements ApexEventProducer {
* Send the event as a JSON string as a REST request.
*
* @param event the event to send
- * @return the response tot he JSON request
+ * @return the response to the JSON request
*/
- public Response sendEventAsRestRequest(final String event) {
+ private Response sendEventAsRestRequest(final String event) {
// We have already checked that it is a PUT or POST request
- if ("POST".equalsIgnoreCase(restProducerProperties.getHttpMethod())) {
+ if (RestClientCarrierTechnologyParameters.HttpMethod.POST.equals(restProducerProperties.getHttpMethod())) {
return client.target(restProducerProperties.getUrl()).request("application/json").post(Entity.json(event));
} else {
return client.target(restProducerProperties.getUrl()).request("application/json").put(Entity.json(event));
}
}
+
+ /**
+ * Hook for unit test mocking of HTTP client.
+ *
+ * @param client the mocked client
+ */
+ protected void setClient(final Client client) {
+ this.client = client;
+ }
}
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java
index 86c8bb4cf..0329d4440 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java
@@ -20,9 +20,15 @@
package org.onap.policy.apex.plugins.event.carrier.restclient;
+import java.util.Arrays;
+
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+
import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
import org.onap.policy.common.parameters.GroupValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.utils.validation.ParameterValidationUtils;
/**
* Apex parameters for REST as an event carrier technology with Apex as a REST client.
@@ -38,6 +44,10 @@ import org.onap.policy.common.parameters.ValidationStatus;
* @author Joss Armstrong (joss.armstrong@ericsson.com)
*/
public class RestClientCarrierTechnologyParameters extends CarrierTechnologyParameters {
+ /** The supported HTTP methods. */
+ public enum HttpMethod {
+ GET, PUT, POST, DELETE
+ }
/** The label of this carrier technology. */
public static final String RESTCLIENT_CARRIER_TECHNOLOGY_LABEL = "RESTCLIENT";
@@ -48,14 +58,12 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara
/** The consumer plugin class for the REST carrier technology. */
public static final String RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestClientConsumer.class.getCanonicalName();
- /** The default HTTP method for output of events. */
- public static final String DEFAULT_PRODUCER_HTTP_METHOD = "POST";
-
- /** The HTTP method for input of events. */
- public static final String CONSUMER_HTTP_METHOD = "GET";
+ // Commonly occurring strings
+ private static final String HTTP_HEADERS = "httpHeaders";
private String url = null;
- private String httpMethod = null;
+ private HttpMethod httpMethod = null;
+ private String[][] httpHeaders = null;
/**
* Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
@@ -94,7 +102,7 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara
*
* @return the HTTP method
*/
- public String getHttpMethod() {
+ public HttpMethod getHttpMethod() {
return httpMethod;
}
@@ -103,23 +111,59 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara
*
* @param httpMethod the HTTP method
*/
- public void setHttpMethod(final String httpMethod) {
+ public void setHttpMethod(final HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
- /*
- * (non-Javadoc)
+ /**
+ * Check if http headers have been set for the REST request.
*
- * @see java.lang.Object#toString()
+ * @return true if headers have beenset
*/
- @Override
- public String toString() {
- return "RESTClientCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + "]";
+ public boolean checkHttpHeadersSet() {
+ return httpHeaders != null && httpHeaders.length > 0;
}
- /*
+ /**
+ * Gets the http headers for the REST request.
*
- * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
+ * @return the headers
+ */
+ public String[][] getHttpHeaders() {
+ return httpHeaders;
+ }
+
+ /**
+ * Gets the http headers for the REST request as a multivalued map.
+ *
+ * @return the headers
+ */
+ public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
+ if (httpHeaders == null) {
+ return null;
+ }
+
+ // Load the HTTP headers into the map
+ MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
+
+ for (String[] httpHeader : httpHeaders) {
+ httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
+ }
+
+ return httpHeaderMap;
+ }
+
+ /**
+ * Sets the header for the REST request.
+ *
+ * @param httpHeaders the incoming HTTP headers
+ */
+ public void setHttpHeaders(final String[][] httpHeaders) {
+ this.httpHeaders = httpHeaders;
+ }
+
+ /**
+ * {@inheritDoc}
*/
@Override
public GroupValidationResult validate() {
@@ -130,6 +174,35 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara
result.setResult("url", ValidationStatus.INVALID, "no URL has been set for event sending on REST client");
}
+ if (httpHeaders == null) {
+ return result;
+ }
+
+ for (String[] httpHeader : httpHeaders) {
+ if (httpHeader == null) {
+ result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
+ } else if (httpHeader.length != 2) {
+ result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
+ "HTTP header array entries must have one key and one value: "
+ + Arrays.deepToString(httpHeader));
+ } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
+ result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
+ "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
+ } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
+ result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
+ "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
+ }
+ }
+
return result;
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ return "RestClientCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
+ + Arrays.deepToString(httpHeaders) + "]";
+ }
}