001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.IOException;
021    
022    import javax.servlet.FilterChain;
023    import javax.servlet.FilterConfig;
024    import javax.servlet.ServletException;
025    import javax.servlet.ServletRequest;
026    import javax.servlet.ServletResponse;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Raymond Aug??
032     * @author Eduardo Lundgren
033     */
034    public abstract class BaseFilter implements LiferayFilter {
035    
036            @Override
037            public void destroy() {
038                    LiferayFilterTracker.removeLiferayFilter(this);
039            }
040    
041            @Override
042            public void doFilter(
043                            ServletRequest servletRequest, ServletResponse servletResponse,
044                            FilterChain filterChain)
045                    throws IOException, ServletException {
046    
047                    try {
048                            processFilter(
049                                    (HttpServletRequest)servletRequest,
050                                    (HttpServletResponse)servletResponse, filterChain);
051                    }
052                    catch (IOException ioe) {
053                            throw ioe;
054                    }
055                    catch (ServletException se) {
056                            throw se;
057                    }
058                    catch (Exception e) {
059                            Log log = getLog();
060    
061                            log.error(e, e);
062                    }
063            }
064    
065            public FilterConfig getFilterConfig() {
066                    return _filterConfig;
067            }
068    
069            @Override
070            public void init(FilterConfig filterConfig) {
071                    _filterConfig = filterConfig;
072    
073                    LiferayFilterTracker.addLiferayFilter(this);
074            }
075    
076            @Override
077            public boolean isFilterEnabled() {
078                    return _filterEnabled;
079            }
080    
081            @Override
082            public boolean isFilterEnabled(
083                    HttpServletRequest request, HttpServletResponse response) {
084    
085                    return _filterEnabled;
086            }
087    
088            @Override
089            public void setFilterEnabled(boolean filterEnabled) {
090                    _filterEnabled = filterEnabled;
091            }
092    
093            protected abstract Log getLog();
094    
095            protected void processFilter(
096                            Class<?> filterClass, HttpServletRequest request,
097                            HttpServletResponse response, FilterChain filterChain)
098                    throws Exception {
099    
100                    long startTime = 0;
101    
102                    String threadName = null;
103                    String depther = null;
104                    String path = null;
105    
106                    Log log = getLog();
107    
108                    if (log.isDebugEnabled()) {
109                            startTime = System.currentTimeMillis();
110    
111                            Thread currentThread = Thread.currentThread();
112    
113                            threadName = currentThread.getName();
114    
115                            depther = (String)request.getAttribute(_DEPTHER);
116    
117                            if (depther == null) {
118                                    depther = StringPool.BLANK;
119                            }
120                            else {
121                                    depther += StringPool.EQUAL;
122                            }
123    
124                            request.setAttribute(_DEPTHER, depther);
125    
126                            path = request.getRequestURI();
127    
128                            log.debug(
129                                    "[" + threadName + "]" + depther + "> " +
130                                            filterClass.getName() + " " + path);
131                    }
132    
133                    filterChain.doFilter(request, response);
134    
135                    if (!log.isDebugEnabled()) {
136                            return;
137                    }
138    
139                    long endTime = System.currentTimeMillis();
140    
141                    depther = (String)request.getAttribute(_DEPTHER);
142    
143                    if (depther == null) {
144                            return;
145                    }
146    
147                    log.debug(
148                            "[" + threadName + "]" + depther + "< " +
149                                    filterClass.getName() + " " + path + " " +
150                                            (endTime - startTime) + " ms");
151    
152                    if (depther.length() > 0) {
153                            depther = depther.substring(1);
154                    }
155    
156                    request.setAttribute(_DEPTHER, depther);
157            }
158    
159            protected void processFilter(
160                            HttpServletRequest request, HttpServletResponse response,
161                            FilterChain filterChain)
162                    throws Exception {
163    
164                    throw new UnsupportedOperationException(
165                            "Please implement processFilter(HttpServletRequest, " +
166                                    "HttpServletResponse, FilterChain)");
167            }
168    
169            private static final String _DEPTHER = "DEPTHER";
170    
171            private FilterConfig _filterConfig;
172            private boolean _filterEnabled = true;
173    
174    }