Merge pull request #1587 from dreis2211/SPR-16165

Reduce access on headers for STOMP messaging

Issue: SPR-16165
This commit is contained in:
Juergen Hoeller 2017-11-06 20:41:10 +01:00 committed by GitHub
commit 899994e7c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 36 deletions

View File

@ -215,15 +215,18 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
SimpMessageType messageType = getMessageType(); SimpMessageType messageType = getMessageType();
sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER); sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER);
if (getDestination() != null) { String destination = getDestination();
sb.append(" destination=").append(getDestination()); if (destination != null) {
sb.append(" destination=").append(destination);
} }
if (getSubscriptionId() != null) { String subscriptionId = getSubscriptionId();
sb.append(" subscriptionId=").append(getSubscriptionId()); if (subscriptionId != null) {
sb.append(" subscriptionId=").append(subscriptionId);
} }
sb.append(" session=").append(getSessionId()); sb.append(" session=").append(getSessionId());
if (getUser() != null) { Principal user = getUser();
sb.append(" user=").append(getUser().getName()); if (user != null) {
sb.append(" user=").append(user.getName());
} }
return sb; return sb;
} }

View File

@ -150,7 +150,7 @@ public class StompDecoder {
if (payload.length > 0) { if (payload.length > 0) {
StompCommand stompCommand = headerAccessor.getCommand(); StompCommand stompCommand = headerAccessor.getCommand();
if (stompCommand != null && !stompCommand.isBodyAllowed()) { if (stompCommand != null && !stompCommand.isBodyAllowed()) {
throw new StompConversionException(headerAccessor.getCommand() + throw new StompConversionException(stompCommand +
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers); " shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
} }
} }

View File

@ -18,6 +18,7 @@ package org.springframework.messaging.simp.stomp;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -165,11 +166,13 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
} }
void updateStompHeadersFromSimpMessageHeaders() { void updateStompHeadersFromSimpMessageHeaders() {
if (getDestination() != null) { String destination = getDestination();
setNativeHeader(STOMP_DESTINATION_HEADER, getDestination()); if (destination != null) {
setNativeHeader(STOMP_DESTINATION_HEADER, destination);
} }
if (getContentType() != null) { MimeType contentType = getContentType();
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, getContentType().toString()); if (contentType != null) {
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString());
} }
trySetStompHeaderForSubscriptionId(); trySetStompHeaderForSubscriptionId();
} }
@ -188,21 +191,24 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
} }
public StompCommand updateStompCommandAsClientMessage() { public StompCommand updateStompCommandAsClientMessage() {
if (getMessageType() != SimpMessageType.MESSAGE) { SimpMessageType messageType = getMessageType();
throw new IllegalStateException("Unexpected message type " + 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); setHeader(COMMAND_HEADER, StompCommand.SEND);
} }
else if (!getCommand().equals(StompCommand.SEND)) { else if (!command.equals(StompCommand.SEND)) {
throw new IllegalStateException("Unexpected STOMP command " + getCommand()); throw new IllegalStateException("Unexpected STOMP command " + command);
} }
return getCommand(); return command;
} }
public void updateStompCommandAsServerMessage() { public void updateStompCommandAsServerMessage() {
if (getMessageType() != SimpMessageType.MESSAGE) { SimpMessageType messageType = getMessageType();
throw new IllegalStateException("Unexpected message type " + getMessageType()); if (messageType != SimpMessageType.MESSAGE) {
throw new IllegalStateException("Unexpected message type " + messageType);
} }
StompCommand command = getCommand(); StompCommand command = getCommand();
if ((command == null) || StompCommand.SEND.equals(command)) { if ((command == null) || StompCommand.SEND.equals(command)) {
@ -278,7 +284,8 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
private void trySetStompHeaderForSubscriptionId() { private void trySetStompHeaderForSubscriptionId() {
String subscriptionId = getSubscriptionId(); String subscriptionId = getSubscriptionId();
if (subscriptionId != null) { 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); setNativeHeader(STOMP_SUBSCRIPTION_HEADER, subscriptionId);
} }
else { else {
@ -403,23 +410,26 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
@Override @Override
public String getShortLogMessage(Object payload) { public String getShortLogMessage(Object payload) {
if (StompCommand.SUBSCRIBE.equals(getCommand())) { StompCommand command = getCommand();
if (StompCommand.SUBSCRIBE.equals(command)) {
return "SUBSCRIBE " + getDestination() + " id=" + getSubscriptionId() + appendSession(); return "SUBSCRIBE " + getDestination() + " id=" + getSubscriptionId() + appendSession();
} }
else if (StompCommand.UNSUBSCRIBE.equals(getCommand())) { else if (StompCommand.UNSUBSCRIBE.equals(command)) {
return "UNSUBSCRIBE id=" + getSubscriptionId() + appendSession(); return "UNSUBSCRIBE id=" + getSubscriptionId() + appendSession();
} }
else if (StompCommand.SEND.equals(getCommand())) { else if (StompCommand.SEND.equals(command)) {
return "SEND " + getDestination() + appendSession() + appendPayload(payload); return "SEND " + getDestination() + appendSession() + appendPayload(payload);
} }
else if (StompCommand.CONNECT.equals(getCommand())) { else if (StompCommand.CONNECT.equals(command)) {
return "CONNECT" + (getUser() != null ? " user=" + getUser().getName() : "") + appendSession(); 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(); return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession();
} }
else if (StompCommand.DISCONNECT.equals(getCommand())) { else if (StompCommand.DISCONNECT.equals(command)) {
return "DISCONNECT" + (getReceipt() != null ? " receipt=" + getReceipt() : "") + appendSession(); String receipt = getReceipt();
return "DISCONNECT" + (receipt != null ? " receipt=" + receipt : "") + appendSession();
} }
else { else {
return getDetailedLogMessage(payload); return getDetailedLogMessage(payload);
@ -462,11 +472,12 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
"Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass())); "Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass()));
} }
byte[] bytes = (byte[]) payload; byte[] bytes = (byte[]) payload;
String contentType = (getContentType() != null ? " " + getContentType().toString() : ""); MimeType mimeType = getContentType();
if (bytes.length == 0 || getContentType() == null || !isReadableContentType()) { String contentType = (mimeType != null ? " " + mimeType.toString() : "");
if (bytes.length == 0 || mimeType == null || !isReadableContentType()) {
return contentType; return contentType;
} }
Charset charset = getContentType().getCharset(); Charset charset = mimeType.getCharset();
charset = (charset != null ? charset : StandardCharsets.UTF_8); charset = (charset != null ? charset : StandardCharsets.UTF_8);
return (bytes.length < 80) ? return (bytes.length < 80) ?
contentType + " payload=" + new String(bytes, charset) : contentType + " payload=" + new String(bytes, charset) :

View File

@ -546,8 +546,9 @@ public class MessageHeaderAccessor {
} }
protected boolean isReadableContentType() { protected boolean isReadableContentType() {
MimeType contentType = getContentType();
for (MimeType mimeType : READABLE_MIME_TYPES) { for (MimeType mimeType : READABLE_MIME_TYPES) {
if (mimeType.includes(getContentType())) { if (mimeType.includes(contentType)) {
return true; return true;
} }
} }

View File

@ -268,11 +268,12 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
logger.trace("From client: " + headerAccessor.getShortLogMessage(message.getPayload())); 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) { if (isConnect) {
this.stats.incrementConnectCount(); this.stats.incrementConnectCount();
} }
else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) { else if (StompCommand.DISCONNECT.equals(command)) {
this.stats.incrementDisconnectCount(); this.stats.incrementDisconnectCount();
} }
@ -292,10 +293,10 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
if (isConnect) { if (isConnect) {
publishEvent(this.eventPublisher, new SessionConnectEvent(this, message, user)); 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)); 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)); publishEvent(this.eventPublisher, new SessionUnsubscribeEvent(this, message, user));
} }
} }