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.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.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.util.EnumSet;
024    import java.util.List;
025    import java.util.Set;
026    import java.util.regex.Matcher;
027    import java.util.regex.Pattern;
028    
029    import javax.servlet.Filter;
030    import javax.servlet.FilterConfig;
031    import javax.servlet.http.HttpServletRequest;
032    
033    /**
034     * @author Mika Koivisto
035     * @author Brian Wing Shun Chan
036     */
037    public class FilterMapping {
038    
039            public FilterMapping(
040                    String filterName, Filter filter, FilterConfig filterConfig,
041                    List<String> urlPatterns, List<String> dispatchers) {
042    
043                    _filterName = filterName;
044                    _filter = filter;
045                    _urlPatterns = urlPatterns;
046    
047                    String urlRegexPattern = filterConfig.getInitParameter(
048                            "url-regex-pattern");
049    
050                    if (Validator.isNull(urlRegexPattern)) {
051                            _urlRegexPattern = null;
052                    }
053                    else {
054                            _urlRegexPattern = Pattern.compile(urlRegexPattern);
055                    }
056    
057                    String urlRegexIgnorePattern = filterConfig.getInitParameter(
058                            "url-regex-ignore-pattern");
059    
060                    if (Validator.isNull(urlRegexIgnorePattern)) {
061                            _urlRegexIgnorePattern = null;
062                    }
063                    else {
064                            _urlRegexIgnorePattern = Pattern.compile(urlRegexIgnorePattern);
065                    }
066    
067                    _dispatchers = EnumSet.noneOf(Dispatcher.class);
068    
069                    for (String dispatcher : dispatchers) {
070                            _dispatchers.add(Dispatcher.valueOf(dispatcher));
071                    }
072    
073                    if (_dispatchers.isEmpty()) {
074                            _dispatchers.add(Dispatcher.REQUEST);
075                    }
076            }
077    
078            public Filter getFilter() {
079                    return _filter;
080            }
081    
082            public String getFilterName() {
083                    return _filterName;
084            }
085    
086            public boolean isMatch(
087                    HttpServletRequest request, Dispatcher dispatcher, String uri) {
088    
089                    if (!_dispatchers.contains(dispatcher) || (uri == null)) {
090                            return false;
091                    }
092    
093                    boolean matchURLPattern = false;
094    
095                    for (String urlPattern : _urlPatterns) {
096                            if (isMatchURLPattern(uri, urlPattern)) {
097                                    matchURLPattern = true;
098    
099                                    break;
100                            }
101                    }
102    
103                    if (_log.isDebugEnabled()) {
104                            if (matchURLPattern) {
105                                    _log.debug(
106                                            _filter.getClass() + " has a pattern match with " + uri);
107                            }
108                            else {
109                                    _log.debug(
110                                            _filter.getClass() +
111                                                    " does not have a pattern match with " + uri);
112                            }
113                    }
114    
115                    if (matchURLPattern && isMatchURLRegexPattern(request, uri)) {
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122            public boolean isMatchURLRegexPattern(
123                    HttpServletRequest request, String uri) {
124    
125                    String url = uri;
126    
127                    String queryString = request.getQueryString();
128    
129                    if (Validator.isNotNull(queryString)) {
130                            url = url.concat(StringPool.QUESTION).concat(queryString);
131                    }
132    
133                    boolean matchURLRegexPattern = true;
134    
135                    if (_urlRegexPattern != null) {
136                            Matcher matcher = _urlRegexPattern.matcher(url);
137    
138                            matchURLRegexPattern = matcher.find();
139                    }
140    
141                    if (matchURLRegexPattern && (_urlRegexIgnorePattern != null)) {
142                            Matcher matcher = _urlRegexIgnorePattern.matcher(url);
143    
144                            matchURLRegexPattern = !matcher.find();
145                    }
146    
147                    if (_log.isDebugEnabled()) {
148                            if (matchURLRegexPattern) {
149                                    _log.debug(
150                                            _filter.getClass() + " has a regex match with " + url);
151                            }
152                            else {
153                                    _log.debug(
154                                            _filter.getClass() + " does not have a regex match with " +
155                                                    url);
156                            }
157                    }
158    
159                    return matchURLRegexPattern;
160            }
161    
162            public FilterMapping replaceFilter(Filter filter) {
163                    return new FilterMapping(
164                            _filterName, filter, _urlPatterns, _dispatchers,
165                            _urlRegexIgnorePattern, _urlRegexPattern);
166            }
167    
168            protected boolean isMatchURLPattern(String uri, String urlPattern) {
169                    if (urlPattern.equals(uri) || urlPattern.equals(_SLASH_STAR)) {
170                            return true;
171                    }
172    
173                    if (urlPattern.endsWith(_SLASH_STAR)) {
174                            if (uri.equals(urlPattern.substring(0, urlPattern.length() - 2)) ||
175                                    uri.startsWith(
176                                            urlPattern.substring(0, urlPattern.length() - 1))) {
177    
178                                    return true;
179                            }
180                    }
181                    else if (urlPattern.startsWith(_STAR_PERIOD) &&
182                                     (uri.indexOf(CharPool.SLASH) != -1) &&
183                                     uri.endsWith(urlPattern.substring(1))) {
184    
185                            return true;
186                    }
187    
188                    return false;
189            }
190    
191            private FilterMapping(
192                    String filterName, Filter filter, List<String> urlPatterns,
193                    Set<Dispatcher> dispatchers, Pattern urlRegexIgnorePattern,
194                    Pattern urlRegexPattern) {
195    
196                    _filterName = filterName;
197                    _filter = filter;
198                    _urlPatterns = urlPatterns;
199                    _dispatchers = dispatchers;
200                    _urlRegexIgnorePattern = urlRegexIgnorePattern;
201                    _urlRegexPattern = urlRegexPattern;
202            }
203    
204            private static final String _SLASH_STAR = "/*";
205    
206            private static final String _STAR_PERIOD = "*.";
207    
208            private static final Log _log = LogFactoryUtil.getLog(FilterMapping.class);
209    
210            private final Set<Dispatcher> _dispatchers;
211            private final Filter _filter;
212            private final String _filterName;
213            private final List<String> _urlPatterns;
214            private final Pattern _urlRegexIgnorePattern;
215            private final Pattern _urlRegexPattern;
216    
217    }