001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.util.StringPool;
019
020 import java.io.IOException;
021
022 import javax.servlet.FilterChain;
023 import javax.servlet.FilterConfig;
024 import javax.servlet.ServletException;
025 import javax.servlet.ServletRequest;
026 import javax.servlet.ServletResponse;
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.http.HttpServletResponse;
029
030
034 public abstract class BaseFilter implements LiferayFilter {
035
036 public void destroy() {
037 }
038
039 public void doFilter(
040 ServletRequest servletRequest, ServletResponse servletResponse,
041 FilterChain filterChain)
042 throws IOException, ServletException {
043
044 try {
045 HttpServletRequest request = (HttpServletRequest)servletRequest;
046 HttpServletResponse response = (HttpServletResponse)servletResponse;
047
048 processFilter(request, response, filterChain);
049 }
050 catch (IOException ioe) {
051 throw ioe;
052 }
053 catch (ServletException se) {
054 throw se;
055 }
056 catch (Exception e) {
057 Log log = getLog();
058
059 log.error(e, e);
060 }
061 }
062
063 public FilterConfig getFilterConfig() {
064 return _filterConfig;
065 }
066
067 public void init(FilterConfig filterConfig) {
068 _filterConfig = filterConfig;
069 }
070
071 public boolean isFilterEnabled() {
072 return _filterEnabled;
073 }
074
075 public boolean isFilterEnabled(
076 HttpServletRequest request, HttpServletResponse response) {
077
078 return _filterEnabled;
079 }
080
081 protected abstract Log getLog();
082
083 protected void processFilter(
084 Class<?> filterClass, HttpServletRequest request,
085 HttpServletResponse response, FilterChain filterChain)
086 throws Exception {
087
088 long startTime = 0;
089
090 String threadName = null;
091 String depther = null;
092 String path = null;
093
094 Log log = getLog();
095
096 if (log.isDebugEnabled()) {
097 startTime = System.currentTimeMillis();
098
099 Thread currentThread = Thread.currentThread();
100
101 threadName = currentThread.getName();
102
103 depther = (String)request.getAttribute(_DEPTHER);
104
105 if (depther == null) {
106 depther = StringPool.BLANK;
107 }
108 else {
109 depther += StringPool.EQUAL;
110 }
111
112 request.setAttribute(_DEPTHER, depther);
113
114 path = request.getRequestURI();
115
116 log.debug(
117 "[" + threadName + "]" + depther + "> " +
118 filterClass.getName() + " " + path);
119 }
120
121 filterChain.doFilter(request, response);
122
123 if (log.isDebugEnabled()) {
124 long endTime = System.currentTimeMillis();
125
126 depther = (String)request.getAttribute(_DEPTHER);
127
128 if (depther == null) {
129 return;
130 }
131
132 log.debug(
133 "[" + threadName + "]" + depther + "< " +
134 filterClass.getName() + " " + path + " " +
135 (endTime - startTime) + " ms");
136
137 if (depther.length() > 0) {
138 depther = depther.substring(1);
139 }
140
141 request.setAttribute(_DEPTHER, depther);
142 }
143 }
144
145 protected void processFilter(
146 HttpServletRequest request, HttpServletResponse response,
147 FilterChain filterChain)
148 throws Exception {
149
150 throw new UnsupportedOperationException(
151 "Please implement processFilter(HttpServletRequest, " +
152 "HttpServletResponse, FilterChain)");
153 }
154
155 private static final String _DEPTHER = "DEPTHER";
156
157 private FilterConfig _filterConfig;
158 private boolean _filterEnabled = true;
159
160 }