diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java index abb71aaa9d01..82e8fd833e65 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageHeaderAccessor.java @@ -215,15 +215,18 @@ private StringBuilder getBaseLogMessage() { StringBuilder sb = new StringBuilder(); SimpMessageType messageType = getMessageType(); sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER); - if (getDestination() != null) { - sb.append(" destination=").append(getDestination()); + String destination = getDestination(); + if (destination != null) { + sb.append(" destination=").append(destination); } - if (getSubscriptionId() != null) { - sb.append(" subscriptionId=").append(getSubscriptionId()); + String subscriptionId = getSubscriptionId(); + if (subscriptionId != null) { + sb.append(" subscriptionId=").append(subscriptionId); } sb.append(" session=").append(getSessionId()); - if (getUser() != null) { - sb.append(" user=").append(getUser().getName()); + Principal user = getUser(); + if (user != null) { + sb.append(" user=").append(user.getName()); } return sb; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java index 5ae43cbbde2d..ddd7bb1c54bd 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java @@ -150,7 +150,7 @@ private Message decodeMessage(ByteBuffer byteBuffer, @Nullable MultiValu if (payload.length > 0) { StompCommand stompCommand = headerAccessor.getCommand(); if (stompCommand != null && !stompCommand.isBodyAllowed()) { - throw new StompConversionException(headerAccessor.getCommand() + + throw new StompConversionException(stompCommand + " shouldn't have a payload: length=" + payload.length + ", headers=" + headers); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java index 5b19e6de7ff3..c3dc19270498 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaderAccessor.java @@ -18,6 +18,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.security.Principal; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -165,11 +166,13 @@ else if (StompCommand.CONNECT.equals(command)) { } void updateStompHeadersFromSimpMessageHeaders() { - if (getDestination() != null) { - setNativeHeader(STOMP_DESTINATION_HEADER, getDestination()); + String destination = getDestination(); + if (destination != null) { + setNativeHeader(STOMP_DESTINATION_HEADER, destination); } - if (getContentType() != null) { - setNativeHeader(STOMP_CONTENT_TYPE_HEADER, getContentType().toString()); + MimeType contentType = getContentType(); + if (contentType != null) { + setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString()); } trySetStompHeaderForSubscriptionId(); } @@ -188,21 +191,24 @@ protected Map> getNativeHeaders() { } public StompCommand updateStompCommandAsClientMessage() { - if (getMessageType() != SimpMessageType.MESSAGE) { - throw new IllegalStateException("Unexpected message type " + getMessageType()); + SimpMessageType messageType = getMessageType(); + if (messageType != SimpMessageType.MESSAGE) { + throw new IllegalStateException("Unexpected message type " + messageType); } - if (getCommand() == null) { + StompCommand command = getCommand(); + if (command == null) { setHeader(COMMAND_HEADER, StompCommand.SEND); } - else if (!getCommand().equals(StompCommand.SEND)) { - throw new IllegalStateException("Unexpected STOMP command " + getCommand()); + else if (!command.equals(StompCommand.SEND)) { + throw new IllegalStateException("Unexpected STOMP command " + command); } - return getCommand(); + return command; } public void updateStompCommandAsServerMessage() { - if (getMessageType() != SimpMessageType.MESSAGE) { - throw new IllegalStateException("Unexpected message type " + getMessageType()); + SimpMessageType messageType = getMessageType(); + if (messageType != SimpMessageType.MESSAGE) { + throw new IllegalStateException("Unexpected message type " + messageType); } StompCommand command = getCommand(); if ((command == null) || StompCommand.SEND.equals(command)) { @@ -278,7 +284,8 @@ public void setSubscriptionId(@Nullable String subscriptionId) { private void trySetStompHeaderForSubscriptionId() { String subscriptionId = getSubscriptionId(); if (subscriptionId != null) { - if (getCommand() != null && StompCommand.MESSAGE.equals(getCommand())) { + StompCommand command = getCommand(); + if (command != null && StompCommand.MESSAGE.equals(command)) { setNativeHeader(STOMP_SUBSCRIPTION_HEADER, subscriptionId); } else { @@ -403,23 +410,26 @@ public void setVersion(@Nullable String version) { @Override public String getShortLogMessage(Object payload) { - if (StompCommand.SUBSCRIBE.equals(getCommand())) { + StompCommand command = getCommand(); + if (StompCommand.SUBSCRIBE.equals(command)) { return "SUBSCRIBE " + getDestination() + " id=" + getSubscriptionId() + appendSession(); } - else if (StompCommand.UNSUBSCRIBE.equals(getCommand())) { + else if (StompCommand.UNSUBSCRIBE.equals(command)) { return "UNSUBSCRIBE id=" + getSubscriptionId() + appendSession(); } - else if (StompCommand.SEND.equals(getCommand())) { + else if (StompCommand.SEND.equals(command)) { return "SEND " + getDestination() + appendSession() + appendPayload(payload); } - else if (StompCommand.CONNECT.equals(getCommand())) { - return "CONNECT" + (getUser() != null ? " user=" + getUser().getName() : "") + appendSession(); + else if (StompCommand.CONNECT.equals(command)) { + Principal user = getUser(); + return "CONNECT" + (user != null ? " user=" + user.getName() : "") + appendSession(); } - else if (StompCommand.CONNECTED.equals(getCommand())) { + else if (StompCommand.CONNECTED.equals(command)) { return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession(); } - else if (StompCommand.DISCONNECT.equals(getCommand())) { - return "DISCONNECT" + (getReceipt() != null ? " receipt=" + getReceipt() : "") + appendSession(); + else if (StompCommand.DISCONNECT.equals(command)) { + String receipt = getReceipt(); + return "DISCONNECT" + (receipt != null ? " receipt=" + receipt : "") + appendSession(); } else { return getDetailedLogMessage(payload); @@ -462,11 +472,12 @@ private String appendPayload(Object payload) { "Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass())); } byte[] bytes = (byte[]) payload; - String contentType = (getContentType() != null ? " " + getContentType().toString() : ""); - if (bytes.length == 0 || getContentType() == null || !isReadableContentType()) { + MimeType mimeType = getContentType(); + String contentType = (mimeType != null ? " " + mimeType.toString() : ""); + if (bytes.length == 0 || mimeType == null || !isReadableContentType()) { return contentType; } - Charset charset = getContentType().getCharset(); + Charset charset = mimeType.getCharset(); charset = (charset != null ? charset : StandardCharsets.UTF_8); return (bytes.length < 80) ? contentType + " payload=" + new String(bytes, charset) : diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java index e58bcc5e7204..025a3d31d501 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java @@ -546,8 +546,9 @@ else if (payload instanceof byte[]) { } protected boolean isReadableContentType() { + MimeType contentType = getContentType(); for (MimeType mimeType : READABLE_MIME_TYPES) { - if (mimeType.includes(getContentType())) { + if (mimeType.includes(contentType)) { return true; } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index 98b161023241..6230c50bbe1f 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -268,11 +268,12 @@ else if (webSocketMessage instanceof BinaryMessage) { logger.trace("From client: " + headerAccessor.getShortLogMessage(message.getPayload())); } - boolean isConnect = StompCommand.CONNECT.equals(headerAccessor.getCommand()); + StompCommand command = headerAccessor.getCommand(); + boolean isConnect = StompCommand.CONNECT.equals(command); if (isConnect) { this.stats.incrementConnectCount(); } - else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) { + else if (StompCommand.DISCONNECT.equals(command)) { this.stats.incrementDisconnectCount(); } @@ -292,10 +293,10 @@ else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) { if (isConnect) { publishEvent(this.eventPublisher, new SessionConnectEvent(this, message, user)); } - else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) { + else if (StompCommand.SUBSCRIBE.equals(command)) { publishEvent(this.eventPublisher, new SessionSubscribeEvent(this, message, user)); } - else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) { + else if (StompCommand.UNSUBSCRIBE.equals(command)) { publishEvent(this.eventPublisher, new SessionUnsubscribeEvent(this, message, user)); } }