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