Skip to content

Commit 0756709

Browse files
committed
SPR-14901 Allow customization of STOMP message header encoding
Fixes SPR-14901
1 parent 8ad95b0 commit 0756709

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

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

+45-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.Collections;
24+
import java.util.LinkedHashMap;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.Map.Entry;
@@ -50,6 +51,16 @@ public class StompEncoder {
5051

5152
private static final Log logger = LogFactory.getLog(StompEncoder.class);
5253

54+
private static final int HEADER_KEY_CACHE_LIMIT = 32;
55+
56+
@SuppressWarnings("serial")
57+
private final Map<String, byte[]> headerKeyCache =
58+
new LinkedHashMap<String, byte[]>(HEADER_KEY_CACHE_LIMIT, 0.75f, true) {
59+
@Override
60+
protected boolean removeEldestEntry(Map.Entry<String, byte[]> eldest) {
61+
return size() > HEADER_KEY_CACHE_LIMIT;
62+
}
63+
};
5364

5465
/**
5566
* Encodes the given STOMP {@code message} into a {@code byte[]}
@@ -130,11 +141,11 @@ private void writeHeaders(StompCommand command, Map<String, Object> headers, byt
130141
values = Collections.singletonList(StompHeaderAccessor.getPasscode(headers));
131142
}
132143

133-
byte[] encodedKey = encodeHeaderString(entry.getKey(), shouldEscape);
144+
byte[] encodedKey = encodeHeaderKey(entry.getKey(), shouldEscape);
134145
for (String value : values) {
135146
output.write(encodedKey);
136147
output.write(COLON);
137-
output.write(encodeHeaderString(value, shouldEscape));
148+
output.write(encodeHeaderValue(value, shouldEscape));
138149
output.write(LF);
139150
}
140151
}
@@ -147,36 +158,62 @@ private void writeHeaders(StompCommand command, Map<String, Object> headers, byt
147158
}
148159
}
149160

150-
private byte[] encodeHeaderString(String input, boolean escape) {
161+
private byte[] encodeHeaderKey(String input, boolean escape) {
162+
String inputToUse = (escape ? escape(input) : input);
163+
if (headerKeyCache.containsKey(inputToUse)) {
164+
return headerKeyCache.get(inputToUse);
165+
}
166+
byte[] bytes = encodeHeaderString(inputToUse);
167+
headerKeyCache.put(inputToUse, bytes);
168+
return bytes;
169+
}
170+
171+
private byte[] encodeHeaderValue(String input, boolean escape) {
151172
String inputToUse = (escape ? escape(input) : input);
152-
return inputToUse.getBytes(StandardCharsets.UTF_8);
173+
return encodeHeaderString(inputToUse);
174+
}
175+
176+
private byte[] encodeHeaderString(String input) {
177+
return input.getBytes(StandardCharsets.UTF_8);
153178
}
154179

155180
/**
156181
* See STOMP Spec 1.2:
157182
* <a href="http://stomp.github.io/stomp-specification-1.2.html#Value_Encoding">"Value Encoding"</a>.
158183
*/
159184
private String escape(String inString) {
160-
StringBuilder sb = new StringBuilder(inString.length());
185+
StringBuilder sb = null;
161186
for (int i = 0; i < inString.length(); i++) {
162187
char c = inString.charAt(i);
163188
if (c == '\\') {
189+
sb = getStringBuilder(sb, inString, i);
164190
sb.append("\\\\");
165191
}
166192
else if (c == ':') {
193+
sb = getStringBuilder(sb, inString, i);
167194
sb.append("\\c");
168195
}
169196
else if (c == '\n') {
170-
sb.append("\\n");
197+
sb = getStringBuilder(sb, inString, i);
198+
sb.append("\\n");
171199
}
172200
else if (c == '\r') {
201+
sb = getStringBuilder(sb, inString, i);
173202
sb.append("\\r");
174203
}
175-
else {
204+
else if (sb != null){
176205
sb.append(c);
177206
}
178207
}
179-
return sb.toString();
208+
return (sb != null ? sb.toString() : inString);
209+
}
210+
211+
private StringBuilder getStringBuilder(StringBuilder sb, String inString, int i) {
212+
if (sb == null) {
213+
sb = new StringBuilder(inString.length());
214+
sb.append(inString.substring(0, i));
215+
}
216+
return sb;
180217
}
181218

182219
private void writeBody(byte[] payload, DataOutputStream output) throws IOException {

spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ protected void setApplicationContext(ApplicationContext applicationContext) {
142142
this.stompHandler.setApplicationEventPublisher(applicationContext);
143143
}
144144

145-
146145
/**
147146
* Return a handler mapping with the mapped ViewControllers; or {@code null}
148147
* in case of no registrations.

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
8888

8989
private static final byte[] EMPTY_PAYLOAD = new byte[0];
9090

91-
9291
private StompSubProtocolErrorHandler errorHandler;
9392

9493
private int messageSizeLimit = 64 * 1024;
9594

96-
private final StompEncoder stompEncoder = new StompEncoder();
95+
private StompEncoder stompEncoder;
9796

98-
private final StompDecoder stompDecoder = new StompDecoder();
97+
private StompDecoder stompDecoder;
9998

10099
private final Map<String, BufferingStompDecoder> decoders = new ConcurrentHashMap<>();
101100

@@ -107,6 +106,10 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
107106

108107
private final Stats stats = new Stats();
109108

109+
public StompSubProtocolHandler() {
110+
setEncoder(new StompEncoder());
111+
setDecoder(new StompDecoder());
112+
}
110113

111114
/**
112115
* Configure a handler for error messages sent to clients which allows
@@ -126,6 +129,24 @@ public StompSubProtocolErrorHandler getErrorHandler() {
126129
return this.errorHandler;
127130
}
128131

132+
/**
133+
* Configure a {@link StompEncoder} for encoding STOMP frames
134+
* @param encoder the encoder
135+
* @since 4.3.5
136+
*/
137+
public void setEncoder(StompEncoder encoder) {
138+
this.stompEncoder = encoder;
139+
}
140+
141+
/**
142+
* Configure a {@link StompDecoder} for decoding STOMP frames
143+
* @param decoder the decoder
144+
* @since 4.3.5
145+
*/
146+
public void setDecoder(StompDecoder decoder) {
147+
this.stompDecoder = decoder;
148+
}
149+
129150
/**
130151
* Configure the maximum size allowed for an incoming STOMP message.
131152
* Since a STOMP message can be received in multiple WebSocket messages,

0 commit comments

Comments
 (0)