diff options
author | RehanRaza <muhammad.rehan.raza@est.tech> | 2019-05-13 14:02:39 +0000 |
---|---|---|
committer | RehanRaza <muhammad.rehan.raza@est.tech> | 2019-05-13 14:02:39 +0000 |
commit | 75d51a299e7d36cb988ef074fce00eb4b29a3394 (patch) | |
tree | ce2c20f9d24c30822473098556991dec39f58490 /datafile-app-server | |
parent | 0e7fe2901b830841efb9d575c14c6c7ada7c7e0f (diff) |
Bugfix: add MDC context to http redirect logs
Change-Id: Icdf00480eed43536a8b4693cbb208560f0881c95
Issue-ID: DCAEGEN2-1462
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
Diffstat (limited to 'datafile-app-server')
4 files changed, 32 insertions, 9 deletions
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<String, String> 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<String, String> 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<String, String> 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<String, String> contextMap; + + /** + * Constructor PublishRedirectStrategy. + * + * @param contextMap - MDC context map + */ + public PublishRedirectStrategy(Map<String, String> 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(); diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java index add47b0a..e21bbd7b 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerHttpClientTest.java @@ -26,7 +26,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; - import java.net.URI; import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; @@ -110,7 +109,7 @@ class DmaapProducerHttpClientTest { verify(clientBuilderMock).setSslContext(any(SSLContext.class)); verify(clientBuilderMock).setSslHostnameVerifier(any(NoopHostnameVerifier.class)); - verify(clientBuilderMock).setRedirectStrategy(PublishRedirectStrategy.INSTANCE); + verify(clientBuilderMock).setRedirectStrategy(any(PublishRedirectStrategy.class)); verify(clientBuilderMock).setDefaultRequestConfig(any()); verify(clientBuilderMock).build(); verifyNoMoreInteractions(clientBuilderMock); diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java index 298656a8..df13f2db 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/web/PublishRedirectStrategyTest.java @@ -19,7 +19,8 @@ package org.onap.dcaegen2.collectors.datafile.web; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - +import java.util.HashMap; +import java.util.Map; import org.apache.http.Header; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; @@ -32,16 +33,23 @@ import org.apache.http.protocol.HttpContext; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables; +import org.slf4j.MDC; class PublishRedirectStrategyTest { private static final String URI = "sftp://localhost:80/"; + private static final String SAMPLE_REQUEST_ID = "A20000626.2315+0200-2330+0200_PNF0-0-1MB.tar.gz"; + private static final String ANOTHER_REQUEST_ID = "something_else"; private static PublishRedirectStrategy publishRedirectStrategy; @BeforeAll static void setUp() { - publishRedirectStrategy = new PublishRedirectStrategy(); + MDC.put(MdcVariables.REQUEST_ID, ANOTHER_REQUEST_ID); + Map<String, String> contextMap = new HashMap<>(); + contextMap.put(MdcVariables.REQUEST_ID, SAMPLE_REQUEST_ID); + publishRedirectStrategy = new PublishRedirectStrategy(contextMap); } @Test @@ -70,5 +78,8 @@ class PublishRedirectStrategyTest { HttpUriRequest actualRedirect = publishRedirectStrategy.getRedirect(requestMock, responseMock, contextMock); assertEquals(URI, actualRedirect.getURI().toString()); + + String actualRequestId = MDC.get(MdcVariables.REQUEST_ID); + assertEquals(SAMPLE_REQUEST_ID, actualRequestId); } } |