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) {
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              StringBuilder sb = new StringBuilder();
86  
87              sb.append(request.getRequestURL());
88  
89              if (Validator.isNotNull(request.getQueryString())) {
90                  sb.append(StringPool.QUESTION);
91                  sb.append(request.getQueryString());
92              }
93  
94              Matcher matcher = _urlRegexPattern.matcher(sb.toString());
95  
96              filterEnabled = matcher.find();
97          }
98  
99          try {
100             if (filterEnabled) {
101                 processFilter(request, response, filterChain);
102             }
103             else {
104                 processFilter(_filterClass, request, response, filterChain);
105             }
106         }
107         catch (IOException ioe) {
108             throw ioe;
109         }
110         catch (ServletException se) {
111             throw se;
112         }
113         catch (Exception e) {
114             getLog().error(e, e);
115         }
116     }
117 
118     public FilterConfig getFilterConfig() {
119         return _filterConfig;
120     }
121 
122     public void destroy() {
123     }
124 
125     protected abstract Log getLog();
126 
127     protected boolean isFilterEnabled() {
128         return _filterEnabled;
129     }
130 
131     protected abstract void processFilter(
132             HttpServletRequest request, HttpServletResponse response,
133             FilterChain filterChain)
134         throws Exception;
135 
136     protected void processFilter(
137             Class<?> filterClass, HttpServletRequest request,
138             HttpServletResponse response, FilterChain filterChain)
139         throws Exception {
140 
141         long startTime = 0;
142 
143         String threadName = null;
144         String depther = null;
145         String path = null;
146 
147         Log log = getLog();
148 
149         if (log.isDebugEnabled()) {
150             startTime = System.currentTimeMillis();
151 
152             Thread currentThread = Thread.currentThread();
153 
154             threadName = currentThread.getName();
155 
156             depther = (String)request.getAttribute(_DEPTHER);
157 
158             if (depther == null) {
159                 depther = StringPool.BLANK;
160             }
161             else {
162                 depther += StringPool.EQUAL;
163             }
164 
165             request.setAttribute(_DEPTHER, depther);
166 
167             path = request.getRequestURI();
168 
169             log.debug(
170                 "[" + threadName + "]" + depther + "> " +
171                     filterClass.getName() + " " + path);
172         }
173 
174         filterChain.doFilter(request, response);
175 
176         if (log.isDebugEnabled()) {
177             long endTime = System.currentTimeMillis();
178 
179             depther = (String)request.getAttribute(_DEPTHER);
180 
181             log.debug(
182                 "[" + threadName + "]" + depther + "< " +
183                     filterClass.getName() + " " + path + " " +
184                         (endTime - startTime) + " ms");
185 
186             if (depther.length() > 0) {
187                 depther = depther.substring(1);
188             }
189 
190             request.setAttribute(_DEPTHER, depther);
191         }
192     }
193 
194     private static final String _DEPTHER = "DEPTHER";
195 
196     private FilterConfig _filterConfig;
197     private Class<?> _filterClass = getClass();
198     private boolean _filterEnabled = true;
199     private Pattern _urlRegexPattern;
200 
201 }