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.client.methods.HttpGet;
24 import org.apache.http.client.methods.HttpRequestBase;
25 import org.apache.http.client.utils.URIBuilder;
26 import org.apache.http.impl.client.CloseableHttpClient;
27 import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration;
28 import org.onap.dcaegen2.services.service.CommonMethods;
29 import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.io.IOException;
35 import java.net.URISyntaxException;
36 import java.util.Optional;
39 public class ExtendedDmaapConsumerHttpClientImpl {
41 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapConsumerHttpClientImpl.class);
43 private final CloseableHttpClient closeableHttpClient;
44 private final String dmaapHostName;
45 private final String dmaapProtocol;
46 private final Integer dmaapPortNumber;
47 private final String dmaapTopicName;
48 private final String consumerGroup;
49 private final String consumerId;
50 private final String dmaapContentType;
53 ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) {
54 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
55 this.dmaapHostName = configuration.dmaapHostName();
56 this.dmaapProtocol = configuration.dmaapProtocol();
57 this.dmaapPortNumber = configuration.dmaapPortNumber();
58 this.dmaapTopicName = configuration.dmaapTopicName();
59 this.consumerGroup = configuration.consumerGroup();
60 this.consumerId = configuration.consumerId();
61 this.dmaapContentType = configuration.dmaapContentType();
64 public Optional<String> getHttpConsumerResponse() {
66 Optional<String> extendedDetails = Optional.empty();
67 Optional<HttpRequestBase> request = createRequest();
70 extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler());
71 } catch (IOException | NullPointerException e) {
72 logger.error("Exception while executing HTTP request: {}", e);
75 return extendedDetails;
78 private static HttpRequestBase createHttpRequest(URI extendedURI) {
79 if (isExtendedURINotNull(extendedURI)) {
80 return new HttpGet(extendedURI);
86 private static Boolean isExtendedURINotNull(URI extendedURI) {
87 return extendedURI != null;
90 private Optional<HttpRequestBase> createRequest() {
92 Optional<HttpRequestBase> request = Optional.empty();
93 final URI extendedURI = createDmaapConsumerExtendedURI();
95 if ("application/json".equals(dmaapContentType)) {
96 request = Optional.ofNullable(createHttpRequest(extendedURI));
97 request.get().addHeader("Content-type", dmaapContentType);
103 private String createRequestPath() {
104 return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
107 private URI createDmaapConsumerExtendedURI() {
108 URI extendedURI = null;
110 final URIBuilder uriBuilder = new URIBuilder()
111 .setScheme(dmaapProtocol)
112 .setHost(dmaapHostName)
113 .setPort(dmaapPortNumber)
114 .setPath(createRequestPath());
117 extendedURI = uriBuilder.build();
118 logger.info("Building extended URI: {}", extendedURI);
119 } catch (URISyntaxException e) {
120 logger.error("Exception while building extended URI: {}", e);