Skip to content

Commit b1d6ae7

Browse files
committed
Polishing
1 parent b35103c commit b1d6ae7

File tree

5 files changed

+41
-43
lines changed

5 files changed

+41
-43
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,41 +22,38 @@
2222
import org.springframework.core.ResolvableType;
2323
import org.springframework.messaging.Message;
2424
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
25+
import org.springframework.util.ClassUtils;
2526

2627
/**
27-
* A {@link HandlerMethodArgumentResolver} for {@link Message} parameters. Validates
28-
* that the generic type of the payload matches with the message value.
28+
* A {@link HandlerMethodArgumentResolver} for {@link Message} parameters.
29+
* Validates that the generic type of the payload matches with the message value.
2930
*
3031
* @author Rossen Stoyanchev
3132
* @author Stephane Nicoll
3233
* @since 4.0
3334
*/
3435
public class MessageMethodArgumentResolver implements HandlerMethodArgumentResolver {
3536

36-
3737
@Override
3838
public boolean supportsParameter(MethodParameter parameter) {
3939
return Message.class.isAssignableFrom(parameter.getParameterType());
4040
}
4141

4242
@Override
4343
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
44-
4544
Class<?> paramType = parameter.getParameterType();
46-
4745
if (!paramType.isAssignableFrom(message.getClass())) {
4846
throw new MethodArgumentTypeMismatchException(message, parameter,
49-
"The actual message type [" + message.getClass().getName() + "] " +
50-
"does not match the expected type [" + paramType.getName() + "]");
47+
"The actual message type [" + ClassUtils.getQualifiedName(message.getClass()) + "] " +
48+
"does not match the expected type [" + ClassUtils.getQualifiedName(paramType) + "]");
5149
}
5250

5351
Class<?> expectedPayloadType = getPayloadType(parameter);
5452
Object payload = message.getPayload();
55-
56-
if (expectedPayloadType != null && !expectedPayloadType.isInstance(payload)) {
53+
if (payload != null && expectedPayloadType != null && !expectedPayloadType.isInstance(payload)) {
5754
throw new MethodArgumentTypeMismatchException(message, parameter,
58-
"The expected Message<?> payload type [" + expectedPayloadType.getName() +
59-
"] does not match the actual payload type [" + payload.getClass().getName() + "]");
55+
"The expected Message<?> payload type [" + ClassUtils.getQualifiedName(expectedPayloadType) +
56+
"] does not match the actual payload type [" + ClassUtils.getQualifiedName(payload.getClass()) + "]");
6057
}
6158

6259
return message;

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
import org.springframework.util.ObjectUtils;
4141
import org.springframework.util.PropertyPlaceholderHelper;
4242
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
43+
import org.springframework.util.StringUtils;
4344

