Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2541 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/head
13 changed files with 259 additions and 43 deletions
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
||||
import org.springframework.beans.factory.support.RootBeanDefinition; |
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser; |
||||
import org.springframework.beans.factory.xml.ParserContext; |
||||
import org.springframework.util.xml.DomUtils; |
||||
import org.springframework.web.servlet.handler.MappedInterceptor; |
||||
import org.w3c.dom.Element; |
||||
|
||||
/** |
||||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses a {@code interceptors} element to register |
||||
* a set of {@link MappedInterceptor} definitions. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { |
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) { |
||||
List<Element> interceptors = DomUtils.getChildElementsByTagName(element, "interceptor"); |
||||
for (Element interceptor : interceptors) { |
||||
RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class); |
||||
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptor.getAttribute("path")); |
||||
RootBeanDefinition interceptorDef = new RootBeanDefinition(interceptor.getAttribute("class")); |
||||
BeanDefinitionHolder holder = new BeanDefinitionHolder(interceptorDef, parserContext.getReaderContext().generateBeanName(interceptorDef)); |
||||
holder = parserContext.getDelegate().decorateBeanDefinitionIfRequired(interceptor, holder); |
||||
parserContext.getDelegate().parseConstructorArgElements(interceptor, interceptorDef); |
||||
parserContext.getDelegate().parsePropertyElements(interceptor, interceptorDef); |
||||
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, holder); |
||||
parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptorDef); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2002-2009 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.web.servlet.handler; |
||||
|
||||
import org.springframework.web.context.request.WebRequestInterceptor; |
||||
import org.springframework.web.servlet.HandlerInterceptor; |
||||
|
||||
/** |
||||
* Holds information about a HandlerInterceptor mapped to a path into the application. |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
public final class MappedInterceptor { |
||||
|
||||
private final String pathPattern; |
||||
|
||||
private final HandlerInterceptor interceptor; |
||||
|
||||
/** |
||||
* Creates a new mapped interceptor. |
||||
* @param pathPattern the path pattern |
||||
* @param interceptor the interceptor |
||||
*/ |
||||
public MappedInterceptor(String pathPattern, HandlerInterceptor interceptor) { |
||||
this.pathPattern = pathPattern; |
||||
this.interceptor = interceptor; |
||||
} |
||||
|
||||
/** |
||||
* Creates a new mapped interceptor. |
||||
* @param pathPattern the path pattern |
||||
* @param interceptor the interceptor |
||||
*/ |
||||
public MappedInterceptor(String pathPattern, WebRequestInterceptor interceptor) { |
||||
this.pathPattern = pathPattern; |
||||
this.interceptor = new WebRequestHandlerInterceptorAdapter(interceptor); |
||||
} |
||||
|
||||
/** |
||||
* The path into the application the interceptor is mapped to. |
||||
*/ |
||||
public String getPathPattern() { |
||||
return pathPattern; |
||||
} |
||||
|
||||
/** |
||||
* The actual Interceptor reference. |
||||
*/ |
||||
public HandlerInterceptor getInterceptor() { |
||||
return interceptor; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
package org.springframework.web.servlet.handler; |
||||
|
||||
import java.util.LinkedHashSet; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.util.PathMatcher; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.web.servlet.HandlerInterceptor; |
||||
|
||||
class MappedInterceptors { |
||||
|
||||
private MappedInterceptor[] mappedInterceptors; |
||||
|
||||
public MappedInterceptors(MappedInterceptor[] mappedInterceptors) { |
||||
this.mappedInterceptors = mappedInterceptors; |
||||
} |
||||
|
||||
public Set<HandlerInterceptor> getInterceptors(String lookupPath, PathMatcher pathMatcher) { |
||||
Set<HandlerInterceptor> interceptors = new LinkedHashSet<HandlerInterceptor>(); |
||||
for (MappedInterceptor interceptor : this.mappedInterceptors) { |
||||
if (matches(interceptor, lookupPath, pathMatcher)) { |
||||
interceptors.add(interceptor.getInterceptor()); |
||||
} |
||||
} |
||||
return interceptors; |
||||
} |
||||
|
||||
private boolean matches(MappedInterceptor interceptor, String lookupPath, PathMatcher pathMatcher) { |
||||
String pathPattern = interceptor.getPathPattern(); |
||||
if (StringUtils.hasText(pathPattern)) { |
||||
return pathMatcher.match(pathPattern, lookupPath); |
||||
} else { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue