9b6c96ff0920cd7fb86dbfe6dbf0a0083e1e22e1
[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.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.config.DmaapPublisherConfiguration;
29 import org.onap.dcaegen2.services.service.CommonMethods;
30 import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.IOException;
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.Optional;
39
40 public class ExtendedDmaapProducerHttpClientImpl {
41
42     private static Logger logger = LoggerFactory.getLogger(ExtendedDmaapProducerHttpClientImpl.class);
43
44     private final CloseableHttpClient closeableHttpClient;
45     private final String dmaapHostName;
46     private final String dmaapProtocol;
47     private final Integer dmaapPortNumber;
48     private final String dmaapTopicName;
49     private final String dmaapContentType;
50
51
52     ExtendedDmaapProducerHttpClientImpl(DmaapPublisherConfiguration configuration) {
53         this.closeableHttpClient = new DmaapHttpClientImpl(configuration).getHttpClient();
54         this.dmaapHostName = configuration.dmaapHostName();
55         this.dmaapProtocol = configuration.dmaapProtocol();
56         this.dmaapPortNumber = configuration.dmaapPortNumber();
57         this.dmaapTopicName = configuration.dmaapTopicName();
58         this.dmaapContentType = configuration.dmaapContentType();
59     }
60
61      Optional<String> getHttpProducerResponse(DmaapPublisherRequestDetails requestDetails) {
62
63         Optional<String> extendedDetails = Optional.empty();
64         Optional<HttpRequestBase> request = createRequest(requestDetails);
65
66         try {
67             extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler());
68         } catch (IOException | NullPointerException e) {
69             logger.error("Exception while executing HTTP request: {}", e);
70         }
71
72         return extendedDetails;
73     }
74
75     private Boolean isExtendedURINotNull(URI extendedURI) {
76         return extendedURI != null;
77     }
78
79     private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
80         return Optional.of(parseJson(jsonBody).get());
81     }
82
83     private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
84         Optional<StringEntity> stringEntity = Optional.empty();
85
86         try {
87             stringEntity = Optional.of(new StringEntity(jsonBody.get()));
88         } catch (UnsupportedEncodingException e) {
89             logger.error("Exception while parsing JSON: {}", e);
90         }
91
92         return stringEntity;
93     }
94
95     private Optional<HttpRequestBase> createRequest (DmaapPublisherRequestDetails requestDetails) {
96
97         Optional<HttpRequestBase> request = Optional.empty();
98         final URI extendedURI = createDmaapPublisherExtendedURI(requestDetails);
99
100         if ("application/json".equals(dmaapContentType)) {
101             request = Optional.ofNullable(createRequest(extendedURI, requestDetails));
102             request.get().addHeader("Content-type", dmaapContentType);
103         }
104
105         return request;
106     }
107
108     private URI createDmaapPublisherExtendedURI(DmaapPublisherRequestDetails requestDetails) {
109         URI extendedURI = null;
110
111         final URIBuilder uriBuilder = new URIBuilder()
112                 .setScheme(dmaapProtocol)
113                 .setHost(dmaapHostName)
114                 .setPort(dmaapPortNumber)
115                 .setPath(requestDetails.dmaapAPIPath() + "/" + dmaapTopicName);
116
117         try {
118             extendedURI = uriBuilder.build();
119             logger.info("Building extended URI: {}",extendedURI);
120         } catch (URISyntaxException e) {
121             logger.error("Exception while building extended URI: {}", e);
122         }
123
124         return extendedURI;
125     }
126
127     private HttpRequestBase createRequest(URI extendedURI, DmaapPublisherRequestDetails requestDetails) {
128         if (isExtendedURINotNull(extendedURI) && requestDetails.jsonBody().isPresent()) {
129             return createHttpPost(extendedURI, requestDetails.jsonBody());
130         } else {
131             return null;
132         }
133     }
134
135     private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
136         HttpPost post = new HttpPost(extendedURI);
137         Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
138         post.setEntity(stringEntity.get());
139         return post;
140     }
141 }