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 5bec0672bb..4d1f6f07d9 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2017 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. @@ -205,16 +205,20 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor { private StringBuilder getBaseLogMessage() { StringBuilder sb = new StringBuilder(); - sb.append(getMessageType().name()); - if (getDestination() != null) { - sb.append(" destination=").append(getDestination()); + SimpMessageType messageType = getMessageType(); + sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER); + 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 1d054f61ad..21540819d0 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 @@ -138,9 +138,12 @@ public class StompDecoder { payload = readPayload(buffer, headerAccessor); } if (payload != null) { - if (payload.length > 0 && !headerAccessor.getCommand().isBodyAllowed()) { - throw new StompConversionException(headerAccessor.getCommand() + - " shouldn't have a payload: length=" + payload.length + ", headers=" + headers); + if (payload.length > 0) { + StompCommand stompCommand = headerAccessor.getCommand(); + if (stompCommand != null && !stompCommand.isBodyAllowed()) { + throw new StompConversionException(stompCommand + + " shouldn't have a payload: length=" + payload.length + ", headers=" + headers); + } } headerAccessor.updateSimpMessageHeadersFromStompHeaders(); headerAccessor.setLeaveMutable(true); 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 44524afa3f..43222b7dc4 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 @@ -17,6 +17,7 @@ package org.springframework.messaging.simp.stomp; import java.nio.charset.Charset; +import java.security.Principal; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -163,11 +164,13 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { } 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(); } @@ -185,21 +188,24 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { } 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)) { @@ -273,7 +279,8 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { 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 { @@ -286,10 +293,8 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { } public Integer getContentLength() { - if (containsNativeHeader(STOMP_CONTENT_LENGTH_HEADER)) { - return Integer.valueOf(getFirstNativeHeader(STOMP_CONTENT_LENGTH_HEADER)); - } - return null; + String header = getFirstNativeHeader(STOMP_CONTENT_LENGTH_HEADER); + return (header != null ? Integer.valueOf(header) : null); } public void setContentLength(int contentLength) { @@ -390,23 +395,26 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { @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); @@ -444,11 +452,12 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor { "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 : StompDecoder.UTF8_CHARSET); 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 de182947ff..397bc7b14b 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 @@ -535,8 +535,9 @@ public class MessageHeaderAccessor { } protected boolean isReadableContentType() { + MimeType contentType = getContentType(); for (MimeType mimeType : READABLE_MIME_TYPES) { - if (mimeType.includes(getContentType())) { + if (mimeType.includes(contentType)) { return true; } } @@ -557,6 +558,8 @@ public class MessageHeaderAccessor { * its type does not match the required type. *

This is for cases where the existence of an accessor is strongly expected * (followed up with an assertion) or where an accessor will be created otherwise. + * @param message the message to get an accessor for + * @param requiredType the required accessor type (or {@code null} for any) * @return an accessor instance of the specified type, or {@code null} if none * @since 4.1 */ @@ -568,6 +571,8 @@ public class MessageHeaderAccessor { * A variation of {@link #getAccessor(org.springframework.messaging.Message, Class)} * with a {@code MessageHeaders} instance instead of a {@code Message}. *

This is for cases when a full message may not have been created yet. + * @param messageHeaders the message headers to get an accessor for + * @param requiredType the required accessor type (or {@code null} for any) * @return an accessor instance of the specified type, or {@code null} if none * @since 4.1 */ 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 78603874e7..879ac2d453 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -285,11 +285,12 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE 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(); } @@ -308,10 +309,10 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE if (isConnect) { publishEvent(new SessionConnectEvent(this, message, getUser(session))); } - else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) { + else if (StompCommand.SUBSCRIBE.equals(command)) { publishEvent(new SessionSubscribeEvent(this, message, getUser(session))); } - else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) { + else if (StompCommand.UNSUBSCRIBE.equals(command)) { publishEvent(new SessionUnsubscribeEvent(this, message, getUser(session))); } }