2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.prh.service.producer;
23 import org.apache.http.HttpEntity;
24 import org.apache.http.client.ResponseHandler;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.client.methods.HttpRequestBase;
27 import org.apache.http.client.utils.URIBuilder;
28 import org.apache.http.entity.StringEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
32 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
35 import org.onap.dcaegen2.services.prh.service.HttpUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import java.io.IOException;
40 import java.io.UnsupportedEncodingException;
42 import java.net.URISyntaxException;
43 import java.util.Optional;
45 public class ExtendedDmaapProducerHttpClientImpl {
47 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
49 private final CloseableHttpClient closeableHttpClient;
50 private final String dmaapHostName;
51 private final String dmaapProtocol;
52 private final Integer dmaapPortNumber;
53 private final String dmaapTopicName;
54 private final String dmaapContentType;
57 public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
58 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
59 this.dmaapHostName = configuration.dmaapHostName();
60 this.dmaapProtocol = configuration.dmaapProtocol();
61 this.dmaapPortNumber = configuration.dmaapPortNumber();
62 this.dmaapTopicName = configuration.dmaapTopicName();
63 this.dmaapContentType = configuration.dmaapContentType();
66 public Optional<String> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
67 Optional<String> extendedDetails = Optional.empty();
68 Optional<HttpRequestBase> request = createRequest(consumerDmaapModel);
70 extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler());
71 } catch (IOException | NullPointerException e) {
72 logger.warn("Exception while executing HTTP request: ", e);
74 return extendedDetails;
77 private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
78 return Optional.of(parseJson(jsonBody).get());
81 private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
82 Optional<StringEntity> stringEntity = Optional.empty();
84 stringEntity = Optional.of(new StringEntity(jsonBody.get()));
85 } catch (UnsupportedEncodingException e) {
86 logger.warn("Exception while parsing JSON: ", e);
91 private Optional<HttpRequestBase> createRequest(ConsumerDmaapModel consumerDmaapModel) {
92 Optional<HttpRequestBase> request = Optional.empty();
93 final URI extendedURI = createDmaapPublisherExtendedURI();
95 if ("application/json".equals(dmaapContentType)) {
96 request = Optional.ofNullable(createRequest(extendedURI, consumerDmaapModel));
97 request.get().addHeader("Content-type", dmaapContentType);
103 private URI createDmaapPublisherExtendedURI() {
104 URI extendedURI = null;
105 final URIBuilder uriBuilder = new URIBuilder()
106 .setScheme(dmaapProtocol)
107 .setHost(dmaapHostName)
108 .setPort(dmaapPortNumber)
109 .setPath(dmaapTopicName);
111 extendedURI = uriBuilder.build();
112 logger.trace("Building extended URI: {}", extendedURI);
113 } catch (URISyntaxException e) {
114 logger.warn("Exception while building extended URI: ", e);
119 private HttpRequestBase createRequest(URI extendedURI, ConsumerDmaapModel consumerDmaapModel) {
120 if (extendedURI != null) {
121 return createHttpPost(extendedURI, Optional.ofNullable(CommonFunctions.createJsonBody(consumerDmaapModel)));
127 private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
128 HttpPost post = new HttpPost(extendedURI);
129 Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
130 post.setEntity(stringEntity.get());
134 private ResponseHandler<Optional<String>> dmaapProducerResponseHandler() {
135 return httpResponse -> {
136 final int responseCode = httpResponse.getStatusLine().getStatusCode();
137 final HttpEntity responseEntity = httpResponse.getEntity();
139 if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
140 logger.trace("HTTP response successful.");
141 return Optional.of("" + responseCode);
143 String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
144 logger.warn("HTTP response not successful : {}", response);
145 return Optional.of("" + responseCode);