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 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 return extendedDetails;
75 private Boolean isExtendedURINotNull(URI extendedURI) {
76 return extendedURI != null;
79 private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
80 return Optional.of(parseJson(jsonBody).get());
83 private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
84 Optional<StringEntity> stringEntity = Optional.empty();
87 stringEntity = Optional.of(new StringEntity(jsonBody.get()));
88 } catch (UnsupportedEncodingException e) {
89 logger.error("Exception while parsing JSON: {}", e);
95 private Optional<HttpRequestBase> createRequest (DmaapPublisherRequestDetails requestDetails) {
97 Optional<HttpRequestBase> request = Optional.empty();
98 final URI extendedURI = createDmaapPublisherExtendedURI(requestDetails);
100 if ("application/json".equals(dmaapContentType)) {
101 request = Optional.ofNullable(createRequest(extendedURI, requestDetails));
102 request.get().addHeader("Content-type", dmaapContentType);
108 private URI createDmaapPublisherExtendedURI(DmaapPublisherRequestDetails requestDetails) {
109 URI extendedURI = null;
111 final URIBuilder uriBuilder = new URIBuilder()
112 .setScheme(dmaapProtocol)
113 .setHost(dmaapHostName)
114 .setPort(dmaapPortNumber)
115 .setPath(requestDetails.dmaapAPIPath() + "/" + dmaapTopicName);
118 extendedURI = uriBuilder.build();
119 logger.info("Building extended URI: {}",extendedURI);
120 } catch (URISyntaxException e) {
121 logger.error("Exception while building extended URI: {}", e);
127 private HttpRequestBase createRequest(URI extendedURI, DmaapPublisherRequestDetails requestDetails) {
128 if (isExtendedURINotNull(extendedURI) && requestDetails.jsonBody().isPresent()) {
129 return createHttpPost(extendedURI, requestDetails.jsonBody());
135 private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
136 HttpPost post = new HttpPost(extendedURI);
137 Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
138 post.setEntity(stringEntity.get());