diff options
author | Lusheng Ji <lji@research.att.com> | 2018-05-22 16:13:32 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-05-22 16:13:32 +0000 |
commit | 58d7f019d3ef6d378c9c3625773f49e79060e20f (patch) | |
tree | c7e298bada9cf31463544ead582ee4bb8f6c79b7 /prh-aai-client/src/main/java | |
parent | 69da977930bf2ec5a7665f0144e698118c3e0f1b (diff) | |
parent | 91292c21d8ee51c3840ed455955636b7ceda36fb (diff) |
Merge "Removing warnings related to optionals"
Diffstat (limited to 'prh-aai-client/src/main/java')
3 files changed, 37 insertions, 59 deletions
diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java index 0ada9072..99a4a8b2 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIConsumerClient.java @@ -42,7 +42,7 @@ import java.util.Optional; public class AAIConsumerClient { - Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); + private Logger logger = LoggerFactory.getLogger(AAIConsumerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java index cb884aed..14c7e8f3 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIExtendedHttpClient.java @@ -21,11 +21,11 @@ package org.onap.dcaegen2.services.prh.service; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; -import java.io.IOException; +import java.net.URISyntaxException; import java.util.Optional; @FunctionalInterface public interface AAIExtendedHttpClient { - Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException; + Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException; } diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java index 3a07e94d..4f48c1c3 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java @@ -41,11 +41,10 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.Map; -import java.util.Objects; import java.util.Optional; public class AAIProducerClient implements AAIExtendedHttpClient { - Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); + private Logger logger = LoggerFactory.getLogger(AAIProducerClient.class); private final CloseableHttpClient closeableHttpClient; private final String aaiHost; @@ -66,30 +65,34 @@ public class AAIProducerClient implements AAIExtendedHttpClient { @Override - public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws IOException { - Optional<HttpRequestBase> request = createRequest(consumerDmaapModel); + public Optional<Integer> getHttpResponse(ConsumerDmaapModel consumerDmaapModel) throws + URISyntaxException { try { - return closeableHttpClient.execute(request.get(), aaiResponseHandler()); - } catch (IOException e) { + return createRequest(consumerDmaapModel).flatMap(x->{ + try { + return closeableHttpClient.execute(x, aaiResponseHandler()); + } catch (IOException e) { + logger.warn("Exception while executing http client: ", e); + return Optional.empty(); + } + }); + } catch (URISyntaxException e ) { logger.warn("Exception while executing http client: ", e); - throw new IOException(); + throw e; } } - private URI createAAIExtendedURI(final String pnfName) { - URI extendedURI = null; - final URIBuilder uriBuilder = new URIBuilder() + private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { + final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); + return createHttpRequest(extendedURI, consumerDmaapModel); + } + + private URI createAAIExtendedURI(final String pnfName) throws URISyntaxException { + return new URIBuilder() .setScheme(aaiProtocol) .setHost(aaiHost) .setPort(aaiHostPortNumber) - .setPath(aaiPath + "/" + pnfName); - try { - extendedURI = uriBuilder.build(); - logger.trace("Building extended URI: {}", extendedURI); - } catch (URISyntaxException e) { - logger.warn("Exception while building extended URI: ", e); - } - return extendedURI; + .setPath(aaiPath + "/" + pnfName).build(); } private ResponseHandler<Optional<Integer>> aaiResponseHandler() { @@ -109,47 +112,22 @@ public class AAIProducerClient implements AAIExtendedHttpClient { }; } - private HttpRequestBase createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { - String jsonBody = CommonFunctions.createJsonBody(consumerDmaapModel); - - if (isExtendedURINotNull(extendedURI) && jsonBody != null && !"".equals(jsonBody)) { - return createHttpPatch(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel))); - } else { - return null; - } - } - - private Boolean isExtendedURINotNull(URI extendedURI) { - return extendedURI != null; - } - - - private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) { - return Optional.of(parseJson(jsonBody).get()); + private Optional<HttpRequestBase> createHttpRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) { + return Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)).filter(x-> !x.isEmpty()).flatMap(myJson -> { + try { + return Optional.of(createHttpPatch(extendedURI, myJson)); + } catch (UnsupportedEncodingException e) { + logger.warn("Exception while executing http client: ", e); + } + return Optional.empty(); + }); } - private HttpPatch createHttpPatch(URI extendedURI, Optional<String> jsonBody) { + private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException { HttpPatch httpPatch = new HttpPatch(extendedURI); - Optional<StringEntity> stringEntity = createStringEntity(jsonBody); - httpPatch.setEntity(stringEntity.get()); + httpPatch.setEntity( new StringEntity(jsonBody)); + aaiHeaders.forEach(httpPatch::addHeader); + httpPatch.addHeader("Content-Type", "application/merge-patch+json"); return httpPatch; } - - private Optional<StringEntity> parseJson(Optional<String> jsonBody) { - Optional<StringEntity> stringEntity = Optional.empty(); - try { - stringEntity = Optional.of(new StringEntity(jsonBody.get())); - } catch (UnsupportedEncodingException e) { - logger.warn("Exception while parsing JSON: ", e); - } - return stringEntity; - } - - private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) { - final URI extendedURI = createAAIExtendedURI(consumerDmaapModel.getPnfName()); - HttpRequestBase request = createHttpRequest(extendedURI, consumerDmaapModel); - aaiHeaders.forEach(Objects.requireNonNull(request)::addHeader); - Objects.requireNonNull(request).addHeader("Content-Type", "application/merge-patch+json"); - return Optional.of(request); - } } |