Consistent declaration and use of UTF-8 Charset constants, plus related polishing
This commit is contained in:
@@ -67,21 +67,20 @@ public class StompDecoder {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the configured {@code MessageHeaderInitializer} if any.
|
||||
* Return the configured {@code MessageHeaderInitializer}, if any.
|
||||
*/
|
||||
public MessageHeaderInitializer getHeaderInitializer() {
|
||||
return this.headerInitializer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes one or more STOMP frames from the given {@code ByteBuffer} into a
|
||||
* list of {@link Message}s. If the input buffer contains any incplcontains partial STOMP frame content, or additional
|
||||
* content with a partial STOMP frame, the buffer is reset and {@code null} is
|
||||
* returned.
|
||||
*
|
||||
* list of {@link Message}s. If the input buffer contains partial STOMP frame
|
||||
* content, or additional content with a partial STOMP frame, the buffer is
|
||||
* reset and {@code null} is returned.
|
||||
* @param buffer The buffer to decode the STOMP frame from
|
||||
*
|
||||
* @return the decoded messages or an empty list
|
||||
* @return the decoded messages, or an empty list if none
|
||||
*/
|
||||
public List<Message<byte[]>> decode(ByteBuffer buffer) {
|
||||
return decode(buffer, null);
|
||||
@@ -90,33 +89,28 @@ public class StompDecoder {
|
||||
/**
|
||||
* Decodes one or more STOMP frames from the given {@code buffer} and returns
|
||||
* a list of {@link Message}s.
|
||||
*
|
||||
* <p>If the given ByteBuffer contains only partial STOMP frame content and no
|
||||
* complete STOMP frames, an empty list is returned, and the buffer is reset to
|
||||
* to where it was.
|
||||
*
|
||||
* <p>If the buffer contains one ore more STOMP frames, those are returned and
|
||||
* the buffer reset to point to the beginning of the unused partial content.
|
||||
*
|
||||
* <p>The output partialMessageHeaders map is used to store successfully parsed
|
||||
* headers in case of partial content. The caller can then check if a
|
||||
* "content-length" header was read, which helps to determine how much more
|
||||
* content is needed before the next attempt to decode.
|
||||
*
|
||||
* @param buffer The buffer to decode the STOMP frame from
|
||||
* @param partialMessageHeaders an empty output map that will store the last
|
||||
* successfully parsed partialMessageHeaders in case of partial message content
|
||||
* in cases where the partial buffer ended with a partial STOMP frame
|
||||
*
|
||||
* @return decoded messages or an empty list
|
||||
* @throws StompConversionException raised in case of decoding issues
|
||||
*/
|
||||
public List<Message<byte[]>> decode(ByteBuffer buffer, MultiValueMap<String, String> partialMessageHeaders) {
|
||||
List<Message<byte[]>> messages = new ArrayList<Message<byte[]>>();
|
||||
while (buffer.hasRemaining()) {
|
||||
Message<byte[]> m = decodeMessage(buffer, partialMessageHeaders);
|
||||
if (m != null) {
|
||||
messages.add(m);
|
||||
Message<byte[]> message = decodeMessage(buffer, partialMessageHeaders);
|
||||
if (message != null) {
|
||||
messages.add(message);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
@@ -129,28 +123,23 @@ public class StompDecoder {
|
||||
* Decode a single STOMP frame from the given {@code buffer} into a {@link Message}.
|
||||
*/
|
||||
private Message<byte[]> decodeMessage(ByteBuffer buffer, MultiValueMap<String, String> headers) {
|
||||
|
||||
Message<byte[]> decodedMessage = null;
|
||||
skipLeadingEol(buffer);
|
||||
buffer.mark();
|
||||
|
||||
String command = readCommand(buffer);
|
||||
if (command.length() > 0) {
|
||||
|
||||
StompHeaderAccessor headerAccessor = null;
|
||||
byte[] payload = null;
|
||||
|
||||
if (buffer.remaining() > 0) {
|
||||
StompCommand stompCommand = StompCommand.valueOf(command);
|
||||
headerAccessor = StompHeaderAccessor.create(stompCommand);
|
||||
initHeaders(headerAccessor);
|
||||
|
||||
readHeaders(buffer, headerAccessor);
|
||||
payload = readPayload(buffer, headerAccessor);
|
||||
}
|
||||
|
||||
if (payload != null) {
|
||||
if ((payload.length > 0) && (!headerAccessor.getCommand().isBodyAllowed())) {
|
||||
if (payload.length > 0 && !headerAccessor.getCommand().isBodyAllowed()) {
|
||||
throw new StompConversionException(headerAccessor.getCommand() +
|
||||
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
|
||||
}
|
||||
@@ -185,12 +174,14 @@ public class StompDecoder {
|
||||
logger.trace("Decoded " + headerAccessor.getDetailedLogMessage(null));
|
||||
}
|
||||
}
|
||||
|
||||
return decodedMessage;
|
||||
}
|
||||
|
||||
private void initHeaders(StompHeaderAccessor headerAccessor) {
|
||||
if (getHeaderInitializer() != null) {
|
||||
getHeaderInitializer().initHeaders(headerAccessor);
|
||||
MessageHeaderInitializer initializer = getHeaderInitializer();
|
||||
if (initializer != null) {
|
||||
initializer.initHeaders(headerAccessor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,14 +244,13 @@ public class StompDecoder {
|
||||
* <a href="http://stomp.github.io/stomp-specification-1.2.html#Value_Encoding">"Value Encoding"</a>.
|
||||
*/
|
||||
private String unescape(String inString) {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int pos = 0; // position in the old string
|
||||
StringBuilder sb = new StringBuilder(inString.length());
|
||||
int pos = 0; // position in the old string
|
||||
int index = inString.indexOf("\\");
|
||||
|
||||
while (index >= 0) {
|
||||
sb.append(inString.substring(pos, index));
|
||||
if((index + 1) >= inString.length()) {
|
||||
if (index + 1 >= inString.length()) {
|
||||
throw new StompConversionException("Illegal escape sequence at index " + index + ": " + inString);
|
||||
}
|
||||
Character c = inString.charAt(index + 1);
|
||||
@@ -289,7 +279,6 @@ public class StompDecoder {
|
||||
}
|
||||
|
||||
private byte[] readPayload(ByteBuffer buffer, StompHeaderAccessor headerAccessor) {
|
||||
|
||||
Integer contentLength;
|
||||
try {
|
||||
contentLength = headerAccessor.getContentLength();
|
||||
@@ -329,7 +318,6 @@ public class StompDecoder {
|
||||
|
||||
/**
|
||||
* Try to read an EOL incrementing the buffer position if successful.
|
||||
*
|
||||
* @return whether an EOL was consumed
|
||||
*/
|
||||
private boolean tryConsumeEndOfLine(ByteBuffer buffer) {
|
||||
|
||||
@@ -51,7 +51,6 @@ public final class StompEncoder {
|
||||
|
||||
/**
|
||||
* Encodes the given STOMP {@code message} into a {@code byte[]}
|
||||
*
|
||||
* @param message the message to encode
|
||||
* @return the encoded message
|
||||
*/
|
||||
@@ -61,7 +60,6 @@ public final class StompEncoder {
|
||||
|
||||
/**
|
||||
* Encodes the given payload and headers into a {@code byte[]}.
|
||||
*
|
||||
* @param headers the headers
|
||||
* @param payload the payload
|
||||
* @return the encoded message
|
||||
@@ -69,6 +67,7 @@ public final class StompEncoder {
|
||||
public byte[] encode(Map<String, Object> headers, byte[] payload) {
|
||||
Assert.notNull(headers, "'headers' is required");
|
||||
Assert.notNull(payload, "'payload' is required");
|
||||
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(128 + payload.length);
|
||||
DataOutputStream output = new DataOutputStream(baos);
|
||||
@@ -89,8 +88,8 @@ public final class StompEncoder {
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers + ".", e);
|
||||
catch (IOException ex) {
|
||||
throw new StompConversionException("Failed to encode STOMP frame, headers=" + headers, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +101,7 @@ public final class StompEncoder {
|
||||
(Map<String, List<String>>) headers.get(NativeMessageHeaderAccessor.NATIVE_HEADERS);
|
||||
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Encoding STOMP " + command + ", headers=" + nativeHeaders + ".");
|
||||
logger.trace("Encoding STOMP " + command + ", headers=" + nativeHeaders);
|
||||
}
|
||||
|
||||
if (nativeHeaders == null) {
|
||||
@@ -137,8 +136,8 @@ public final class StompEncoder {
|
||||
}
|
||||
|
||||
private byte[] encodeHeaderString(String input, boolean escape) {
|
||||
input = escape ? escape(input) : input;
|
||||
return input.getBytes(StompDecoder.UTF8_CHARSET);
|
||||
String inputToUse = (escape ? escape(input) : input);
|
||||
return inputToUse.getBytes(StompDecoder.UTF8_CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,6 +86,8 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class FormHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, ?>> {
|
||||
|
||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
private static final byte[] BOUNDARY_CHARS =
|
||||
new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
|
||||
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
|
||||
@@ -93,7 +95,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
||||
'V', 'W', 'X', 'Y', 'Z'};
|
||||
|
||||
|
||||
private Charset charset = Charset.forName("UTF-8");
|
||||
private Charset charset = DEFAULT_CHARSET;
|
||||
|
||||
private Charset multipartCharset;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -41,6 +41,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
|
||||
public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
|
||||
|
||||
|
||||
private final Charset defaultCharset;
|
||||
|
||||
private final List<Charset> availableCharsets;
|
||||
@@ -66,6 +67,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
|
||||
* <p>Default is {@code true}.
|
||||
@@ -74,6 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
this.writeAcceptCharset = writeAcceptCharset;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return String.class.equals(clazz);
|
||||
@@ -86,10 +89,10 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long getContentLength(String s, MediaType contentType) {
|
||||
protected Long getContentLength(String str, MediaType contentType) {
|
||||
Charset charset = getContentTypeCharset(contentType);
|
||||
try {
|
||||
return (long) s.getBytes(charset.name()).length;
|
||||
return (long) str.getBytes(charset.name()).length;
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
// should not occur
|
||||
@@ -98,17 +101,19 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
|
||||
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
|
||||
if (this.writeAcceptCharset) {
|
||||
outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
|
||||
}
|
||||
Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
|
||||
StreamUtils.copy(s, charset, outputMessage.getBody());
|
||||
StreamUtils.copy(str, charset, outputMessage.getBody());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the list of supported {@link Charset}.
|
||||
* <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
|
||||
* Return the list of supported {@link Charset}s.
|
||||
* <p>By default, returns {@link Charset#availableCharsets()}.
|
||||
* Can be overridden in subclasses.
|
||||
* @return the list of accepted charsets
|
||||
*/
|
||||
protected List<Charset> getAcceptedCharsets() {
|
||||
@@ -123,4 +128,5 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
||||
return this.defaultCharset;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public final class TextMessage extends AbstractWebSocketMessage<String> {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
private final byte[] bytes;
|
||||
|
||||
@@ -46,7 +46,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
|
||||
* @param payload the non-null payload
|
||||
*/
|
||||
public TextMessage(byte[] payload) {
|
||||
super(new String(payload, UTF_8));
|
||||
super(new String(payload, UTF8_CHARSET));
|
||||
this.bytes = payload;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
|
||||
}
|
||||
|
||||
public byte[] asBytes() {
|
||||
return (this.bytes != null ? this.bytes : getPayload().getBytes(UTF_8));
|
||||
return (this.bytes != null ? this.bytes : getPayload().getBytes(UTF8_CHARSET));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.socket.server.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -60,6 +61,9 @@ import org.springframework.web.socket.server.RequestUpgradeStrategy;
|
||||
*/
|
||||
public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||
|
||||
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
private static final boolean jettyWsPresent = ClassUtils.isPresent(
|
||||
"org.eclipse.jetty.websocket.server.WebSocketServerFactory", DefaultHandshakeHandler.class.getClassLoader());
|
||||
|
||||
@@ -223,7 +227,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||
logger.error("Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade());
|
||||
}
|
||||
response.setStatusCode(HttpStatus.BAD_REQUEST);
|
||||
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes("UTF-8"));
|
||||
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(UTF8_CHARSET));
|
||||
}
|
||||
|
||||
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
|
||||
@@ -231,7 +235,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||
logger.error("Handshake failed due to invalid Connection header " + request.getHeaders().getConnection());
|
||||
}
|
||||
response.setStatusCode(HttpStatus.BAD_REQUEST);
|
||||
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes("UTF-8"));
|
||||
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(UTF8_CHARSET));
|
||||
}
|
||||
|
||||
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) {
|
||||
|
||||
@@ -337,8 +337,8 @@ public abstract class AbstractSockJsService implements SockJsService {
|
||||
try {
|
||||
if (sockJsPath.equals("") || sockJsPath.equals("/")) {
|
||||
logger.debug(requestInfo);
|
||||
response.getHeaders().setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
|
||||
response.getBody().write("Welcome to SockJS!\n".getBytes("UTF-8"));
|
||||
response.getHeaders().setContentType(new MediaType("text", "plain", UTF8_CHARSET));
|
||||
response.getBody().write("Welcome to SockJS!\n".getBytes(UTF8_CHARSET));
|
||||
}
|
||||
else if (sockJsPath.equals("/info")) {
|
||||
logger.debug(requestInfo);
|
||||
|
||||
@@ -87,7 +87,7 @@ public abstract class AbstractHttpReceivingTransportHandler extends AbstractTran
|
||||
private void handleReadError(ServerHttpResponse response, String error, String sessionId) {
|
||||
try {
|
||||
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
response.getBody().write(error.getBytes("UTF-8"));
|
||||
response.getBody().write(error.getBytes(UTF8_CHARSET));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new SockJsException("Failed to send error: " + error, sessionId, ex);
|
||||
|
||||
@@ -49,7 +49,7 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp
|
||||
|
||||
AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;
|
||||
|
||||
String protocol = null; // https://github.com/sockjs/sockjs-client/issues/130
|
||||
String protocol = null; // https://github.com/sockjs/sockjs-client/issues/130
|
||||
sockJsSession.setAcceptedProtocol(protocol);
|
||||
|
||||
// Set content type before writing
|
||||
@@ -99,20 +99,22 @@ public abstract class AbstractHttpSendingTransportHandler extends AbstractTransp
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected abstract MediaType getContentType();
|
||||
|
||||
protected abstract SockJsFrameFormat getFrameFormat(ServerHttpRequest request);
|
||||
|
||||
|
||||
protected final String getCallbackParam(ServerHttpRequest request) {
|
||||
String query = request.getURI().getQuery();
|
||||
MultiValueMap<String, String> params = UriComponentsBuilder.newInstance().query(query).build().getQueryParams();
|
||||
String value = params.getFirst("c");
|
||||
try {
|
||||
return StringUtils.isEmpty(value) ? null : UriUtils.decode(value, "UTF-8");
|
||||
return (!StringUtils.isEmpty(value) ? UriUtils.decode(value, "UTF-8") : null);
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
// should never happen
|
||||
throw new SockJsException("Unable to decode callback query parameter", null, e);
|
||||
throw new SockJsException("Unable to decode callback query parameter", null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,8 +49,8 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamingSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public StreamingSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new EventSourceStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
@@ -61,9 +61,9 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan
|
||||
}
|
||||
|
||||
|
||||
private final class EventSourceStreamingSockJsSession extends StreamingSockJsSession {
|
||||
private class EventSourceStreamingSockJsSession extends StreamingSockJsSession {
|
||||
|
||||
private EventSourceStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
public EventSourceStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
WebSocketHandler wsHandler, Map<String, Object> attributes) {
|
||||
|
||||
super(sessionId, config, wsHandler, attributes);
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.sockjs.SockJsException;
|
||||
import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
|
||||
import org.springframework.web.socket.sockjs.frame.DefaultSockJsFrameFormat;
|
||||
import org.springframework.web.socket.sockjs.frame.SockJsFrameFormat;
|
||||
@@ -52,6 +53,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
|
||||
// http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
|
||||
private static final int MINIMUM_PARTIAL_HTML_CONTENT_LENGTH = 1024;
|
||||
|
||||
|
||||
static {
|
||||
StringBuilder sb = new StringBuilder(
|
||||
"<!doctype html>\n" +
|
||||
@@ -71,7 +73,6 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
|
||||
while (sb.length() < MINIMUM_PARTIAL_HTML_CONTENT_LENGTH) {
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
PARTIAL_HTML_CONTENT = sb.toString();
|
||||
}
|
||||
|
||||
@@ -87,25 +88,25 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamingSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public StreamingSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new HtmlFileStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
|
||||
AbstractHttpSockJsSession sockJsSession) {
|
||||
AbstractHttpSockJsSession sockJsSession) throws SockJsException {
|
||||
|
||||
String callback = getCallbackParam(request);
|
||||
if (! StringUtils.hasText(callback)) {
|
||||
if (!StringUtils.hasText(callback)) {
|
||||
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
try {
|
||||
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
|
||||
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
|
||||
}
|
||||
catch (IOException t) {
|
||||
sockJsSession.tryCloseWithSockJsTransportError(t, CloseStatus.SERVER_ERROR);
|
||||
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), t);
|
||||
catch (IOException ex) {
|
||||
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
|
||||
throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -124,9 +125,9 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
|
||||
}
|
||||
|
||||
|
||||
private final class HtmlFileStreamingSockJsSession extends StreamingSockJsSession {
|
||||
private class HtmlFileStreamingSockJsSession extends StreamingSockJsSession {
|
||||
|
||||
private HtmlFileStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
public HtmlFileStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
WebSocketHandler wsHandler, Map<String, Object> attributes) {
|
||||
|
||||
super(sessionId, config, wsHandler, attributes);
|
||||
@@ -134,18 +135,16 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
|
||||
|
||||
@Override
|
||||
protected void writePrelude(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
// we already validated the parameter above..
|
||||
// We already validated the parameter above...
|
||||
String callback = getCallbackParam(request);
|
||||
|
||||
String html = String.format(PARTIAL_HTML_CONTENT, callback);
|
||||
|
||||
try {
|
||||
response.getBody().write(html.getBytes("UTF-8"));
|
||||
response.getBody().write(html.getBytes(UTF8_CHARSET));
|
||||
response.flush();
|
||||
}
|
||||
catch (IOException e) {
|
||||
tryCloseWithSockJsTransportError(e, CloseStatus.SERVER_ERROR);
|
||||
throw new SockJsTransportFailureException("Failed to write HTML content", getId(), e);
|
||||
catch (IOException ex) {
|
||||
tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
|
||||
throw new SockJsTransportFailureException("Failed to write HTML content", getId(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -53,8 +53,8 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
}
|
||||
|
||||
@Override
|
||||
public PollingSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public PollingSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new PollingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
@@ -65,9 +65,9 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
|
||||
try {
|
||||
String callback = getCallbackParam(request);
|
||||
if (! StringUtils.hasText(callback)) {
|
||||
if (!StringUtils.hasText(callback)) {
|
||||
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
response.getBody().write("\"callback\" parameter required".getBytes("UTF-8"));
|
||||
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
|
||||
@Override
|
||||
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
|
||||
// we already validated the parameter above...
|
||||
// We already validated the parameter above...
|
||||
String callback = getCallbackParam(request);
|
||||
|
||||
return new DefaultSockJsFrameFormat(callback + "(\"%s\");\r\n") {
|
||||
|
||||
@@ -53,7 +53,7 @@ public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTranspo
|
||||
|
||||
super.handleRequestInternal(request, response, wsHandler, sockJsSession);
|
||||
try {
|
||||
response.getBody().write("ok".getBytes("UTF-8"));
|
||||
response.getBody().write("ok".getBytes(UTF8_CHARSET));
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new SockJsException("Failed to write to the response body", sockJsSession.getId(), ex);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -67,8 +67,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public AbstractSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new WebSocketServerSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
@@ -88,8 +88,6 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
|
||||
}
|
||||
}
|
||||
|
||||
// HandshakeHandler methods
|
||||
|
||||
@Override
|
||||
public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response,
|
||||
WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureException {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -51,8 +51,8 @@ public class XhrPollingTransportHandler extends AbstractHttpSendingTransportHand
|
||||
}
|
||||
|
||||
@Override
|
||||
public PollingSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public PollingSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new PollingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -49,8 +49,8 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
}
|
||||
|
||||
@Override
|
||||
public StreamingSockJsSession createSession(String sessionId, WebSocketHandler handler,
|
||||
Map<String, Object> attributes) {
|
||||
public StreamingSockJsSession createSession(
|
||||
String sessionId, WebSocketHandler handler, Map<String, Object> attributes) {
|
||||
|
||||
return new XhrStreamingSockJsSession(sessionId, getServiceConfig(), handler, attributes);
|
||||
}
|
||||
@@ -61,9 +61,9 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
}
|
||||
|
||||
|
||||
private final class XhrStreamingSockJsSession extends StreamingSockJsSession {
|
||||
private class XhrStreamingSockJsSession extends StreamingSockJsSession {
|
||||
|
||||
private XhrStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
public XhrStreamingSockJsSession(String sessionId, SockJsServiceConfig config,
|
||||
WebSocketHandler wsHandler, Map<String, Object> attributes) {
|
||||
|
||||
super(sessionId, config, wsHandler, attributes);
|
||||
@@ -71,11 +71,12 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
|
||||
|
||||
@Override
|
||||
protected void writePrelude(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
|
||||
for (int i=0; i < 2048; i++) {
|
||||
for (int i = 0; i < 2048; i++) {
|
||||
response.getBody().write('h');
|
||||
}
|
||||
response.getBody().write('\n');
|
||||
response.flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user