Add request method based mapping

Closes #22
This commit is contained in:
Sebastien Deleuze
2015-10-07 12:15:39 +02:00
parent 906dead596
commit 0dabdb8207
2 changed files with 218 additions and 7 deletions
@@ -15,8 +15,14 @@
*/
package org.springframework.reactive.web.dispatch.method.annotation;
import java.util.LinkedHashMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -25,10 +31,12 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpMethod;
import org.springframework.reactive.web.dispatch.HandlerMapping;
import org.springframework.reactive.web.http.ServerHttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.HandlerMethodSelector;
@@ -42,7 +50,7 @@ public class RequestMappingHandlerMapping implements HandlerMapping,
private static final Log logger = LogFactory.getLog(RequestMappingHandlerMapping.class);
private final Map<String, HandlerMethod> methodMap = new LinkedHashMap<>();
private final Map<RequestMappingInfo, HandlerMethod> methodMap = new TreeMap<>();
private ApplicationContext applicationContext;
@@ -67,11 +75,16 @@ public class RequestMappingHandlerMapping implements HandlerMapping,
RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annotation != null && annotation.value().length > 0) {
String path = annotation.value()[0];
RequestMethod[] methods = annotation.method();
HandlerMethod handlerMethod = new HandlerMethod(bean, method);
if (logger.isInfoEnabled()) {
logger.info("Mapped \"" + path + "\" onto " + handlerMethod);
}
methodMap.put(path, handlerMethod);
RequestMappingInfo info = new RequestMappingInfo(path, methods);
if (this.methodMap.containsKey(info)) {
throw new IllegalStateException("Duplicate mapping found for " + info);
}
methodMap.put(info, handlerMethod);
}
return false;
});
@@ -81,11 +94,66 @@ public class RequestMappingHandlerMapping implements HandlerMapping,
@Override
public Object getHandler(ServerHttpRequest request) {
String path = request.getURI().getPath();
HandlerMethod handlerMethod = this.methodMap.get(path);
if (logger.isDebugEnabled()) {
logger.debug("Mapped " + path + " to [" + handlerMethod + "]");
HttpMethod method = request.getMethod();
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : this.methodMap.entrySet()) {
RequestMappingInfo info = entry.getKey();
if (path.equals(info.getPath()) && (info.getMethods().isEmpty() || info.getMethods().contains(RequestMethod.valueOf(method.name())))) {
if (logger.isDebugEnabled()) {
logger.debug("Mapped " + method + " " + path + " to [" + entry.getValue() + "]");
}
return entry.getValue();
}
}
return null;
}
private static class RequestMappingInfo implements Comparable {
private String path;
private Set<RequestMethod> methods;
public RequestMappingInfo(String path, RequestMethod... methods) {
this(path, asList(methods));
}
public RequestMappingInfo(String path, Collection<RequestMethod> methods) {
this.path = path;
this.methods = new TreeSet<>(methods);
}
public String getPath() {
return path;
}
public Set<RequestMethod> getMethods() {
return methods;
}
private static List<RequestMethod> asList(RequestMethod... requestMethods) {
return (requestMethods != null ? Arrays.asList(requestMethods) : Collections.<RequestMethod>emptyList());
}
@Override
public int compareTo(Object o) {
RequestMappingInfo other = (RequestMappingInfo)o;
if (!this.path.equals(other.getPath())) {
return -1;
}
if (this.methods.isEmpty() && !other.methods.isEmpty()) {
return 1;
}
if (!this.methods.isEmpty() && other.methods.isEmpty()) {
return -1;
}
if (this.methods.equals(other.methods)) {
return 0;
}
return -1;
}
return handlerMethod;
}
}