2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
23 import java.util.Arrays;
25 import javax.ws.rs.core.MultivaluedHashMap;
26 import javax.ws.rs.core.MultivaluedMap;
28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.parameters.ValidationStatus;
31 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
35 * Apex parameters for REST as an event carrier technology with Apex issuing a REST request and receiving a REST
38 * <p>The parameters for this plugin are:
40 * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending.
41 * This parameter is mandatory.
42 * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default),
43 * POST, PUT, and DELETE.
44 * <li>restRequestTimeout: The time in milliseconds to wait for a REST request to complete.
45 * <li>restRequestHeader: The necessary header needed
48 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyParameters {
52 /** The supported HTTP methods. */
53 public enum HttpMethod {
54 GET, PUT, POST, DELETE
57 /** The label of this carrier technology. */
58 public static final String RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL = "RESTREQUESTOR";
60 /** The producer plugin class for the REST carrier technology. */
61 public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestRequestorProducer.class
64 /** The consumer plugin class for the REST carrier technology. */
65 public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestRequestorConsumer.class
68 /** The default HTTP method for request events. */
69 public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET;
71 /** The default timeout for REST requests. */
72 public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500;
74 // Commonly occurring strings
75 private static final String HTTP_HEADERS = "httpHeaders";
77 private String url = null;
78 private HttpMethod httpMethod = null;
79 private String[][] httpHeaders = null;
82 * Constructor to create a REST carrier technology parameters instance and regiaaaster the instance with the
85 public RestRequestorCarrierTechnologyParameters() {
88 // Set the carrier technology properties for the web socket carrier technology
89 this.setLabel(RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL);
90 this.setEventProducerPluginClass(RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS);
91 this.setEventConsumerPluginClass(RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS);
95 * Gets the URL for the REST request.
99 public String getUrl() {
104 * Sets the URL for the REST request.
106 * @param incomingUrl the URL
108 public void setUrl(final String incomingUrl) {
109 this.url = incomingUrl;
113 * Gets the HTTP method to use for the REST request.
115 * @return the HTTP method
117 public HttpMethod getHttpMethod() {
122 * Sets the HTTP method to use for the REST request.
124 * @param httpMethod the HTTP method
126 public void setHttpMethod(final HttpMethod httpMethod) {
127 this.httpMethod = httpMethod;
131 * Check if http headers have been set for the REST request.
133 * @return true if headers have beenset
135 public boolean checkHttpHeadersSet() {
136 return httpHeaders != null && httpHeaders.length > 0;
140 * Gets the http headers for the REST request.
142 * @return the headers
144 public String[][] getHttpHeaders() {
149 * Gets the http headers for the REST request as a multivalued map.
151 * @return the headers
153 public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
154 if (httpHeaders == null) {
158 // Load the HTTP headers into the map
159 MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
161 for (String[] httpHeader : httpHeaders) {
162 httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
165 return httpHeaderMap;
169 * Sets the header for the REST request.
171 * @param httpHeaders the incoming HTTP headers
173 public void setHttpHeaders(final String[][] httpHeaders) {
174 this.httpHeaders = httpHeaders;
181 public GroupValidationResult validate() {
182 final GroupValidationResult result = super.validate();
184 if (httpHeaders == null) {
188 for (String[] httpHeader : httpHeaders) {
189 if (httpHeader == null) {
190 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
191 } else if (httpHeader.length != 2) {
192 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
193 "HTTP header array entries must have one key and one value: "
194 + Arrays.deepToString(httpHeader));
195 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
196 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
197 "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
198 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
199 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
200 "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
211 public String toString() {
212 return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
213 + Arrays.deepToString(httpHeaders) + "]";