001
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
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 Matcher matcher = _uriJSessionIdPattern.matcher(uri);
065
066 uri = matcher.replaceFirst(StringPool.BLANK);
067
068 boolean matchURLPattern = false;
069
070 for (String urlPattern : _urlPatterns) {
071 if (isMatchURLPattern(uri, urlPattern)) {
072 matchURLPattern = true;
073
074 break;
075 }
076 }
077
078 if (_log.isDebugEnabled()) {
079 if (matchURLPattern) {
080 _log.debug(
081 _filter.getClass() + " has a pattern match with " + uri);
082 }
083 else {
084 _log.debug(
085 _filter.getClass() +
086 " does not have a pattern match with " + uri);
087 }
088 }
089
090 if (!matchURLPattern) {
091 return false;
092 }
093
094 if (isMatchURLRegexPattern(request, uri)) {
095 return true;
096 }
097
098 return false;
099 }
100
101 public boolean isMatchURLRegexPattern(
102 HttpServletRequest request, String uri) {
103
104 String url = uri;
105
106 String queryString = request.getQueryString();
107
108 if (Validator.isNotNull(queryString)) {
109 url = url.concat(StringPool.QUESTION).concat(queryString);
110 }
111
112 boolean matchURLRegexPattern = true;
113
114 if (_urlRegexPattern != null) {
115 Matcher matcher = _urlRegexPattern.matcher(url);
116
117 matchURLRegexPattern = matcher.find();
118 }
119
120 if (matchURLRegexPattern && (_urlRegexIgnorePattern != null)) {
121 Matcher matcher = _urlRegexIgnorePattern.matcher(url);
122
123 matchURLRegexPattern = !matcher.find();
124 }
125
126 if (_log.isDebugEnabled()) {
127 if (matchURLRegexPattern) {
128 _log.debug(
129 _filter.getClass() + " has a regex match with " + url);
130 }
131 else {
132 _log.debug(
133 _filter.getClass() + " does not have a regex match with " +
134 url);
135 }
136 }
137
138 return matchURLRegexPattern;
139 }
140
141 public void setFilter(Filter filter) {
142 _filter = filter;
143 }
144
145 protected void initDispatchers(List<String> dispatchers) {
146 for (String dispatcher : dispatchers) {
147 if (dispatcher.equals("ERROR")) {
148 _dispatcherError = true;
149 }
150 else if (dispatcher.equals("FORWARD")) {
151 _dispatcherForward = true;
152 }
153 else if (dispatcher.equals("INCLUDE")) {
154 _dispatcherInclude = true;
155 }
156 else if (dispatcher.equals("REQUEST")) {
157 _dispatcherRequest = true;
158 }
159 else {
160 throw new IllegalArgumentException(
161 "Invalid dispatcher " + dispatcher);
162 }
163 }
164
165 if (!_dispatcherError && !_dispatcherForward && !_dispatcherInclude &&
166 !_dispatcherRequest) {
167
168 _dispatcherRequest = true;
169 }
170 }
171
172 protected void initFilterConfig(FilterConfig filterConfig) {
173 String urlRegexPattern = GetterUtil.getString(
174 filterConfig.getInitParameter("url-regex-pattern"));
175
176 if (Validator.isNotNull(urlRegexPattern)) {
177 _urlRegexPattern = Pattern.compile(urlRegexPattern);
178 }
179
180 String urlRegexIgnorePattern = GetterUtil.getString(
181 filterConfig.getInitParameter("url-regex-ignore-pattern"));
182
183 if (Validator.isNotNull(urlRegexIgnorePattern)) {
184 _urlRegexIgnorePattern = Pattern.compile(urlRegexIgnorePattern);
185 }
186 }
187
188 protected boolean isMatchDispatcher(Dispatcher dispatcher) {
189 if (((dispatcher == Dispatcher.ERROR) && _dispatcherError) ||
190 ((dispatcher == Dispatcher.FORWARD) && _dispatcherForward) ||
191 ((dispatcher == Dispatcher.INCLUDE) && _dispatcherInclude) ||
192 ((dispatcher == Dispatcher.REQUEST) && _dispatcherRequest)) {
193
194 return true;
195 }
196 else {
197 return false;
198 }
199 }
200
201 protected boolean isMatchURLPattern(String uri, String urlPattern) {
202 if (urlPattern.equals(uri)) {
203 return true;
204 }
205
206 if (urlPattern.equals(_SLASH_STAR)) {
207 return true;
208 }
209
210 if (urlPattern.endsWith(_SLASH_STAR)) {
211 if (urlPattern.regionMatches(0, uri, 0, urlPattern.length() - 2)) {
212 if (uri.length() == (urlPattern.length() - 2)) {
213 return true;
214 }
215 else if (CharPool.SLASH ==
216 uri.charAt(urlPattern.length() - 2)) {
217
218 return true;
219 }
220 }
221 }
222 else if (urlPattern.startsWith(_STAR_PERIOD)) {
223 int slashPos = uri.lastIndexOf(CharPool.SLASH);
224 int periodPos = uri.lastIndexOf(CharPool.PERIOD);
225
226 if ((slashPos >= 0) && (periodPos > slashPos) &&
227 (periodPos != (uri.length() - 1)) &&
228 ((uri.length() - periodPos) == (urlPattern.length() - 1))) {
229
230 if (urlPattern.regionMatches(
231 2, uri, periodPos + 1, urlPattern.length() - 2)) {
232
233 return true;
234 }
235 }
236 }
237
238 return false;
239 }
240
241 private static final String _SLASH_STAR = "