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 threadName = Thread.currentThread().getName();
113
114 depther = (String)request.getAttribute(_DEPTHER);
115
116 if (depther == null) {
117 depther = StringPool.BLANK;
118 }
119 else {
120 depther += StringPool.EQUAL;
121 }
122
123 request.setAttribute(_DEPTHER, depther);
124
125 path = request.getRequestURI();
126
127 log.debug(
128 "[" + threadName + "]" + depther + "> " +
129 filterClass.getName() + " " + path);
130 }
131
132 filterChain.doFilter(request, response);
133
134 if (log.isDebugEnabled()) {
135 long endTime = System.currentTimeMillis();
136
137 depther = (String)request.getAttribute(_DEPTHER);
138
139 log.debug(
140 "[" + threadName + "]" + depther + "< " +
141 filterClass.getName() + " " + path + " " +
142 (endTime - startTime) + " ms");
143
144 if (depther.length() > 0) {
145 depther = depther.substring(1);
146 }
147
148 request.setAttribute(_DEPTHER, depther);
149 }
150 }
151
152 private static final String _DEPTHER = "DEPTHER";
153
154 private FilterConfig _filterConfig;
155 private Class<?> _filterClass = getClass();
156 private boolean _filterEnabled = true;
157
158 }