Reduce access on headers for STOMP messaging
Issue: SPR-16165
This commit is contained in:
parent
ffd6eff369
commit
9fab208fdd
|
|
@ -215,15 +215,18 @@ public class SimpMessageHeaderAccessor extends NativeMessageHeaderAccessor {
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ public class StompDecoder {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package org.springframework.messaging.simp.stomp;
|
|||
|
||||
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 @@ 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();
|
||||
}
|
||||
|
|
@ -188,21 +191,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)) {
|
||||
|
|
@ -278,7 +284,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 {
|
||||
|
|
@ -403,23 +410,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);
|
||||
|
|
@ -462,11 +472,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 : StandardCharsets.UTF_8);
|
||||
return (bytes.length < 80) ?
|
||||
contentType + " payload=" + new String(bytes, charset) :
|
||||
|
|
|
|||
|
|
@ -546,8 +546,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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -268,11 +268,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();
|
||||
}
|
||||
|
||||
|
|
@ -292,10 +293,10 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue