diff options
Diffstat (limited to 'aai-els-onap-logging')
-rw-r--r-- | aai-els-onap-logging/pom.xml | 15 | ||||
-rw-r--r-- | aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientRequestLoggingInterceptor.java (renamed from aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptor.java) | 77 | ||||
-rw-r--r-- | aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientResponseLoggingInterceptor.java | 91 | ||||
-rw-r--r-- | aai-els-onap-logging/src/test/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptorTest.java | 24 |
4 files changed, 142 insertions, 65 deletions
diff --git a/aai-els-onap-logging/pom.xml b/aai-els-onap-logging/pom.xml index 1f7cec5c..bf4c00f3 100644 --- a/aai-els-onap-logging/pom.xml +++ b/aai-els-onap-logging/pom.xml @@ -123,7 +123,7 @@ <artifactId>commons-io</artifactId> </dependency> <dependency> - <groupId>com.sun.jersey</groupId> + <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> </dependency> <dependency> @@ -165,17 +165,4 @@ </exclusions> </dependency> </dependencies> - - <!-- <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - </plugin> - </plugins> - </build> --> </project> diff --git a/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptor.java b/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientRequestLoggingInterceptor.java index c09023d1..7a90aff0 100644 --- a/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptor.java +++ b/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientRequestLoggingInterceptor.java @@ -20,57 +20,39 @@ package org.onap.aai.aailog.filter; -import com.sun.jersey.api.client.ClientHandler; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientRequest; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.filter.ClientFilter; - +import java.io.IOException; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.UUID; import javax.ws.rs.core.MultivaluedMap; - +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import org.glassfish.jersey.client.ClientResponse; import org.onap.aai.aailog.logs.ServiceName; import org.onap.logging.filter.base.Constants; import org.onap.logging.filter.base.MDCSetup; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.slf4j.*; -public class RestControllerClientLoggingInterceptor extends ClientFilter { - private static final Logger logger = LoggerFactory.getLogger(RestControllerClientLoggingInterceptor.class); +public class RestControllerClientRequestLoggingInterceptor implements ClientRequestFilter { + private static final Logger logger = LoggerFactory.getLogger(RestControllerClientRequestLoggingInterceptor.class); private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN"); private final MDCSetup mdcSetup; private final String partnerName; - public RestControllerClientLoggingInterceptor() { + public RestControllerClientRequestLoggingInterceptor() { mdcSetup = new MDCSetup(); partnerName = getPartnerName(); } - @Override - public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException { - ClientResponse clientResponse = null; - pre(clientRequest); - // Call the next client handler in the filter chain - ClientHandler nextHandler = getNext(); - if (nextHandler != null) { - clientResponse = nextHandler.handle(clientRequest); - } - if (clientResponse != null) { - post(clientResponse); - } - return clientResponse; - } - - protected String getTargetServiceName(ClientRequest clientRequest) { - return getServiceName(clientRequest); + protected String getTargetServiceName(ClientRequestContext requestContext) { + return getServiceName(requestContext); } - protected String getServiceName(ClientRequest clientRequest) { - String path = clientRequest.getURI().getRawPath(); + protected String getServiceName(ClientRequestContext requestContext) { + String path = requestContext.getUri().getRawPath(); return ServiceName.extractServiceName(path); } @@ -82,24 +64,29 @@ public class RestControllerClientLoggingInterceptor extends ClientFilter { return String.valueOf(clientResponse.getStatus()); } - protected String getTargetEntity(ClientRequest ClientRequest) { + protected String getTargetEntity(ClientRequestContext requestContext) { return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY; }; - protected void pre(ClientRequest clientRequest) { + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + pre(requestContext); + } + + protected void pre(ClientRequestContext requestContext) { try { - setInvocationId(clientRequest); - setupMDC(clientRequest); - setupHeaders(clientRequest); + setInvocationId(requestContext); + setupMDC(requestContext); + setupHeaders(requestContext); logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke"); } catch (Exception e) { logger.warn("Error in RestControllerClientLoggingInterceptor pre", e.getMessage()); } } - public void setInvocationId(ClientRequest clientRequest) { + public void setInvocationId(ClientRequestContext requestContext) { String invocationId = null; - MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders(); + MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders(); Object id = requestHeaders.get(ONAPLogConstants.Headers.INVOCATION_ID); if (id != null) { invocationId = (String) id; @@ -111,9 +98,9 @@ public class RestControllerClientLoggingInterceptor extends ClientFilter { MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); } - protected void setupHeaders(ClientRequest clientRequest) { - String requestId = extractRequestID(clientRequest); - MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders(); + protected void setupHeaders(ClientRequestContext requestContext) { + String requestId = extractRequestID(requestContext); + MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders(); addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId); addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId); Object requestIdObj = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID); @@ -127,15 +114,15 @@ public class RestControllerClientLoggingInterceptor extends ClientFilter { } } - protected void setupMDC(ClientRequest clientRequest) { + protected void setupMDC(ClientRequestContext requestContext) { MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP, ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); - MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(clientRequest)); + MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(requestContext)); MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString()); mdcSetup.setInvocationIdFromMDC(); if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) { - String targetEntity = getTargetEntity(clientRequest); + String targetEntity = getTargetEntity(requestContext); if (targetEntity != null) { MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity); } else { @@ -144,15 +131,15 @@ public class RestControllerClientLoggingInterceptor extends ClientFilter { } if (MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME) == null) { - MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, getServiceName(clientRequest)); + MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, getServiceName(requestContext)); } mdcSetup.setServerFQDN(); } - protected String extractRequestID(ClientRequest clientRequest) { + protected String extractRequestID(ClientRequestContext requestContext) { String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID); if (requestId == null || requestId.isEmpty()) { - MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders(); + MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders(); Object requestIdObj = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID); if (requestIdObj != null) { requestId = (String) requestIdObj; diff --git a/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientResponseLoggingInterceptor.java b/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientResponseLoggingInterceptor.java new file mode 100644 index 00000000..f35e9490 --- /dev/null +++ b/aai-els-onap-logging/src/main/java/org/onap/aai/aailog/filter/RestControllerClientResponseLoggingInterceptor.java @@ -0,0 +1,91 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.aailog.filter; + +import java.io.IOException; +import java.util.UUID; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientResponseContext; +import javax.ws.rs.client.ClientResponseFilter; +import javax.ws.rs.core.MultivaluedMap; + +import org.onap.logging.filter.base.Constants; +import org.onap.logging.filter.base.MDCSetup; +import org.onap.logging.ref.slf4j.ONAPLogConstants; +import org.slf4j.*; + +public class RestControllerClientResponseLoggingInterceptor implements ClientResponseFilter { + private static final Logger logger = LoggerFactory.getLogger(RestControllerClientRequestLoggingInterceptor.class); + private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN"); + private final MDCSetup mdcSetup; + private final String partnerName; + + public RestControllerClientResponseLoggingInterceptor() { + mdcSetup = new MDCSetup(); + partnerName = getPartnerName(); + } + + @Override + public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException { + post(responseContext); + } + + protected void post(ClientResponseContext responseContext) { + try { + mdcSetup.setLogTimestamp(); + mdcSetup.setElapsedTimeInvokeTimestamp(); + mdcSetup.setResponseStatusCode(getHttpStatusCode(responseContext)); + mdcSetup.setResponseDescription(getHttpStatusCode(responseContext)); + MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(responseContext)); + logger.info(INVOKE_RETURN, "InvokeReturn"); + mdcSetup.clearClientMDCs(); + } catch (Exception e) { + logger.warn("Error in RestControllerClientLoggingInterceptor post", e.getMessage()); + } + } + + protected int getHttpStatusCode(ClientResponseContext responseContext) { + return responseContext.getStatus(); + } + + protected String getResponseCode(ClientResponseContext responseContext) { + return String.valueOf(responseContext.getStatus()); + } + + public void setInvocationId(ClientRequestContext requestContext) { + String invocationId = null; + MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders(); + Object id = requestHeaders.get(ONAPLogConstants.Headers.INVOCATION_ID); + if (id != null) { + invocationId = (String) id; + } + requestHeaders.remove(ONAPLogConstants.Headers.INVOCATION_ID); + if (invocationId == null) { + invocationId = UUID.randomUUID().toString(); + } + MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId); + } + + protected String getPartnerName() { + return mdcSetup.getProperty(Constants.Property.PARTNER_NAME); + } +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptorTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptorTest.java index 9c4b2655..84e032d8 100644 --- a/aai-els-onap-logging/src/test/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptorTest.java +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/aailog/filter/RestControllerClientLoggingInterceptorTest.java @@ -22,14 +22,14 @@ package org.onap.aai.aailog.filter; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; - -import com.sun.jersey.api.client.ClientRequest; +import static org.mockito.Mockito.when; import java.net.InetAddress; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; +import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; @@ -38,6 +38,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; import org.onap.logging.filter.base.Constants; @@ -47,17 +48,24 @@ import org.slf4j.MDC; @RunWith(MockitoJUnitRunner.class) public class RestControllerClientLoggingInterceptorTest { - private ClientRequest clientRequest; + @Spy + private ClientRequestContext clientRequest; @Spy @InjectMocks - private RestControllerClientLoggingInterceptor restControllerClientLoggingInterceptor; + private RestControllerClientRequestLoggingInterceptor restControllerClientLoggingInterceptor; @Before public void init() throws URISyntaxException { System.setProperty("javax.ws.rs.ext.RuntimeDelegate", "com.sun.ws.rs.ext.RuntimeDelegateImpl"); - clientRequest = ClientRequest.create().build( - new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1"), "GET"); + when(clientRequest.getHeaders()).thenReturn(new MultivaluedHashMap<String, Object>()); + when(clientRequest.getUri()) + .thenReturn(new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1")); + + // clientRequest = ClientRequest.create().build( + // new + // URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1"), + // "GET"); } @After @@ -71,6 +79,10 @@ public class RestControllerClientLoggingInterceptorTest { String transId = "37b3ab2a-e57e-4fe8-8d8f-eee3019efce6"; MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<String, Object>(); requestHeaders.add(Constants.HttpHeaders.TRANSACTION_ID, transId); + when(clientRequest.getHeaders()).thenReturn(requestHeaders); + when(clientRequest.getUri()) + .thenReturn(new URI("https://localhost:9999/aai/v1/cloud-infrastructure/complexes/complex/complex-1")); + clientRequest.getHeaders().putAll(requestHeaders); restControllerClientLoggingInterceptor.pre(clientRequest); MultivaluedMap<String, Object> headers = clientRequest.getHeaders(); |