Top Description Inners Fields Constructors Methods
org.apache.tomcat.websocket

public abstract Class WsFrameBase

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.tomcat.websocket.WsFrameClient, org.apache.tomcat.websocket.server.WsFrameServer
Imports
java.io.IOException, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.CharsetDecoder, .CoderResult, .CodingErrorAction, .StandardCharsets, java.util.List, java.util.concurrent.atomic.AtomicReferenceFieldUpdater, jakarta.websocket.CloseReason, .CloseReason.CloseCodes, .Extension, .MessageHandler, .PongMessage, org.apache.juli.logging.Log, org.apache.tomcat.util.ExceptionUtils, org.apache.tomcat.util.res.StringManager

Takes the ServletInputStream, processes the WebSocket frames it contains and extracts the messages. WebSocket Pings received will be responded to automatically without any action required by the application.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
WsFrameBase.NoopTransformation

For use by the client implementation that needs to obtain payload data without the need for unmasking.

protected static enum
WsFrameBase.ReadState

WAITING            - not suspended
                     Server case: waiting for a notification that data is ready to be read from the socket, the
                                  socket is registered to the poller
                     Client case: data has been read from the socket and is waiting for data to be processed

PROCESSING         - not suspended
                     Server case: reading from the socket and processing the data
                     Client case: processing the data if such has already been read and more data will be read
                                  from the socket

SUSPENDING_WAIT    - suspended, a call to suspend() was made while in WAITING state.
private static enum
private abstract static class
private class
WsFrameBase.UnmaskTransformation

For use by the server implementation that needs to obtain payload data and unmask it before any further processing.

Field Summary

Modifier and TypeField and Description
private MessageHandler
private boolean
private final ByteBuffer
private final CharBuffer
private boolean
protected final ByteBuffer
private final byte[]
private int
private ByteBuffer
private CharBuffer
private byte
private volatile boolean
private long
private volatile long
private static final AtomicReferenceFieldUpdater<WsFrameBase, WsFrameBase.ReadState>
private volatile WsFrameBase.ReadState
private int
private static final StringManager
private volatile WsFrameBase.State
private boolean
private MessageHandler
private final Transformation
private final CharsetDecoder
private final CharsetDecoder
protected final WsSession

Constructor Summary

AccessConstructor and Description
public
WsFrameBase(WsSession wsSession, Transformation transformation)

Method Summary

Modifier and TypeMethod and Description
protected static long
byteArrayToLong(byte[] b, int start, int len)

protected void
protected boolean
private void
private void
protected abstract Log
protected WsFrameBase.ReadState
protected Transformation
private void
protected abstract boolean
protected boolean
protected boolean
private void
private void
private void
private boolean
private boolean
private boolean
private boolean
private boolean

Returns:

true if sufficient data was present to process all of the initial header
processInitialHeader
()

protected void
private boolean

Returns:

true if sufficient data was present to complete the processing of the header
processRemainingHeader
()

public void
protected abstract void
resumeProcessing()

This method will be invoked when the read operation is resumed.

protected void
sendMessageBinary(ByteBuffer msg, boolean last)

protected void
sendMessageText(boolean last)

public void
private boolean
protected void
updateStats(long
Size of message payload
payloadLength
)

Hook for updating server side statistics.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

binaryMsgHandlerback to summary
private MessageHandler binaryMsgHandler
continuationExpectedback to summary
private boolean continuationExpected
controlBufferBinaryback to summary
private final ByteBuffer controlBufferBinary
controlBufferTextback to summary
private final CharBuffer controlBufferText
finback to summary
private boolean fin
inputBufferback to summary
protected final ByteBuffer inputBuffer
maskback to summary
private final byte[] mask
maskIndexback to summary
private int maskIndex
messageBufferBinaryback to summary
private ByteBuffer messageBufferBinary
messageBufferTextback to summary
private CharBuffer messageBufferText
opCodeback to summary
private byte opCode
openback to summary
private volatile boolean open
payloadLengthback to summary
private long payloadLength
payloadWrittenback to summary
private volatile long payloadWritten
READ_STATE_UPDATERback to summary
private static final AtomicReferenceFieldUpdater<WsFrameBase, WsFrameBase.ReadState> READ_STATE_UPDATER
readStateback to summary
private volatile WsFrameBase.ReadState readState
rsvback to summary
private int rsv
smback to summary
private static final StringManager sm
stateback to summary
private volatile WsFrameBase.State state
textMessageback to summary
private boolean textMessage
textMsgHandlerback to summary
private MessageHandler textMsgHandler
transformationback to summary
private final Transformation transformation
utf8DecoderControlback to summary
private final CharsetDecoder utf8DecoderControl
utf8DecoderMessageback to summary
private final CharsetDecoder utf8DecoderMessage
wsSessionback to summary
protected final WsSession wsSession

