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)
46              throws IOException, ServletException {
47  
48          if (active && request instanceof HttpServletRequest) {
49              try {
50                  TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
51                  TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
52  
53                  // System.out.println("BEFORE TeeFilter. filterChain.doFilter()");
54                  filterChain.doFilter(teeRequest, teeResponse);
55                  // System.out.println("AFTER TeeFilter. filterChain.doFilter()");
56  
57                  teeResponse.finish();
58                  // let the output contents be available for later use by
59                  // logback-access-logging
60                  teeRequest.setAttribute(LB_OUTPUT_BUFFER, teeResponse.getOutputBuffer());
61              } catch (IOException e) {
62                  e.printStackTrace();
63                  throw e;
64              } catch (ServletException e) {
65                  e.printStackTrace();
66                  throw e;
67              }
68          } else {
69              filterChain.doFilter(request, response);
70          }
71  
72      }
73  
74      @Override
75      public void init(FilterConfig filterConfig) throws ServletException {
76          String includeListAsStr = filterConfig.getInitParameter(TEE_FILTER_INCLUDES_PARAM);
77          String excludeListAsStr = filterConfig.getInitParameter(TEE_FILTER_EXCLUDES_PARAM);
78          String localhostName = getLocalhostName();
79  
80          active = computeActivation(localhostName, includeListAsStr, excludeListAsStr);
81          if (active)
82              System.out.println("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
83          else
84              System.out.println("TeeFilter will be DISABLED on this host [" + localhostName + "]");
85  
86      }
87  
88      static List<String> extractNameList(String nameListAsStr) {
89          List<String> nameList = new ArrayList<String>();
90          if (nameListAsStr == null) {
91              return nameList;
92          }
93  
94          nameListAsStr = nameListAsStr.trim();
95          if (nameListAsStr.length() == 0) {
96              return nameList;
97          }
98  
99          String[] nameArray = nameListAsStr.split("[,;]");
100         for (String n : nameArray) {
101             n = n.trim();
102             nameList.add(n);
103         }
104         return nameList;
105     }
106 
107     static String getLocalhostName() {
108         String hostname = "127.0.0.1";
109 
110         try {
111             hostname = InetAddress.getLocalHost().getHostName();
112         } catch (UnknownHostException uhe) {
113             uhe.printStackTrace();
114         }
115         return hostname;
116     }
117 
118     static boolean computeActivation(String hostname, String includeListAsStr, String excludeListAsStr) {
119         List<String> includeList = extractNameList(includeListAsStr);
120         List<String> excludeList = extractNameList(excludeListAsStr);
121         boolean inIncludesList = mathesIncludesList(hostname, includeList);
122         boolean inExcludesList = mathesExcludesList(hostname, excludeList);
123         return inIncludesList && (!inExcludesList);
124     }
125 
126     static boolean mathesIncludesList(String hostname, List<String> includeList) {
127         if (includeList.isEmpty())
128             return true;
129         return includeList.contains(hostname);
130     }
131 
132     static boolean mathesExcludesList(String hostname, List<String> excludesList) {
133         if (excludesList.isEmpty())
134             return false;
135         return excludesList.contains(hostname);
136     }
137 
138 }