View Javadoc

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