Constructor Detail

WsFrameBaseback to summary
public WsFrameBase(WsSession wsSession, Transformation transformation)

Method Detail

byteArrayToLongback to summary
protected static long byteArrayToLong(byte[] b, int start, int len) throws IOException
changeReadStateback to summary
protected void changeReadState(WsFrameBase.ReadState newState)
changeReadStateback to summary
protected boolean changeReadState(WsFrameBase.ReadState oldState, WsFrameBase.ReadState newState)
checkRoomHeadersback to summary
private void checkRoomHeaders()
checkRoomPayloadback to summary
private void checkRoomPayload()
getLogback to summary
protected abstract Log getLog()
getReadStateback to summary
protected WsFrameBase.ReadState getReadState()
getTransformationback to summary
protected Transformation getTransformation()
handleThrowableOnSendback to summary
private void handleThrowableOnSend(Throwable t) throws WsIOException
isMaskedback to summary
protected abstract boolean isMasked()
isOpenback to summary
protected boolean isOpen()
isSuspendedback to summary
protected boolean isSuspended()
makeRoomback to summary
private void makeRoom()
newFrameback to summary
private void newFrame()
newMessageback to summary
private void newMessage()
processDataback to summary
private boolean processData() throws IOException
processDataBinaryback to summary
private boolean processDataBinary() throws IOException
processDataControlback to summary
private boolean processDataControl() throws IOException
processDataTextback to summary
private boolean processDataText() throws IOException
processInitialHeaderback to summary
private boolean processInitialHeader() throws IOException
Returns:boolean

true if sufficient data was present to process all of the initial header

processInputBufferback to summary
protected void processInputBuffer() throws IOException
processRemainingHeaderback to summary
private boolean processRemainingHeader() throws IOException
Returns:boolean

true if sufficient data was present to complete the processing of the header

resumeback to summary
public void resume()
resumeProcessingback to summary
protected abstract void resumeProcessing()

This method will be invoked when the read operation is resumed. As the suspend of the read operation can be invoked at any time, when implementing this method one should consider that there might still be data remaining into the internal buffers that needs to be processed before reading again from the socket.

sendMessageBinaryback to summary
protected void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOException
Annotations
@SuppressWarnings:unchecked
sendMessageTextback to summary
protected void sendMessageText(boolean last) throws WsIOException
Annotations
@SuppressWarnings:unchecked
suspendback to summary
public void suspend()
swallowInputback to summary
private boolean swallowInput()
updateStatsback to summary
protected void updateStats(long payloadLength)

Hook for updating server side statistics. Called on every frame received.

Parameters
payloadLength:long

Size of message payload

usePartialback to summary
private boolean usePartial()
org.apache.tomcat.websocket back to summary

private final Class WsFrameBase.NoopTransformation

extends TerminalTransformation
Class Inheritance

For use by the client implementation that needs to obtain payload data without the need for unmasking.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public TransformationResult
getMoreData(byte
The opcode for the frame currently being processed
opCode
,
boolean
Is this the final frame in this WebSocket message?
fin
,
int
The reserved bits for the frame currently being processed
rsv
,
ByteBuffer
The buffer in which the data is to be written
dest
)

Implements org.apache.tomcat.websocket.Transformation.getMoreData.

Obtain more input data.

public List<MessagePart>
sendMessagePart(List<MessagePart>
The list of messages to be transformed
messageParts
)

