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   */
50  public abstract class BaseFilter implements Filter {
51  
52      public void init(FilterConfig filterConfig) throws ServletException {
53          _filterConfig = filterConfig;
54  
55          String urlRegexPattern = GetterUtil.getString(
56              filterConfig.getInitParameter("url-regex-pattern"));
57  
58          if (Validator.isNotNull(urlRegexPattern)) {
59              _urlRegexPattern = Pattern.compile(urlRegexPattern);
60          }
61      }
62  
63      public void doFilter(
64              ServletRequest servletRequest, ServletResponse servletResponse,
65              FilterChain filterChain)
66          throws IOException, ServletException {
67  
68          Log log = getLog();
69  
70          if (log.isDebugEnabled()) {
71              if (isFilterEnabled()) {
72                  log.debug(_filterClass + " is enabled");
73              }
74              else {
75                  log.debug(_filterClass + " is disabled");
76              }
77          }
78  
79          HttpServletRequest request = (HttpServletRequest)servletRequest;
80          HttpServletResponse response = (HttpServletResponse)servletResponse;
81  
82          boolean filterEnabled = isFilterEnabled();
83  
84          if (filterEnabled && (_urlRegexPattern != null)) {
85              Matcher matcher = _urlRegexPattern.matcher(request.getRequestURL());
86  
87              filterEnabled = matcher.matches();
88          }
89  
90          if (filterEnabled) {
91              processFilter(request, response, filterChain);
92          }
93          else {
94              processFilter(_filterClass, request, response, filterChain);
95          }
96      }
97  
98      public FilterConfig getFilterConfig() {
99          return _filterConfig;
100     }
101 
102     public void destroy() {
103     }
104 
105     protected abstract Log getLog();
106 
107     protected boolean isFilterEnabled() {
108         return _filterEnabled;
109     }
110 
111     protected abstract void processFilter(
112             HttpServletRequest request, HttpServletResponse response,
113             FilterChain filterChain)
114         throws IOException, ServletException;
115 
116     protected void processFilter(
117             Class<?> filterClass, HttpServletRequest request,
118             HttpServletResponse response, FilterChain filterChain)
119         throws IOException, ServletException {
120 
121         long startTime = 0;
122 
123         String threadName = null;
124         String depther = null;
125         String path = null;
126 
127         Log log = getLog();
128 
129         if (log.isDebugEnabled()) {
130             startTime = System.currentTimeMillis();
131 
132             Thread currentThread = Thread.currentThread();
133 
134             threadName = currentThread.getName();
135 
136             depther = (String)request.getAttribute(_DEPTHER);
137 
138             if (depther == null) {
139                 depther = StringPool.BLANK;
140             }
141             else {
142                 depther += StringPool.EQUAL;
143             }
144 
145             request.setAttribute(_DEPTHER, depther);
146 
147             path = request.getRequestURI();
148 
149             log.debug(
150                 "[" + threadName + "]" + depther + "> " +
151                     filterClass.getName() + " " + path);
152         }
153 
154         filterChain.doFilter(request, response);
155 
156         if (log.isDebugEnabled()) {
157             long endTime = System.currentTimeMillis();
158 
159             depther = (String)request.getAttribute(_DEPTHER);
160 
161             log.debug(
162                 "[" + threadName + "]" + depther + "< " +
163                     filterClass.getName() + " " + path + " " +
164                         (endTime - startTime) + " ms");
165 
166             if (depther.length() > 0) {
167                 depther = depther.substring(1);
168             }
169 
170             request.setAttribute(_DEPTHER, depther);
171         }
172     }
173 
174     private static final String _DEPTHER = "DEPTHER";
175 
176     private FilterConfig _filterConfig;
177     private Class<?> _filterClass = getClass();
178     private boolean _filterEnabled = true;
179     private Pattern _urlRegexPattern;
180 
181 }