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.restclient;
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;
34 * Apex parameters for REST as an event carrier technology with Apex as a REST client.
36 * <p>The parameters for this plugin are:
38 * <li>url: The URL that the Apex Rest client will connect to over REST for event reception or event sending. This
39 * parameter is mandatory.
40 * <li>httpMethod: The HTTP method to use when sending events over REST, legal values are POST (default) and PUT. When
41 * receiving events, the REST client plugin always uses the HTTP GET method.
44 * @author Joss Armstrong (joss.armstrong@ericsson.com)
46 public class RestClientCarrierTechnologyParameters extends CarrierTechnologyParameters {
47 /** The supported HTTP methods. */
48 public enum HttpMethod {
49 GET, PUT, POST, DELETE
52 /** The label of this carrier technology. */
53 public static final String RESTCLIENT_CARRIER_TECHNOLOGY_LABEL = "RESTCLIENT";
55 /** The producer plugin class for the REST carrier technology. */
56 public static final String RESTCLIENT_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestClientProducer.class.getName();
58 /** The consumer plugin class for the REST carrier technology. */
59 public static final String RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestClientConsumer.class.getName();
61 // Commonly occurring strings
62 private static final String HTTP_HEADERS = "httpHeaders";
64 private String url = null;
65 private HttpMethod httpMethod = null;
66 private String[][] httpHeaders = null;
69 * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
72 public RestClientCarrierTechnologyParameters() {
75 // Set the carrier technology properties for the web socket carrier technology
76 this.setLabel(RESTCLIENT_CARRIER_TECHNOLOGY_LABEL);
77 this.setEventProducerPluginClass(RESTCLIENT_EVENT_PRODUCER_PLUGIN_CLASS);
78 this.setEventConsumerPluginClass(RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS);
83 * Gets the URL for the REST request.
87 public String getUrl() {
92 * Sets the URL for the REST request.
94 * @param incomingUrl the URL
96 public void setUrl(final String incomingUrl) {
97 this.url = incomingUrl;
101 * Gets the HTTP method to use for the REST request.
103 * @return the HTTP method
105 public HttpMethod getHttpMethod() {
110 * Sets the HTTP method to use for the REST request.
112 * @param httpMethod the HTTP method
114 public void setHttpMethod(final HttpMethod httpMethod) {
115 this.httpMethod = httpMethod;
119 * Check if http headers have been set for the REST request.
121 * @return true if headers have been set
123 public boolean checkHttpHeadersSet() {
124 return httpHeaders != null && httpHeaders.length > 0;
128 * Gets the http headers for the REST request.
130 * @return the headers
132 public String[][] getHttpHeaders() {
137 * Gets the http headers for the REST request as a multivalued map.
139 * @return the headers
141 public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
142 if (httpHeaders == null) {
146 // Load the HTTP headers into the map
147 MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
149 for (String[] httpHeader : httpHeaders) {
150 httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
153 return httpHeaderMap;
157 * Sets the header for the REST request.
159 * @param httpHeaders the incoming HTTP headers
161 public void setHttpHeaders(final String[][] httpHeaders) {
162 this.httpHeaders = httpHeaders;
169 public GroupValidationResult validate() {
170 final GroupValidationResult result = super.validate();
172 // Check if the URL has been set for event output
173 if (getUrl() == null) {
174 result.setResult("url", ValidationStatus.INVALID, "no URL has been set for event sending on REST client");
177 if (httpHeaders == null) {
181 for (String[] httpHeader : httpHeaders) {
182 if (httpHeader == null) {
183 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
184 } else if (httpHeader.length != 2) {
185 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
186 "HTTP header array entries must have one key and one value: "
187 + Arrays.deepToString(httpHeader));
188 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
189 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
190 "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
191 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
192 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
193 "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
204 public String toString() {
205 return "RestClientCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
206 + Arrays.deepToString(httpHeaders) + "]";