From 75d51a299e7d36cb988ef074fce00eb4b29a3394 Mon Sep 17 00:00:00 2001 From: RehanRaza Date: Mon, 13 May 2019 14:02:39 +0000 Subject: Bugfix: add MDC context to http redirect logs Change-Id: Icdf00480eed43536a8b4693cbb208560f0881c95 Issue-ID: DCAEGEN2-1462 Signed-off-by: RehanRaza --- .../datafile/service/producer/DmaapProducerHttpClient.java | 9 +++++---- .../collectors/datafile/web/PublishRedirectStrategy.java | 14 +++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'datafile-app-server/src/main/java') diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java index c61b7a4d..8d433827 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClient.java @@ -85,7 +85,7 @@ public class DmaapProducerHttpClient { public HttpResponse getDmaapProducerResponseWithRedirect(HttpUriRequest request, Map contextMap) throws DatafileTaskException { MDC.setContextMap(contextMap); - try (CloseableHttpAsyncClient webClient = createWebClient(true, DEFAULT_REQUEST_TIMEOUT)) { + try (CloseableHttpAsyncClient webClient = createWebClient(true, DEFAULT_REQUEST_TIMEOUT, contextMap)) { webClient.start(); logger.trace(INVOKE, "Starting to produce to DR {}", request); @@ -112,7 +112,7 @@ public class DmaapProducerHttpClient { public HttpResponse getDmaapProducerResponseWithCustomTimeout(HttpUriRequest request, Duration requestTimeout, Map contextMap) throws DatafileTaskException { MDC.setContextMap(contextMap); - try (CloseableHttpAsyncClient webClient = createWebClient(false, requestTimeout)) { + try (CloseableHttpAsyncClient webClient = createWebClient(false, requestTimeout, contextMap)) { webClient.start(); logger.trace(INVOKE, "Starting to produce to DR {}", request); @@ -152,7 +152,8 @@ public class DmaapProducerHttpClient { .port(configuration.dmaapPortNumber()); } - private CloseableHttpAsyncClient createWebClient(boolean expectRedirect, Duration requestTimeout) + private CloseableHttpAsyncClient createWebClient(boolean expectRedirect, Duration requestTimeout, + Map contextMap) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build(); @@ -162,7 +163,7 @@ public class DmaapProducerHttpClient { .setSslHostnameVerifier(new NoopHostnameVerifier()); if (expectRedirect) { - clientBuilder.setRedirectStrategy(PublishRedirectStrategy.INSTANCE); + clientBuilder.setRedirectStrategy(new PublishRedirectStrategy(contextMap)); } if (requestTimeout.toMillis() > 0) { diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java index de07461c..3ceec627 100644 --- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java +++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategy.java @@ -17,6 +17,7 @@ package org.onap.dcaegen2.collectors.datafile.web; import java.net.URI; +import java.util.Map; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.ProtocolException; @@ -33,6 +34,7 @@ import org.apache.http.impl.client.DefaultRedirectStrategy; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; /** * PublishRedirectStrategy implementation @@ -44,8 +46,17 @@ import org.slf4j.LoggerFactory; @Contract(threading = ThreadingBehavior.IMMUTABLE) public class PublishRedirectStrategy extends DefaultRedirectStrategy { - public static final PublishRedirectStrategy INSTANCE = new PublishRedirectStrategy(); private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final Map contextMap; + + /** + * Constructor PublishRedirectStrategy. + * + * @param contextMap - MDC context map + */ + public PublishRedirectStrategy(Map contextMap) { + this.contextMap = contextMap; + } /** * Redirectable methods. @@ -71,6 +82,7 @@ public class PublishRedirectStrategy extends DefaultRedirectStrategy { @Override public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { + MDC.setContextMap(contextMap); final URI uri = getLocationURI(request, response, context); logger.trace("getRedirect...: {}", request); return RequestBuilder.copy(request).setUri(uri).build(); -- cgit 1.2.3-korg