Skip to content

Commit 899994e

Browse files
authored
Merge pull request #1587 from dreis2211/SPR-16165
Reduce access on headers for STOMP messaging Issue: SPR-16165
2 parents ffd6eff + 9fab208 commit 899994e

File tree

5 files changed

+52
-36
lines changed

5 files changed

+52
-36
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,18 @@ private StringBuilder getBaseLogMessage() {
215215
StringBuilder sb = new StringBuilder();
216216
SimpMessageType messageType = getMessageType();
217217
sb.append(messageType != null ? messageType.name() : SimpMessageType.OTHER);
218-
if (getDestination() != null) {
219-
sb.append(" destination=").append(getDestination());
218+
String destination = getDestination();
219+
if (destination != null) {
220+
sb.append(" destination=").append(destination);
220221
}
221-
if (getSubscriptionId() != null) {
222-
sb.append(" subscriptionId=").append(getSubscriptionId());
222+
String subscriptionId = getSubscriptionId();
223+
if (subscriptionId != null) {
224+
sb.append(" subscriptionId=").append(subscriptionId);
223225
}
224226
sb.append(" session=").append(getSessionId());
225-
if (getUser() != null) {
226-
sb.append(" user=").append(getUser().getName());
227+
Principal user = getUser();
228+
if (user != null) {
229+
sb.append(" user=").append(user.getName());
227230
}
228231
return sb;
229232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private Message<byte[]> decodeMessage(ByteBuffer byteBuffer, @Nullable MultiValu
150150
if (payload.length > 0) {
151151
StompCommand stompCommand = headerAccessor.getCommand();
152152
if (stompCommand != null && !stompCommand.isBodyAllowed()) {
153-
throw new StompConversionException(headerAccessor.getCommand() +
153+
throw new StompConversionException(stompCommand +
154154
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
155155
}
156156
}

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.nio.charset.Charset;
2020
import java.nio.charset.StandardCharsets;
21+
import java.security.Principal;
2122
import java.util.Arrays;
2223
import java.util.Collections;
2324
import java.util.List;
@@ -165,11 +166,13 @@ else if (StompCommand.CONNECT.equals(command)) {
165166
}
166167

167168
void updateStompHeadersFromSimpMessageHeaders() {
168-
if (getDestination() != null) {
169-
setNativeHeader(STOMP_DESTINATION_HEADER, getDestination());
169+
String destination = getDestination();
170+
if (destination != null) {
171+
setNativeHeader(STOMP_DESTINATION_HEADER, destination);
170172
}
171-
if (getContentType() != null) {
172-
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, getContentType().toString());
173+
MimeType contentType = getContentType();
174+
if (contentType != null) {
175+
setNativeHeader(STOMP_CONTENT_TYPE_HEADER, contentType.toString());
173176
}
174177
trySetStompHeaderForSubscriptionId();
175178
}
@@ -188,21 +191,24 @@ protected Map<String, List<String>> getNativeHeaders() {
188191
}
189192

190193
public StompCommand updateStompCommandAsClientMessage() {
191-
if (getMessageType() != SimpMessageType.MESSAGE) {
192-
throw new IllegalStateException("Unexpected message type " + getMessageType());
194+
SimpMessageType messageType = getMessageType();
195+
if (messageType != SimpMessageType.MESSAGE) {
196+
throw new IllegalStateException("Unexpected message type " + messageType);
193197
}
194-
if (getCommand() == null) {
198+
StompCommand command = getCommand();
199+
if (command == null) {
195200
setHeader(COMMAND_HEADER, StompCommand.SEND);
196201
}
197-
else if (!getCommand().equals(StompCommand.SEND)) {
198-
throw new IllegalStateException("Unexpected STOMP command " + getCommand());
202+
else if (!command.equals(StompCommand.SEND)) {
203+
throw new IllegalStateException("Unexpected STOMP command " + command);
199204
}
200-
return getCommand();
205+
return command;
201206
}
202207

203208
public void updateStompCommandAsServerMessage() {
204-
if (getMessageType() != SimpMessageType.MESSAGE) {
205-
throw new IllegalStateException("Unexpected message type " + getMessageType());
209+
SimpMessageType messageType = getMessageType();
210+
if (messageType != SimpMessageType.MESSAGE) {
211+
throw new IllegalStateException("Unexpected message type " + messageType);
206212
}
207213
StompCommand command = getCommand();
208214
if ((command == null) || StompCommand.SEND.equals(command)) {
@@ -278,7 +284,8 @@ public void setSubscriptionId(@Nullable String subscriptionId) {
278284
private void trySetStompHeaderForSubscriptionId() {
279285
String subscriptionId = getSubscriptionId();
280286
if (subscriptionId != null) {
281-
if (getCommand() != null && StompCommand.MESSAGE.equals(getCommand())) {
287+
StompCommand command = getCommand();
288+
if (command != null && StompCommand.MESSAGE.equals(command)) {
282289
setNativeHeader(STOMP_SUBSCRIPTION_HEADER, subscriptionId);
283290
}
284291
else {
@@ -403,23 +410,26 @@ public void setVersion(@Nullable String version) {
403410

404411
@Override
405412
public String getShortLogMessage(Object payload) {
406-
if (StompCommand.SUBSCRIBE.equals(getCommand())) {
413+
StompCommand command = getCommand();
414+
if (StompCommand.SUBSCRIBE.equals(command)) {
407415
return "SUBSCRIBE " + getDestination() + " id=" + getSubscriptionId() + appendSession();
408416
}
409-
else if (StompCommand.UNSUBSCRIBE.equals(getCommand())) {
417+
else if (StompCommand.UNSUBSCRIBE.equals(command)) {
410418
return "UNSUBSCRIBE id=" + getSubscriptionId() + appendSession();
411419
}
412-
else if (StompCommand.SEND.equals(getCommand())) {
420+
else if (StompCommand.SEND.equals(command)) {
413421
return "SEND " + getDestination() + appendSession() + appendPayload(payload);
414422
}
415-
else if (StompCommand.CONNECT.equals(getCommand())) {
416-
return "CONNECT" + (getUser() != null ? " user=" + getUser().getName() : "") + appendSession();
423+
else if (StompCommand.CONNECT.equals(command)) {
424+
Principal user = getUser();
425+
return "CONNECT" + (user != null ? " user=" + user.getName() : "") + appendSession();
417426
}
418-
else if (StompCommand.CONNECTED.equals(getCommand())) {
427+
else if (StompCommand.CONNECTED.equals(command)) {
419428
return "CONNECTED heart-beat=" + Arrays.toString(getHeartbeat()) + appendSession();
420429
}
421-
else if (StompCommand.DISCONNECT.equals(getCommand())) {
422-
return "DISCONNECT" + (getReceipt() != null ? " receipt=" + getReceipt() : "") + appendSession();
430+
else if (StompCommand.DISCONNECT.equals(command)) {
431+
String receipt = getReceipt();
432+
return "DISCONNECT" + (receipt != null ? " receipt=" + receipt : "") + appendSession();
423433
}
424434
else {
425435
return getDetailedLogMessage(payload);
@@ -462,11 +472,12 @@ private String appendPayload(Object payload) {
462472
"Expected byte array payload but got: " + ClassUtils.getQualifiedName(payload.getClass()));
463473
}
464474
byte[] bytes = (byte[]) payload;
465-
String contentType = (getContentType() != null ? " " + getContentType().toString() : "");
466-
if (bytes.length == 0 || getContentType() == null || !isReadableContentType()) {
475+
MimeType mimeType = getContentType();
476+
String contentType = (mimeType != null ? " " + mimeType.toString() : "");
477+
if (bytes.length == 0 || mimeType == null || !isReadableContentType()) {
467478
return contentType;
468479
}
469-
Charset charset = getContentType().getCharset();
480+
Charset charset = mimeType.getCharset();
470481
charset = (charset != null ? charset : StandardCharsets.UTF_8);
471482
return (bytes.length < 80) ?
472483
contentType + " payload=" + new String(bytes, charset) :

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,9 @@ else if (payload instanceof byte[]) {
546546
}
547547

548548
protected boolean isReadableContentType() {
549+
MimeType contentType = getContentType();
549550
for (MimeType mimeType : READABLE_MIME_TYPES) {
550-
if (mimeType.includes(getContentType())) {
551+
if (mimeType.includes(contentType)) {
551552
return true;
552553
}
553554
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ else if (webSocketMessage instanceof BinaryMessage) {
268268
logger.trace("From client: " + headerAccessor.getShortLogMessage(message.getPayload()));
269269
}
270270

271-
boolean isConnect = StompCommand.CONNECT.equals(headerAccessor.getCommand());
271+
StompCommand command = headerAccessor.getCommand();
272+
boolean isConnect = StompCommand.CONNECT.equals(command);
272273
if (isConnect) {
273274
this.stats.incrementConnectCount();
274275
}
275-
else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) {
276+
else if (StompCommand.DISCONNECT.equals(command)) {
276277
this.stats.incrementDisconnectCount();
277278
}
278279

@@ -292,10 +293,10 @@ else if (StompCommand.DISCONNECT.equals(headerAccessor.getCommand())) {
292293
if (isConnect) {
293294
publishEvent(this.eventPublisher, new SessionConnectEvent(this, message, user));
294295
}
295-
else if (StompCommand.SUBSCRIBE.equals(headerAccessor.getCommand())) {
296+
else if (StompCommand.SUBSCRIBE.equals(command)) {
296297
publishEvent(this.eventPublisher, new SessionSubscribeEvent(this, message, user));
297298
}
298-
else if (StompCommand.UNSUBSCRIBE.equals(headerAccessor.getCommand())) {
299+
else if (StompCommand.UNSUBSCRIBE.equals(command)) {
299300
publishEvent(this.eventPublisher, new SessionUnsubscribeEvent(this, message, user));
300301
}
301302
}

0 commit comments

Comments
 (0)