diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceUtils.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceUtils.java index bc1763cce1..6d56216ec5 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceUtils.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketNamespaceUtils.java @@ -128,6 +128,10 @@ class WebSocketNamespaceUtils { if (!attrValue.isEmpty()) { sockJsServiceDef.getPropertyValues().add("heartbeatTime", Long.valueOf(attrValue)); } + attrValue = sockJsElement.getAttribute("message-codec"); + if (!attrValue.isEmpty()) { + sockJsServiceDef.getPropertyValues().add("messageCodec", new RuntimeBeanReference(attrValue)); + } sockJsServiceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); String sockJsServiceName = context.getReaderContext().registerWithGeneratedName(sockJsServiceDef); return new RuntimeBeanReference(sockJsServiceName); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java index 418b1d3a12..5cb733e5b5 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/SockJsServiceRegistration.java @@ -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. @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.web.socket.server.HandshakeInterceptor; import org.springframework.web.socket.sockjs.SockJsService; +import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec; import org.springframework.web.socket.sockjs.transport.TransportHandler; import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService; import org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService; @@ -61,6 +62,8 @@ public class SockJsServiceRegistration { private final List interceptors = new ArrayList(); + private SockJsMessageCodec messageCodec; + public SockJsServiceRegistration(TaskScheduler defaultTaskScheduler) { this.taskScheduler = defaultTaskScheduler; @@ -200,6 +203,18 @@ public class SockJsServiceRegistration { return this; } + /** + * The codec to use for encoding and decoding SockJS messages. + *

By default {@code Jackson2SockJsMessageCodec} is used requiring the + * Jackson library to be present on the classpath. + * @param codec the codec to use. + * @since 4.1 + */ + public SockJsServiceRegistration setMessageCodec(SockJsMessageCodec codec) { + this.messageCodec = codec; + return this; + } + protected SockJsService getSockJsService() { TransportHandlingSockJsService service = createSockJsService(); service.setHandshakeInterceptors(this.interceptors); @@ -224,6 +239,9 @@ public class SockJsServiceRegistration { if (this.webSocketEnabled != null) { service.setWebSocketEnabled(this.webSocketEnabled); } + if (this.messageCodec != null) { + service.setMessageCodec(this.messageCodec); + } return service; } diff --git a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.1.xsd b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.1.xsd index 447928b262..ea3dfa0fe8 100644 --- a/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.1.xsd +++ b/spring-websocket/src/main/resources/org/springframework/web/socket/config/spring-websocket-4.1.xsd @@ -224,6 +224,14 @@ ]]> + + + + + diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java index bbcc50038c..966850fd5e 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java @@ -16,6 +16,8 @@ package org.springframework.web.socket.config; +import java.io.IOException; +import java.io.InputStream; import java.util.Date; import java.util.List; import java.util.Map; @@ -45,6 +47,7 @@ import org.springframework.web.socket.server.HandshakeInterceptor; import org.springframework.web.socket.server.support.DefaultHandshakeHandler; import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler; import org.springframework.web.socket.sockjs.SockJsService; +import org.springframework.web.socket.sockjs.frame.SockJsMessageCodec; import org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler; import org.springframework.web.socket.sockjs.transport.TransportHandler; import org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService; @@ -218,6 +221,7 @@ public class HandlersBeanDefinitionParserTests { assertEquals(256, transportService.getDisconnectDelay()); assertEquals(1024, transportService.getHttpMessageCacheSize()); assertEquals(20, transportService.getHeartbeatTime()); + assertEquals(TestMessageCodec.class, transportService.getMessageCodec().getClass()); } private void loadBeanDefinitions(String fileName) { @@ -327,4 +331,22 @@ class TestTaskScheduler implements TaskScheduler { return null; } +} + +class TestMessageCodec implements SockJsMessageCodec { + + @Override + public String encode(String... messages) { + return null; + } + + @Override + public String[] decode(String content) throws IOException { + return new String[0]; + } + + @Override + public String[] decodeInputStream(InputStream content) throws IOException { + return new String[0]; + } } \ No newline at end of file diff --git a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-handlers-sockjs-attributes.xml b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-handlers-sockjs-attributes.xml index 4a2476c45c..e937d82c3e 100644 --- a/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-handlers-sockjs-attributes.xml +++ b/spring-websocket/src/test/resources/org/springframework/web/socket/config/websocket-config-handlers-sockjs-attributes.xml @@ -9,7 +9,7 @@ + message-cache-size="1024" heartbeat-time="20" message-codec="messageCodec"> @@ -20,5 +20,6 @@ +