1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  
30  import java.io.IOException;
31  
32  import java.util.regex.Matcher;
33  import java.util.regex.Pattern;
34  
35  import javax.servlet.Filter;
36  import javax.servlet.FilterChain;
37  import javax.servlet.FilterConfig;
38  import javax.servlet.ServletException;
39  import javax.servlet.ServletRequest;
40  import javax.servlet.ServletResponse;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  /**
45   * <a href="BaseFilter.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Raymond Aug�
48   */
49  public abstract class BaseFilter implements Filter {
50  
51      public void init(FilterConfig filterConfig) {
52          _filterConfig = filterConfig;
53  
54          String urlRegexPattern = GetterUtil.getString(
55              filterConfig.getInitParameter("url-regex-pattern"));
56  
57          if (Validator.isNotNull(urlRegexPattern)) {
58              _urlRegexPattern = Pattern.compile(urlRegexPattern);
59          }
60  
61          _servlet24Dispatcher = filterConfig.getInitParameter(
62              "servlet-2.4-dispatcher");
63      }
64  
65      public void doFilter(
66              ServletRequest servletRequest, ServletResponse servletResponse,
67              FilterChain filterChain)
68          throws IOException, ServletException {
69  
70          Log log = getLog();
71  
72          if (log.isDebugEnabled()) {
73              if (isFilterEnabled()) {
74                  log.debug(_filterClass + " is enabled");
75              }
76              else {
77                  log.debug(_filterClass + " is disabled");
78              }
79          }
80  
81          HttpServletRequest request = (HttpServletRequest)servletRequest;
82          HttpServletResponse response = (HttpServletResponse)servletResponse;
83  
84          boolean filterEnabled = isFilterEnabled();
85  
86          if (filterEnabled && Validator.isNotNull(_servlet24Dispatcher)) {
87              if ((_servlet24Dispatcher.equalsIgnoreCase(
88                      _SERVLET_24_DISPATCHER_REQUEST)) &&
89                  (request.getRequestURL() == null)) {
90  
91                  filterEnabled = false;
92              }
93          }
94  
95          if (filterEnabled && (_urlRegexPattern != null)) {
96              StringBuilder sb = new StringBuilder();
97  
98              sb.append(request.getRequestURL());
99  
100             if (Validator.isNotNull(request.getQueryString())) {
101                 sb.append(StringPool.QUESTION);
102                 sb.append(request.getQueryString());
103             }
104 
105             Matcher matcher = _urlRegexPattern.matcher(sb.toString());
106 
107             filterEnabled = matcher.find();
108         }
109 
110         try {
111             if (filterEnabled) {
112                 processFilter(request, response, filterChain);
113             }
114             else {
115                 processFilter(_filterClass, request, response, filterChain);
116             }
117         }
118         catch (IOException ioe) {
119             throw ioe;
120         }
121         catch (ServletException se) {
122             throw se;
123         }
124         catch (Exception e) {
125             getLog().error(e, e);
126         }
127     }
128 
129     public FilterConfig getFilterConfig() {
130         return _filterConfig;
131     }
132 
133     public void destroy() {
134     }
135 
136     protected abstract Log getLog();
137 
138     protected boolean isFilterEnabled() {
139         return _filterEnabled;
140     }
141 
142     protected abstract void processFilter(
143             HttpServletRequest request, HttpServletResponse response,
144             FilterChain filterChain)
145         throws Exception;
146 
147     protected void processFilter(
148             Class<?> filterClass, HttpServletRequest request,
149             HttpServletResponse response, FilterChain filterChain)
150         throws Exception {
151 
152         long startTime = 0;
153 
154         String threadName = null;
155         String depther = null;
156         String path = null;
157 
158         Log log = getLog();
159 
160         if (log.isDebugEnabled()) {
161             startTime = System.currentTimeMillis();
162 
163             Thread currentThread = Thread.currentThread();
164 
165             threadName = currentThread.getName();
166 
167             depther = (String)request.getAttribute(_DEPTHER);
168 
169             if (depther == null) {
170                 depther = StringPool.BLANK;
171             }
172             else {
173                 depther += StringPool.EQUAL;
174             }
175 
176             request.setAttribute(_DEPTHER, depther);
177 
178             path = request.getRequestURI();
179 
180             log.debug(
181                 "[" + threadName + "]" + depther + "> " +
182                     filterClass.getName() + " " + path);
183         }
184 
185         filterChain.doFilter(request, response);
186 
187         if (log.isDebugEnabled()) {
188             long endTime = System.currentTimeMillis();
189 
190             depther = (String)request.getAttribute(_DEPTHER);
191 
192             log.debug(
193                 "[" + threadName + "]" + depther + "< " +
194                     filterClass.getName() + " " + path + " " +
195                         (endTime - startTime) + " ms");
196 
197             if (depther.length() > 0) {
198                 depther = depther.substring(1);
199             }
200 
201             request.setAttribute(_DEPTHER, depther);
202         }
203     }
204 
205     private static final String _DEPTHER = "DEPTHER";
206 
207     private static final String _SERVLET_24_DISPATCHER_REQUEST = "REQUEST";
208 
209     private FilterConfig _filterConfig;
210     private Class<?> _filterClass = getClass();
211     private boolean _filterEnabled = true;
212     private String _servlet24Dispatcher;
213     private Pattern _urlRegexPattern;
214 
215 }