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.service.producer;
23 import org.apache.http.client.methods.HttpPost;
24 import org.apache.http.client.methods.HttpRequestBase;
25 import org.apache.http.client.utils.URIBuilder;
26 import org.apache.http.entity.StringEntity;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration;
29 import org.onap.dcaegen2.services.service.CommonMethods;
30 import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.io.IOException;
35 import java.io.UnsupportedEncodingException;
37 import java.net.URISyntaxException;
38 import java.util.Optional;
40 public class ExtendedDmaapProducerHttpClientImpl {
42 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
44 private final CloseableHttpClient closeableHttpClient;
45 private final String dmaapHostName;
46 private final String dmaapProtocol;
47 private final Integer dmaapPortNumber;
48 private final String dmaapTopicName;
49 private final String dmaapContentType;
52 ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
53 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
54 this.dmaapHostName = configuration.dmaapHostName();
55 this.dmaapProtocol = configuration.dmaapProtocol();
56 this.dmaapPortNumber = configuration.dmaapPortNumber();
57 this.dmaapTopicName = configuration.dmaapTopicName();
58 this.dmaapContentType = configuration.dmaapContentType();
61 public Optional<String> getHttpProducerResponse(DmaapPublisherRequestDetails requestDetails) {
63 Optional<String> extendedDetails = Optional.empty();
64 Optional<HttpRequestBase> request = createRequest(requestDetails);
67 extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler());
68 } catch (IOException | NullPointerException e) {
69 logger.error("Exception while executing HTTP request: {}", e);
72 if (extendedDetails.isPresent()) {
73 return extendedDetails;
75 return Optional.empty();
79 private Boolean isExtendedURINotNull(URI extendedURI) {
80 return extendedURI != null;
83 private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
84 return Optional.of(parseJson(jsonBody).get());
87 private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
88 Optional<StringEntity> stringEntity = Optional.empty();
91 stringEntity = Optional.of(new StringEntity(jsonBody.get()));
92 } catch (UnsupportedEncodingException e) {
93 logger.error("Exception while parsing JSON: {}", e);
99 private Optional<HttpRequestBase> createRequest (DmaapPublisherRequestDetails requestDetails) {
101 Optional<HttpRequestBase> request = Optional.empty();
102 final URI extendedURI = createDmaapPublisherExtendedURI(requestDetails);
104 if ("application/json".equals(dmaapContentType)) {
105 request = Optional.of(createRequest(extendedURI, requestDetails));
106 request.get().addHeader("Content-type", dmaapContentType);
112 private URI createDmaapPublisherExtendedURI(DmaapPublisherRequestDetails requestDetails) {
113 URI extendedURI = null;
115 final URIBuilder uriBuilder = new URIBuilder()
116 .setScheme(dmaapProtocol)
117 .setHost(dmaapHostName)
118 .setPort(dmaapPortNumber)
119 .setPath(requestDetails.dmaapAPIPath() + "/" + dmaapTopicName);
122 logger.info("Building extended URI");
123 extendedURI = uriBuilder.build();
124 } catch (URISyntaxException e) {
125 logger.error("Exception while building extended URI: {}", e);
131 private HttpRequestBase createRequest(URI extendedURI, DmaapPublisherRequestDetails requestDetails) {
132 if (isExtendedURINotNull(extendedURI) && requestDetails.jsonBody().isPresent()) {
133 return createHttpPost(extendedURI, requestDetails.jsonBody());
139 private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
140 HttpPost post = new HttpPost(extendedURI);
141 Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
142 post.setEntity(stringEntity.get());