2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.parameters.carriertechnology;
24 import java.util.Arrays;
25 import java.util.HashSet;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.regex.PatternSyntaxException;
30 import javax.ws.rs.core.MultivaluedHashMap;
31 import javax.ws.rs.core.MultivaluedMap;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.policy.common.parameters.GroupValidationResult;
36 import org.onap.policy.common.parameters.ValidationStatus;
37 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
43 * Apex plugin parameters for REST as an event carrier technology with Apex.
45 * <p>The parameters for this plugin are:
47 * <li>url: The URL that the Apex Rest client will connect to over REST for event reception or event sending. This
48 * parameter is mandatory.
49 * <li>httpMethod: The HTTP method to use when sending events over REST, legal values are POST (default) and PUT. When
50 * receiving events, the REST client plugin always uses the HTTP GET method.
51 * <li>httpHeaders, the HTTP headers to send on REST requests, optional parameter, defaults to none.
52 * <li>httpCodeFilter: a regular expression filter for returned HTTP codes, if the returned HTTP code passes this
53 * filter, then the request is assumed to have succeeded by the plugin, optional, defaults to allowing 2xx codes
54 * through, that is a regular expression of "[2][0-9][0-9]"
57 * @author Ning Xi(ning.xi@ericsson.com)
62 public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyParameters {
63 // Get a reference to the logger
64 private static final Logger LOGGER = LoggerFactory.getLogger(RestPluginCarrierTechnologyParameters.class);
66 /** The supported HTTP methods. */
67 public enum HttpMethod {
74 /** The default HTTP code filter, allows 2xx HTTP codes through. */
75 public static final String DEFAULT_HTTP_CODE_FILTER = "[2][0-9][0-9]";
77 // Commonly occurring strings
78 private static final String HTTP_HEADERS = "httpHeaders";
79 private static final String HTTP_CODE_FILTER = "httpCodeFilter";
81 // Regular expression patterns for finding and checking keys in URLs
82 private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})");
83 protected static final Pattern patternErrorKey =
84 Pattern.compile("(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}");
87 protected String url = null;
88 protected HttpMethod httpMethod = null;
89 protected String[][] httpHeaders = null;
90 protected String httpCodeFilter = DEFAULT_HTTP_CODE_FILTER;
93 * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
96 public RestPluginCarrierTechnologyParameters() {
101 * Check if http headers have been set for the REST request.
103 * @return true if headers have been set
105 public boolean checkHttpHeadersSet() {
106 return httpHeaders != null && httpHeaders.length > 0;
110 * Gets the http headers for the REST request as a multivalued map.
112 * @return the headers
114 public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
115 if (httpHeaders == null) {
119 // Load the HTTP headers into the map
120 MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
122 for (String[] httpHeader : httpHeaders) {
123 httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
126 return httpHeaderMap;
130 * Sets the header for the REST request.
132 * @param httpHeaders the incoming HTTP headers
134 public void setHttpHeaders(final String[][] httpHeaders) {
135 this.httpHeaders = httpHeaders;
139 * Get the tag for the REST Producer Properties.
141 * @return set of the tags
143 public Set<String> getKeysFromUrl() {
144 Matcher matcher = patternProperKey.matcher(getUrl());
145 Set<String> key = new HashSet<>();
146 while (matcher.find()) {
147 key.add(matcher.group());
156 public GroupValidationResult validate() {
157 GroupValidationResult result = super.validate();
160 validateHttpHeaders(result);
162 return validateHttpCodeFilter(result);
170 * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$
171 * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{
172 * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.}
173 * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*}
174 * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*}
175 * @param result the result of the validation
178 public GroupValidationResult validateUrl(final GroupValidationResult result) {
179 // Check if the URL has been set for event output
180 String urlErrorMessage = "no URL has been set for event sending on " + getLabel();
181 if (getUrl() == null) {
182 result.setResult("url", ValidationStatus.INVALID, urlErrorMessage);
186 Matcher matcher = patternErrorKey.matcher(getUrl());
187 if (matcher.find()) {
188 result.setResult("url", ValidationStatus.INVALID, urlErrorMessage);
195 * Validate the HTTP headers.
197 * @param result the result of the validation
199 private GroupValidationResult validateHttpHeaders(final GroupValidationResult result) {
200 if (httpHeaders == null) {
204 for (String[] httpHeader : httpHeaders) {
205 if (httpHeader == null) {
206 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
207 } else if (httpHeader.length != 2) {
208 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
209 "HTTP header array entries must have one key and one value: "
210 + Arrays.deepToString(httpHeader));
211 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
212 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
213 "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
214 } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
215 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
216 "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
224 * Validate the HTTP code filter.
226 * @param result the result of the validation
228 public GroupValidationResult validateHttpCodeFilter(final GroupValidationResult result) {
229 if (httpCodeFilter == null) {
230 httpCodeFilter = DEFAULT_HTTP_CODE_FILTER;
232 } else if (StringUtils.isBlank(httpCodeFilter)) {
233 result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID,
234 "HTTP code filter must be specified as a three digit regular expression");
237 Pattern.compile(httpCodeFilter);
238 } catch (PatternSyntaxException pse) {
240 "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: "
242 result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, message);
243 LOGGER.debug(message, pse);
254 public String toString() {
255 return getLabel() + "CarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
256 + Arrays.deepToString(httpHeaders) + ", httpCodeFilter=" + httpCodeFilter + "]";