Allow setting SockJsMessageCodec in WebSocket config
Issue: SPR-12091
This commit is contained in:
parent
15f496bc2a
commit
2a0765e76d
|
@ -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);
|
||||
|
|
|
@ -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<HandshakeInterceptor> interceptors = new ArrayList<HandshakeInterceptor>();
|
||||
|
||||
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.
|
||||
* <p>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;
|
||||
}
|
||||
|
||||
|
|
|
@ -224,6 +224,14 @@
|
|||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="message-codec" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.web.socket.sockjs.support.AbstractSockJsService"><![CDATA[
|
||||
The bean name of a SockJsMessageCodec to use for encoding and decoding SockJS messages.
|
||||
By default Jackson2SockJsMessageCodec is used requiring the Jackson library to be present on the classpath.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="stomp-broker-relay">
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@
|
|||
<websocket:mapping path="/test" handler="testHandler"/>
|
||||
<websocket:sockjs name="testSockJsService" scheduler="testTaskScheduler" websocket-enabled="false"
|
||||
session-cookie-needed="false" stream-bytes-limit="2048" disconnect-delay="256"
|
||||
message-cache-size="1024" heartbeat-time="20">
|
||||
message-cache-size="1024" heartbeat-time="20" message-codec="messageCodec">
|
||||
<websocket:transport-handlers register-defaults="false">
|
||||
<bean class="org.springframework.web.socket.sockjs.transport.handler.XhrPollingTransportHandler"/>
|
||||
<ref bean="xhrStreamingTransportHandler"/>
|
||||
|
@ -20,5 +20,6 @@
|
|||
<bean id="testHandler" class="org.springframework.web.socket.config.TestWebSocketHandler"/>
|
||||
<bean id="testTaskScheduler" class="org.springframework.web.socket.config.TestTaskScheduler"/>
|
||||
<bean id="xhrStreamingTransportHandler" class="org.springframework.web.socket.sockjs.transport.handler.XhrStreamingTransportHandler"/>
|
||||
<bean id="messageCodec" class="org.springframework.web.socket.config.TestMessageCodec" />
|
||||
|
||||
</beans>
|
||||
|
|
Loading…
Reference in New Issue