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