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 public 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 if (extendedDetails.isPresent()) {
76 return extendedDetails;
78 return Optional.empty();
82 private static HttpRequestBase createHttpRequest(URI extendedURI) {
83 if (isExtendedURINotNull(extendedURI)) {
84 return new HttpGet(extendedURI);
90 private static Boolean isExtendedURINotNull(URI extendedURI) {
91 return extendedURI != null;
94 private Optional<HttpRequestBase> createRequest() {
96 Optional<HttpRequestBase> request = Optional.empty();
97 final URI extendedURI = createDmaapConsumerExtendedURI();
99 if ("application/json".equals(dmaapContentType)) {
100 request = Optional.of(createHttpRequest(extendedURI));
101 request.get().addHeader("Content-type", dmaapContentType);
107 private String createRequestPath() {
108 return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
111 private URI createDmaapConsumerExtendedURI() {
112 URI extendedURI = null;
114 final URIBuilder uriBuilder = new URIBuilder()
115 .setScheme(dmaapProtocol)
116 .setHost(dmaapHostName)
117 .setPort(dmaapPortNumber)
118 .setPath(createRequestPath());
121 logger.info("Building extended URI");
122 extendedURI = uriBuilder.build();
123 } catch (URISyntaxException e) {
124 logger.error("Exception while building extended URI: {}", e);