Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / interceptors / pre / SetLoggingContext.java
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 package org.onap.aai.interceptors.pre;
21
22 import java.io.IOException;
23
24 import javax.annotation.Priority;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.container.ContainerRequestContext;
27 import javax.ws.rs.container.ContainerRequestFilter;
28 import javax.ws.rs.container.PreMatching;
29
30 import org.onap.aai.interceptors.AAIContainerFilter;
31 import org.onap.aai.interceptors.AAIHeaderProperties;
32 import org.onap.aai.logging.LoggingContext;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.core.env.Environment;
35
36 @PreMatching
37 @Priority(AAIRequestFilterPriority.SET_LOGGING_CONTEXT)
38 public class SetLoggingContext extends AAIContainerFilter implements ContainerRequestFilter {
39
40         @Autowired
41         private Environment environment;
42
43         @Autowired
44         private HttpServletRequest httpServletRequest;
45         
46         @Override
47         public void filter(ContainerRequestContext requestContext) throws IOException {
48
49                 String uri = httpServletRequest.getRequestURI();
50                 String queryString = httpServletRequest.getQueryString();
51
52                 if(queryString != null && !queryString.isEmpty()){
53                     uri = uri + "?" + queryString;
54                 }
55
56                 String httpMethod = requestContext.getMethod();
57                 String transId = requestContext.getHeaderString(AAIHeaderProperties.TRANSACTION_ID);
58                 String fromAppId = requestContext.getHeaderString(AAIHeaderProperties.FROM_APP_ID);
59                 
60                 LoggingContext.init();
61                 LoggingContext.requestId(transId);
62                 LoggingContext.partnerName(fromAppId);
63                 LoggingContext.targetEntity(environment.getProperty("spring.application.name"));
64                 LoggingContext.component(fromAppId);
65                 LoggingContext.serviceName(httpMethod + " " + uri);
66                 LoggingContext.targetServiceName(httpMethod + " " + uri);
67         }
68         
69 }