Add SockJsMessageCodec

A SockJS message frame is an array of JSON-encoded messages and before
this change the use of the Jackson 2 library was hard-coded.

A Jackson 2 and Jackson 1.x implementations are provided and
automatically used if those libraries are present on the classpath.

Issue: SPR-10800
This commit is contained in:
Rossen Stoyanchev
2013-07-31 16:21:52 -04:00
parent 2af891683e
commit dad7115c23
31 changed files with 517 additions and 218 deletions

View File

@@ -224,6 +224,11 @@ public class AbstractSockJsServiceTests extends AbstractHttpRequestTests {
super(scheduler);
}
@Override
public SockJsMessageCodec getMessageCodecRequired() {
throw new UnsupportedOperationException();
}
@Override
protected void handleRawWebSocketRequest(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler handler) throws IOException {

View File

@@ -18,6 +18,7 @@ package org.springframework.web.socket.sockjs;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.sockjs.support.Jackson2SockJsMessageCodec;
/**
* @author Rossen Stoyanchev
@@ -30,6 +31,8 @@ public class StubSockJsConfig implements SockJsConfiguration {
private TaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
private SockJsMessageCodec messageCodec = new Jackson2SockJsMessageCodec();
@Override
public int getStreamBytesLimit() {
@@ -58,4 +61,17 @@ public class StubSockJsConfig implements SockJsConfiguration {
this.taskScheduler = taskScheduler;
}
@Override
public SockJsMessageCodec getMessageCodecRequired() {
return this.messageCodec;
}
public SockJsMessageCodec getMessageCodec() {
return messageCodec;
}
public void setMessageCodec(SockJsMessageCodec messageCodec) {
this.messageCodec = messageCodec;
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.web.socket.sockjs.support;
package org.springframework.web.socket.sockjs.transport;
import java.util.Collections;
import java.util.HashSet;
@@ -30,6 +30,7 @@ import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.sockjs.AbstractSockJsSession;
import org.springframework.web.socket.sockjs.SockJsConfiguration;
import org.springframework.web.socket.sockjs.SockJsSessionFactory;
import org.springframework.web.socket.sockjs.StubSockJsConfig;
import org.springframework.web.socket.sockjs.TestSockJsSession;
@@ -164,6 +165,10 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
return TransportType.XHR;
}
@Override
public void setSockJsConfiguration(SockJsConfiguration sockJsConfig) {
}
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler handler, AbstractSockJsSession session) throws TransportErrorException {
@@ -176,6 +181,7 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
public AbstractSockJsSession createSession(String sessionId, WebSocketHandler webSocketHandler) {
return new TestSockJsSession(sessionId, new StubSockJsConfig(), webSocketHandler);
}
}
private static class StubXhrSendTransportHandler implements TransportHandler {
@@ -185,6 +191,10 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
return TransportType.XHR_SEND;
}
@Override
public void setSockJsConfiguration(SockJsConfiguration sockJsConfig) {
}
@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler handler, AbstractSockJsSession session) throws TransportErrorException {

View File

@@ -105,16 +105,20 @@ public class HttpReceivingTransportHandlerTests extends AbstractHttpRequestTest
@Test
public void delegateMessageException() throws Exception {
StubSockJsConfig sockJsConfig = new StubSockJsConfig();
this.servletRequest.setContent("[\"x\"]".getBytes("UTF-8"));
WebSocketHandler webSocketHandler = mock(WebSocketHandler.class);
TestSockJsSession session = new TestSockJsSession("1", new StubSockJsConfig(), webSocketHandler);
TestSockJsSession session = new TestSockJsSession("1", sockJsConfig, webSocketHandler);
session.delegateConnectionEstablished();
doThrow(new Exception()).when(webSocketHandler).handleMessage(session, new TextMessage("x"));
try {
new XhrTransportHandler().handleRequest(this.request, this.response, webSocketHandler, session);
XhrTransportHandler transportHandler = new XhrTransportHandler();
transportHandler.setSockJsConfiguration(sockJsConfig);
transportHandler.handleRequest(this.request, this.response, webSocketHandler, session);
fail("Expected exception");
}
catch (TransportErrorException ex) {
@@ -129,6 +133,7 @@ public class HttpReceivingTransportHandlerTests extends AbstractHttpRequestTest
WebSocketHandler webSocketHandler = mock(WebSocketHandler.class);
AbstractSockJsSession session = new TestSockJsSession("1", new StubSockJsConfig(), webSocketHandler);
transportHandler.setSockJsConfiguration(new StubSockJsConfig());
transportHandler.handleRequest(this.request, this.response, webSocketHandler, session);
assertEquals("text/plain;charset=UTF-8", this.response.getHeaders().getContentType().toString());