Skip to content

Commit f861f18

Browse files
committed
Reduce access on headers for STOMP messaging
Issue: SPR-16165
1 parent d5f34ed commit f861f18

File tree

5 files changed

+67
-45
lines changed

5 files changed

+67
-45
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -205,16 +205,20 @@ public String getDetailedLogMessage(Object payload) {
205205

206206
private StringBuilder getBaseLogMessage() {
207207
StringBuilder sb = new StringBuilder();
208-
sb.append(getMessageType().name());
209-
if (getDestination() != null) {
210-
sb.append(" destination=").append(getDestination());
208+
SimpMessageType messageType = getMessageType();
209+
sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER);
210+
String destination = getDestination();
211+
if (destination != null) {
212+
sb.append(" destination=").append(destination);
211213
}
212-
if (getSubscriptionId() != null) {
213-
sb.append(" subscriptionId=").append(getSubscriptionId());
214+
String subscriptionId = getSubscriptionId();
215+
if (subscriptionId != null) {
216+
sb.append(" subscriptionId=").append(subscriptionId);
214217
}
215218
sb.append(" session=").append(getSessionId());
216-
if (getUser() != null) {
217-
sb.append(" user=").append(getUser().getName());
219+
Principal user = getUser();
220+
if (user != null) {
221+
sb.append(" user=").append(user.getName());
218222
}
219223
return sb;
220224
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,12 @@ private Message<byte[]> decodeMessage(ByteBuffer buffer, MultiValueMap<String, S
138138
payload = readPayload(buffer, headerAccessor);
139139
}
140140
if (payload != null) {
141-
if (payload.length > 0 && !headerAccessor.getCommand().isBodyAllowed()) {
142-
throw new StompConversionException(headerAccessor.getCommand() +
143-
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
141+
if (payload.length > 0) {
142+
StompCommand stompCommand = headerAccessor.getCommand();
143+
if (stompCommand != null && !stompCommand.isBodyAllowed()) {
144+
throw new StompConversionException(stompCommand +
145+
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
146+
}
144147
}
145148
headerAccessor.updateSimpMessageHeadersFromStompHeaders();
146149
headerAccessor.setLeaveMutable(true);

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java

+37-28
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.simp.stomp;
1818

1919
import java.nio.charset.Charset;
20+
import java.security.Principal;
2021
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.List;
@@ -163,11 +164,13 @@ else if (StompCommand.CONNECT.equals(command)) {
163164
}
164165

165166
void updateStompHeadersFromSimpMessageHeaders() {
166-
if (getDestination() != null) {
167-
setNativeHeader(STOMP_DESTINATION_HEADER, getDestination());
167+
String destination = getDestination();
168+
if (destination != null) {
169+
setNativeHeader(STOMP_DESTINATION_HEADER, destination);
168170
}
169-
if (getContentType() != null) {
170-
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, getContentType().toString());
171+
MimeType contentType = getContentType();
172+
if (contentType != null) {
173+
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString());
171174
}
172175
trySetStompHeaderForSubscriptionId();
173176
}
@@ -185,21 +188,24 @@ Map<String, List<String>> getNativeHeaders() {
185188
}
186189

187190
public StompCommand updateStompCommandAsClientMessage() {
188-
if (getMessageType() != SimpMessageType.MESSAGE) {
189-
throw new IllegalStateException("Unexpected message type " + getMessageType());
191+
SimpMessageType messageType = getMessageType();
192+
if (messageType != SimpMessageType.MESSAGE) {
193+
throw new IllegalStateException("Unexpected message type " + messageType);
190194
}
191-
if (getCommand() == null) {
195+
StompCommand command = getCommand();
196+
if (command == null) {
192197
setHeader(COMMAND_HEADER, StompCommand.SEND);
193198
}
194-
else if (!getCommand().equals(StompCommand.SEND)) {
195-
throw new IllegalStateException("Unexpected STOMP command " + getCommand());
199+
else if (!command.equals(StompCommand.SEND)) {
200+
throw new IllegalStateException("Unexpected STOMP command " + command);
196201
}
197-
return getCommand();
202+
return command;
198203
}
199204

200205
public void updateStompCommandAsServerMessage() {
201-
if (getMessageType() != SimpMessageType.MESSAGE) {
202-
throw new IllegalStateException("Unexpected message type " + getMessageType());
206+
SimpMessageType messageType = getMessageType();
207+
if (messageType != SimpMessageType.MESSAGE) {
208+
throw new IllegalStateException("Unexpected message type " + messageType);
203209
}
204210
StompCommand command = getCommand();
205211
if ((command == null) || StompCommand.SEND.equals(command)) {
@@ -273,7 +279,8 @@ public void setSubscriptionId(String subscriptionId) {
273279
private void trySetStompHeaderForSubscriptionId() {
274280
String subscriptionId = getSubscriptionId();
275281
if (subscriptionId != null) {
276-
if (getCommand() != null && StompCommand.MESSAGE.equals(getCommand())) {
282+
StompCommand command = getCommand();
283+
if (command != null && StompCommand.MESSAGE.equals(command)) {
277284
setNativeHeader(STOMP_SUBSCRIPTION_HEADER, subscriptionId);
278285
}
279286
else {
@@ -286,10 +293,8 @@ private void trySetStompHeaderForSubscriptionId() {
286293
}
287294

288295
public Integer getContentLength() {
289-
if (containsNativeHeader(STOMP_CONTENT_LENGTH_HEADER)) {
290-
return Integer.valueOf(getFirstNativeHeader(STOMP_CONTENT_LENGTH_HEADER));
291-
}
292-
return null;
296+
String header = getFirstNativeHeader(STOMP_CONTENT_LENGTH_HEADER);
297+
return (header != null ? Integer.valueOf(header) : null);
293298
}
294299

295300
public void setContentLength(int contentLength) {
@@ -390,23 +395,26 @@ public void setVersion(String version) {
390395

391396
@Override
392397
public String getShortLogMessage(Object payload) {
393-
if (StompCommand.SUBSCRIBE.equals(getCommand())) {
398+
StompCommand command = getCommand();
399+
if (StompCommand.SUBSCRIBE.equals(command)) {
394400
return "SUBSCRIBE " + getDestination() + " id=" + getSubscriptionId() + appendSession();
395401
}
396-
else if (StompCommand.UNSUBSCRIBE.equals(getCommand())) {
402+
else if (StompCommand.UNSUBSCRIBE.equals(command)) {
397403
return "UNSUBSCRIBE id=" + getSubscriptionId() + appendSession();
398404
}
399-
else if (StompCommand.SEND.equals(getCommand())) {
405+
else if (StompCommand.SEND.equals(command)) {
400406
return "SEND " + getDestination() + appendSession() + appendPayload(payload);
401407
}
402-
else if (StompCommand.CONNECT.equals(getCommand())) {
403-
return "CONNECT" + (getUser() != null ? " user=" + getUser().getName() : "") + appendSession();
408+
else if (StompCommand.CONNECT.equals(command)) {
409+
Principal user = getUser();
410+
return "CONNECT" + (user != null ? " user=" + user.getName() : "") + appendSession();
404411
}
405-
else if (StompCommand.CONNECTED.equals(getCommand())) {
412+
else if (StompCommand.CONNECTED.equals(command)) {
406413
return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession();
407414
}
408-
else if (StompCommand.DISCONNECT.equals(getCommand())) {
409-
return "DISCONNECT" + (getReceipt() != null ? " receipt=" + getReceipt() : "") + appendSession();
415+
else if (StompCommand.DISCONNECT.equals(command)) {
416+
String receipt = getReceipt();
417+
return "DISCONNECT" + (receipt != null ? " receipt=" + receipt : "") + appendSession();
410418
}
411419
else {
412420
return getDetailedLogMessage(payload);
@@ -444,11 +452,12 @@ private String appendPayload(Object payload) {
444452
"Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass()));
445453
}
446454
byte[] bytes = (byte[]) payload;
447-
String contentType = (getContentType() != null ? " " + getContentType().toString() : "");
448-
if (bytes.length == 0 || getContentType() == null || !isReadableContentType()) {
455+
MimeType mimeType = getContentType();
456+
String contentType = (mimeType != null ? " " + mimeType.toString() : "");
457+
if (bytes.length == 0 || mimeType == null || !isReadableContentType()) {
449458
return contentType;
450459
}
451-
Charset charset = getContentType().getCharset();
460+
Charset charset = mimeType.getCharset();
452461
charset = (charset != null ? charset : StompDecoder.UTF8_CHARSET);
453462
return (bytes.length < 80) ?
454463
contentType + " payload=" + new String(bytes, charset) :

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ else if (payload instanceof byte[]) {
535535
}
536536

537537
protected boolean isReadableContentType() {
538+
MimeType contentType = getContentType();
538539
for (MimeType mimeType : READABLE_MIME_TYPES) {
539-
if (mimeType.includes(getContentType())) {
540+
if (mimeType.includes(contentType)) {
540541
return true;
541542
}
542543
}
@@ -557,6 +558,8 @@ public String toString() {
557558
* its type does not match the required type.
558559
* <p>This is for cases where the existence of an accessor is strongly expected
559560
* (followed up with an assertion) or where an accessor will be created otherwise.
561+
* @param message the message to get an accessor for
562+
* @param requiredType the required accessor type (or {@code null} for any)
560563
* @return an accessor instance of the specified type, or {@code null} if none
561564
* @since 4.1
562565
*/
@@ -568,6 +571,8 @@ public static <T extends MessageHeaderAccessor> T getAccessor(Message<?> message
568571
* A variation of {@link #getAccessor(org.springframework.messaging.Message, Class)}
569572
* with a {@code MessageHeaders} instance instead of a {@code Message}.
570573
* <p>This is for cases when a full message may not have been created yet.
574+
* @param messageHeaders the message headers to get an accessor for
575+
* @param requiredType the required accessor type (or {@code null} for any)
571576
* @return an accessor instance of the specified type, or {@code null} if none
572577
* @since 4.1
573578
*/

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -285,11 +285,12 @@ else if (webSocketMessage instanceof BinaryMessage) {
285285
logger.trace("From client: " + headerAccessor.getShortLogMessage(message.getPayload()));
286286
}
287287

288-
boolean isConnect = StompCommand.CONNECT.equals(headerAccessor.getCommand());
288+
StompCommand command = headerAccessor.getCommand();
289+
boolean isConnect = StompCommand.CONNECT.equals(command);
289290
if (isConnect) {
290291
this.stats.incrementConnectCount();
291292
}
292-
else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) {
293+
else if (StompCommand.DISCONNECT.equals(command)) {
293294
this.stats.incrementDisconnectCount();
294295
}
295296

@@ -308,10 +309,10 @@ else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) {
308309
if (isConnect) {
309310
publishEvent(new SessionConnectEvent(this, message, getUser(session)));
310311
}
311-
else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) {
312+
else if (StompCommand.SUBSCRIBE.equals(command)) {
312313
publishEvent(new SessionSubscribeEvent(this, message, getUser(session)));
313314
}
314-
else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) {
315+
else if (StompCommand.UNSUBSCRIBE.equals(command)) {
315316
publishEvent(new SessionUnsubscribeEvent(this, message, getUser(session)));
316317
}
317318
}

0 commit comments

Comments
 (0)