1
2
3
4
5
6
7
8
9
10
11
12
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
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
54 filterChain.doFilter(teeRequest, teeResponse);
55
56
57 teeResponse.finish();
58
59
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 }