T
- the type being converted to (for Encoder) or from (for Decoder)M
- the WebSocket message type (String
or ByteBuffer
)public abstract class ConvertingEncoderDecoderSupport<T,M>
extends java.lang.Object
javax.websocket.Encoder
and/or javax.websocket.Decoder
. It provides encode and decode method
implementations that delegate to a Spring ConversionService
.
By default, this class looks up a ConversionService
registered in the
active ApplicationContext
under
the name 'webSocketConversionService'
. This works fine for both client
and server endpoints, in a Servlet container environment. If not running in a
Servlet container, subclasses will need to override the
getConversionService()
method to provide an alternative lookup strategy.
Subclasses can extend this class and should also implement one or
both of javax.websocket.Encoder
and javax.websocket.Decoder
.
For convenience ConvertingEncoderDecoderSupport.BinaryEncoder
,
ConvertingEncoderDecoderSupport.BinaryDecoder
,
ConvertingEncoderDecoderSupport.TextEncoder
and
ConvertingEncoderDecoderSupport.TextDecoder
subclasses are provided.
Since JSR-356 only allows Encoder/Decoder to be registered by type, instances
of this class are therefore managed by the WebSocket runtime, and do not need to
be registered as Spring Beans. They can, however, by injected with Spring-managed
dependencies via @Autowire
.
Converters to convert between the type
and String
or
ByteBuffer
should be registered.
ConvertingEncoderDecoderSupport.BinaryEncoder
,
ConvertingEncoderDecoderSupport.BinaryDecoder
,
ConvertingEncoderDecoderSupport.TextEncoder
,
ConvertingEncoderDecoderSupport.TextDecoder
Modifier and Type | Class and Description |
---|---|
static class |
ConvertingEncoderDecoderSupport.BinaryDecoder<T>
A binary
javax.websocket.Encoder that delegates
to Spring's conversion service. |
static class |
ConvertingEncoderDecoderSupport.BinaryEncoder<T>
A binary
javax.websocket.Encoder that delegates
to Spring's conversion service. |
static class |
ConvertingEncoderDecoderSupport.TextDecoder<T>
A Text
javax.websocket.Encoder that delegates
to Spring's conversion service. |
static class |
ConvertingEncoderDecoderSupport.TextEncoder<T>
A text
javax.websocket.Encoder that delegates
to Spring's conversion service. |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
CONVERSION_SERVICE_BEAN_NAME |
Constructor and Description |
---|
ConvertingEncoderDecoderSupport() |
Modifier and Type | Method and Description |
---|---|
T |
decode(M message) |
void |
destroy() |
M |
encode(T object) |
protected ApplicationContext |
getApplicationContext()
Returns the active
ApplicationContext . |
protected ConversionService |
getConversionService()
Strategy method used to obtain the
ConversionService . |
protected TypeDescriptor |
getMessageType()
Returns the websocket message type.
|
protected TypeDescriptor |
getType()
Returns the type being converted.
|
void |
init(EndpointConfig config) |
private java.lang.Class<?>[] |
resolveTypeArguments() |
boolean |
willDecode(M bytes) |
private static final java.lang.String CONVERSION_SERVICE_BEAN_NAME
public void init(EndpointConfig config)
javax.websocket.Encoder#init(EndpointConfig)
,
javax.websocket.Decoder#init(EndpointConfig)
public void destroy()
javax.websocket.Encoder#destroy()
,
javax.websocket.Decoder#destroy()
protected ConversionService getConversionService()
ConversionService
. By default this
method expects a bean named 'webSocketConversionService'
in the
active ApplicationContext
.ConversionService
(never null)protected ApplicationContext getApplicationContext()
ApplicationContext
. Be default this method obtains
the context via ContextLoader.getCurrentWebApplicationContext()
, which
finds the ApplicationContext loaded via ContextLoader
typically in a
Servlet container environment. When not running in a Servlet container and
not using ContextLoader
, this method should be overridden.ApplicationContext
or null
protected TypeDescriptor getType()
protected TypeDescriptor getMessageType()
private java.lang.Class<?>[] resolveTypeArguments()
public M encode(T object) throws EncodeException
EncodeException
javax.websocket.Encoder.Text#encode(Object)
,
javax.websocket.Encoder.Binary#encode(Object)
public boolean willDecode(M bytes)
javax.websocket.Decoder.Text#willDecode(String)
,
javax.websocket.Decoder.Binary#willDecode(ByteBuffer)