1   /**
2    * Copyright (c) 2000-2008 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.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  
29  import java.io.IOException;
30  
31  import javax.servlet.Filter;
32  import javax.servlet.FilterChain;
33  import javax.servlet.FilterConfig;
34  import javax.servlet.ServletException;
35  import javax.servlet.ServletRequest;
36  import javax.servlet.ServletResponse;
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="BaseFilter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Raymond Augé
43   *
44   */
45  public abstract class BaseFilter implements Filter {
46  
47      public void init(FilterConfig config) throws ServletException {
48          _config = config;
49      }
50  
51      public abstract void doFilter(
52              ServletRequest req, ServletResponse res, FilterChain chain)
53          throws IOException, ServletException;
54  
55      public FilterConfig getFilterConfig() {
56          return _config;
57      }
58  
59      public void destroy() {
60      }
61  
62      protected void doFilter(
63              Class<?> filterClass, ServletRequest req, ServletResponse res,
64              FilterChain chain)
65          throws IOException, ServletException {
66  
67          long startTime = 0;
68  
69          String threadName = null;
70          String depther = null;
71          String path = null;
72  
73          if (_log.isDebugEnabled()) {
74              HttpServletRequest httpReq = (HttpServletRequest)req;
75  
76              startTime = System.currentTimeMillis();
77  
78              threadName = Thread.currentThread().getName();
79  
80              depther = (String)req.getAttribute(_DEPTHER);
81  
82              if (depther == null) {
83                  depther = StringPool.BLANK;
84              }
85              else {
86                  depther += StringPool.EQUAL;
87              }
88  
89              req.setAttribute(_DEPTHER, depther);
90  
91              path = httpReq.getRequestURI();
92  
93              _log.debug(
94                  "[" + threadName + "]" + depther + "> " +
95                      filterClass.getName() + " " + path);
96          }
97  
98          chain.doFilter(req, res);
99  
100         if (_log.isDebugEnabled()) {
101             long endTime = System.currentTimeMillis();
102 
103             depther = (String)req.getAttribute(_DEPTHER);
104 
105             _log.debug(
106                 "[" + threadName + "]" + depther + "< " +
107                     filterClass.getName() + " " + path + " " +
108                         (endTime - startTime) + " ms");
109 
110             if (depther.length() > 0) {
111                 depther = depther.substring(1);
112             }
113 
114             req.setAttribute(_DEPTHER, depther);
115         }
116     }
117 
118     private static final String _DEPTHER = "DEPTHER";
119 
120     private static Log _log = LogFactoryUtil.getLog(BaseFilter.class);
121 
122     private FilterConfig _config;
123 
124 }