1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.common.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 ch.qos.logback.access.common.AccessConstants;
23 import jakarta.servlet.Filter;
24 import jakarta.servlet.FilterChain;
25 import jakarta.servlet.FilterConfig;
26 import jakarta.servlet.ServletException;
27 import jakarta.servlet.ServletRequest;
28 import jakarta.servlet.ServletResponse;
29 import jakarta.servlet.http.HttpServletRequest;
30 import jakarta.servlet.http.HttpServletResponse;
31
32 public class TeeFilter implements Filter {
33
34 boolean active;
35
36 @Override
37 public void destroy() {
38
39 }
40
41 @Override
42 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
43 throws IOException, ServletException {
44
45 if (active && request instanceof HttpServletRequest) {
46 try {
47 TeeHttpServletRequest teeRequest = new TeeHttpServletRequest((HttpServletRequest) request);
48 TeeHttpServletResponse teeResponse = new TeeHttpServletResponse((HttpServletResponse) response);
49
50
51 filterChain.doFilter(teeRequest, teeResponse);
52
53
54 teeResponse.finish();
55
56
57 teeRequest.setAttribute(AccessConstants.LB_OUTPUT_BUFFER, teeResponse.getOutputBuffer());
58 } catch (IOException e) {
59 e.printStackTrace();
60 throw e;
61 } catch (ServletException e) {
62 e.printStackTrace();
63 throw e;
64 }
65 } else {
66 filterChain.doFilter(request, response);
67 }
68
69 }
70
71 @Override
72 public void init(FilterConfig filterConfig) throws ServletException {
73 String includeListAsStr = filterConfig.getInitParameter(AccessConstants.TEE_FILTER_INCLUDES_PARAM);
74 String excludeListAsStr = filterConfig.getInitParameter(AccessConstants.TEE_FILTER_EXCLUDES_PARAM);
75 String localhostName = getLocalhostName();
76
77 active = computeActivation(localhostName, includeListAsStr, excludeListAsStr);
78 if (active)
79 System.out.println("TeeFilter will be ACTIVE on this host [" + localhostName + "]");
80 else
81 System.out.println("TeeFilter will be DISABLED on this host [" + localhostName + "]");
82
83 }
84
85 public static List<String> extractNameList(String nameListAsStr) {
86 List<String> nameList = new ArrayList<String>();
87 if (nameListAsStr == null) {
88 return nameList;
89 }
90
91 nameListAsStr = nameListAsStr.trim();
92 if (nameListAsStr.length() == 0) {
93 return nameList;
94 }
95
96 String[] nameArray = nameListAsStr.split("[,;]");
97 for (String n : nameArray) {
98 n = n.trim();
99 nameList.add(n);
100 }
101 return nameList;
102 }
103
104 static String getLocalhostName() {
105 String hostname = "127.0.0.1";
106
107 try {
108 hostname = InetAddress.getLocalHost().getHostName();
109 } catch (UnknownHostException uhe) {
110 uhe.printStackTrace();
111 }
112 return hostname;
113 }
114
115 public static boolean computeActivation(String hostname, String includeListAsStr, String excludeListAsStr) {
116 List<String> includeList = extractNameList(includeListAsStr);
117 List<String> excludeList = extractNameList(excludeListAsStr);
118 boolean inIncludesList = mathesIncludesList(hostname, includeList);
119 boolean inExcludesList = mathesExcludesList(hostname, excludeList);
120 return inIncludesList && (!inExcludesList);
121 }
122
123 static boolean mathesIncludesList(String hostname, List<String> includeList) {
124 if (includeList.isEmpty())
125 return true;
126 return includeList.contains(hostname);
127 }
128
129 static boolean mathesExcludesList(String hostname, List<String> excludesList) {
130 if (excludesList.isEmpty())
131 return false;
132 return excludesList.contains(hostname);
133 }
134
135 }