Change the header to SO
[so.git] / common / src / main / java / org / openecomp / mso / logger / LogFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.openecomp.mso.logger;
22
23
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.slf4j.MDC;
34
35 import java.io.IOException;
36 import java.security.Principal;
37
38 public class LogFilter implements Filter {
39         @Override
40         public void destroy() {
41                 // Nothing to do.
42         }
43
44         @Override
45         public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
46                 final HttpServletRequest httpRequest = (HttpServletRequest) request;
47                 final HttpServletResponse httpResponse = (HttpServletResponse) response;
48
49                 MDC.clear ();
50                 MDC.put (MsoLogger.REMOTE_HOST, httpRequest.getRemoteAddr());
51
52                 Principal userPrincipal = httpRequest.getUserPrincipal();
53                 if (null != userPrincipal) {
54                         MDC.put (MsoLogger.PARTNERNAME, userPrincipal.getName ());
55                 }
56                 //Set identity of calling application / component
57                 String fromAppId = httpRequest.getHeader(MsoLogger.HEADER_FROM_APP_ID);
58                 if(fromAppId != null && !fromAppId.isEmpty()) {
59                         MDC.put (MsoLogger.FROM_APP_ID, fromAppId);
60                 }
61                 chain.doFilter(httpRequest, httpResponse);
62         }
63         
64         @Override
65         public void init(final FilterConfig config) throws ServletException {
66                 // Nothing to do
67         }
68 }