Implements org.apache.tomcat.websocket.Transformation.sendMessagePart.

Takes the provided list of messages, transforms them, passes the transformed list on to the next transformation (if any) and then returns the resulting list of message parts after all of the transformations have been applied.

Inherited from org.apache.tomcat.websocket.WsFrameBase.TerminalTransformation:
closegetExtensionResponsesetNextvalidateRsvvalidateRsvBits

Constructor Detail

NoopTransformationback to summary
private NoopTransformation()

Method Detail

getMoreDataback to summary
public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest)

Implements org.apache.tomcat.websocket.Transformation.getMoreData.

Doc from org.apache.tomcat.websocket.Transformation.getMoreData.

Obtain more input data.

Parameters
opCode:byte

The opcode for the frame currently being processed

fin:boolean

Is this the final frame in this WebSocket message?

rsv:int

The reserved bits for the frame currently being processed

dest:ByteBuffer

The buffer in which the data is to be written

Returns:TransformationResult

The result of trying to read more data from the transform

Annotations
@Override
sendMessagePartback to summary
public List<MessagePart> sendMessagePart(List<MessagePart> messageParts)

Implements org.apache.tomcat.websocket.Transformation.sendMessagePart.

Doc from org.apache.tomcat.websocket.Transformation.sendMessagePart.

Takes the provided list of messages, transforms them, passes the transformed list on to the next transformation (if any) and then returns the resulting list of message parts after all of the transformations have been applied.

Parameters
messageParts:List<MessagePart>

The list of messages to be transformed

Returns:List<MessagePart>

The list of messages after this any any subsequent transformations have been applied. The size of the returned list may be bigger or smaller than the size of the input list

Annotations
@Override
org.apache.tomcat.websocket back to summary

protected final Enum WsFrameBase.ReadState

extends Enum<WsFrameBase.ReadState>
Class Inheritance

WAITING            - not suspended
                     Server case: waiting for a notification that data is ready to be read from the socket, the
                                  socket is registered to the poller
                     Client case: data has been read from the socket and is waiting for data to be processed

PROCESSING         - not suspended
                     Server case: reading from the socket and processing the data
                     Client case: processing the data if such has already been read and more data will be read
                                  from the socket

SUSPENDING_WAIT    - suspended, a call to suspend() was made while in WAITING state. A call to resume() will do
                     nothing and will transition to WAITING state

SUSPENDING_PROCESS - suspended, a call to suspend() was made while in PROCESSING state. A call to resume() will
                     do nothing and will transition to PROCESSING state

SUSPENDED          - suspended
                     Server case: processing data finished (SUSPENDING_PROCESS) / a notification was received
                                  that data is ready to be read from the socket (SUSPENDING_WAIT), socket is not
                                  registered to the poller
                     Client case: processing data finished (SUSPENDING_PROCESS) / data has been read from the
                                  socket and is available for processing (SUSPENDING_WAIT)
                     A call to resume() will:
                     Server case: register the socket to the poller
                     Client case: resume data processing

CLOSING            - not suspended, a close will be sent


    resume           data to be        resume
    no action        processed         no action
 |---------------| |---------------| |----------|
 |               v |               v v          |
 |  |----------WAITING«--------PROCESSING----|  |
 |  |             ^   processing             |  |
 |  |             |   finished               |  |
 |  |             |                          |  |
 | suspend        |                     suspend |
 |  |             |                          |  |
 |  |          resume                        |  |
 |  |    register socket to poller (server)  |  |
 |  |    resume data processing (client)     |  |
 |  |             |                          |  |
 |  v             |                          v  |
SUSPENDING_WAIT   |                  SUSPENDING_PROCESS
 |                |                             |
 | data available |        processing finished  |
 |-------------»SUSPENDED«----------------------|

Field Summary

Modifier and TypeField and Description
public static final WsFrameBase.ReadState
private final boolean
public static final WsFrameBase.ReadState
public static final WsFrameBase.ReadState
public static final WsFrameBase.ReadState
public static final WsFrameBase.ReadState
public static final WsFrameBase.ReadState

Constructor Summary

AccessConstructor and Description
private
ReadState(boolean isSuspended)

