diff options
author | Jim Hahn <jrh3@att.com> | 2019-07-11 12:24:21 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-07-11 12:24:21 +0000 |
commit | 45d7389dacffb824b66e8a7c43583dc0212cb7d5 (patch) | |
tree | 7f54b01283dbdfe7e604f7c29bf7cde8f3eb6698 /plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main | |
parent | adf67497761295115dc75b525500d687518fc4fd (diff) | |
parent | 99913f91b06abedd84e3837f836461cf8a14c0d7 (diff) |
Merge "Make URL configurable in REST Requestor/Client"
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main')
2 files changed, 72 insertions, 9 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/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 6739d82a9..822ac46a2 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,9 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import java.util.EnumMap; import java.util.Map; import java.util.Properties; +import java.util.Set; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -36,6 +40,8 @@ import org.onap.policy.apex.service.engine.event.PeeredReference; import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,12 +140,28 @@ public class ApexRestClientProducer implements ApexEventProducer { synchronousEventCache.removeCachedEventToApexIfExists(executionId); } + String untaggedUrl = restProducerProperties.getUrl(); + if (executionProperties != null) { + Set<String> names = restProducerProperties.getKeysFromUrl(); + Set<String> inputProperty = executionProperties.stringPropertyNames(); + + names.stream().map(key -> Optional.of(key)).forEach(op -> { + op.filter(str -> inputProperty.contains(str)) + .orElseThrow(() -> new ApexEventRuntimeException( + "key\"" + op.get() + "\"specified on url \"" + restProducerProperties.getUrl() + + "\"not found in execution properties passed by the current policy")); + }); + + untaggedUrl = names.stream().reduce(untaggedUrl, + (acc, str) -> acc.replace("{" + str + "}", (String) executionProperties.get(str))); + } + // Send the event as a REST request - final Response response = sendEventAsRestRequest((String) event); + final Response response = sendEventAsRestRequest(untaggedUrl, (String) event); // Check that the request worked if (response.getStatus() != Response.Status.OK.getStatusCode()) { - final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" using HTTP \"" + final String errorMessage = "send of event to URL \"" + untaggedUrl + "\" using HTTP \"" + restProducerProperties.getHttpMethod() + "\" failed with status code " + response.getStatus() + " and message \"" + response.readEntity(String.class) + "\", event:\n" + event; LOGGER.warn(errorMessage); @@ -148,7 +170,7 @@ public class ApexRestClientProducer implements ApexEventProducer { if (LOGGER.isTraceEnabled()) { LOGGER.trace("event sent from engine using {} to URL {} with HTTP {} : {} and response {} ", this.name, - restProducerProperties.getUrl(), restProducerProperties.getHttpMethod(), event, response); + untaggedUrl, restProducerProperties.getHttpMethod(), event, response); } } @@ -167,13 +189,13 @@ public class ApexRestClientProducer implements ApexEventProducer { * @param event the event to send * @return the response to the JSON request */ - private Response sendEventAsRestRequest(final String event) { + private Response sendEventAsRestRequest(final String untaggedUrl, final String event) { // We have already checked that it is a PUT or POST request if (RestClientCarrierTechnologyParameters.HttpMethod.POST.equals(restProducerProperties.getHttpMethod())) { - return client.target(restProducerProperties.getUrl()).request("application/json") + return client.target(untaggedUrl).request("application/json") .headers(restProducerProperties.getHttpHeadersAsMultivaluedMap()).post(Entity.json(event)); } else { - return client.target(restProducerProperties.getUrl()).request("application/json") + return client.target(untaggedUrl).request("application/json") .headers(restProducerProperties.getHttpHeadersAsMultivaluedMap()).put(Entity.json(event)); } } 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 258c01d3f..ca0084982 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +22,12 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import java.util.Arrays; - +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; 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; @@ -64,6 +67,9 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara private String url = null; private HttpMethod httpMethod = null; private String[][] httpHeaders = null; + private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); + private static final Pattern patternErrorKey = Pattern.compile( + "(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); /** * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter @@ -163,6 +169,36 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara } /** + * Get the tag for the REST Producer Properties. + * + * @return set of the tags + */ + public Set<String> getKeysFromUrl() { + Matcher matcher = patternProperKey.matcher(this.url); + Set<String> key = new HashSet<>(); + while (matcher.find()) { + key.add(matcher.group()); + } + return key; + } + + /** + * Validate tags in url. + * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ + * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ + * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} + * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} + * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} + * + * @return if url is legal + */ + public boolean validateTagInUrl() { + // Check url tag syntax error + Matcher matcher = patternErrorKey.matcher(this.url); + return (!matcher.find()); + } + + /** * {@inheritDoc}. */ @Override @@ -171,7 +207,8 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara // Check if the URL has been set for event output if (getUrl() == null) { - result.setResult("url", ValidationStatus.INVALID, "no URL has been set for event sending on REST client"); + result.setResult("url", ValidationStatus.INVALID, + "no URL has been set for event sending on REST client"); } if (httpHeaders == null) { @@ -194,6 +231,10 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara } } + if (!validateTagInUrl()) { + result.setResult("url", ValidationStatus.INVALID, + "no proper URL has been set for event sending on REST client"); + } return result; } |