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 java.io.IOException;
24 import java.io.UnsupportedEncodingException;
26 import java.net.URISyntaxException;
27 import java.util.Optional;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.client.utils.URIBuilder;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.impl.client.CloseableHttpClient;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
37 import org.onap.dcaegen2.services.prh.model.CommonFunctions;
38 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
39 import org.onap.dcaegen2.services.prh.service.DmaapHttpClientImpl;
40 import org.onap.dcaegen2.services.prh.service.HttpUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class ExtendedDmaapProducerHttpClientImpl {
46 private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
48 private final CloseableHttpClient closeableHttpClient;
49 private final String dmaapHostName;
50 private final String dmaapProtocol;
51 private final Integer dmaapPortNumber;
52 private final String dmaapTopicName;
53 private final String dmaapContentType;
54 private ConsumerDmaapModel consumerDmaapModel;
57 public ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
58 this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
59 this.dmaapHostName = configuration.dmaapHostName();
60 this.dmaapProtocol = configuration.dmaapProtocol();
61 this.dmaapPortNumber = configuration.dmaapPortNumber();
62 this.dmaapTopicName = configuration.dmaapTopicName();
63 this.dmaapContentType = configuration.dmaapContentType();
66 public Optional<String> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
67 this.consumerDmaapModel = consumerDmaapModel;
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, this::getDmaapProducerResponseHandler);
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 ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
92 private Optional<URI> createDmaapPublisherExtendedURI() throws URISyntaxException {
93 return Optional.ofNullable(new URIBuilder()
94 .setScheme(dmaapProtocol)
95 .setHost(dmaapHostName)
96 .setPort(dmaapPortNumber)
97 .setPath(dmaapTopicName).build());
100 private HttpPost createHttpPostRequest(URI extendedURI) {
101 HttpPost post = new HttpPost(extendedURI);
102 post.addHeader("Content-type", dmaapContentType);
103 createStringEntity().ifPresent(post::setEntity);
107 private Optional<StringEntity> createStringEntity() {
109 return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
110 } catch (UnsupportedEncodingException | IllegalArgumentException e) {
111 logger.warn("Exception while parsing JSON: ", e);
113 return Optional.empty();
116 private Optional<String> getDmaapProducerResponseHandler(HttpResponse httpResponse) throws IOException {
117 final int responseCode = httpResponse.getStatusLine().getStatusCode();
118 logger.info("Status code of operation: {}", responseCode);
119 final HttpEntity responseEntity = httpResponse.getEntity();
121 if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
122 logger.trace("HTTP response successful.");
123 return Optional.of("" + responseCode);
125 String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
126 logger.trace("HTTP response not successful : {}", response);
127 return Optional.of("" + responseCode);