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.prh.service.consumer;
23 import java.io.IOException;
25 import java.net.URISyntaxException;
26 import java.util.Optional;
27 import org.apache.http.HttpEntity;
28 import org.apache.http.client.ResponseHandler;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.client.methods.HttpRequestBase;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.util.EntityUtils;
34 import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration;
35 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
36 import org.onap.dcaegen2.services.prh.service.HttpUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class ExtendedDmaapConsumerHttpClientImpl {
43 private final Logger logger = LoggerFactory.getLogger(this.getClass());
45 private final CloseableHttpClient closeableHttpClient;
46 private final String dmaapHostName;
47 private final String dmaapProtocol;
48 private final Integer dmaapPortNumber;
49 private final String dmaapTopicName;
50 private final String consumerGroup;
51 private final String consumerId;
52 private final String dmaapContentType;
55 public ExtendedDmaapConsumerHttpClientImpl(DmaapConsumerConfiguration configuration) {
56 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
57 this.dmaapHostName = configuration.dmaapHostName();
58 this.dmaapProtocol = configuration.dmaapProtocol();
59 this.dmaapPortNumber = configuration.dmaapPortNumber();
60 this.dmaapTopicName = configuration.dmaapTopicName();
61 this.consumerGroup = configuration.consumerGroup();
62 this.consumerId = configuration.consumerId();
63 this.dmaapContentType = configuration.dmaapContentType();
66 public Optional<String> getHttpConsumerResponse() {
69 return createRequest()
70 .flatMap(this::executeHttpClient);
71 } catch (URISyntaxException e) {
72 logger.warn("Exception while executing HTTP request: ", e);
74 return Optional.empty();
77 private Optional<String> executeHttpClient(HttpRequestBase httpRequestBase) {
79 return closeableHttpClient.execute(httpRequestBase, getDmaapConsumerResponseHandler());
80 } catch (IOException e) {
81 logger.warn("Exception while executing HTTP request: ", e);
83 return Optional.empty();
86 private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
87 return "application/json".equals(dmaapContentType)
88 ? createDmaapConsumerExtendedURI().map(this::createHttpRequest)
92 private HttpRequestBase createHttpRequest(URI extendedURI) {
93 HttpRequestBase httpRequestBase = new HttpGet(extendedURI);
94 httpRequestBase.addHeader("Content-type", dmaapContentType);
95 return httpRequestBase;
99 private String createRequestPath() {
100 return dmaapTopicName + "/" + consumerGroup + "/" + consumerId;
103 private Optional<URI> createDmaapConsumerExtendedURI() throws URISyntaxException {
104 return Optional.ofNullable(new URIBuilder()
105 .setScheme(dmaapProtocol)
106 .setHost(dmaapHostName)
107 .setPort(dmaapPortNumber)
108 .setPath(createRequestPath()).build());
111 private ResponseHandler<Optional<String>> getDmaapConsumerResponseHandler() {
112 return httpResponse -> {
113 final int responseCode = httpResponse.getStatusLine().getStatusCode();
114 logger.info("Status code of operation: {}", responseCode);
115 final HttpEntity responseEntity = httpResponse.getEntity();
117 if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
118 logger.trace("HTTP response successful.");
119 final String dmaapResponse = EntityUtils.toString(responseEntity);
120 return Optional.of(dmaapResponse);
122 String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
123 logger.trace("HTTP response not successful : {}", dmaapResponse);
124 return Optional.of(String.valueOf(responseCode));