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.producer;
23 import org.apache.http.HttpEntity;
24 import org.apache.http.HttpResponse;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.client.methods.HttpRequestBase;
27 import org.apache.http.client.utils.URIBuilder;
28 import org.apache.http.entity.StringEntity;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
32 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.onap.dcaegen2.services.prh.model.utils.HttpUtils;
35 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import java.io.IOException;
40 import java.io.UnsupportedEncodingException;
42 import java.net.URISyntaxException;
43 import java.util.Optional;
45 public class ExtendedDmaapProducerHttpClientImpl {
47 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
49 private final CloseableHttpClient closeableHttpClient;
50 private final String dmaapHostName;
51 private final String dmaapProtocol;
52 private final Integer dmaapPortNumber;
53 private final String dmaapTopicName;
54 private final String dmaapContentType;
55 private ConsumerDmaapModel consumerDmaapModel;
58 public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
59 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
60 this.dmaapHostName = configuration.dmaapHostName();
61 this.dmaapProtocol = configuration.dmaapProtocol();
62 this.dmaapPortNumber = configuration.dmaapPortNumber();
63 this.dmaapTopicName = configuration.dmaapTopicName();
64 this.dmaapContentType = configuration.dmaapContentType();
67 public Optional<Integer> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
68 this.consumerDmaapModel = consumerDmaapModel;
70 return createRequest()
71 .flatMap(this::executeHttpClient);
72 } catch (URISyntaxException e) {
73 logger.warn("Exception while executing HTTP request: ", e);
75 return Optional.empty();
78 private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
80 return closeableHttpClient.execute(httpRequestBase, this::handleResponse);
81 } catch (IOException e) {
82 logger.warn("Exception while executing HTTP request: ", e);
84 return Optional.empty();
87 private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
88 return "application/json".equals(dmaapContentType)
89 ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
93 private Optional<URI> createDmaapPublisherExtendedURI() throws URISyntaxException {
94 return Optional.ofNullable(new URIBuilder()
95 .setScheme(dmaapProtocol)
96 .setHost(dmaapHostName)
97 .setPort(dmaapPortNumber)
98 .setPath(dmaapTopicName).build());
101 private HttpPost createHttpPostRequest(URI extendedURI) {
102 HttpPost post = new HttpPost(extendedURI);
103 post.addHeader("Content-type", dmaapContentType);
104 createStringEntity().ifPresent(post::setEntity);
108 private Optional<StringEntity> createStringEntity() {
110 return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
111 } catch (UnsupportedEncodingException | IllegalArgumentException e) {
112 logger.warn("Exception while parsing JSON: ", e);
114 return Optional.empty();
117 Optional<Integer> handleResponse(HttpResponse response) throws IOException {
119 final Integer responseCode = response.getStatusLine().getStatusCode();
120 logger.info("Status code of operation: {}", responseCode);
121 final HttpEntity responseEntity = response.getEntity();
123 if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
124 logger.trace("HTTP response successful.");
125 return Optional.of(responseCode);
127 String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
128 logger.warn("HTTP response not successful : {}", aaiResponse);
129 return Optional.of(responseCode);