f35e94908854b3f703397401e12c03ab3e11f98b
[aai/aai-common.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.aailog.filter;
22
23 import java.io.IOException;
24 import java.util.UUID;
25
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;
30
31 import org.onap.logging.filter.base.Constants;
32 import org.onap.logging.filter.base.MDCSetup;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
34 import org.slf4j.*;
35
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;
41
42     public RestControllerClientResponseLoggingInterceptor() {
43         mdcSetup = new MDCSetup();
44         partnerName = getPartnerName();
45     }
46
47     @Override
48     public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
49         post(responseContext);
50     }
51
52     protected void post(ClientResponseContext responseContext) {
53         try {
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());
63         }
64     }
65
66     protected int getHttpStatusCode(ClientResponseContext responseContext) {
67         return responseContext.getStatus();
68     }
69
70     protected String getResponseCode(ClientResponseContext responseContext) {
71         return String.valueOf(responseContext.getStatus());
72     }
73
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);
78         if (id != null) {
79             invocationId = (String) id;
80         }
81         requestHeaders.remove(ONAPLogConstants.Headers.INVOCATION_ID);
82         if (invocationId == null) {
83             invocationId = UUID.randomUUID().toString();
84         }
85         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
86     }
87
88     protected String getPartnerName() {
89         return mdcSetup.getProperty(Constants.Property.PARTNER_NAME);
90     }
91 }