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;
39 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
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.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.web.util.DefaultUriBuilderFactory;
51 import reactor.core.publisher.Flux;
54 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
55 * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
57 public class DmaapProducerReactiveHttpClient {
59 private static final String X_ATT_DR_META = "X-ATT-DR-META";
60 private static final String NAME_JSON_TAG = "name";
61 private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
62 private static final String URI_SEPARATOR = "/";
63 private static final String DEFAULT_FEED_ID = "1";
65 private final Logger logger = LoggerFactory.getLogger(this.getClass());
67 private final String dmaapHostName;
68 private final Integer dmaapPortNumber;
69 private final String dmaapTopicName;
70 private final String dmaapProtocol;
71 private final String dmaapContentType;
72 private final String user;
73 private final String pwd;
75 private IFileSystemResource fileResource;
76 private CloseableHttpAsyncClient webClient;
79 * Constructor DmaapProducerReactiveHttpClient.
81 * @param dmaapPublisherConfiguration - DMaaP producer configuration object
83 public DmaapProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
84 this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
85 this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
86 this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
87 this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
88 this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
89 this.user = dmaapPublisherConfiguration.dmaapUserName();
90 this.pwd = dmaapPublisherConfiguration.dmaapUserPassword();
94 * Function for calling DMaaP HTTP producer - post request to DMaaP DataRouter.
96 * @param consumerDmaapModel - object which will be sent to DMaaP DataRouter
97 * @return status code of operation
99 public Flux<String> getDmaapProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
100 logger.trace("Entering getDmaapProducerResponse with {}", consumerDmaapModel);
102 logger.trace("Starting to publish to DR");
104 webClient = getWebClient();
107 HttpPut put = new HttpPut();
108 prepareHead(consumerDmaapModel, put);
109 prepareBody(consumerDmaapModel, put);
110 addUserCredentialsToHead(put);
112 Future<HttpResponse> future = webClient.execute(put, null);
113 HttpResponse response = future.get();
114 logger.trace(response.toString());
116 handleHttpResponse(response);
117 return Flux.just(response.toString());
118 } catch (Exception e) {
119 logger.error("Unable to send file to DataRouter. Data: {}", consumerDmaapModel, e);
124 private void handleHttpResponse(HttpResponse response) {
125 int statusCode = response.getStatusLine().getStatusCode();
126 if (HttpUtils.isSuccessfulResponseCode(statusCode)) {
127 logger.trace("Publish to DR successful!");
129 logger.error("Publish to DR unsuccessful, response code: " + statusCode);
133 private void addUserCredentialsToHead(HttpPut put) {
134 String plainCreds = user + ":" + pwd;
135 byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
136 byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
137 String base64Creds = new String(base64CredsBytes);
138 logger.trace("base64Creds...: {}", base64Creds);
139 put.addHeader("Authorization", "Basic " + base64Creds);
142 private void prepareHead(ConsumerDmaapModel model, HttpPut put) {
143 put.addHeader(HttpHeaders.CONTENT_TYPE, dmaapContentType);
144 JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(model));
145 String name = metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
146 metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
147 put.addHeader(X_ATT_DR_META, metaData.toString());
148 put.setURI(getUri(name));
151 private void prepareBody(ConsumerDmaapModel model, HttpPut put) {
152 String fileLocation = model.getInternalLocation();
153 IFileSystemResource fileSystemResource = getFileSystemResource();
154 fileSystemResource.setPath(fileLocation);
155 InputStream fileInputStream = null;
157 fileInputStream = fileSystemResource.getInputStream();
158 } catch (IOException e) {
159 logger.error("Unable to get stream from filesystem.", e);
162 put.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
163 } catch (IOException e) {
164 logger.error("Unable to set put request body from ByteArray.", e);
168 private URI getUri(String fileName) {
169 String path = dmaapTopicName + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + fileName;
170 return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
174 private IFileSystemResource getFileSystemResource() {
175 if (fileResource == null) {
176 fileResource = new FileSystemResourceWrapper();
181 protected void setFileSystemResource(IFileSystemResource fileSystemResource) {
182 fileResource = fileSystemResource;
185 protected CloseableHttpAsyncClient getWebClient() {
186 if (webClient != null) {
189 SSLContext sslContext = null;
191 sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
192 } catch (Exception e) {
193 logger.trace("Unable to get sslContext.", e);
196 return HttpAsyncClients.custom()
197 .setSSLContext(sslContext)
198 .setSSLHostnameVerifier(new NoopHostnameVerifier())
199 .setRedirectStrategy(PublishRedirectStrategy.INSTANCE)
204 protected void setWebClient(CloseableHttpAsyncClient client) {
205 this.webClient = client;