Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -22,10 +22,11 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* A {@link HandlerMethodArgumentResolver} for {@link Message} parameters. Validates
|
||||
* that the generic type of the payload matches with the message value.
|
||||
* A {@link HandlerMethodArgumentResolver} for {@link Message} parameters.
|
||||
* Validates that the generic type of the payload matches with the message value.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Stephane Nicoll
|
||||
@@ -33,7 +34,6 @@ import org.springframework.messaging.handler.invocation.HandlerMethodArgumentRes
|
||||
*/
|
||||
public class MessageMethodArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return Message.class.isAssignableFrom(parameter.getParameterType());
|
||||
@@ -41,22 +41,19 @@ public class MessageMethodArgumentResolver implements HandlerMethodArgumentResol
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
|
||||
|
||||
Class<?> paramType = parameter.getParameterType();
|
||||
|
||||
if (!paramType.isAssignableFrom(message.getClass())) {
|
||||
throw new MethodArgumentTypeMismatchException(message, parameter,
|
||||
"The actual message type [" + message.getClass().getName() + "] " +
|
||||
"does not match the expected type [" + paramType.getName() + "]");
|
||||
"The actual message type [" + ClassUtils.getQualifiedName(message.getClass()) + "] " +
|
||||
"does not match the expected type [" + ClassUtils.getQualifiedName(paramType) + "]");
|
||||
}
|
||||
|
||||
Class<?> expectedPayloadType = getPayloadType(parameter);
|
||||
Object payload = message.getPayload();
|
||||
|
||||
if (expectedPayloadType != null && !expectedPayloadType.isInstance(payload)) {
|
||||
if (payload != null && expectedPayloadType != null && !expectedPayloadType.isInstance(payload)) {
|
||||
throw new MethodArgumentTypeMismatchException(message, parameter,
|
||||
"The expected Message<?> payload type [" + expectedPayloadType.getName() +
|
||||
"] does not match the actual payload type [" + payload.getClass().getName() + "]");
|
||||
"The expected Message<?> payload type [" + ClassUtils.getQualifiedName(expectedPayloadType) +
|
||||
"] does not match the actual payload type [" + ClassUtils.getQualifiedName(payload.getClass()) + "]");
|
||||
}
|
||||
|
||||
return message;
|
||||
|
||||
@@ -40,16 +40,16 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
|
||||
* {@link SendTo} or {@link SendToUser} method-level annotations.
|
||||
*
|
||||
* <p>The value returned from the method is converted, and turned to a {@link Message} and
|
||||
* sent through the provided {@link MessageChannel}. The
|
||||
* message is then enriched with the sessionId of the input message as well as the
|
||||
* destination from the annotation(s). If multiple destinations are specified, a copy of
|
||||
* the message is sent to each destination.
|
||||
* sent through the provided {@link MessageChannel}. The message is then enriched with the
|
||||
* session id of the input message as well as the destination from the annotation(s).
|
||||
* If multiple destinations are specified, a copy of the message is sent to each destination.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
@@ -116,7 +116,6 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||
/**
|
||||
* Configure a {@link MessageHeaderInitializer} to apply to the headers of all
|
||||
* messages sent to the client outbound channel.
|
||||
*
|
||||
* <p>By default this property is not set.
|
||||
*/
|
||||
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
|
||||
@@ -133,8 +132,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||
|
||||
@Override
|
||||
public boolean supportsReturnType(MethodParameter returnType) {
|
||||
if ((returnType.getMethodAnnotation(SendTo.class) != null) ||
|
||||
(returnType.getMethodAnnotation(SendToUser.class) != null)) {
|
||||
if (returnType.getMethodAnnotation(SendTo.class) != null ||
|
||||
returnType.getMethodAnnotation(SendToUser.class) != null) {
|
||||
return true;
|
||||
}
|
||||
return (!this.annotationRequired);
|
||||
@@ -164,10 +163,12 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||
for (String destination : destinations) {
|
||||
destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
|
||||
if (broadcast) {
|
||||
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(null, returnType));
|
||||
this.messagingTemplate.convertAndSendToUser(
|
||||
user, destination, returnValue, createHeaders(null, returnType));
|
||||
}
|
||||
else {
|
||||
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId, returnType));
|
||||
this.messagingTemplate.convertAndSendToUser(
|
||||
user, destination, returnValue, createHeaders(sessionId, returnType));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,7 +207,9 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||
}
|
||||
String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
|
||||
String destination = (String) message.getHeaders().get(name);
|
||||
Assert.hasText(destination, "No lookup destination header in " + message);
|
||||
if (!StringUtils.hasText(destination)) {
|
||||
throw new IllegalStateException("No lookup destination header in " + message);
|
||||
}
|
||||
|
||||
return (destination.startsWith("/") ?
|
||||
new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + "/" + destination});
|
||||
@@ -231,11 +234,11 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
|
||||
return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
|
||||
}
|
||||
|
||||
|
||||
private static class DestinationVariablePlaceholderResolver implements PlaceholderResolver {
|
||||
|
||||
private final Map<String, String> vars;
|
||||
|
||||
|
||||
public DestinationVariablePlaceholderResolver(Map<String, String> vars) {
|
||||
this.vars = vars;
|
||||
}
|
||||
|
||||
@@ -104,14 +104,16 @@ public class SubscriptionMethodReturnValueHandler implements HandlerMethodReturn
|
||||
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
|
||||
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
|
||||
|
||||
Assert.state(subscriptionId != null,
|
||||
"No subscriptionId in message=" + message + ", method=" + returnType.getMethod());
|
||||
if (subscriptionId == null) {
|
||||
throw new IllegalStateException(
|
||||
"No subscriptionId in " + message + " returned by: " + returnType.getMethod());
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Reply to @SubscribeMapping: " + returnValue);
|
||||
}
|
||||
|
||||
this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
|
||||
this.messagingTemplate.convertAndSend(
|
||||
destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
|
||||
}
|
||||
|
||||
private MessageHeaders createHeaders(String sessionId, String subscriptionId, MethodParameter returnType) {
|
||||
|
||||
Reference in New Issue
Block a user