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.util.StringPool;
27  
28  import java.io.IOException;
29  
30  import javax.servlet.Filter;
31  import javax.servlet.FilterChain;
32  import javax.servlet.FilterConfig;
33  import javax.servlet.ServletException;
34  import javax.servlet.ServletRequest;
35  import javax.servlet.ServletResponse;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
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 filterConfig) {
48          _filterConfig = filterConfig;
49      }
50  
51      public void doFilter(
52              ServletRequest servletRequest, ServletResponse servletResponse,
53              FilterChain filterChain)
54          throws IOException, ServletException {
55  
56          Log log = getLog();
57  
58          if (log.isDebugEnabled()) {
59              if (isFilterEnabled()) {
60                  log.debug(_filterClass + " is enabled");
61              }
62              else {
63                  log.debug(_filterClass + " is disabled");
64              }
65          }
66  
67          HttpServletRequest request = (HttpServletRequest)servletRequest;
68          HttpServletResponse response = (HttpServletResponse)servletResponse;
69  
70          if (isFilterEnabled()) {
71              processFilter(request, response, filterChain);
72          }
73          else {
74              processFilter(_filterClass, request, response, filterChain);
75          }
76      }
77  
78      public FilterConfig getFilterConfig() {
79          return _filterConfig;
80      }
81  
82      public void destroy() {
83      }
84  
85      protected abstract Log getLog();
86  
87      protected boolean isFilterEnabled() {
88          return _filterEnabled;
89      }
90  
91      protected abstract void processFilter(
92              HttpServletRequest request, HttpServletResponse response,
93              FilterChain filterChain)
94          throws IOException, ServletException;
95  
96      protected void processFilter(
97              Class<?> filterClass, HttpServletRequest request,
98              HttpServletResponse response, FilterChain filterChain)
99          throws IOException, ServletException {
100 
101         long startTime = 0;
102 
103         String threadName = null;
104         String depther = null;
105         String path = null;
106 
107         Log log = getLog();
108 
109         if (log.isDebugEnabled()) {
110             startTime = System.currentTimeMillis();
111 
112             threadName = Thread.currentThread().getName();
113 
114             depther = (String)request.getAttribute(_DEPTHER);
115 
116             if (depther == null) {
117                 depther = StringPool.BLANK;
118             }
119             else {
120                 depther += StringPool.EQUAL;
121             }
122 
123             request.setAttribute(_DEPTHER, depther);
124 
125             path = request.getRequestURI();
126 
127             log.debug(
128                 "[" + threadName + "]" + depther + "> " +
129                     filterClass.getName() + " " + path);
130         }
131 
132         filterChain.doFilter(request, response);
133 
134         if (log.isDebugEnabled()) {
135             long endTime = System.currentTimeMillis();
136 
137             depther = (String)request.getAttribute(_DEPTHER);
138 
139             log.debug(
140                 "[" + threadName + "]" + depther + "< " +
141                     filterClass.getName() + " " + path + " " +
142                         (endTime - startTime) + " ms");
143 
144             if (depther.length() > 0) {
145                 depther = depther.substring(1);
146             }
147 
148             request.setAttribute(_DEPTHER, depther);
149         }
150     }
151 
152     private static final String _DEPTHER = "DEPTHER";
153 
154     private FilterConfig _filterConfig;
155     private Class<?> _filterClass = getClass();
156     private boolean _filterEnabled = true;
157 
158 }