2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.aailog.filter;
23 import java.io.IOException;
24 import java.util.UUID;
26 import javax.ws.rs.client.ClientRequestContext;
27 import javax.ws.rs.client.ClientResponseContext;
28 import javax.ws.rs.client.ClientResponseFilter;
29 import javax.ws.rs.core.MultivaluedMap;
31 import org.onap.logging.filter.base.Constants;
32 import org.onap.logging.filter.base.MDCSetup;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
36 public class RestControllerClientResponseLoggingInterceptor implements ClientResponseFilter {
37 private static final Logger logger = LoggerFactory.getLogger(RestControllerClientRequestLoggingInterceptor.class);
38 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
39 private final MDCSetup mdcSetup;
40 private final String partnerName;
42 public RestControllerClientResponseLoggingInterceptor() {
43 mdcSetup = new MDCSetup();
44 partnerName = getPartnerName();
48 public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
49 post(responseContext);
52 protected void post(ClientResponseContext responseContext) {
54 mdcSetup.setLogTimestamp();
55 mdcSetup.setElapsedTimeInvokeTimestamp();
56 mdcSetup.setResponseStatusCode(getHttpStatusCode(responseContext));
57 mdcSetup.setResponseDescription(getHttpStatusCode(responseContext));
58 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(responseContext));
59 logger.info(INVOKE_RETURN, "InvokeReturn");
60 mdcSetup.clearClientMDCs();
61 } catch (Exception e) {
62 logger.warn("Error in RestControllerClientLoggingInterceptor post", e.getMessage());
66 protected int getHttpStatusCode(ClientResponseContext responseContext) {
67 return responseContext.getStatus();
70 protected String getResponseCode(ClientResponseContext responseContext) {
71 return String.valueOf(responseContext.getStatus());
74 public void setInvocationId(ClientRequestContext requestContext) {
75 String invocationId = null;
76 MultivaluedMap<String, Object> requestHeaders = requestContext.getHeaders();
77 Object id = requestHeaders.get(ONAPLogConstants.Headers.INVOCATION_ID);
79 invocationId = (String) id;
81 requestHeaders.remove(ONAPLogConstants.Headers.INVOCATION_ID);
82 if (invocationId == null) {
83 invocationId = UUID.randomUUID().toString();
85 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
88 protected String getPartnerName() {
89 return mdcSetup.getProperty(Constants.Property.PARTNER_NAME);