summaryrefslogtreecommitdiffstats
path: root/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main
diff options
context:
space:
mode:
authorJorge Hernandez <jorge.hernandez-herrero@att.com>2018-12-06 14:42:18 +0000
committerGerrit Code Review <gerrit@onap.org>2018-12-06 14:42:18 +0000
commitcbf3ade089e2cb866758efcb5fb9a3027b331700 (patch)
treef6fbd54db361656000977a8c6b2e7a766e8f1ad4 /plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main
parent259039fa91e3038c41f4a322d81e7ea07a78b96e (diff)
parentc01eff1ee4aff0cc4b177a0bab5c6b96a9efb647 (diff)
Merge "Support HTTP headers in REST Requestor"
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main')
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java22
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java2
-rw-r--r--plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java117
3 files changed, 123 insertions, 18 deletions
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java
index 7d6c60ca1..746fc85c4 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java
@@ -22,6 +22,7 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
@@ -144,11 +145,17 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable {
throw new ApexEventException(errorMessage, e);
}
- // Set the peer timeout to the default value if its not set
+ // Set the requestor timeout
if (consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR) != 0) {
restRequestTimeout = consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR);
}
+ // Check if HTTP headers has been set
+ if (restConsumerProperties.checkHttpHeadersSet()) {
+ LOGGER.debug("REST Requestor consumer has http headers ({}): {}", this.name,
+ Arrays.deepToString(restConsumerProperties.getHttpHeaders()));
+ }
+
// Initialize the HTTP client
client = ClientBuilder.newClient();
}
@@ -163,10 +170,10 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable {
// Push the event onto the queue for handling
try {
incomingRestRequestQueue.add(restRequest);
- } catch (final Exception e) {
+ } catch (final Exception requestException) {
final String errorMessage = "could not queue request \"" + restRequest + "\" on REST Requestor consumer ("
+ this.name + ")";
- LOGGER.warn(errorMessage, e);
+ LOGGER.warn(errorMessage, requestException);
throw new ApexEventRuntimeException(errorMessage);
}
}
@@ -386,23 +393,28 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable {
/**
* Execute the REST request.
*
+ *
* @return the response to the REST request
*/
public Response sendEventAsRestRequest() {
switch (restConsumerProperties.getHttpMethod()) {
case GET:
- return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON).get();
+ return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON)
+ .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).get();
case PUT:
return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON)
+ .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap())
.put(Entity.json(request.getEvent()));
case POST:
return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON)
+ .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap())
.post(Entity.json(request.getEvent()));
case DELETE:
- return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON).delete();
+ return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON)
+ .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).delete();
default:
break;
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java
index 69ad05b27..4985fe16f 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java
@@ -70,7 +70,7 @@ public class ApexRestRequestorProducer implements ApexEventProducer {
if (!(producerParameters
.getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
final String errorMessage =
- "specified consumer properties are not applicable to REST requestor producer (" + this.name + ")";
+ "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java
index acd5e52e8..d8009eeef 100644
--- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java
+++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java
@@ -20,23 +20,34 @@
package org.onap.policy.apex.plugins.event.carrier.restrequestor;
+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;
+// @formatter:off
/**
* Apex parameters for REST as an event carrier technology with Apex issuing a REST request and receiving a REST
* response.
*
* <p>The parameters for this plugin are:
* <ol>
- * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending. This parameter is
- * mandatory.
- * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default), POST, PUT, and
- * DELETE.
+ * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending.
+ * This parameter is mandatory.
+ * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default),
+ * POST, PUT, and DELETE.
* <li>restRequestTimeout: The time in milliseconds to wait for a REST request to complete.
+ * <li>restRequestHeader: The necessary header needed
* </ol>
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
+//@formatter:on
public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyParameters {
/** The supported HTTP methods. */
public enum HttpMethod {
@@ -47,12 +58,12 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP
public static final String RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL = "RESTREQUESTOR";
/** The producer plugin class for the REST carrier technology. */
- public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS =
- ApexRestRequestorProducer.class.getCanonicalName();
+ public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestRequestorProducer.class
+ .getCanonicalName();
/** The consumer plugin class for the REST carrier technology. */
- public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS =
- ApexRestRequestorConsumer.class.getCanonicalName();
+ public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestRequestorConsumer.class
+ .getCanonicalName();
/** The default HTTP method for request events. */
public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET;
@@ -60,12 +71,16 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP
/** The default timeout for REST requests. */
public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500;
+ // Commonly occurring strings
+ private static final String HTTP_HEADERS = "httpHeaders";
+
private String url = 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
- * service.
+ * Constructor to create a REST carrier technology parameters instance and regiaaaster the instance with the
+ * parameter service.
*/
public RestRequestorCarrierTechnologyParameters() {
super();
@@ -112,14 +127,92 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP
this.httpMethod = httpMethod;
}
+ /**
+ * Check if http headers have been set for the REST request.
+ *
+ * @return true if headers have beenset
+ */
+ public boolean checkHttpHeadersSet() {
+ return httpHeaders != null && httpHeaders.length > 0;
+ }
+
+ /**
+ * Gets the http headers for the REST request.
+ *
+ * @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() {
+ final GroupValidationResult result = super.validate();
+
+ 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;
+ }
+
/*
* (non-Javadoc)
*
- * @see java.lang.Object#toString()
+ * @see new LinkedHashMap<>()java.lang.Object#toString()
*/
@Override
public String toString() {
- return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + "]";
+ return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
+ + Arrays.deepToString(httpHeaders) + "]";
}
}