Top Description Inners Fields Constructors Methods
org.springframework.web.socket.adapter.standard

public abstract Class ConvertingEncoderDecoderSupport<T, M>

extends Object
Class Inheritance
Known Direct Subclasses
org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport.BinaryEncoder, org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport.BinaryDecoder, org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport.TextEncoder, org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport.TextDecoder
Type Parameters
<T>
the type being converted to (for Encoder) or from (for Decoder)
<M>
the WebSocket message type (String or ByteBuffer)
Imports
java.nio.ByteBuffer, jakarta.websocket.DecodeException, .Decoder, .EncodeException, .Encoder, .EndpointConfig, org.springframework.beans.BeansException, org.springframework.beans.factory.annotation.Autowired, org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.ApplicationContext, .ConfigurableApplicationContext, org.springframework.core.GenericTypeResolver, org.springframework.core.convert.ConversionException, .ConversionService, .TypeDescriptor, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.context.ContextLoader

Base class that can be used to implement a standard jakarta.websocket.Encoder and/or jakarta.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 jakarta.websocket.Encoder and jakarta.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.

Author
Phillip Webb
Since
4.0
See Also
ConvertingEncoderDecoderSupport.BinaryEncoder, ConvertingEncoderDecoderSupport.BinaryDecoder, ConvertingEncoderDecoderSupport.TextEncoder, ConvertingEncoderDecoderSupport.TextDecoder

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
ConvertingEncoderDecoderSupport.BinaryDecoder<
the type that this Decoder can convert from
T
>

A binary jakarta.websocket.Encoder that delegates to Spring's conversion service.

public abstract static class
ConvertingEncoderDecoderSupport.BinaryEncoder<
the type that this Encoder can convert to
T
>

A binary jakarta.websocket.Encoder that delegates to Spring's conversion service.

public abstract static class
ConvertingEncoderDecoderSupport.TextDecoder<
the type that this Decoder can convert from
T
>

A Text jakarta.websocket.Encoder that delegates to Spring's conversion service.

public abstract static class
ConvertingEncoderDecoderSupport.TextEncoder<
the type that this Encoder can convert to
T
>

A text jakarta.websocket.Encoder that delegates to Spring's conversion service.

Field Summary

Modifier and TypeField and Description
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public T
decode(M message)

Decode the message into an object.

public void
destroy()

Called to destroy the encoder/decoder.

public M
encode(T object)

Encode an object to a message.

protected ApplicationContext

Returns:

the ApplicationContext or null
getApplicationContext
()

Returns the active ApplicationContext.

protected ConversionService

Returns:

the ConversionService (never null)
getConversionService
()

Strategy method used to obtain the ConversionService.

protected TypeDescriptor
getMessageType()

Returns the websocket message type.

protected TypeDescriptor
getType()

Returns the type being converted.

public void
init(EndpointConfig config)

Called to initialize the encoder/decoder.

private Class<?>[]
public boolean
willDecode(M bytes)

Determine if a given message can be decoded.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONVERSION_SERVICE_BEAN_NAMEback to summary
private static final String CONVERSION_SERVICE_BEAN_NAME

Constructor Detail

ConvertingEncoderDecoderSupportback to summary
public ConvertingEncoderDecoderSupport()

Method Detail

decodeback to summary
public T decode(M message) throws DecodeException

Decode the message into an object.

Annotations
@SuppressWarnings:unchecked
@Nullable
See Also
jakarta.websocket.Decoder.Text#decode(String), jakarta.websocket.Decoder.Binary#decode(ByteBuffer)
destroyback to summary
public void destroy()

Called to destroy the encoder/decoder.

See Also
jakarta.websocket.Encoder#destroy(), jakarta.websocket.Decoder#destroy()
encodeback to summary
public M encode(T object) throws EncodeException

Encode an object to a message.

Annotations
@SuppressWarnings:unchecked
@Nullable
See Also
jakarta.websocket.Encoder.Text#encode(Object), jakarta.websocket.Encoder.Binary#encode(Object)
getApplicationContextback to summary
protected ApplicationContext getApplicationContext()

Returns the active ApplicationContext. By 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.

Returns:ApplicationContext

the ApplicationContext or null

