Skip to content

Commit f2e1e1b

Browse files
committed
Efficient STOMP content-length header check
Issue: SPR-14747 (cherry picked from commit a6b0b6e)
1 parent 6c764f6 commit f2e1e1b

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public List<Message<byte[]>> decode(ByteBuffer newBuffer) {
102102
this.chunks.add(newBuffer);
103103
checkBufferLimits();
104104

105-
if (getExpectedContentLength() != null && getBufferSize() < this.expectedContentLength) {
105+
if (this.expectedContentLength != null && getBufferSize() < this.expectedContentLength) {
106106
return Collections.<Message<byte[]>>emptyList();
107107
}
108108

@@ -139,12 +139,12 @@ private void checkBufferLimits() {
139139
if (this.expectedContentLength != null) {
140140
if (this.expectedContentLength > this.bufferSizeLimit) {
141141
throw new StompConversionException(
142-
"'content-length' header value " + this.expectedContentLength +
143-
" exceeds configured message buffer size limit " + this.bufferSizeLimit);
142+
"STOMP 'content-length' header value " + this.expectedContentLength +
143+
" exceeds configured buffer size limit " + this.bufferSizeLimit);
144144
}
145145
}
146146
if (getBufferSize() > this.bufferSizeLimit) {
147-
throw new StompConversionException("The configured stomp frame buffer size limit of " +
147+
throw new StompConversionException("The configured STOMP buffer size limit of " +
148148
this.bufferSizeLimit + " bytes has been exceeded");
149149
}
150150
}

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

+10-14
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@
2929
import org.springframework.messaging.simp.SimpMessageType;
3030
import org.springframework.messaging.support.MessageHeaderAccessor;
3131
import org.springframework.util.ClassUtils;
32+
import org.springframework.util.CollectionUtils;
3233
import org.springframework.util.MimeType;
3334
import org.springframework.util.MimeTypeUtils;
3435
import org.springframework.util.StringUtils;
3536

3637
/**
37-
* A {@code MessageHeaderAccessor} to use when creating a {@code Message} from a
38-
* decoded STOMP frame, or when encoding a {@code Message} to a STOMP frame.
38+
* A {@code MessageHeaderAccessor} to use when creating a {@code Message} from
39+
* a decoded STOMP frame, or when encoding a {@code Message} to a STOMP frame.
3940
*
40-
* <p>When created from STOMP frame content, the actual STOMP headers are stored
41-
* in the native header sub-map managed by the parent class
41+
* <p>When created from STOMP frame content, the actual STOMP headers are
42+
* stored in the native header sub-map managed by the parent class
4243
* {@link org.springframework.messaging.support.NativeMessageHeaderAccessor}
43-
* while the parent class
44-
* {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor} manages
45-
* common processing headers some of which are based on STOMP headers (e.g.
46-
* destination, content-type, etc).
44+
* while the parent class {@link SimpMessageHeaderAccessor} manages common
45+
* processing headers some of which are based on STOMP headers
46+
* (e.g. destination, content-type, etc).
4747
*
4848
* <p>An instance of this class can also be created by wrapping an existing
4949
* {@code Message}. That message may have been created with the more generic
@@ -502,12 +502,8 @@ public static String getPasscode(Map<String, Object> headers) {
502502
}
503503

504504
public static Integer getContentLength(Map<String, List<String>> nativeHeaders) {
505-
if (nativeHeaders.containsKey(STOMP_CONTENT_LENGTH_HEADER)) {
506-
List<String> values = nativeHeaders.get(STOMP_CONTENT_LENGTH_HEADER);
507-
String value = (values != null ? values.get(0) : null);
508-
return Integer.valueOf(value);
509-
}
510-
return null;
505+
List<String> values = nativeHeaders.get(STOMP_CONTENT_LENGTH_HEADER);
506+
return (!CollectionUtils.isEmpty(values) ? Integer.valueOf(values.get(0)) : null);
511507
}
512508

513509

0 commit comments

Comments
 (0)