Revise encoding steps towards use of JDK Charset and StandardCharsets

Issue: SPR-14492
This commit is contained in:
Juergen Hoeller
2016-07-19 23:43:05 +02:00
parent 79d30d8c8a
commit 99be15f58b
95 changed files with 480 additions and 569 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.web.socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A text WebSocket message.
@@ -26,8 +26,6 @@ import java.nio.charset.Charset;
*/
public final class TextMessage extends AbstractWebSocketMessage<String> {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private final byte[] bytes;
@@ -46,7 +44,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
* @param payload the non-null payload
*/
public TextMessage(byte[] payload) {
super(new String(payload, UTF8_CHARSET));
super(new String(payload, StandardCharsets.UTF_8));
this.bytes = payload;
}
@@ -70,7 +68,7 @@ public final class TextMessage extends AbstractWebSocketMessage<String> {
}
public byte[] asBytes() {
return (this.bytes != null ? this.bytes : getPayload().getBytes(UTF8_CHARSET));
return (this.bytes != null ? this.bytes : getPayload().getBytes(StandardCharsets.UTF_8));
}
@Override

View File

@@ -17,7 +17,7 @@
package org.springframework.web.socket.server.support;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
@@ -70,9 +70,6 @@ import org.springframework.web.socket.server.RequestUpgradeStrategy;
*/
public abstract class AbstractHandshakeHandler implements HandshakeHandler, Lifecycle {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private static final boolean jettyWsPresent = ClassUtils.isPresent(
"org.eclipse.jetty.websocket.server.WebSocketServerFactory", AbstractHandshakeHandler.class.getClassLoader());
@@ -286,7 +283,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
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(UTF8_CHARSET));
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(StandardCharsets.UTF_8));
}
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
@@ -294,7 +291,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
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(UTF8_CHARSET));
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(StandardCharsets.UTF_8));
}
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
package org.springframework.web.socket.sockjs.frame;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.util.Assert;
@@ -28,7 +29,7 @@ import org.springframework.util.Assert;
*/
public class SockJsFrame {
public static final Charset CHARSET = Charset.forName("UTF-8");
public static final Charset CHARSET = StandardCharsets.UTF_8;
private static final SockJsFrame OPEN_FRAME = new SockJsFrame("o");

View File

@@ -17,7 +17,7 @@
package org.springframework.web.socket.sockjs.support;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -67,8 +67,6 @@ import org.springframework.web.util.WebUtils;
*/
public abstract class AbstractSockJsService implements SockJsService, CorsConfigurationSource {
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
private static final long ONE_YEAR = TimeUnit.DAYS.toSeconds(365);
private static final Random random = new Random();
@@ -354,8 +352,8 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
if (requestInfo != null) {
logger.debug("Processing transport request: " + requestInfo);
}
response.getHeaders().setContentType(new MediaType("text", "plain", UTF8_CHARSET));
response.getBody().write("Welcome to SockJS!\n".getBytes(UTF8_CHARSET));
response.getHeaders().setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));
response.getBody().write("Welcome to SockJS!\n".getBytes(StandardCharsets.UTF_8));
}
else if (sockJsPath.equals("/info")) {
@@ -547,7 +545,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
if (HttpMethod.GET == request.getMethod()) {
addNoCacheHeaders(response);
if (checkOrigin(request, response)) {
response.getHeaders().setContentType(new MediaType("application", "json", UTF8_CHARSET));
response.getHeaders().setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
String content = String.format(
INFO_CONTENT, random.nextInt(), isSessionCookieNeeded(), isWebSocketEnabled());
response.getBody().write(content.getBytes());
@@ -595,7 +593,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
}
String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
byte[] contentBytes = content.getBytes(UTF8_CHARSET);
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
StringBuilder builder = new StringBuilder("\"0");
DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
builder.append('"');
@@ -607,7 +605,7 @@ public abstract class AbstractSockJsService implements SockJsService, CorsConfig
return;
}
response.getHeaders().setContentType(new MediaType("text", "html", UTF8_CHARSET));
response.getHeaders().setContentType(new MediaType("text", "html", StandardCharsets.UTF_8));
response.getHeaders().setContentLength(contentBytes.length);
// No cache in order to check every time if IFrame are authorized

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.springframework.http.HttpStatus;
@@ -79,7 +80,7 @@ public abstract class AbstractHttpReceivingTransportHandler extends AbstractTran
logger.trace("Received message(s): " + Arrays.asList(messages));
}
response.setStatusCode(getResponseStatus());
response.getHeaders().setContentType(new MediaType("text", "plain", UTF8_CHARSET));
response.getHeaders().setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));
sockJsSession.delegateMessages(messages);
}
@@ -87,7 +88,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(UTF8_CHARSET));
response.getBody().write(error.getBytes(StandardCharsets.UTF_8));
}
catch (IOException ex) {
throw new SockJsException("Failed to send error: " + error, sessionId, ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,8 +16,6 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.nio.charset.Charset;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -30,9 +28,6 @@ import org.springframework.web.socket.sockjs.transport.TransportHandler;
*/
public abstract class AbstractTransportHandler implements TransportHandler {
protected static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
protected final Log logger = LogFactory.getLog(this.getClass());
private SockJsServiceConfig serviceConfig;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,6 +16,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.http.MediaType;
@@ -43,7 +44,7 @@ public class EventSourceTransportHandler extends AbstractHttpSendingTransportHan
@Override
protected MediaType getContentType() {
return new MediaType("text", "event-stream", UTF8_CHARSET);
return new MediaType("text", "event-stream", StandardCharsets.UTF_8);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.http.HttpStatus;
@@ -84,7 +85,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
@Override
protected MediaType getContentType() {
return new MediaType("text", "html", UTF8_CHARSET);
return new MediaType("text", "html", StandardCharsets.UTF_8);
}
@Override
@@ -102,7 +103,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
if (!StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
try {
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
}
catch (IOException ex) {
sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
@@ -138,7 +139,7 @@ public class HtmlFileTransportHandler extends AbstractHttpSendingTransportHandle
// We already validated the parameter above...
String callback = getCallbackParam(request);
String html = String.format(PARTIAL_HTML_CONTENT, callback);
return html.getBytes(UTF8_CHARSET);
return html.getBytes(StandardCharsets.UTF_8);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,6 +16,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.http.HttpStatus;
@@ -49,7 +50,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
@Override
protected MediaType getContentType() {
return new MediaType("application", "javascript", UTF8_CHARSET);
return new MediaType("application", "javascript", StandardCharsets.UTF_8);
}
@Override
@@ -67,7 +68,7 @@ public class JsonpPollingTransportHandler extends AbstractHttpSendingTransportHa
String callback = getCallbackParam(request);
if (!StringUtils.hasText(callback)) {
response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
response.getBody().write("\"callback\" parameter required".getBytes(UTF8_CHARSET));
response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
return;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -17,6 +17,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -53,7 +54,7 @@ public class JsonpReceivingTransportHandler extends AbstractHttpReceivingTranspo
super.handleRequestInternal(request, response, wsHandler, sockJsSession);
try {
response.getBody().write("ok".getBytes(UTF8_CHARSET));
response.getBody().write("ok".getBytes(StandardCharsets.UTF_8));
}
catch (IOException ex) {
throw new SockJsException("Failed to write to the response body", sockJsSession.getId(), ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,6 +16,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.http.MediaType;
@@ -42,7 +43,7 @@ public class XhrPollingTransportHandler extends AbstractHttpSendingTransportHand
@Override
protected MediaType getContentType() {
return new MediaType("application", "javascript", UTF8_CHARSET);
return new MediaType("application", "javascript", StandardCharsets.UTF_8);
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,6 +16,7 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.http.MediaType;
@@ -53,7 +54,7 @@ public class XhrStreamingTransportHandler extends AbstractHttpSendingTransportHa
@Override
protected MediaType getContentType() {
return new MediaType("application", "javascript", UTF8_CHARSET);
return new MediaType("application", "javascript", StandardCharsets.UTF_8);
}
@Override

View File

@@ -17,7 +17,7 @@
package org.springframework.web.socket.messaging;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ScheduledFuture;
import org.junit.Before;
@@ -56,9 +56,6 @@ import static org.mockito.Mockito.*;
*/
public class WebSocketStompClientTests {
private static final Charset UTF_8 = Charset.forName("UTF-8");
private TestWebSocketStompClient stompClient;
@Mock
@@ -137,7 +134,7 @@ public class WebSocketStompClientTests {
StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
assertEquals(StompCommand.SEND, accessor.getCommand());
assertEquals("alpha", headers.getFirst("a"));
assertEquals("Message payload", new String(message.getPayload(), UTF_8));
assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
@Test
@@ -162,14 +159,14 @@ public class WebSocketStompClientTests {
StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
assertEquals(StompCommand.SEND, accessor.getCommand());
assertEquals("alpha", headers.getFirst("a"));
assertEquals("Message payload", new String(message.getPayload(), UTF_8));
assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void handleWebSocketMessageBinary() throws Exception {
String text = "SEND\na:alpha\n\nMessage payload\0";
connect().handleMessage(this.webSocketSession, new BinaryMessage(text.getBytes(UTF_8)));
connect().handleMessage(this.webSocketSession, new BinaryMessage(text.getBytes(StandardCharsets.UTF_8)));
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
verify(this.stompSession).handleMessage(captor.capture());
@@ -180,7 +177,7 @@ public class WebSocketStompClientTests {
StompHeaders headers = StompHeaders.readOnlyStompHeaders(accessor.toNativeHeaderMap());
assertEquals(StompCommand.SEND, accessor.getCommand());
assertEquals("alpha", headers.getFirst("a"));
assertEquals("Message payload", new String(message.getPayload(), UTF_8));
assertEquals("Message payload", new String(message.getPayload(), StandardCharsets.UTF_8));
}
@Test
@@ -193,7 +190,7 @@ public class WebSocketStompClientTests {
public void sendWebSocketMessage() throws Exception {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
accessor.setDestination("/topic/foo");
byte[] payload = "payload".getBytes(UTF_8);
byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
getTcpConnection().send(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
@@ -209,7 +206,7 @@ public class WebSocketStompClientTests {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
accessor.setDestination("/b");
accessor.setContentType(MimeTypeUtils.APPLICATION_OCTET_STREAM);
byte[] payload = "payload".getBytes(UTF_8);
byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
getTcpConnection().send(MessageBuilder.createMessage(payload, accessor.getMessageHeaders()));
@@ -218,7 +215,7 @@ public class WebSocketStompClientTests {
BinaryMessage binaryMessage = binaryMessageCaptor.getValue();
assertNotNull(binaryMessage);
assertEquals("SEND\ndestination:/b\ncontent-type:application/octet-stream\ncontent-length:7\n\npayload\0",
new String(binaryMessage.getPayload().array(), UTF_8));
new String(binaryMessage.getPayload().array(), StandardCharsets.UTF_8));
}
@Test

View File

@@ -20,7 +20,7 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
@@ -109,7 +109,7 @@ public class RestTemplateXhrTransportTests {
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
accessor.setDestination("/destination");
MessageHeaders headers = accessor.getMessageHeaders();
Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(Charset.forName("UTF-8")), headers);
Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
byte[] bytes = new StompEncoder().encode(message);
TextMessage textMessage = new TextMessage(bytes);
SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
@@ -197,7 +197,7 @@ public class RestTemplateXhrTransportTests {
}
private InputStream getInputStream(String content) {
byte[] bytes = content.getBytes(Charset.forName("UTF-8"));
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
return new ByteArrayInputStream(bytes);
}