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.consumer;
23 import org.apache.http.HttpEntity;
24 import org.apache.http.client.ResponseHandler;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.client.methods.HttpRequestBase;
27 import org.apache.http.client.utils.URIBuilder;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.util.EntityUtils;
30 import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration;
31 import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
32 import org.onap.dcaegen2.services.service.HttpUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.io.IOException;
38 import java.net.URISyntaxException;
39 import java.util.Optional;
42 public class ExtendedDmaapConsumerHttpClientImpl {
44 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class);
46 private final CloseableHttpClient closeableHttpClient;
47 private final String dmaapHostName;
48 private final String dmaapProtocol;
49 private final Integer dmaapPortNumber;
50 private final String dmaapTopicName;
51 private final String consumerGroup;
52 private final String consumerId;
53 private final String dmaapContentType;
56 public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) {
57 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
58 this.dmaapHostName = configuration.dmaapHostName();
59 this.dmaapProtocol = configuration.dmaapProtocol();
60 this.dmaapPortNumber = configuration.dmaapPortNumber();
61 this.dmaapTopicName = configuration.dmaapTopicName();
62 this.consumerGroup = configuration.consumerGroup();
63 this.consumerId = configuration.consumerId();
64 this.dmaapContentType = configuration.dmaapContentType();
67 public Optional<String> getHttpConsumerResponse() {
69 Optional<String> extendedDetails = Optional.empty();
70 Optional<HttpRequestBase> request = createRequest();
73 extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler());
74 } catch (IOException | NullPointerException e) {
75 logger.error("Exception while executing HTTP request: {}", e);
78 return extendedDetails;
81 private static HttpRequestBase createHttpRequest(URI extendedURI) {
82 if (isExtendedURINotNull(extendedURI)) {
83 return new HttpGet(extendedURI);
89 private static Boolean isExtendedURINotNull(URI extendedURI) {
90 return extendedURI != null;
93 private Optional<HttpRequestBase> createRequest() {
95 Optional<HttpRequestBase> request = Optional.empty();
96 final URI extendedURI = createDmaapConsumerExtendedURI();
98 if ("application/json".equals(dmaapContentType)) {
99 request = Optional.ofNullable(createHttpRequest(extendedURI));
100 request.get().addHeader("Content-type", dmaapContentType);
106 private String createRequestPath() {
107 return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
110 private URI createDmaapConsumerExtendedURI() {
112 return new URIBuilder()
113 .setScheme(dmaapProtocol)
114 .setHost(dmaapHostName)
115 .setPort(dmaapPortNumber)
116 .setPath(createRequestPath()).build();
117 } catch (URISyntaxException e) {
118 throw new RuntimeException("Exception while building extended URI: {}", e);
122 private ResponseHandler<Optional<String>> dmaapConsumerResponseHandler() {
123 return httpResponse -> {
124 final int responseCode = httpResponse.getStatusLine().getStatusCode();
125 logger.info("Status code of operation: {}", responseCode);
126 final HttpEntity responseEntity = httpResponse.getEntity();
128 if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
129 logger.info("HTTP response successful.");
130 final String dmaapResponse = EntityUtils.toString(responseEntity);
131 return Optional.of(dmaapResponse);
133 String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
134 logger.error("HTTP response not successful : {}", dmaapResponse);
135 return Optional.of(String.valueOf(responseCode));