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                            HttpServletRequest request, HttpServletResponse response,
097                            FilterChain filterChain)
098                    throws Exception {
099    
100                    Class<?> clazz = getClass();
101    
102                    processFilter(clazz.getName(), request, response, filterChain);
103            }
104    
105            protected void processFilter(
106                            String logName, HttpServletRequest request,
107                            HttpServletResponse response, FilterChain filterChain)
108                    throws Exception {
109    
110                    long startTime = 0;
111    
112                    String threadName = null;
113                    String depther = null;
114                    String path = null;
115    
116                    Log log = getLog();
117    
118                    if (log.isDebugEnabled()) {
119                            startTime = System.currentTimeMillis();
120    
121                            Thread currentThread = Thread.currentThread();
122    
123                            threadName = currentThread.getName();
124    
125                            depther = (String)request.getAttribute(_DEPTHER);
126    
127                            if (depther == null) {
128                                    depther = StringPool.BLANK;
129                            }
130                            else {
131                                    depther += StringPool.EQUAL;
132                            }
133    
134                            request.setAttribute(_DEPTHER, depther);
135    
136                            path = request.getRequestURI();
137    
138                            log.debug(
139                                    "[" + threadName + "]" + depther + "> " + logName + " " + path);
140                    }
141    
142                    filterChain.doFilter(request, response);
143    
144                    if (!log.isDebugEnabled()) {
145                            return;
146                    }
147    
148                    long endTime = System.currentTimeMillis();
149    
150                    depther = (String)request.getAttribute(_DEPTHER);
151    
152                    if (depther == null) {
153                            return;
154                    }
155    
156                    log.debug(
157                            "[" + threadName + "]" + depther + "< " +
158                                    logName + " " + path + " " + (endTime - startTime) + " ms");
159    
160                    if (depther.length() > 0) {
161                            depther = depther.substring(1);
162                    }
163    
164                    request.setAttribute(_DEPTHER, depther);
165            }
166    
167            private static final String _DEPTHER = "DEPTHER";
168    
169            private FilterConfig _filterConfig;
170            private boolean _filterEnabled = true;
171    
172    }