Method Summary

Modifier and TypeMethod and Description
public boolean
public static WsFrameBase.ReadState
public static WsFrameBase.ReadState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CLOSINGback to summary
public static final WsFrameBase.ReadState CLOSING
isSuspendedback to summary
private final boolean isSuspended
PROCESSINGback to summary
public static final WsFrameBase.ReadState PROCESSING
SUSPENDEDback to summary
public static final WsFrameBase.ReadState SUSPENDED
SUSPENDING_PROCESSback to summary
public static final WsFrameBase.ReadState SUSPENDING_PROCESS
SUSPENDING_WAITback to summary
public static final WsFrameBase.ReadState SUSPENDING_WAIT
WAITINGback to summary
public static final WsFrameBase.ReadState WAITING

Constructor Detail

ReadStateback to summary
private ReadState(boolean isSuspended)

Method Detail

isSuspendedback to summary
public boolean isSuspended()
valueOfback to summary
public static WsFrameBase.ReadState valueOf(String name)
valuesback to summary
public static WsFrameBase.ReadState[] values()
org.apache.tomcat.websocket back to summary

private final Enum WsFrameBase.State

extends Enum<WsFrameBase.State>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final WsFrameBase.State
public static final WsFrameBase.State
public static final WsFrameBase.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static WsFrameBase.State
public static WsFrameBase.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DATAback to summary
public static final WsFrameBase.State DATA
NEW_FRAMEback to summary
public static final WsFrameBase.State NEW_FRAME
PARTIAL_HEADERback to summary
public static final WsFrameBase.State PARTIAL_HEADER

Constructor Detail

Stateback to summary
private State()

Method Detail

valueOfback to summary
public static WsFrameBase.State valueOf(String name)
valuesback to summary
public static WsFrameBase.State[] values()
org.apache.tomcat.websocket back to summary

private abstract Class WsFrameBase.TerminalTransformation

extends Object
implements Transformation
Class Inheritance
  • java.lang.Object
  • org.apache.tomcat.websocket.WsFrameBase.TerminalTransformation
All Implemented Interfaces
org.apache.tomcat.websocket.Transformation
Known Direct Subclasses
org.apache.tomcat.websocket.WsFrameBase.NoopTransformation, org.apache.tomcat.websocket.WsFrameBase.UnmaskTransformation

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements org.apache.tomcat.websocket.Transformation.close.

Clean-up any resources that were used by the transformation.

public Extension
getExtensionResponse()

Implements org.apache.tomcat.websocket.Transformation.getExtensionResponse.

Obtain the extension that describes the information to be returned to the client.

public void
setNext(Transformation
The next transformation
t
)

Implements org.apache.tomcat.websocket.Transformation.setNext.

Sets the next transformation in the pipeline.

public boolean
validateRsv(int
The RSV bits received as an int in the range zero to seven with RSV1 as the MSB and RSV3 as the LSB
rsv
,
byte
The opCode received
opCode
)

Implements org.apache.tomcat.websocket.Transformation.validateRsv.

Validates the RSV and opcode combination (assumed to have been extracted from a WebSocket Frame) for this extension.

public boolean
validateRsvBits(int
The RSV bits marked as in use so far as an int in the range zero to seven with RSV1 as the MSB and RSV3 as the LSB
i
)

Implements org.apache.tomcat.websocket.Transformation.validateRsvBits.

Validate that the RSV bit(s) required by this transformation are not being used by another extension.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

TerminalTransformationback to summary
private TerminalTransformation()

Method Detail

closeback to summary
public void close()

Implements org.apache.tomcat.websocket.Transformation.close.

Doc from org.apache.tomcat.websocket.Transformation.close.

Clean-up any resources that were used by the transformation.

Annotations
@Override
getExtensionResponseback to summary
public Extension getExtensionResponse()

Implements org.apache.tomcat.websocket.Transformation.getExtensionResponse.

Doc from org.apache.tomcat.websocket.Transformation.getExtensionResponse.

Obtain the extension that describes the information to be returned to the client.

Returns:Extension

The extension information that describes the parameters that have been agreed for this transformation

Annotations
@Override
setNextback to summary
public void setNext(Transformation t)

Implements org.apache.tomcat.websocket.Transformation.setNext.

Doc from org.apache.tomcat.websocket.Transformation.setNext.

Sets the next transformation in the pipeline.

Parameters
t:Transformation

The next transformation

Annotations
@Override
validateRsvback to summary
public boolean validateRsv(int rsv, byte opCode)

Implements org.apache.tomcat.websocket.Transformation.validateRsv.

Doc from org.apache.tomcat.websocket.Transformation.validateRsv.

Validates the RSV and opcode combination (assumed to have been extracted from a WebSocket Frame) for this extension. The implementation is expected to unset any RSV bits it has validated before passing the remaining RSV bits to the next transformation in the pipeline.

Anything other than a value of zero for rsv is invalid.

Parameters
rsv:int

The RSV bits received as an int in the range zero to seven with RSV1 as the MSB and RSV3 as the LSB

opCode:byte

The opCode received

Returns:boolean

true if the RSV is valid otherwise false

Annotations
@Override
validateRsvBitsback to summary
public boolean validateRsvBits(int i)

Implements org.apache.tomcat.websocket.Transformation.validateRsvBits.

Doc from org.apache.tomcat.websocket.Transformation.validateRsvBits.

Validate that the RSV bit(s) required by this transformation are not being used by another extension. The implementation is expected to set any bits it requires before passing the set of in-use bits to the next transformation.

Parameters
i:int

The RSV bits marked as in use so far as an int in the range zero to seven with RSV1 as the MSB and RSV3 as the LSB

Returns:boolean

true if the combination of RSV bits used by the transformations in the pipeline do not conflict otherwise false

Annotations
@Override
org.apache.tomcat.websocket back to summary

private final Class WsFrameBase.UnmaskTransformation

extends TerminalTransformation
Class Inheritance

For use by the server implementation that needs to obtain payload data and unmask it before any further processing.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public TransformationResult
getMoreData(byte
The opcode for the frame currently being processed
opCode
,
boolean
Is this the final frame in this WebSocket message?
fin
,
int
The reserved bits for the frame currently being processed
rsv
,
ByteBuffer
The buffer in which the data is to be written
dest
)

Implements org.apache.tomcat.websocket.Transformation.getMoreData.

Obtain more input data.

public List<MessagePart>
sendMessagePart(List<MessagePart>
The list of messages to be transformed
messageParts
)

Implements org.apache.tomcat.websocket.Transformation.sendMessagePart.

Takes the provided list of messages, transforms them, passes the transformed list on to the next transformation (if any) and then returns the resulting list of message parts after all of the transformations have been applied.

Inherited from org.apache.tomcat.websocket.WsFrameBase.TerminalTransformation:
closegetExtensionResponsesetNextvalidateRsvvalidateRsvBits

Constructor Detail

UnmaskTransformationback to summary
private UnmaskTransformation()

Method Detail

getMoreDataback to summary
public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest)

Implements org.apache.tomcat.websocket.Transformation.getMoreData.

Doc from org.apache.tomcat.websocket.Transformation.getMoreData.

Obtain more input data.

Parameters
opCode:byte

The opcode for the frame currently being processed

fin:boolean

Is this the final frame in this WebSocket message?

rsv:int

The reserved bits for the frame currently being processed

dest:ByteBuffer

The buffer in which the data is to be written

Returns:TransformationResult

The result of trying to read more data from the transform

Annotations
@Override
sendMessagePartback to summary
public List<MessagePart> sendMessagePart(List<MessagePart> messageParts)

Implements org.apache.tomcat.websocket.Transformation.sendMessagePart.

Doc from org.apache.tomcat.websocket.Transformation.sendMessagePart.

Takes the provided list of messages, transforms them, passes the transformed list on to the next transformation (if any) and then returns the resulting list of message parts after all of the transformations have been applied.

Parameters
messageParts:List<MessagePart>

The list of messages to be transformed

Returns:List<MessagePart>

The list of messages after this any any subsequent transformations have been applied. The size of the returned list may be bigger or smaller than the size of the input list

Annotations
@Override