View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.servlet;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.UnknownHostException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.servlet.Filter;
23  import javax.servlet.FilterChain;
24  import javax.servlet.FilterConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import static ch.qos.logback.access.AccessConstants.LB_OUTPUT_BUFFER;
32  import static ch.qos.logback.access.AccessConstants.TEE_FILTER_INCLUDES_PARAM;
33  import static ch.qos.logback.access.AccessConstants.TEE_FILTER_EXCLUDES_PARAM;
34  
35  public class TeeFilter implements Filter {
36  
37      boolean active;
38  
39      @Override
40      public void destroy() {
41          // NOP
42      }
43  
44      @Override
45      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
46  
47          if (active && request instanceof HttpServletRequest) {
48              try {
49                  TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
50                  TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
51  
52                  // System.out.println("BEFORE TeeFilter. filterChain.doFilter()");
53                  filterChain.doFilter(teeRequest, teeResponse);
54                  // System.out.println("AFTER TeeFilter. filterChain.doFilter()");
55  
56                  teeResponse.finish();
57                  // let the output contents be available for later use by
58                  // logback-access-logging
59                  teeRequest.setAttribute(LB_OUTPUT_BUFFER, teeResponse.getOutputBuffer());
60              } catch (IOException e) {
61                  e.printStackTrace();
62                  throw e;
63              } catch (ServletException e) {
64                  e.printStackTrace();
65                  throw e;
66              }
67          } else {
68              filterChain.doFilter(request, response);
69          }
70  
71      }
72  
73      @Override
74      public void init(FilterConfig filterConfig) throws ServletException {
75          String includeListAsStr = filterConfig.getInitParameter(TEE_FILTER_INCLUDES_PARAM);
76          String excludeListAsStr = filterConfig.getInitParameter(TEE_FILTER_EXCLUDES_PARAM);
77          String localhostName = getLocalhostName();
78  
79          active = computeActivation(localhostName, includeListAsStr, excludeListAsStr);
80          if (active)
81              System.out.println("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
82          else
83              System.out.println("TeeFilter will be DISABLED on this host [" + localhostName + "]");
84  
85      }
86  
87      static List<String> extractNameList(String nameListAsStr) {
88          List<String> nameList = new ArrayList<String>();
89          if (nameListAsStr == null) {
90              return nameList;
91          }
92  
93          nameListAsStr = nameListAsStr.trim();
94          if (nameListAsStr.length() == 0) {
95              return nameList;
96          }
97  
98          String[] nameArray = nameListAsStr.split("[,;]");
99          for (String n : nameArray) {
100             n = n.trim();
101             nameList.add(n);
102         }
103         return nameList;
104     }
105 
106     static String getLocalhostName() {
107         String hostname = "127.0.0.1";
108 
109         try {
110             hostname = InetAddress.getLocalHost().getHostName();
111         } catch (UnknownHostException uhe) {
112             uhe.printStackTrace();
113         }
114         return hostname;
115     }
116 
117     static boolean computeActivation(String hostname, String includeListAsStr, String excludeListAsStr) {
118         List<String> includeList = extractNameList(includeListAsStr);
119         List<String> excludeList = extractNameList(excludeListAsStr);
120         boolean inIncludesList = mathesIncludesList(hostname, includeList);
121         boolean inExcludesList = mathesExcludesList(hostname, excludeList);
122         return inIncludesList && (!inExcludesList);
123     }
124 
125     static boolean mathesIncludesList(String hostname, List<String> includeList) {
126         if (includeList.isEmpty())
127             return true;
128         return includeList.contains(hostname);
129     }
130 
131     static boolean mathesExcludesList(String hostname, List<String> excludesList) {
132         if (excludesList.isEmpty())
133             return false;
134         return excludesList.contains(hostname);
135     }
136 
137 }