001    /**
002     * Copyright (c) 2000-2013 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.filters.invoker;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.List;
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    import javax.servlet.Filter;
029    import javax.servlet.FilterConfig;
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Mika Koivisto
034     * @author Brian Wing Shun Chan
035     */
036    public class FilterMapping {
037    
038            public FilterMapping(
039                    Filter filter, FilterConfig filterConfig, List<String> urlPatterns,
040                    List<String> dispatchers) {
041    
042                    _filter = filter;
043                    _urlPatterns = urlPatterns;
044    
045                    initFilterConfig(filterConfig);
046                    initDispatchers(dispatchers);
047            }
048    
049            public Filter getFilter() {
050                    return _filter;
051            }
052    
053            public boolean isMatch(
054                    HttpServletRequest request, Dispatcher dispatcher, String uri) {
055    
056                    if (!isMatchDispatcher(dispatcher)) {
057                            return false;
058                    }
059    
060                    if (uri == null) {
061                            return false;
062                    }
063    
064                    boolean matchURLPattern = false;
065    
066                    for (String urlPattern : _urlPatterns) {
067                            if (isMatchURLPattern(uri, urlPattern)) {
068                                    matchURLPattern = true;
069    
070                                    break;
071                            }
072                    }
073    
074                    if (_log.isDebugEnabled()) {
075                            if (matchURLPattern) {
076                                    _log.debug(
077                                            _filter.getClass() + " has a pattern match with " + uri);
078                            }
079                            else {
080                                    _log.debug(
081                                            _filter.getClass() +
082                                                    " does not have a pattern match with " + uri);
083                            }
084                    }
085    
086                    if (!matchURLPattern) {
087                            return false;
088                    }
089    
090                    if (isMatchURLRegexPattern(request, uri)) {
091                            return true;
092                    }
093    
094                    return false;
095            }
096    
097            public boolean isMatchURLRegexPattern(
098                    HttpServletRequest request, String uri) {
099    
100                    String url = uri;
101    
102                    String queryString = request.getQueryString();
103    
104                    if (Validator.isNotNull(queryString)) {
105                            url = url.concat(StringPool.QUESTION).concat(queryString);
106                    }
107    
108                    boolean matchURLRegexPattern = true;
109    
110                    if (_urlRegexPattern != null) {
111                            Matcher matcher = _urlRegexPattern.matcher(url);
112    
113                            matchURLRegexPattern = matcher.find();
114                    }
115    
116                    if (matchURLRegexPattern && (_urlRegexIgnorePattern != null)) {
117                            Matcher matcher = _urlRegexIgnorePattern.matcher(url);
118    
119                            matchURLRegexPattern = !matcher.find();
120                    }
121    
122                    if (_log.isDebugEnabled()) {
123                            if (matchURLRegexPattern) {
124                                    _log.debug(
125                                            _filter.getClass() + " has a regex match with " + url);
126                            }
127                            else {
128                                    _log.debug(
129                                            _filter.getClass() + " does not have a regex match with " +
130                                                    url);
131                            }
132                    }
133    
134                    return matchURLRegexPattern;
135            }
136    
137            public void setFilter(Filter filter) {
138                    _filter = filter;
139            }
140    
141            protected void initDispatchers(List<String> dispatchers) {
142                    for (String dispatcher : dispatchers) {
143                            if (dispatcher.equals("ERROR")) {
144                                    _dispatcherError = true;
145                            }
146                            else if (dispatcher.equals("FORWARD")) {
147                                    _dispatcherForward = true;
148                            }
149                            else if (dispatcher.equals("INCLUDE")) {
150                                    _dispatcherInclude = true;
151                            }
152                            else if (dispatcher.equals("REQUEST")) {
153                                    _dispatcherRequest = true;
154                            }
155                            else {
156                                    throw new IllegalArgumentException(
157                                            "Invalid dispatcher " + dispatcher);
158                            }
159                    }
160    
161                    if (!_dispatcherError && !_dispatcherForward && !_dispatcherInclude &&
162                            !_dispatcherRequest) {
163    
164                            _dispatcherRequest = true;
165                    }
166            }
167    
168            protected void initFilterConfig(FilterConfig filterConfig) {
169                    String urlRegexPattern = GetterUtil.getString(
170                            filterConfig.getInitParameter("url-regex-pattern"));
171    
172                    if (Validator.isNotNull(urlRegexPattern)) {
173                            _urlRegexPattern = Pattern.compile(urlRegexPattern);
174                    }
175    
176                    String urlRegexIgnorePattern = GetterUtil.getString(
177                            filterConfig.getInitParameter("url-regex-ignore-pattern"));
178    
179                    if (Validator.isNotNull(urlRegexIgnorePattern)) {
180                            _urlRegexIgnorePattern = Pattern.compile(urlRegexIgnorePattern);
181                    }
182            }
183    
184            protected boolean isMatchDispatcher(Dispatcher dispatcher) {
185                    if (((dispatcher == Dispatcher.ERROR) && _dispatcherError) ||
186                            ((dispatcher == Dispatcher.FORWARD) && _dispatcherForward) ||
187                            ((dispatcher == Dispatcher.INCLUDE) && _dispatcherInclude) ||
188                            ((dispatcher == Dispatcher.REQUEST) && _dispatcherRequest)) {
189    
190                            return true;
191                    }
192                    else {
193                            return false;
194                    }
195            }
196    
197            protected boolean isMatchURLPattern(String uri, String urlPattern) {
198                    if (urlPattern.equals(uri)) {
199                            return true;
200                    }
201    
202                    if (urlPattern.equals(_SLASH_STAR)) {
203                            return true;
204                    }
205    
206                    if (urlPattern.endsWith(_SLASH_STAR)) {
207                            if (urlPattern.regionMatches(0, uri, 0, urlPattern.length() - 2)) {
208                                    if (uri.length() == (urlPattern.length() - 2)) {
209                                            return true;
210                                    }
211                                    else if (CharPool.SLASH ==
212                                                            uri.charAt(urlPattern.length() - 2)) {
213    
214                                            return true;
215                                    }
216                            }
217                    }
218                    else if (urlPattern.startsWith(_STAR_PERIOD)) {
219                            int slashPos = uri.lastIndexOf(CharPool.SLASH);
220                            int periodPos = uri.lastIndexOf(CharPool.PERIOD);
221    
222                            if ((slashPos >= 0) && (periodPos > slashPos) &&
223                                    (periodPos != (uri.length() - 1)) &&
224                                    ((uri.length() - periodPos) == (urlPattern.length() - 1))) {
225    
226                                    if (urlPattern.regionMatches(
227                                                    2, uri, periodPos + 1, urlPattern.length() - 2)) {
228    
229                                            return true;
230                                    }
231                            }
232                    }
233    
234                    return false;
235            }
236    
237            private static final String _SLASH_STAR = "/*";
238    
239            private static final String _STAR_PERIOD = "*.";
240    
241            private static Log _log = LogFactoryUtil.getLog(FilterMapping.class);
242    
243            private boolean _dispatcherError;
244            private boolean _dispatcherForward;
245            private boolean _dispatcherInclude;
246            private boolean _dispatcherRequest;
247            private Filter _filter;
248            private List<String> _urlPatterns;
249            private Pattern _urlRegexIgnorePattern;
250            private Pattern _urlRegexPattern;
251    
252    }