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.client.methods.HttpPost;
24 import org.apache.http.client.methods.HttpRequestBase;
25 import org.apache.http.client.utils.URIBuilder;
26 import org.apache.http.entity.StringEntity;
27 import org.apache.http.impl.client.CloseableHttpClient;
28 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
29 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
30 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
31 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.io.IOException;
36 import java.io.UnsupportedEncodingException;
38 import java.net.URISyntaxException;
39 import java.util.Optional;
41 public class ExtendedDmaapProducerHttpClientImpl {
43 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
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 dmaapContentType;
51 private ConsumerDmaapModel consumerDmaapModel;
54 public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
55 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
56 this.dmaapHostName = configuration.dmaapHostName();
57 this.dmaapProtocol = configuration.dmaapProtocol();
58 this.dmaapPortNumber = configuration.dmaapPortNumber();
59 this.dmaapTopicName = configuration.dmaapTopicName();
60 this.dmaapContentType = configuration.dmaapContentType();
63 public Optional<Integer> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
64 this.consumerDmaapModel = consumerDmaapModel;
66 return createRequest()
67 .flatMap(this::executeHttpClient);
68 } catch (URISyntaxException e) {
69 logger.warn("Exception while executing HTTP request: ", e);
71 return Optional.empty();
74 private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
76 return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
77 } catch (IOException e) {
78 logger.warn("Exception while executing HTTP request: ", e);
80 return Optional.empty();
83 private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
84 return "application/json".equals(dmaapContentType)
85 ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
89 private Optional<URI> createDmaapPublisherExtendedURI() throws URISyntaxException {
90 return Optional.ofNullable(new URIBuilder()
91 .setScheme(dmaapProtocol)
92 .setHost(dmaapHostName)
93 .setPort(dmaapPortNumber)
94 .setPath(dmaapTopicName).build());
97 private HttpPost createHttpPostRequest(URI extendedURI) {
98 HttpPost post = new HttpPost(extendedURI);
99 post.addHeader("Content-type", dmaapContentType);
100 createStringEntity().ifPresent(post::setEntity);
104 private Optional<StringEntity> createStringEntity() {
106 return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
107 } catch (UnsupportedEncodingException | IllegalArgumentException e) {
108 logger.warn("Exception while parsing JSON: ", e);
110 return Optional.empty();