001    /**
002     * Copyright (c) 2000-2012 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.servlet.filters.invoker.FilterMapping;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    
024    import java.io.IOException;
025    
026    import java.util.ArrayList;
027    
028    import javax.servlet.FilterChain;
029    import javax.servlet.FilterConfig;
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletException;
032    import javax.servlet.ServletRequest;
033    import javax.servlet.ServletResponse;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Raymond Augé
039     * @author Eduardo Lundgren
040     */
041    public abstract class BaseFilter implements LiferayFilter {
042    
043            public void destroy() {
044                    LiferayFilterTracker.removeLiferayFilter(this);
045            }
046    
047            public void doFilter(
048                            ServletRequest servletRequest, ServletResponse servletResponse,
049                            FilterChain filterChain)
050                    throws IOException, ServletException {
051    
052                    try {
053                            HttpServletRequest request = (HttpServletRequest)servletRequest;
054                            HttpServletResponse response = (HttpServletResponse)servletResponse;
055    
056                            if (_invokerEnabled) {
057                                    processFilter(request, response, filterChain);
058                            }
059                            else {
060                                    String uri = request.getRequestURI();
061    
062                                    if (isFilterEnabled() && isFilterEnabled(request, response) &&
063                                            _filterMapping.isMatchURLRegexPattern(request, uri)) {
064    
065                                            processFilter(request, response, filterChain);
066                                    }
067                                    else {
068                                            filterChain.doFilter(servletRequest, servletResponse);
069                                    }
070                            }
071                    }
072                    catch (IOException ioe) {
073                            throw ioe;
074                    }
075                    catch (ServletException se) {
076                            throw se;
077                    }
078                    catch (Exception e) {
079                            Log log = getLog();
080    
081                            log.error(e, e);
082                    }
083            }
084    
085            public FilterConfig getFilterConfig() {
086                    return _filterConfig;
087            }
088    
089            public void init(FilterConfig filterConfig) {
090                    _filterConfig = filterConfig;
091    
092                    if (_TCK_URL) {
093                            ServletContext servletContext = _filterConfig.getServletContext();
094    
095                            _invokerEnabled = GetterUtil.get(
096                                    servletContext.getInitParameter("liferay-invoker-enabled"),
097                                    true);
098    
099                            if (!_invokerEnabled) {
100                                    _filterMapping = new FilterMapping(
101                                            this, filterConfig, new ArrayList<String>(0),
102                                            new ArrayList<String>(0));
103                            }
104                    }
105    
106                    LiferayFilterTracker.addLiferayFilter(this);
107            }
108    
109            public boolean isFilterEnabled() {
110                    return _filterEnabled;
111            }
112    
113            public boolean isFilterEnabled(
114                    HttpServletRequest request, HttpServletResponse response) {
115    
116                    return _filterEnabled;
117            }
118    
119            public void setFilterEnabled(boolean filterEnabled) {
120                    _filterEnabled = filterEnabled;
121            }
122    
123            protected abstract Log getLog();
124    
125            protected void processFilter(
126                            Class<?> filterClass, HttpServletRequest request,
127                            HttpServletResponse response, FilterChain filterChain)
128                    throws Exception {
129    
130                    long startTime = 0;
131    
132                    String threadName = null;
133                    String depther = null;
134                    String path = null;
135    
136                    Log log = getLog();
137    
138                    if (log.isDebugEnabled()) {
139                            startTime = System.currentTimeMillis();
140    
141                            Thread currentThread = Thread.currentThread();
142    
143                            threadName = currentThread.getName();
144    
145                            depther = (String)request.getAttribute(_DEPTHER);
146    
147                            if (depther == null) {
148                                    depther = StringPool.BLANK;
149                            }
150                            else {
151                                    depther += StringPool.EQUAL;
152                            }
153    
154                            request.setAttribute(_DEPTHER, depther);
155    
156                            path = request.getRequestURI();
157    
158                            log.debug(
159                                    "[" + threadName + "]" + depther + "> " +
160                                            filterClass.getName() + " " + path);
161                    }
162    
163                    filterChain.doFilter(request, response);
164    
165                    if (log.isDebugEnabled()) {
166                            long endTime = System.currentTimeMillis();
167    
168                            depther = (String)request.getAttribute(_DEPTHER);
169    
170                            if (depther == null) {
171                                    return;
172                            }
173    
174                            log.debug(
175                                    "[" + threadName + "]" + depther + "< " +
176                                            filterClass.getName() + " " + path + " " +
177                                                    (endTime - startTime) + " ms");
178    
179                            if (depther.length() > 0) {
180                                    depther = depther.substring(1);
181                            }
182    
183                            request.setAttribute(_DEPTHER, depther);
184                    }
185            }
186    
187            protected void processFilter(
188                            HttpServletRequest request, HttpServletResponse response,
189                            FilterChain filterChain)
190                    throws Exception {
191    
192                    throw new UnsupportedOperationException(
193                            "Please implement processFilter(HttpServletRequest, " +
194                                    "HttpServletResponse, FilterChain)");
195            }
196    
197            private static final String _DEPTHER = "DEPTHER";
198    
199            private static final boolean _TCK_URL = GetterUtil.getBoolean(
200                    PropsUtil.get(PropsKeys.TCK_URL));
201    
202            private FilterConfig _filterConfig;
203            private boolean _filterEnabled = true;
204            private FilterMapping _filterMapping;
205            private boolean _invokerEnabled = true;
206    
207    }