2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.service.producer;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonParser;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.Future;
28 import javax.net.ssl.SSLContext;
30 import org.apache.commons.codec.binary.Base64;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.methods.HttpPut;
34 import org.apache.http.conn.ssl.NoopHostnameVerifier;
35 import org.apache.http.entity.ByteArrayEntity;
36 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
37 import org.apache.http.impl.nio.client.HttpAsyncClients;
38 import org.apache.http.ssl.SSLContextBuilder;
40 import org.onap.dcaegen2.collectors.datafile.io.FileSystemResourceWrapper;
41 import org.onap.dcaegen2.collectors.datafile.io.IFileSystemResource;
42 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
43 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
44 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
45 import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy;
46 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.http.HttpHeaders;
50 import org.springframework.web.util.DefaultUriBuilderFactory;
52 import reactor.core.publisher.Flux;
55 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
56 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
58 public class DmaapProducerReactiveHttpClient {
60 private static final String X_ATT_DR_META = "X-ATT-DR-META";
61 private static final String NAME_JSON_TAG = "name";
62 private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
63 private static final String URI_SEPARATOR = "/";
64 private static final String DEFAULT_FEED_ID = "1";
66 private final Logger logger = LoggerFactory.getLogger(this.getClass());
68 private final String dmaapHostName;
69 private final Integer dmaapPortNumber;
70 private final String dmaapTopicName;
71 private final String dmaapProtocol;
72 private final String dmaapContentType;
73 private final String user;
74 private final String pwd;
76 private IFileSystemResource fileResource;
77 private CloseableHttpAsyncClient webClient;
80 * Constructor DmaapProducerReactiveHttpClient.
82 * @param dmaapPublisherConfiguration - DMaaP producer configuration object
84 public DmaapProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
85 this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
86 this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
87 this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
88 this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
89 this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
90 this.user = dmaapPublisherConfiguration.dmaapUserName();
91 this.pwd = dmaapPublisherConfiguration.dmaapUserPassword();
95 * Function for calling DMaaP HTTP producer - post request to DMaaP DataRouter.
97 * @param consumerDmaapModel - object which will be sent to DMaaP DataRouter
98 * @return status code of operation
100 public Flux<String> getDmaapProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
101 logger.trace("Entering getDmaapProducerResponse with {}", consumerDmaapModel);
103 logger.trace("Starting to publish to DR");
105 webClient = getWebClient();
108 HttpPut put = new HttpPut();
109 prepareHead(consumerDmaapModel, put);
110 prepareBody(consumerDmaapModel, put);
111 addUserCredentialsToHead(put);
113 Future<HttpResponse> future = webClient.execute(put, null);
114 HttpResponse response = future.get();
115 logger.trace(response.toString());
117 handleHttpResponse(response);
118 return Flux.just(response.toString());
119 } catch (Exception e) {
120 logger.error("Unable to send file to DataRouter. Data: {}", consumerDmaapModel, e);
125 private void handleHttpResponse(HttpResponse response) {
126 int statusCode = response.getStatusLine().getStatusCode();
127 if (HttpUtils.isSuccessfulResponseCode(statusCode)) {
128 logger.trace("Publish to DR successful!");
130 logger.error("Publish to DR unsuccessful, response code: " + statusCode);
134 private void addUserCredentialsToHead(HttpPut put) {
135 String plainCreds = user + ":" + pwd;
136 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
137 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
138 String base64Creds = new String(base64CredsBytes);
139 logger.trace("base64Creds...: {}", base64Creds);
140 put.addHeader("Authorization", "Basic " + base64Creds);
143 private void prepareHead(ConsumerDmaapModel model, HttpPut put) {
144 put.addHeader(HttpHeaders.CONTENT_TYPE, dmaapContentType);
145 JsonElement metaData = new JsonParser().parse(new CommonFunctions().createJsonBody(model));
146 String name = metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
147 metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
148 put.addHeader(X_ATT_DR_META, metaData.toString());
149 put.setURI(getUri(name));
152 private void prepareBody(ConsumerDmaapModel model, HttpPut put) {
153 String fileLocation = model.getInternalLocation();
154 IFileSystemResource fileSystemResource = getFileSystemResource();
155 fileSystemResource.setPath(fileLocation);
156 InputStream fileInputStream = null;
158 fileInputStream = fileSystemResource.getInputStream();
159 } catch (IOException e) {
160 logger.error("Unable to get stream from filesystem.", e);
163 put.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
164 } catch (IOException e) {
165 logger.error("Unable to set put request body from ByteArray.", e);
169 private URI getUri(String fileName) {
170 String path = dmaapTopicName + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + fileName;
171 return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
175 private IFileSystemResource getFileSystemResource() {
176 if (fileResource == null) {
177 fileResource = new FileSystemResourceWrapper();
182 protected void setFileSystemResource(IFileSystemResource fileSystemResource) {
183 fileResource = fileSystemResource;
186 protected CloseableHttpAsyncClient getWebClient() {
187 if (webClient != null) {
190 SSLContext sslContext = null;
192 sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
193 } catch (Exception e) {
194 logger.trace("Unable to get sslContext.", e);
197 return HttpAsyncClients.custom()
198 .setSSLContext(sslContext)
199 .setSSLHostnameVerifier(new NoopHostnameVerifier())
200 .setRedirectStrategy(PublishRedirectStrategy.INSTANCE)
205 protected void setWebClient(CloseableHttpAsyncClient client) {
206 this.webClient = client;