Consistent declaration and use of UTF-8 Charset constants, plus related polishing
This commit is contained in:
@@ -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