Method links.

This commit is contained in:
Oliver Gierke
2012-10-26 15:23:13 +02:00
parent 0e0bf16957
commit 220366b240
9 changed files with 492 additions and 21 deletions

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2012 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.hateoas.mvc;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.MethodParameter;
import org.springframework.hateoas.core.AnnotationAttribute;
import org.springframework.hateoas.core.MethodParameters;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Oliver Gierke
*/
public class AnnotatedParametersParameterAccessor {
private final AnnotationAttribute attribute;
public AnnotatedParametersParameterAccessor(AnnotationAttribute attribute) {
Assert.notNull(attribute);
this.attribute = attribute;
}
public Map<String, Object> getBoundParameters(MethodInvocation invocation) {
MethodParameters parameters = new MethodParameters(invocation.getMethod());
Object[] arguments = invocation.getArguments();
Map<String, Object> result = new HashMap<String, Object>();
for (MethodParameter parameter : parameters.getParametersWith(attribute.getAnnotationType())) {
Annotation annotation = parameter.getParameterAnnotation(attribute.getAnnotationType());
String annotationAttributeValue = attribute.getValueFrom(annotation);
String key = StringUtils.hasText(annotationAttributeValue) ? annotationAttributeValue : parameter
.getParameterName();
result.put(key, arguments[parameter.getParameterIndex()]);
}
return result;
}
}

View File

@@ -15,10 +15,13 @@
*/
package org.springframework.hateoas.mvc;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.reflect.Method;
import java.net.URI;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
@@ -29,7 +32,7 @@ import org.springframework.web.util.UriTemplate;
*
* @author Oliver Gierke
*/
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
public class ControllerLinkBuilder extends UriComponentsLinkBuilder<ControllerLinkBuilder> {
/**
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
@@ -63,21 +66,28 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
Assert.notNull(controller);
RequestMapping annotation = AnnotationUtils.findAnnotation(controller, RequestMapping.class);
String[] mapping = annotation == null ? new String[0] : (String[]) AnnotationUtils.getValue(annotation);
if (mapping.length > 1) {
throw new IllegalStateException("Multiple controller mappings defined! Unable to build URI!");
}
ControllerLinkBuilder builder = new ControllerLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping());
if (mapping.length == 0) {
return builder;
UriTemplate template = new UriTemplate(DISCOVERER.getMapping(controller));
return builder.slash(template.expand(parameters));
}
UriTemplate template = new UriTemplate(mapping[0]);
return builder.slash(template.expand(parameters));
public static ControllerLinkBuilder linkTo(Object invocationValue) {
Assert.isInstanceOf(LastInvocationAware.class, invocationValue);
LastInvocationAware invocations = (LastInvocationAware) invocationValue;
MethodInvocation invocation = invocations.getLastInvocation();
Method method = invocation.getMethod();
UriTemplate template = new UriTemplate(DISCOVERER.getMapping(method));
URI uri = template.expand(accessor.getBoundParameters(invocation));
return new ControllerLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping()).slash(uri);
}
public static <T> T methodOn(Class<T> controller) {
return LinkBuilderUtils.methodOn(controller);
}
/*