Rename @[Path/Destination]Variable in spring-messaging
Issue: SPR-11208
This commit is contained in:
@@ -23,26 +23,26 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation that indicates a method parameter should be bound to a path template
|
||||
* variable. Supported on message handling methods such as
|
||||
* {@link MessageMapping @MessageMapping} for messages with path-like destination
|
||||
* semantics.
|
||||
*
|
||||
* <p>A {@code @PathVariable} template variable is always required and does not have a
|
||||
* default value to fall back on.
|
||||
* Annotation that indicates a method parameter should be bound to a template variable
|
||||
* in a destination template string. Supported on message handling methods such as
|
||||
* {@link MessageMapping @MessageMapping}.
|
||||
* <p>
|
||||
* A {@code @DestinationVariable} template variable is always required.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*
|
||||
* @see org.springframework.messaging.handler.annotation.MessageMapping
|
||||
* @see org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface PathVariable {
|
||||
public @interface DestinationVariable {
|
||||
|
||||
/**
|
||||
* The path template variable to bind to.
|
||||
* The name of the destination template variable to bind to.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
@@ -53,8 +53,8 @@ import org.springframework.messaging.Message;
|
||||
* with STOMP over WebSocket support also sub-classes such as
|
||||
* {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor}
|
||||
* for convenient access to all method arguments.</li>
|
||||
* <li>{@link PathVariable}-annotated arguments for access to URI variable
|
||||
* values extracted from the message destination (i.e. /hotels/{hotel}).
|
||||
* <li>{@link DestinationVariable}-annotated arguments for access to template
|
||||
* variable values extracted from the message destination (e.g. /hotels/{hotel}).
|
||||
* Variable values will be converted to the declared method argument type.</li>
|
||||
* <li>{@link java.security.Principal} method arguments are supported with
|
||||
* STOMP over WebSocket messages. It reflects the user logged in to the
|
||||
|
||||
@@ -22,46 +22,45 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.handler.annotation.PathVariable;
|
||||
import org.springframework.messaging.handler.annotation.DestinationVariable;
|
||||
import org.springframework.messaging.handler.annotation.ValueConstants;
|
||||
|
||||
/**
|
||||
* Resolves method parameters annotated with {@link PathVariable @PathVariable}.
|
||||
*
|
||||
* <p>A @{@link PathVariable} is a named value that gets resolved from a path
|
||||
* template variable that matches the Message destination header.
|
||||
* It is always required and does not have a default value to fall back on.
|
||||
* Resolves method parameters annotated with
|
||||
* {@link org.springframework.messaging.handler.annotation.DestinationVariable @DestinationVariable}.
|
||||
*
|
||||
* @author Brian Clozel
|
||||
* @see org.springframework.messaging.handler.annotation.PathVariable
|
||||
* @see org.springframework.messaging.MessageHeaders
|
||||
* @since 4.0
|
||||
*/
|
||||
public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
|
||||
public class DestinationVariableMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
|
||||
|
||||
public static final String PATH_TEMPLATE_VARIABLES_HEADER =
|
||||
PathVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
|
||||
public static final String DESTINATION_TEMPLATE_VARIABLES_HEADER =
|
||||
DestinationVariableMethodArgumentResolver.class.getSimpleName() + ".templateVariables";
|
||||
|
||||
|
||||
public PathVariableMethodArgumentResolver(ConversionService cs) {
|
||||
public DestinationVariableMethodArgumentResolver(ConversionService cs) {
|
||||
super(cs, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return parameter.hasParameterAnnotation(PathVariable.class);
|
||||
return parameter.hasParameterAnnotation(DestinationVariable.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
|
||||
PathVariable annotation = parameter.getParameterAnnotation(PathVariable.class);
|
||||
return new PathVariableNamedValueInfo(annotation);
|
||||
DestinationVariable annotation = parameter.getParameterAnnotation(DestinationVariable.class);
|
||||
return new DestinationVariableNamedValueInfo(annotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, String name) throws Exception {
|
||||
protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, String name)
|
||||
throws Exception {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, String> vars = (Map<String, String>) message.getHeaders().get(PATH_TEMPLATE_VARIABLES_HEADER);
|
||||
Map<String, String> vars = (Map<String, String>) message.getHeaders().get(
|
||||
DESTINATION_TEMPLATE_VARIABLES_HEADER);
|
||||
|
||||
return (vars != null) ? vars.get(name) : null;
|
||||
}
|
||||
|
||||
@@ -72,9 +71,9 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueMethod
|
||||
}
|
||||
|
||||
|
||||
private static class PathVariableNamedValueInfo extends NamedValueInfo {
|
||||
private static class DestinationVariableNamedValueInfo extends NamedValueInfo {
|
||||
|
||||
private PathVariableNamedValueInfo(PathVariable annotation) {
|
||||
private DestinationVariableNamedValueInfo(DestinationVariable annotation) {
|
||||
super(annotation.value(), true, ValueConstants.DEFAULT_NONE);
|
||||
}
|
||||
}
|
||||
@@ -41,12 +41,8 @@ import org.springframework.messaging.converter.StringMessageConverter;
|
||||
import org.springframework.messaging.core.AbstractMessageSendingTemplate;
|
||||
import org.springframework.messaging.handler.HandlerMethod;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.HeaderMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.HeadersMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.PathVariableMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver;
|
||||
import org.springframework.messaging.handler.annotation.support.*;
|
||||
import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver;
|
||||
import org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler;
|
||||
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
||||
@@ -66,10 +62,11 @@ import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
/**
|
||||
* A handler for messages delegating to {@link org.springframework.messaging.simp.annotation.SubscribeMapping @SubscribeMapping} and
|
||||
* A handler for messages delegating to
|
||||
* {@link org.springframework.messaging.simp.annotation.SubscribeMapping @SubscribeMapping} and
|
||||
* {@link MessageMapping @MessageMapping} annotated methods.
|
||||
* <p>
|
||||
* Supports Ant-style path patterns as well as URI template variables in destinations.
|
||||
* Supports Ant-style path patterns with template variables.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
@@ -226,7 +223,7 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
|
||||
// Annotation-based argument resolution
|
||||
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
|
||||
resolvers.add(new HeadersMethodArgumentResolver());
|
||||
resolvers.add(new PathVariableMethodArgumentResolver(this.conversionService));
|
||||
resolvers.add(new DestinationVariableMethodArgumentResolver(this.conversionService));
|
||||
|
||||
// Type-based argument resolution
|
||||
resolvers.add(new PrincipalMethodArgumentResolver());
|
||||
@@ -339,7 +336,7 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
|
||||
Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(matchedPattern, lookupDestination);
|
||||
|
||||
headers.setDestination(lookupDestination);
|
||||
headers.setHeader(PathVariableMethodArgumentResolver.PATH_TEMPLATE_VARIABLES_HEADER, vars);
|
||||
headers.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
|
||||
message = MessageBuilder.withPayload(message.getPayload()).setHeaders(headers).build();
|
||||
|
||||
super.handleMatch(mapping, handlerMethod, lookupDestination, message);
|
||||
|
||||
Reference in New Issue
Block a user