This commit is contained in:
Rossen Stoyanchev 2015-04-23 11:12:24 -04:00
parent 1cd0f433e0
commit cbabb00ba2
1 changed files with 51 additions and 52 deletions

View File

@ -163,28 +163,11 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
* @return a UriComponentsBuilder instance (never {@code null}) * @return a UriComponentsBuilder instance (never {@code null})
*/ */
public static UriComponentsBuilder fromController(UriComponentsBuilder builder, Class<?> controllerType) { public static UriComponentsBuilder fromController(UriComponentsBuilder builder, Class<?> controllerType) {
if (builder != null) { builder = getBaseUrlToUse(builder);
builder = (UriComponentsBuilder) builder.clone();
}
else {
builder = ServletUriComponentsBuilder.fromCurrentServletMapping();
}
String mapping = getTypeRequestMapping(controllerType); String mapping = getTypeRequestMapping(controllerType);
return builder.path(mapping); return builder.path(mapping);
} }
private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class);
if (annot == null || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
return "/";
}
if (annot.value().length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
}
return annot.value()[0];
}
/** /**
* Create a {@link UriComponentsBuilder} from the mapping of a controller * Create a {@link UriComponentsBuilder} from the mapping of a controller
* method and an array of method argument values. This method delegates * method and an array of method argument values. This method delegates
@ -221,24 +204,6 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
return fromMethod(builder, method, args); return fromMethod(builder, method, args);
} }
private static Method getMethod(Class<?> controllerType, String methodName, Object... args) {
Method match = null;
for (Method method : controllerType.getDeclaredMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == args.length) {
if (match != null) {
throw new IllegalArgumentException("Found two methods named '" + methodName + "' having " +
Arrays.asList(args) + " arguments, controller " + controllerType.getName());
}
match = method;
}
}
if (match == null) {
throw new IllegalArgumentException("No method '" + methodName + "' with " + args.length +
" parameters found in " + controllerType.getName());
}
return match;
}
/** /**
* Create a {@link UriComponentsBuilder} by invoking a "mock" controller method. * Create a {@link UriComponentsBuilder} by invoking a "mock" controller method.
* The controller method and the supplied argument values are then used to * The controller method and the supplied argument values are then used to
@ -352,21 +317,21 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
* request or to apply a custom baseUrl not matching the current request. * request or to apply a custom baseUrl not matching the current request.
* @param builder the builder for the base URL; the builder will be cloned * @param builder the builder for the base URL; the builder will be cloned
* and therefore not modified and may be re-used for further calls. * and therefore not modified and may be re-used for further calls.
* @param mappingName the mapping name * @param name the mapping name
* @return a builder to to prepare the URI String * @return a builder to to prepare the URI String
* @throws IllegalArgumentException if the mapping name is not found or * @throws IllegalArgumentException if the mapping name is not found or
* if there is no unique match * if there is no unique match
* @since 4.2 * @since 4.2
*/ */
public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder, String mappingName) { public static MethodArgumentBuilder fromMappingName(UriComponentsBuilder builder, String name) {
RequestMappingInfoHandlerMapping handlerMapping = getRequestMappingInfoHandlerMapping(); RequestMappingInfoHandlerMapping handlerMapping = getRequestMappingInfoHandlerMapping();
List<HandlerMethod> handlerMethods = handlerMapping.getHandlerMethodsForMappingName(mappingName); List<HandlerMethod> handlerMethods = handlerMapping.getHandlerMethodsForMappingName(name);
if (handlerMethods == null) { if (handlerMethods == null) {
throw new IllegalArgumentException("Mapping mappingName not found: " + mappingName); throw new IllegalArgumentException("Mapping mappingName not found: " + name);
} }
if (handlerMethods.size() != 1) { if (handlerMethods.size() != 1) {
throw new IllegalArgumentException( throw new IllegalArgumentException("No unique match for mapping mappingName " +
"No unique match for mapping mappingName " + mappingName + ": " + handlerMethods); name + ": " + handlerMethods);
} }
return new MethodArgumentBuilder(builder, handlerMethods.get(0).getMethod()); return new MethodArgumentBuilder(builder, handlerMethods.get(0).getMethod());
} }
@ -393,27 +358,61 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder {
* This is useful when using MvcUriComponentsBuilder outside the context of * This is useful when using MvcUriComponentsBuilder outside the context of
* processing a request or to apply a custom baseUrl not matching the * processing a request or to apply a custom baseUrl not matching the
* current request. * current request.
* @param builder the builder for the base URL; the builder will be cloned * @param baseUrl the builder for the base URL; the builder will be cloned
* and therefore not modified and may be re-used for further calls. * and therefore not modified and may be re-used for further calls.
* @param method the controller method * @param method the controller method
* @param args argument values for the controller method * @param args argument values for the controller method
* @return a UriComponentsBuilder instance, never {@code null} * @return a UriComponentsBuilder instance, never {@code null}
*/ */
public static UriComponentsBuilder fromMethod(UriComponentsBuilder builder, Method method, Object... args) { public static UriComponentsBuilder fromMethod(UriComponentsBuilder baseUrl, Method method, Object... args) {
if (builder != null) { baseUrl = getBaseUrlToUse(baseUrl);
builder = (UriComponentsBuilder) builder.clone();
}
else {
builder = ServletUriComponentsBuilder.fromCurrentServletMapping();
}
String typePath = getTypeRequestMapping(method.getDeclaringClass()); String typePath = getTypeRequestMapping(method.getDeclaringClass());
String methodPath = getMethodRequestMapping(method); String methodPath = getMethodRequestMapping(method);
String path = pathMatcher.combine(typePath, methodPath); String path = pathMatcher.combine(typePath, methodPath);
builder.path(path); baseUrl.path(path);
UriComponents uriComponents = applyContributors(builder, method, args); UriComponents uriComponents = applyContributors(baseUrl, method, args);
return UriComponentsBuilder.newInstance().uriComponents(uriComponents); return UriComponentsBuilder.newInstance().uriComponents(uriComponents);
} }
private static UriComponentsBuilder getBaseUrlToUse(UriComponentsBuilder baseUrl) {
if (baseUrl != null) {
return (UriComponentsBuilder) baseUrl.clone();
}
else {
return ServletUriComponentsBuilder.fromCurrentServletMapping();
}
}
private static String getTypeRequestMapping(Class<?> controllerType) {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class);
if (annot == null || ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
return "/";
}
if (annot.value().length > 1 && logger.isWarnEnabled()) {
logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
}
return annot.value()[0];
}
private static Method getMethod(Class<?> controllerType, String methodName, Object... args) {
Method match = null;
for (Method method : controllerType.getDeclaredMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == args.length) {
if (match != null) {
throw new IllegalArgumentException("Found two methods named '" + methodName + "' having " +
Arrays.asList(args) + " arguments, controller " + controllerType.getName());
}
match = method;
}
}
if (match == null) {
throw new IllegalArgumentException("No method '" + methodName + "' with " + args.length +
" parameters found in " + controllerType.getName());
}
return match;
}
private static String getMethodRequestMapping(Method method) { private static String getMethodRequestMapping(Method method) {
RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class); RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (annot == null) { if (annot == null) {