1
22
23 package com.liferay.portal.kernel.servlet;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.util.StringPool;
27
28 import java.io.IOException;
29
30 import javax.servlet.Filter;
31 import javax.servlet.FilterChain;
32 import javax.servlet.FilterConfig;
33 import javax.servlet.ServletException;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.ServletResponse;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38
39
45 public abstract class BaseFilter implements Filter {
46
47 public void init(FilterConfig filterConfig) {
48 _filterConfig = filterConfig;
49 }
50
51 public void doFilter(
52 ServletRequest servletRequest, ServletResponse servletResponse,
53 FilterChain filterChain)
54 throws IOException, ServletException {
55
56 Log log = getLog();
57
58 if (log.isDebugEnabled()) {
59 if (isFilterEnabled()) {
60 log.debug(_filterClass + " is enabled");
61 }
62 else {
63 log.debug(_filterClass + " is disabled");
64 }
65 }
66
67 HttpServletRequest request = (HttpServletRequest)servletRequest;
68 HttpServletResponse response = (HttpServletResponse)servletResponse;
69
70 if (isFilterEnabled()) {
71 processFilter(request, response, filterChain);
72 }
73 else {
74 processFilter(_filterClass, request, response, filterChain);
75 }
76 }
77
78 public FilterConfig getFilterConfig() {
79 return _filterConfig;
80 }
81
82 public void destroy() {
83 }
84
85 protected abstract Log getLog();
86
87 protected boolean isFilterEnabled() {
88 return _filterEnabled;
89 }
90
91 protected abstract void processFilter(
92 HttpServletRequest request, HttpServletResponse response,
93 FilterChain filterChain)
94 throws IOException, ServletException;
95
96 protected void processFilter(
97 Class<?> filterClass, HttpServletRequest request,
98 HttpServletResponse response, FilterChain filterChain)
99 throws IOException, ServletException {
100
101 long startTime = 0;
102
103 String threadName = null;
104 String depther = null;
105 String path = null;
106
107 Log log = getLog();
108
109 if (log.isDebugEnabled()) {
110 startTime = System.currentTimeMillis();
111
112 Thread currentThread = Thread.currentThread();
113
114 threadName = currentThread.getName();
115
116 depther = (String)request.getAttribute(_DEPTHER);
117
118 if (depther == null) {
119 depther = StringPool.BLANK;
120 }
121 else {
122 depther += StringPool.EQUAL;
123 }
124
125 request.setAttribute(_DEPTHER, depther);
126
127 path = request.getRequestURI();
128
129 log.debug(
130 "[" + threadName + "]" + depther + "> " +
131 filterClass.getName() + " " + path);
132 }
133
134 filterChain.doFilter(request, response);
135
136 if (log.isDebugEnabled()) {
137 long endTime = System.currentTimeMillis();
138
139 depther = (String)request.getAttribute(_DEPTHER);
140
141 log.debug(
142 "[" + threadName + "]" + depther + "< " +
143 filterClass.getName() + " " + path + " " +
144 (endTime - startTime) + " ms");
145
146 if (depther.length() > 0) {
147 depther = depther.substring(1);
148 }
149
150 request.setAttribute(_DEPTHER, depther);
151 }
152 }
153
154 private static final String _DEPTHER = "DEPTHER";
155
156 private FilterConfig _filterConfig;
157 private Class<?> _filterClass = getClass();
158 private boolean _filterEnabled = true;
159
160 }