Created
May 3, 2012 01:10
-
-
Save ttddyy/2582341 to your computer and use it in GitHub Desktop.
spring3.1: factory bean that allows to map url-path -> handler-interceptors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Factory bean to generate list of HandlerMappings and MappedHandlers. | |
* | |
* MappedHandlers can be specified by url path to interceptors. | |
* | |
*/ | |
public class HandlerInterceptorsFactory extends AbstractFactoryBean<List<Object>> { | |
private List<HandlerInterceptor> handlerInterceptors = new ArrayList<HandlerInterceptor>(); | |
private Map<String, List<HandlerInterceptor>> interceptorsByPath = | |
new LinkedHashMap<String, List<HandlerInterceptor>>(); | |
private List<MappedInterceptor> additionalMappedInterceptors = new ArrayList<MappedInterceptor>(); | |
@Override | |
public Class<?> getObjectType() { | |
return List.class; | |
} | |
public HandlerInterceptorsFactory() { | |
super(); | |
} | |
@Override | |
protected List<Object> createInstance() throws Exception { | |
final List<MappedInterceptor> mappedInterceptors = getMappedInterceptors(); | |
final List<Object> interceptors = new ArrayList<Object>(); | |
interceptors.addAll(mappedInterceptors); | |
interceptors.addAll(additionalMappedInterceptors); | |
interceptors.addAll(handlerInterceptors); // regular interceptors | |
return interceptors; | |
} | |
private List<MappedInterceptor> getMappedInterceptors() { | |
// convert (path -> interceptors) to (interceptor -> paths) | |
final Map<HandlerInterceptor, List<String>> map = new LinkedHashMap<HandlerInterceptor, List<String>>(); | |
for (Map.Entry<String, List<HandlerInterceptor>> entry : interceptorsByPath.entrySet()) { | |
final String path = entry.getKey(); | |
final List<HandlerInterceptor> interceptors = entry.getValue(); | |
for (HandlerInterceptor interceptor : interceptors) { | |
List<String> paths = map.get(interceptor); | |
if (paths == null) { | |
paths = new ArrayList<String>(); | |
map.put(interceptor, paths); | |
} | |
paths.add(path); | |
} | |
} | |
final List<MappedInterceptor> result = new ArrayList<MappedInterceptor>(); | |
// create mapped interceptors | |
for (Map.Entry<HandlerInterceptor, List<String>> entry : map.entrySet()) { | |
final HandlerInterceptor interceptor = entry.getKey(); | |
final List<String> paths = entry.getValue(); | |
final MappedInterceptor mappedInterceptor = | |
new MappedInterceptor(paths.toArray(new String[paths.size()]), interceptor); | |
result.add(mappedInterceptor); | |
} | |
return result; | |
} | |
public void setInterceptorsByPath(Map<String, List<HandlerInterceptor>> interceptorsByPath) { | |
this.interceptorsByPath = interceptorsByPath; | |
} | |
public void setHandlerInterceptors(List<HandlerInterceptor> handlerInterceptors) { | |
this.handlerInterceptors = handlerInterceptors; | |
} | |
public void setAdditionalMappedInterceptors(List<MappedInterceptor> additionalMappedInterceptors) { | |
this.additionalMappedInterceptors = additionalMappedInterceptors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment