Package org.atmosphere.websocket
Class DefaultWebSocketProcessor
java.lang.Object
org.atmosphere.websocket.DefaultWebSocketProcessor
- All Implemented Interfaces:
Serializable,WebSocketPingPongListener,WebSocketProcessor
public class DefaultWebSocketProcessor
extends Object
implements WebSocketProcessor, Serializable, WebSocketPingPongListener
Like the
AsynchronousProcessor class, this class is responsible for dispatching WebSocket request to the
proper WebSocket implementation. This class can be extended in order to support any protocol
running on top websocket.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.atmosphere.websocket.WebSocketProcessor
WebSocketProcessor.WebSocketException, WebSocketProcessor.WebSocketHandlerProxy -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturn the maximum size in bytes for the internalByteBufferused when buffering binary WebSocket streams.byteBufferMaxSize(int byteBufferMaxSize) Set the maximum size in bytes for the internalByteBufferused when buffering binary WebSocket streams.intReturn the maximum size in characters for the internalCharBufferused when buffering text WebSocket streams.charBufferMaxSize(int charBufferMaxSize) Set the maximum size in characters for the internalCharBufferused when buffering text WebSocket streams.voidInvked when the WebServer is closing the native WebSocketlongReturn the delay in milliseconds before a WebSocket close is finalized.configure(AtmosphereConfig config) Configure, or post construct a WebSocketProcessorconfigureHeader(AtmosphereRequest request) Build a header map from the given request's parameters and inject theHeaderConfig.X_ATMOSPHERE_TRANSPORTheader set toHeaderConfig.WEBSOCKET_TRANSPORT.voiddestroy()Destroy all resources associated with this class.booleanReturn whether the processor recyclesAtmosphereRequestandAtmosphereResponseinstances after use.final voiddispatch(WebSocket webSocket, AtmosphereRequest request, AtmosphereResponse r) Dispatch to request/response to theAsyncSupportimplementation as it was a normal HTTP request.protected voiddispatchReader(WebSocket webSocket, Reader r) protected voiddispatchStream(WebSocket webSocket, InputStream is) booleanReturn whether WebSocket protocol messages are dispatched asynchronously via the async executor.voidexecuteClose(WebSocket webSocket, int closeCode) Perform the actual close of the given WebSocket by ending the associatedAtmosphereResourcerequest and notifyingWebSocketEventListeners.handlers()Return the registeredWebSocketProcessor.WebSocketHandlerProxyinstances keyed by their mapped path.booleanhandshake(HttpServletRequest request) Determine if the WebSocket's handshake data can be processed, or if the request be cancelled.booleanReturn whetherAtmosphereInterceptors are invoked for incoming WebSocket messages.voidinvokeWebSocketProtocol(WebSocket webSocket, byte[] data, int offset, int length) Invoked when a WebSocket message gets received from the underlying containervoidinvokeWebSocketProtocol(WebSocket webSocket, InputStream stream) Invoked when a WebSocket message gets received from the underlying containervoidinvokeWebSocketProtocol(WebSocket webSocket, Reader reader) Invoked when a WebSocket message gets received from the underlying containervoidinvokeWebSocketProtocol(WebSocket webSocket, String webSocketMessage) Invoked when a WebSocket message gets received from the underlying containermapper()Return theEndpointMapperused to resolve incoming WebSocket requests to their registeredWebSocketProcessor.WebSocketHandlerProxy.voidnotifyListener(WebSocket webSocket, WebSocketEventListener.WebSocketEvent<?> event) Notify allWebSocketEventListenervoidHandle WebSocket PINGvoidHandle WebSocket PONGfinal voidopen(WebSocket webSocket, AtmosphereRequest request, AtmosphereResponse response) Invoked when a WebSocket gets opened by the underlying containerprotected voidprotected WebSocketHandlerpostProcessMapping(WebSocket webSocket, AtmosphereRequest request, WebSocketProcessor.WebSocketHandlerProxy w) registerWebSocketHandler(String path, WebSocketProcessor.WebSocketHandlerProxy webSockethandler) Register aWebSocketHandlerbooleanReturn whether wildcard URI-template mapping (paths containing{param}) is enabled.wildcardMapping(boolean wildcardMapping) Enable or disable wildcard URI-template mapping.
-
Constructor Details
-
DefaultWebSocketProcessor
public DefaultWebSocketProcessor()Create a new, unconfigured processor. Callers must invokeconfigure(AtmosphereConfig)before this processor can handle WebSocket events.
-
-
Method Details
-
configure
Description copied from interface:WebSocketProcessorConfigure, or post construct a WebSocketProcessor- Specified by:
configurein interfaceWebSocketProcessor- Parameters:
config- anAtmosphereConfig- Returns:
- this
-
handshake
Description copied from interface:WebSocketProcessorDetermine if the WebSocket's handshake data can be processed, or if the request be cancelled. Since it's container related native API, theHttpServletRequestmight be null, so implementation must check for null.- Specified by:
handshakein interfaceWebSocketProcessor- Parameters:
request-HttpServletRequest- Returns:
- true if the processing can continue, false if not.
-
registerWebSocketHandler
public WebSocketProcessor registerWebSocketHandler(String path, WebSocketProcessor.WebSocketHandlerProxy webSockethandler) Description copied from interface:WebSocketProcessorRegister aWebSocketHandler- Specified by:
registerWebSocketHandlerin interfaceWebSocketProcessor- Parameters:
path- the URI mapping the WebSocketHandlerwebSockethandler- an instance ofWebSocketProcessor.WebSocketHandlerProxy- Returns:
- this
-
open
public final void open(WebSocket webSocket, AtmosphereRequest request, AtmosphereResponse response) throws IOException Description copied from interface:WebSocketProcessorInvoked when a WebSocket gets opened by the underlying container- Specified by:
openin interfaceWebSocketProcessor- Parameters:
request-- Throws:
IOException
-
postProcessMapping
protected WebSocketHandler postProcessMapping(WebSocket webSocket, AtmosphereRequest request, WebSocketProcessor.WebSocketHandlerProxy w) -
invokeWebSocketProtocol
Description copied from interface:WebSocketProcessorInvoked when a WebSocket message gets received from the underlying container- Specified by:
invokeWebSocketProtocolin interfaceWebSocketProcessor- Parameters:
webSocketMessage-
-
invokeWebSocketProtocol
Description copied from interface:WebSocketProcessorInvoked when a WebSocket message gets received from the underlying container- Specified by:
invokeWebSocketProtocolin interfaceWebSocketProcessor- Parameters:
data-
-
invokeWebSocketProtocol
Description copied from interface:WebSocketProcessorInvoked when a WebSocket message gets received from the underlying container- Specified by:
invokeWebSocketProtocolin interfaceWebSocketProcessor- Parameters:
stream-
-
invokeWebSocketProtocol
Description copied from interface:WebSocketProcessorInvoked when a WebSocket message gets received from the underlying container- Specified by:
invokeWebSocketProtocolin interfaceWebSocketProcessor- Parameters:
reader-
-
dispatch
Dispatch to request/response to theAsyncSupportimplementation as it was a normal HTTP request.- Parameters:
request- aAtmosphereRequestr- aAtmosphereResponse
-
close
Description copied from interface:WebSocketProcessorInvked when the WebServer is closing the native WebSocket- Specified by:
closein interfaceWebSocketProcessor- Parameters:
closeCode-
-
executeClose
Perform the actual close of the given WebSocket by ending the associatedAtmosphereResourcerequest and notifyingWebSocketEventListeners. This method is separated fromclose(WebSocket, int)to allow deferred invocation (e.g. Firefox delayed-close workaround).- Parameters:
webSocket- the WebSocket being closedcloseCode- the WebSocket close status code
-
destroy
public void destroy()Description copied from interface:WebSocketProcessorDestroy all resources associated with this class.- Specified by:
destroyin interfaceWebSocketProcessor
-
notifyListener
Description copied from interface:WebSocketProcessorNotify allWebSocketEventListener- Specified by:
notifyListenerin interfaceWebSocketProcessor- Parameters:
event-
-
configureHeader
Build a header map from the given request's parameters and inject theHeaderConfig.X_ATMOSPHERE_TRANSPORTheader set toHeaderConfig.WEBSOCKET_TRANSPORT. The resulting map is typically applied to the request viaAtmosphereRequest.headers(Map).- Parameters:
request- the incoming atmosphere request whose parameters are copied as headers- Returns:
- a mutable map of header name/value pairs
-
dispatchStream
- Throws:
IOException
-
dispatchReader
- Throws:
IOException
-
optimizeMapping
protected void optimizeMapping() -
wildcardMapping
public boolean wildcardMapping()Return whether wildcard URI-template mapping (paths containing{param}) is enabled.- Returns:
trueif at least one registered handler path contains URI-template variables
-
wildcardMapping
Enable or disable wildcard URI-template mapping.- Parameters:
wildcardMapping-trueto enable wildcard mapping- Returns:
- this processor for method chaining
-
handlers
Return the registeredWebSocketProcessor.WebSocketHandlerProxyinstances keyed by their mapped path.- Returns:
- an unmodifiable view is not guaranteed; callers should treat the returned map as read-only
-
executeAsync
public boolean executeAsync()Return whether WebSocket protocol messages are dispatched asynchronously via the async executor. Controlled by theApplicationConfig.WEBSOCKET_PROTOCOL_EXECUTIONinit parameter.- Returns:
trueif async dispatch is enabled
-
destroyable
public boolean destroyable()Return whether the processor recyclesAtmosphereRequestandAtmosphereResponseinstances after use. Controlled by theApplicationConfig.RECYCLE_ATMOSPHERE_REQUEST_RESPONSEinit parameter.- Returns:
trueif request/response recycling is enabled
-
byteBufferMaxSize
public int byteBufferMaxSize()Return the maximum size in bytes for the internalByteBufferused when buffering binary WebSocket streams. Defaults to 2 MB.- Returns:
- the byte buffer max size
-
byteBufferMaxSize
Set the maximum size in bytes for the internalByteBufferused when buffering binary WebSocket streams.- Parameters:
byteBufferMaxSize- the new max size in bytes- Returns:
- this processor for method chaining
-
charBufferMaxSize
public int charBufferMaxSize()Return the maximum size in characters for the internalCharBufferused when buffering text WebSocket streams. Defaults to 2 MB.- Returns:
- the char buffer max size
-
charBufferMaxSize
Set the maximum size in characters for the internalCharBufferused when buffering text WebSocket streams.- Parameters:
charBufferMaxSize- the new max size in characters- Returns:
- this processor for method chaining
-
closingTime
public long closingTime()Return the delay in milliseconds before a WebSocket close is finalized. A non-zero value enables delayed-close behavior, used as a workaround for Firefox reconnection issues. Controlled by theApplicationConfig.CLOSED_ATMOSPHERE_THINK_TIMEinit parameter.- Returns:
- the closing delay in milliseconds, or 0 for immediate close
-
mapper
Return theEndpointMapperused to resolve incoming WebSocket requests to their registeredWebSocketProcessor.WebSocketHandlerProxy.- Returns:
- the endpoint mapper
-
invokeInterceptors
public boolean invokeInterceptors()Return whetherAtmosphereInterceptors are invoked for incoming WebSocket messages. Controlled by theApplicationConfig.INVOKE_ATMOSPHERE_INTERCEPTOR_ON_WEBSOCKET_MESSAGEinit parameter. Defaults totrue.- Returns:
trueif interceptors are invoked on each WebSocket message
-
onPong
Description copied from interface:WebSocketPingPongListenerHandle WebSocket PONG- Specified by:
onPongin interfaceWebSocketPingPongListener- Parameters:
webSocket-WebSocketpayload- the received bytesoffset- the offsetlength- the length
-
onPing
Description copied from interface:WebSocketPingPongListenerHandle WebSocket PING- Specified by:
onPingin interfaceWebSocketPingPongListener- Parameters:
webSocket-WebSocketpayload- the received bytesoffset- the offsetlength- the length
-