4445
/**
4546
* A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
4647
* {@link SendTo} or {@link SendToUser} method-level annotations.
4748
*
4849
* <p>The value returned from the method is converted, and turned to a {@link Message} and
49-
* sent through the provided {@link MessageChannel}. The
50-
* message is then enriched with the sessionId of the input message as well as the
51-
* destination from the annotation(s). If multiple destinations are specified, a copy of
52-
* the message is sent to each destination.
50+
* sent through the provided {@link MessageChannel}. The message is then enriched with the
51+
* session id of the input message as well as the destination from the annotation(s).
52+
* If multiple destinations are specified, a copy of the message is sent to each destination.
5353
*
5454
* @author Rossen Stoyanchev
5555
* @author Sebastien Deleuze
@@ -116,7 +116,6 @@ public String getDefaultUserDestinationPrefix() {
116116
/**
117117
* Configure a {@link MessageHeaderInitializer} to apply to the headers of all
118118
* messages sent to the client outbound channel.
119-
*
120119
* <p>By default this property is not set.
121120
*/
122121
public void setHeaderInitializer(MessageHeaderInitializer headerInitializer) {
@@ -133,8 +132,8 @@ public MessageHeaderInitializer getHeaderInitializer() {
133132

134133
@Override
135134
public boolean supportsReturnType(MethodParameter returnType) {
136-
if ((returnType.getMethodAnnotation(SendTo.class) != null) ||
137-
(returnType.getMethodAnnotation(SendToUser.class) != null)) {
135+
if (returnType.getMethodAnnotation(SendTo.class) != null ||
136+
returnType.getMethodAnnotation(SendToUser.class) != null) {
138137
return true;
139138
}
140139
return (!this.annotationRequired);
@@ -164,10 +163,12 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
164163
for (String destination : destinations) {
165164
destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
166165
if (broadcast) {
167-
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(null, returnType));
166+
this.messagingTemplate.convertAndSendToUser(
167+
user, destination, returnValue, createHeaders(null, returnType));
168168
}
169169
else {
170-
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, createHeaders(sessionId, returnType));
170+
this.messagingTemplate.convertAndSendToUser(
171+
user, destination, returnValue, createHeaders(sessionId, returnType));
171172
}
172173
}
173174
}
@@ -206,7 +207,9 @@ protected String[] getTargetDestinations(Annotation annotation, Message<?> messa
206207
}
207208
String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
208209
String destination = (String) message.getHeaders().get(name);
209-
Assert.hasText(destination, "No lookup destination header in " + message);
210+
if (!StringUtils.hasText(destination)) {
211+
throw new IllegalStateException("No lookup destination header in " + message);
212+
}
210213

211214
return (destination.startsWith("/") ?
212215
new String[] {defaultPrefix + destination} : new String[] {defaultPrefix + "/" + destination});
@@ -231,11 +234,11 @@ public String toString() {
231234
return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
232235
}
233236

237+
234238
private static class DestinationVariablePlaceholderResolver implements PlaceholderResolver {
235239

236240
private final Map<String, String> vars;
237241

238-
239242
public DestinationVariablePlaceholderResolver(Map<String, String> vars) {
240243
this.vars = vars;
241244
}

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
104104
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
105105
String subscriptionId = SimpMessageHeaderAccessor.getSubscriptionId(headers);
106106

107-
Assert.state(subscriptionId != null,
108-
"No subscriptionId in message=" + message + ", method=" + returnType.getMethod());
107+
if (subscriptionId == null) {
108+
throw new IllegalStateException(
109+
"No subscriptionId in " + message + " returned by: " + returnType.getMethod());
110+
}
109111

110112
if (logger.isDebugEnabled()) {
111113
logger.debug("Reply to @SubscribeMapping: " + returnValue);
112114
}
113-
114-
this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
115+
this.messagingTemplate.convertAndSend(
116+
destination, returnValue, createHeaders(sessionId, subscriptionId, returnType));
115117
}
116118

117119
private MessageHeaders createHeaders(String sessionId, String subscriptionId, MethodParameter returnType) {

spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolverTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -145,6 +145,7 @@ public void resolveWrongMessageType() throws Exception {
145145
assertSame(message, this.resolver.resolveArgument(parameter, message));
146146
}
147147

148+
148149
@SuppressWarnings("unused")
149150
private void handleMessage(
150151
Message<?> wildcardPayload,

spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ public static Iterable<Object[]> arguments() {
8383

8484
@Override
8585
protected Class<?>[] getAnnotatedConfigClasses() {
86-
return new Class<?>[] { TestMessageBrokerConfiguration.class, TestMessageBrokerConfigurer.class };
86+
return new Class<?>[] {TestMessageBrokerConfiguration.class, TestMessageBrokerConfigurer.class};
8787
}
8888

8989

9090
@Test
9191
public void sendMessageToController() throws Exception {
92-
9392
TextMessage message = create(StompCommand.SEND).headers("destination:/app/simple").build();
9493
WebSocketSession session = doHandshake(new TestClientWebSocketHandler(0, message), "/ws").get();
9594

@@ -104,10 +103,8 @@ public void sendMessageToController() throws Exception {
104103

105104
@Test
106105
public void sendMessageToControllerAndReceiveReplyViaTopic() throws Exception {
107-
108106
TextMessage message1 = create(StompCommand.SUBSCRIBE)
109107
.headers("id:subs1", "destination:/topic/increment").build();
110-
111108
TextMessage message2 = create(StompCommand.SEND)
112109
.headers("destination:/app/increment").body("5").build();
113110

@@ -126,7 +123,6 @@ public void sendMessageToControllerAndReceiveReplyViaTopic() throws Exception {
126123

127124
@Test
128125
public void sendMessageToBrokerAndReceiveReplyViaTopic() throws Exception {
129-
130126
TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", "destination:/topic/foo").build();
131127
TextMessage m2 = create(StompCommand.SEND).headers("destination:/topic/foo").body("5").build();
132128

@@ -148,7 +144,6 @@ public void sendMessageToBrokerAndReceiveReplyViaTopic() throws Exception {
148144

149145
@Test
150146
public void sendSubscribeToControllerAndReceiveReply() throws Exception {
151-
152147
String destHeader = "destination:/app/number";
153148
TextMessage message = create(StompCommand.SUBSCRIBE).headers("id:subs1", destHeader).build();
154149

@@ -168,7 +163,6 @@ public void sendSubscribeToControllerAndReceiveReply() throws Exception {
168163

169164
@Test
170165
public void handleExceptionAndSendToUser() throws Exception {
171-
172166
String destHeader = "destination:/user/queue/error";
173167
TextMessage m1 = create(StompCommand.SUBSCRIBE).headers("id:subs1", destHeader).build();
174168
TextMessage m2 = create(StompCommand.SEND).headers("destination:/app/exception").build();
@@ -178,7 +172,6 @@ public void handleExceptionAndSendToUser() throws Exception {
178172

179173
try {
180174
assertTrue(clientHandler.latch.await(2, TimeUnit.SECONDS));
181-
182175
String payload = clientHandler.actual.get(0).getPayload();
183176
assertTrue(payload.startsWith("MESSAGE\n"));
184177
assertTrue(payload.contains("destination:/user/queue/error\n"));
@@ -191,10 +184,8 @@ public void handleExceptionAndSendToUser() throws Exception {
191184

192185
@Test
193186
public void webSocketScope() throws Exception {
194-
195187
TextMessage message1 = create(StompCommand.SUBSCRIBE)
196188
.headers("id:subs1", "destination:/topic/scopedBeanValue").build();
197-
198189
TextMessage message2 = create(StompCommand.SEND)
199190
.headers("destination:/app/scopedBeanValue").build();
200191

@@ -203,7 +194,6 @@ public void webSocketScope() throws Exception {
203194

204195
try {
205196
assertTrue(clientHandler.latch.await(2, TimeUnit.SECONDS));
206-
207197
String payload = clientHandler.actual.get(0).getPayload();
208198
assertTrue(payload.startsWith("MESSAGE\n"));
209199
assertTrue(payload.contains("destination:/topic/scopedBeanValue\n"));
@@ -221,6 +211,7 @@ public void webSocketScope() throws Exception {
221211
private @interface IntegrationTestController {
222212
}
223213

214+
224215
@IntegrationTestController
225216
static class SimpleController {
226217

@@ -243,6 +234,7 @@ public String handleException(IllegalArgumentException ex) {
243234
}
244235
}
245236

237+
246238
@IntegrationTestController
247239
static class IncrementController {
248240

@@ -257,6 +249,7 @@ public int number() {
257249
}
258250
}
259251

252+
260253
@IntegrationTestController
261254
static class ScopedBeanController {
262255

@@ -279,6 +272,7 @@ static interface ScopedBean {
279272
String getValue();
280273
}
281274

275+
282276
static class ScopedBeanImpl implements ScopedBean {
283277

284278
private final String value;
@@ -304,7 +298,6 @@ private static class TestClientWebSocketHandler extends TextWebSocketHandler {
304298

305299
private final CountDownLatch latch;
306300

307-
308301
public TestClientWebSocketHandler(int expectedNumberOfMessages, TextMessage... messagesToSend) {
309302
this.messagesToSend = messagesToSend;
310303
this.expected = expectedNumberOfMessages;
@@ -325,6 +318,7 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
325318
}
326319
}
327320

321+
328322
@Configuration
329323
@ComponentScan(
330324
basePackageClasses=StompWebSocketIntegrationTests.class,
@@ -333,7 +327,7 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
333327
static class TestMessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
334328

335329
@Autowired
336-
private HandshakeHandler handshakeHandler; // can't rely on classpath for server detection
330+
private HandshakeHandler handshakeHandler; // can't rely on classpath for server detection
337331

338332
@Override
339333
public void registerStompEndpoints(StompEndpointRegistry registry) {
@@ -353,19 +347,20 @@ public ScopedBean scopedBean() {
353347
}
354348
}
355349

350+
356351
@Configuration
357352
static class TestMessageBrokerConfiguration extends DelegatingWebSocketMessageBrokerConfiguration {
358353

359354
@Override
360355
@Bean
361356
public AbstractSubscribableChannel clientInboundChannel() {
362-
return new ExecutorSubscribableChannel(); // synchronous
357+
return new ExecutorSubscribableChannel(); // synchronous
363358
}
364359

365360
@Override
366361
@Bean
367362
public AbstractSubscribableChannel clientOutboundChannel() {
368-
return new ExecutorSubscribableChannel(); // synchronous
363+
return new ExecutorSubscribableChannel(); // synchronous
369364
}
370365
}
371366

0 commit comments

Comments
 (0)