Skip to content

SPR-14901 Allow customization of STOMP message header encoding #1236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -50,6 +51,16 @@ public class StompEncoder {

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

private static final int HEADER_KEY_CACHE_LIMIT = 32;

@SuppressWarnings("serial")
private final Map<String, byte[]> headerKeyCache =
new LinkedHashMap<String, byte[]>(HEADER_KEY_CACHE_LIMIT, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, byte[]> eldest) {
return size() > HEADER_KEY_CACHE_LIMIT;
}
};

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

byte[] encodedKey = encodeHeaderString(entry.getKey(), shouldEscape);
byte[] encodedKey = encodeHeaderKey(entry.getKey(), shouldEscape);
for (String value : values) {
output.write(encodedKey);
output.write(COLON);
output.write(encodeHeaderString(value, shouldEscape));
output.write(encodeHeaderValue(value, shouldEscape));
output.write(LF);
}
}
Expand All @@ -147,36 +158,62 @@ private void writeHeaders(StompCommand command, Map<String, Object> headers, byt
}
}

private byte[] encodeHeaderString(String input, boolean escape) {
private byte[] encodeHeaderKey(String input, boolean escape) {
String inputToUse = (escape ? escape(input) : input);
if (headerKeyCache.containsKey(inputToUse)) {
return headerKeyCache.get(inputToUse);
}
byte[] bytes = encodeHeaderString(inputToUse);
headerKeyCache.put(inputToUse, bytes);
return bytes;
}

private byte[] encodeHeaderValue(String input, boolean escape) {
String inputToUse = (escape ? escape(input) : input);
return inputToUse.getBytes(StandardCharsets.UTF_8);
return encodeHeaderString(inputToUse);
}

private byte[] encodeHeaderString(String input) {
return input.getBytes(StandardCharsets.UTF_8);
}

/**
* See STOMP Spec 1.2:
* <a href="http://stomp.github.io/stomp-specification-1.2.html#Value_Encoding">"Value Encoding"</a>.
*/
private String escape(String inString) {
StringBuilder sb = new StringBuilder(inString.length());
StringBuilder sb = null;
for (int i = 0; i < inString.length(); i++) {
char c = inString.charAt(i);
if (c == '\\') {
sb = getStringBuilder(sb, inString, i);
sb.append("\\\\");
}
else if (c == ':') {
sb = getStringBuilder(sb, inString, i);
sb.append("\\c");
}
else if (c == '\n') {
sb.append("\\n");
sb = getStringBuilder(sb, inString, i);
sb.append("\\n");
}
else if (c == '\r') {
sb = getStringBuilder(sb, inString, i);
sb.append("\\r");
}
else {
else if (sb != null){
sb.append(c);
}
}
return sb.toString();
return (sb != null ? sb.toString() : inString);
}

private StringBuilder getStringBuilder(StringBuilder sb, String inString, int i) {
if (sb == null) {
sb = new StringBuilder(inString.length());
sb.append(inString.substring(0, i));
}
return sb;
}

private void writeBody(byte[] payload, DataOutputStream output) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ protected void setApplicationContext(ApplicationContext applicationContext) {
this.stompHandler.setApplicationEventPublisher(applicationContext);
}


/**
* Return a handler mapping with the mapped ViewControllers; or {@code null}
* in case of no registrations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,13 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE

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


private StompSubProtocolErrorHandler errorHandler;

private int messageSizeLimit = 64 * 1024;

private final StompEncoder stompEncoder = new StompEncoder();
private StompEncoder stompEncoder;

private final StompDecoder stompDecoder = new StompDecoder();
private StompDecoder stompDecoder;

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

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

private final Stats stats = new Stats();

public StompSubProtocolHandler() {
setEncoder(new StompEncoder());
setDecoder(new StompDecoder());
}

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

/**
* Configure a {@link StompEncoder} for encoding STOMP frames
* @param encoder the encoder
* @since 4.3.5
*/
public void setEncoder(StompEncoder encoder) {
this.stompEncoder = encoder;
}

/**
* Configure a {@link StompDecoder} for decoding STOMP frames
* @param decoder the decoder
* @since 4.3.5
*/
public void setDecoder(StompDecoder decoder) {
this.stompDecoder = decoder;
}

/**
* Configure the maximum size allowed for an incoming STOMP message.
* Since a STOMP message can be received in multiple WebSocket messages,
Expand Down