329722730bc27b559a977302c237c1ab3267807f
[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     public 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         if (extendedDetails.isPresent()) {
73             return extendedDetails;
74         } else {
75             return Optional.empty();
76         }
77     }
78
79     private Boolean isExtendedURINotNull(URI extendedURI) {
80         return extendedURI != null;
81     }
82
83     private Optional<StringEntity> createStringEntity(Optional<String> jsonBody) {
84         return Optional.of(parseJson(jsonBody).get());
85     }
86
87     private Optional<StringEntity> parseJson(Optional<String> jsonBody) {
88         Optional<StringEntity> stringEntity = Optional.empty();
89
90         try {
91             stringEntity = Optional.of(new StringEntity(jsonBody.get()));
92         } catch (UnsupportedEncodingException e) {
93             logger.error("Exception while parsing JSON: {}", e);
94         }
95
96         return stringEntity;
97     }
98
99     private Optional<HttpRequestBase> createRequest (DmaapPublisherRequestDetails requestDetails) {
100
101         Optional<HttpRequestBase> request = Optional.empty();
102         final URI extendedURI = createDmaapPublisherExtendedURI(requestDetails);
103
104         if ("application/json".equals(dmaapContentType)) {
105             request = Optional.of(createRequest(extendedURI, requestDetails));
106             request.get().addHeader("Content-type", dmaapContentType);
107         }
108
109         return request;
110     }
111
112     private URI createDmaapPublisherExtendedURI(DmaapPublisherRequestDetails requestDetails) {
113         URI extendedURI = null;
114
115         final URIBuilder uriBuilder = new URIBuilder()
116                 .setScheme(dmaapProtocol)
117                 .setHost(dmaapHostName)
118                 .setPort(dmaapPortNumber)
119                 .setPath(requestDetails.dmaapAPIPath() + "/" + dmaapTopicName);
120
121         try {
122             logger.info("Building extended URI");
123             extendedURI = uriBuilder.build();
124         } catch (URISyntaxException e) {
125             logger.error("Exception while building extended URI: {}", e);
126         }
127
128         return extendedURI;
129     }
130
131     private HttpRequestBase createRequest(URI extendedURI, DmaapPublisherRequestDetails requestDetails) {
132         if (isExtendedURINotNull(extendedURI) && requestDetails.jsonBody().isPresent()) {
133             return createHttpPost(extendedURI, requestDetails.jsonBody());
134         } else {
135             return null;
136         }
137     }
138
139     private HttpPost createHttpPost(URI extendedURI, Optional<String> jsonBody) {
140         HttpPost post = new HttpPost(extendedURI);
141         Optional<StringEntity> stringEntity = createStringEntity(jsonBody);
142         post.setEntity(stringEntity.get());
143         return post;
144     }
145
146
147 }