Annotations
@Nullable
getConversionServiceback to summary
protected ConversionService getConversionService()

Strategy method used to obtain the ConversionService. By default, this method expects a bean named 'webSocketConversionService' in the active ApplicationContext.

Returns:ConversionService

the ConversionService (never null)

getMessageTypeback to summary
protected TypeDescriptor getMessageType()

Returns the websocket message type. By default, the type is resolved using the generic arguments of the class.

getTypeback to summary
protected TypeDescriptor getType()

Returns the type being converted. By default, the type is resolved using the generic arguments of the class.

initback to summary
public void init(EndpointConfig config)

Called to initialize the encoder/decoder.

See Also
jakarta.websocket.Encoder#init(EndpointConfig), jakarta.websocket.Decoder#init(EndpointConfig)
resolveTypeArgumentsback to summary
private Class<?>[] resolveTypeArguments()
willDecodeback to summary
public boolean willDecode(M bytes)

Determine if a given message can be decoded.

See Also
decode(Object), jakarta.websocket.Decoder.Text#willDecode(String), jakarta.websocket.Decoder.Binary#willDecode(ByteBuffer)
org.springframework.web.socket.adapter.standard back to summary

public abstract Class ConvertingEncoderDecoderSupport.BinaryDecoder<T>

extends ConvertingEncoderDecoderSupport<T, ByteBuffer>
implements Decoder.Binary<T>
Class Inheritance
All Implemented Interfaces
jakarta.websocket.Decoder.Binary, jakarta.websocket.Decoder
Type Parameters
<T>
the type that this Decoder can convert from

A binary jakarta.websocket.Encoder that delegates to Spring's conversion service. See ConvertingEncoderDecoderSupport for details.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport:
decodedestroyencodegetApplicationContextgetConversionServicegetMessageTypegetTypeinitwillDecode

Constructor Detail

BinaryDecoderback to summary
public BinaryDecoder()
org.springframework.web.socket.adapter.standard back to summary

public abstract Class ConvertingEncoderDecoderSupport.BinaryEncoder<T>

extends ConvertingEncoderDecoderSupport<T, ByteBuffer>
implements Encoder.Binary<T>
Class Inheritance
All Implemented Interfaces
jakarta.websocket.Encoder.Binary, jakarta.websocket.Encoder
Type Parameters
<T>
the type that this Encoder can convert to

A binary jakarta.websocket.Encoder that delegates to Spring's conversion service. See ConvertingEncoderDecoderSupport for details.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport:
decodedestroyencodegetApplicationContextgetConversionServicegetMessageTypegetTypeinitwillDecode

Constructor Detail

BinaryEncoderback to summary
public BinaryEncoder()
org.springframework.web.socket.adapter.standard back to summary

public abstract Class ConvertingEncoderDecoderSupport.TextDecoder<T>

extends ConvertingEncoderDecoderSupport<T, String>
implements Decoder.Text<T>
Class Inheritance
All Implemented Interfaces
jakarta.websocket.Decoder.Text, jakarta.websocket.Decoder
Type Parameters
<T>
the type that this Decoder can convert from

A Text jakarta.websocket.Encoder that delegates to Spring's conversion service. See ConvertingEncoderDecoderSupport for details.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport:
decodedestroyencodegetApplicationContextgetConversionServicegetMessageTypegetTypeinitwillDecode

Constructor Detail

TextDecoderback to summary
public TextDecoder()
org.springframework.web.socket.adapter.standard back to summary

public abstract Class ConvertingEncoderDecoderSupport.TextEncoder<T>

extends ConvertingEncoderDecoderSupport<T, String>
implements Encoder.Text<T>
Class Inheritance
All Implemented Interfaces
jakarta.websocket.Encoder.Text, jakarta.websocket.Encoder
Type Parameters
<T>
the type that this Encoder can convert to

A text jakarta.websocket.Encoder that delegates to Spring's conversion service. See ConvertingEncoderDecoderSupport for details.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from org.springframework.web.socket.adapter.standard.ConvertingEncoderDecoderSupport:
decodedestroyencodegetApplicationContextgetConversionServicegetMessageTypegetTypeinitwillDecode

Constructor Detail

TextEncoderback to summary
public TextEncoder()