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