001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.interceptor;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.springframework.util.AntPathMatcher;
030: import org.springframework.util.PathMatcher;
031: import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
032: import org.springframework.web.util.UrlPathHelper;
033:
034: public class PathMatchingInterceptor extends HandlerInterceptorAdapter {
035:
036: private PathMatcher pathMatcher = new AntPathMatcher();
037:
038: private UrlPathHelper urlPathHelper = new UrlPathHelper();
039:
040: private String[] includes;
041:
042: private String[] excludes;
043:
044: private boolean includesOverwriteExcludes = false;
045:
046: public void setExcludes(String[] excludes) {
047: this .excludes = excludes;
048: }
049:
050: public void setIncludes(String[] includes) {
051: this .includes = includes;
052: }
053:
054: public void setPathMatcher(PathMatcher pathMatcher) {
055: this .pathMatcher = pathMatcher;
056: }
057:
058: public void setIncludesOverwriteExcludes(
059: boolean includesOverwriteExcludes) {
060: this .includesOverwriteExcludes = includesOverwriteExcludes;
061: }
062:
063: public final boolean preHandle(HttpServletRequest request,
064: HttpServletResponse response, Object handler)
065: throws Exception {
066:
067: String lookupPath = urlPathHelper
068: .getLookupPathForRequest(request);
069: if (include(lookupPath)) {
070: return doPreHandle(request, response, handler);
071: }
072: return true;
073: }
074:
075: protected boolean doPreHandle(HttpServletRequest request,
076: HttpServletResponse response, Object handler)
077: throws Exception {
078:
079: return true;
080: }
081:
082: private boolean include(String path) {
083: if (includesOverwriteExcludes) {
084: return !anyMatch(excludes, path)
085: || anyMatch(includes, path);
086: }
087: return anyMatch(includes, path) && !anyMatch(excludes, path);
088: }
089:
090: protected boolean anyMatch(String[] patterns, String path) {
091: if (patterns == null) {
092: return true;
093: }
094: for (int i = 0; i < patterns.length; i++) {
095: if (pathMatcher.match(patterns[i], path)) {
096: return true;
097: }
098: }
099: return false;
100: }
101:
102: }
|