b93c9c6bcaf3d380b79ebaba8b47faf954bdc281
[dcaegen2/services/prh.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.prh.service.producer;
22
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;
34
35 import java.io.IOException;
36 import java.io.UnsupportedEncodingException;
37 import java.net.URI;
38 import java.net.URISyntaxException;
39 import java.util.Optional;
40
41 public class ExtendedDmaapProducerHttpClientImpl {
42
43     private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
44
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;
52
53
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();
61     }
62
63     public Optional<Integer> getHttpProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
64         this.consumerDmaapModel = consumerDmaapModel;
65         try {
66             return createRequest()
67                 .flatMap(this::executeHttpClient);
68         } catch (URISyntaxException e) {
69             logger.warn("Exception while executing HTTP request: ", e);
70         }
71         return Optional.empty();
72     }
73
74     private Optional<Integer> executeHttpClient(HttpRequestBase httpRequestBase) {
75         try {
76             return closeableHttpClient.execute(httpRequestBase, CommonFunctions::handleResponse);
77         } catch (IOException e) {
78             logger.warn("Exception while executing HTTP request: ", e);
79         }
80         return Optional.empty();
81     }
82
83     private Optional<HttpRequestBase> createRequest() throws URISyntaxException {
84         return "application/json".equals(dmaapContentType)
85             ? createDmaapPublisherExtendedURI().map(this::createHttpPostRequest)
86             : Optional.empty();
87     }
88
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());
95     }
96
97     private HttpPost createHttpPostRequest(URI extendedURI) {
98         HttpPost post = new HttpPost(extendedURI);
99         post.addHeader("Content-type", dmaapContentType);
100         createStringEntity().ifPresent(post::setEntity);
101         return post;
102     }
103
104     private Optional<StringEntity> createStringEntity() {
105         try {
106             return Optional.of(new StringEntity(CommonFunctions.createJsonBody(consumerDmaapModel)));
107         } catch (UnsupportedEncodingException | IllegalArgumentException e) {
108             logger.warn("Exception while parsing JSON: ", e);
109         }
110         return Optional.empty();
111     }
112 }