Top Description Inners Fields Constructors Methods
org.glassfish.tyrus.core

public final Class ProtocolHandler

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.ByteBuffer, java.security.SecureRandom, java.util.Arrays, .List, java.util.concurrent.Future, .TimeUnit, java.util.concurrent.atomic.AtomicInteger, java.util.concurrent.locks.Condition, .Lock, .ReentrantLock, java.util.logging.Level, .Logger, jakarta.websocket.CloseReason, .Extension, .SendHandler, .SendResult, jakarta.websocket.server.HandshakeRequest, org.glassfish.tyrus.core.extension.ExtendedExtension, org.glassfish.tyrus.core.frame.BinaryFrame, .CloseFrame, .Frame, .TextFrame, .TyrusFrame, org.glassfish.tyrus.core.l10n.LocalizationMessages, org.glassfish.tyrus.core.monitoring.MessageEventListener, org.glassfish.tyrus.spi.CompletionHandler, .UpgradeRequest, .UpgradeResponse, .Writer, .WriterInfo

Tyrus protocol handler.

Responsible for framing and unframing raw websocket frames. Tyrus creates exactly one instance per Session.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static enum

Field Summary

Modifier and TypeField and Description
private final boolean
private static final WriterInfo
private volatile ExtendedExtension.ExtensionContext
private volatile List<Extension>
private volatile boolean
private final Condition
idleCondition

If partial message is being send and we want to send partial message with different type or other whole message, we need to wait until "idleCondition" is signalled.

private volatile byte
private final Lock
lock

Synchronizes all public send* (including stream variants) methods.

private static final Logger
public static final int
MASK_SIZE

RFC 6455

private final MaskingKeyGenerator
private volatile MessageEventListener
private static final WriterInfo
private volatile byte
private final ProtocolHandler.ParsingState
private volatile boolean
private volatile ByteBuffer
private static final int
private volatile ProtocolHandler.SendingFragmentState
private volatile String
private volatile TyrusWebSocket
private volatile Writer

Constructor Summary

AccessConstructor and Description
pack-priv
ProtocolHandler(boolean
true when this instance is on client side, false when on server side.
client
,
MaskingKeyGenerator
random number generator that will be used for generating masking keys. Masking keys are required only on the client side and maskingKeyGenerator should be null on the server side. If null on the client side, java.security.SecureRandom will be used by default.
maskingKeyGenerator
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private byte
private void
checkSendingFragment()

Check whether current ProtocolHandler is sending a partial message.

public synchronized Future<Frame>
close(final int code, final String reason)

private long

Returns:

converted byte array.
decodeLength
(byte[]
byte array to be converted.
bytes
)

Convert a byte[] to a long.

pack-priv void
private byte[]

Returns:

the array
encodeLength
(final long
the payload size
length
)

Converts the length given to the appropriate framing data:

  1. 0-125 one element that is the payload length.
  2. up to 0xFFFF, 3 element array starting with 126 with the following 2 bytes interpreted as a 16 bit unsigned integer showing the payload length.
  3. else 9 element array starting with 127 with the following 8 bytes interpreted as a 64-bit unsigned integer (the high bit must be 0) showing the payload length.
pack-priv ByteBuffer
frame(Frame frame)

pack-priv List<Extension>
pack-priv String
public Handshake

Returns:

server handshake object.
handshake
(TyrusEndpointWrapper
endpoint related to the handshake (path is already matched).
endpointWrapper
,
UpgradeRequest
handshake request.
request
,
UpgradeResponse
handshake response.
response
,
ExtendedExtension.ExtensionContext
extension context.
extensionContext
)

Server side handshake processing.

public boolean

Returns:

true if there is at least one negotiated extension associated to this connection, false otherwise.
hasExtensions
()

Returns true when current connection has some negotiated extension.

private boolean
isBitSet(final byte b, int bit)

private boolean
isControlFrame(byte opcode)

public void
process(Frame
TODO.
frame
,
TyrusWebSocket
TODO.
socket
)

TODO.

pack-priv final Future<Frame>
send(TyrusFrame frame, WriterInfo writerInfo)

Not message frames - ping/pong/...

private Future<Frame>
send(TyrusFrame frame, CompletionHandler<Frame> completionHandler, WriterInfo writerInfo, Boolean useTimeout)

private Future<Frame>
send(ByteBuffer frame, CompletionHandler<Frame> completionHandler, WriterInfo writerInfo, Boolean useTimeout)

public Future<Frame>
send(byte[] data)
Deprecated

public Future<Frame>
send(byte[] data, WriterInfo writerInfo)

public void
send(final byte[] data, final SendHandler handler)
Deprecated

public void
send(final byte[] data, final SendHandler handler, WriterInfo writerInfo)

public Future<Frame>
send(String data)
Deprecated

public Future<Frame>
send(String data, WriterInfo writerInfo)

public void
send(final String data, final SendHandler handler)
Deprecated

public void
send(final String data, final SendHandler handler, WriterInfo writerInfo)

public Future<Frame>

Returns:

send future.
sendRawFrame
(ByteBuffer
serialized frame.
data
)

Raw frame is always whole (not partial).

public void
setExtensionContext(ExtendedExtension.ExtensionContext
extension context.
extensionContext
)

Client side.

public void
setExtensions(List<Extension>
list of negotiated extensions. Can be null.
extensions
)

Client side.

public void
setMessageEventListener(MessageEventListener
message event listener.
messageEventListener
)

Set message event listener.

public void
setWebSocket(TyrusWebSocket
client WebSocket connection.
webSocket
)

Client side.

public void
setWriter(Writer
Writer to be set.
writer
)

Set Writer instance.

public Future<Frame>
stream(boolean last, byte[] bytes, int off, int len)
Deprecated

public Future<Frame>
stream(boolean last, byte[] bytes, int off, int len, WriterInfo writerInfo)

public Future<Frame>
stream(boolean last, String fragment)
Deprecated

public Future<Frame>
stream(boolean last, String fragment, WriterInfo writerInfo)

public Frame

Returns:

TODO.
unframe
(ByteBuffer
TODO.
buffer
)

TODO!

private void
validate(final byte fragmentType, byte opcode)

private Future<Frame>
write(final TyrusFrame frame, final CompletionHandler<Frame> completionHandler, WriterInfo data, boolean useTimeout)

private Future<Frame>
write(final ByteBuffer frame, final CompletionHandler<Frame> completionHandler, WriterInfo data, boolean useTimeout)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

clientback to summary
private final boolean client
CLOSEback to summary
private static final WriterInfo CLOSE
extensionContextback to summary
private volatile ExtendedExtension.ExtensionContext extensionContext
extensionsback to summary
private volatile List<Extension> extensions
hasExtensionsback to summary
private volatile boolean hasExtensions
idleConditionback to summary
private final Condition idleCondition

If partial message is being send and we want to send partial message with different type or other whole message, we need to wait until "idleCondition" is signalled.

inFragmentedTypeback to summary
private volatile byte inFragmentedType
lockback to summary
private final Lock lock

Synchronizes all public send* (including stream variants) methods.

The reason for this lock is that we need to have consistent value in {#sendingFragment} field to be able to determine the sending state of this particular instance/session.

LOGGERback to summary
private static final Logger LOGGER
MASK_SIZEback to summary
public static final int MASK_SIZE

RFC 6455

maskingKeyGeneratorback to summary
private final MaskingKeyGenerator maskingKeyGenerator
messageEventListenerback to summary
private volatile MessageEventListener messageEventListener
NULL_INFOback to summary
private static final WriterInfo NULL_INFO
outFragmentedTypeback to summary
private volatile byte outFragmentedType
parsingStateback to summary
private final ProtocolHandler.ParsingState parsingState
processingFragmentback to summary
private volatile boolean processingFragment
remainderback to summary
private volatile ByteBuffer remainder
SEND_TIMEOUTback to summary
private static final int SEND_TIMEOUT
sendingFragmentback to summary
private volatile ProtocolHandler.SendingFragmentState sendingFragment
subProtocolback to summary
private volatile String subProtocol
webSocketback to summary
private volatile TyrusWebSocket webSocket
writerback to summary
private volatile Writer writer

Constructor Detail

ProtocolHandlerback to summary
pack-priv ProtocolHandler(boolean client, MaskingKeyGenerator maskingKeyGenerator)

Constructor.

Parameters
client:boolean

true when this instance is on client side, false when on server side.

maskingKeyGenerator:MaskingKeyGenerator

random number generator that will be used for generating masking keys. Masking keys are required only on the client side and maskingKeyGenerator should be null on the server side. If null on the client side, java.security.SecureRandom will be used by default.

Method Detail

checkForLastFrameback to summary
private byte checkForLastFrame(Frame frame)
checkSendingFragmentback to summary
private void checkSendingFragment()

Check whether current ProtocolHandler is sending a partial message.

If yes, wait for 3000 and if the message still cannot be sent, throw IllegalStateException.

closeback to summary
public synchronized Future<Frame> close(final int code, final String reason)
decodeLengthback to summary
private long decodeLength(byte[] bytes)

Convert a byte[] to a long. Used for rebuilding payload length.

Parameters
bytes:byte[]

byte array to be converted.

Returns:long

converted byte array.

doCloseback to summary
pack-priv void doClose()
encodeLengthback to summary
private byte[] encodeLength(final long length)

Converts the length given to the appropriate framing data:

  1. 0-125 one element that is the payload length.
  2. up to 0xFFFF, 3 element array starting with 126 with the following 2 bytes interpreted as a 16 bit unsigned integer showing the payload length.
  3. else 9 element array starting with 127 with the following 8 bytes interpreted as a 64-bit unsigned integer (the high bit must be 0) showing the payload length.
Parameters
length:long

the payload size

Returns:byte[]

the array

frameback to summary
pack-priv ByteBuffer frame(Frame frame)
getExtensionsback to summary
pack-priv List<Extension> getExtensions()
getSubProtocolback to summary
pack-priv String getSubProtocol()
handshakeback to summary
public Handshake handshake(TyrusEndpointWrapper endpointWrapper, UpgradeRequest request, UpgradeResponse response, ExtendedExtension.ExtensionContext extensionContext) throws HandshakeException

Server side handshake processing.

Parameters
endpointWrapper:TyrusEndpointWrapper

endpoint related to the handshake (path is already matched).

request:UpgradeRequest

handshake request.

response:UpgradeResponse

handshake response.

extensionContext:ExtendedExtension.ExtensionContext

extension context.

Returns:Handshake

server handshake object.

Exceptions
HandshakeException:
when there is problem with received UpgradeRequest.
hasExtensionsback to summary
public boolean hasExtensions()

Returns true when current connection has some negotiated extension.

Returns:boolean

true if there is at least one negotiated extension associated to this connection, false otherwise.

isBitSetback to summary
private boolean isBitSet(final byte b, int bit)
isControlFrameback to summary
private boolean isControlFrame(byte opcode)
processback to summary
public void process(Frame frame, TyrusWebSocket socket)

TODO.

called after Extension execution.

validates frame + processes its content

Parameters
frame:Frame

TODO.

socket:TyrusWebSocket

TODO.

sendback to summary
pack-priv final Future<Frame> send(TyrusFrame frame, WriterInfo writerInfo)

Not message frames - ping/pong/...

sendback to summary
private Future<Frame> send(TyrusFrame frame, CompletionHandler<Frame> completionHandler, WriterInfo writerInfo, Boolean useTimeout)
sendback to summary
private Future<Frame> send(ByteBuffer frame, CompletionHandler<Frame> completionHandler, WriterInfo writerInfo, Boolean useTimeout)
sendback to summary
public Future<Frame> send(byte[] data)

Deprecated

Annotations
@Deprecated
sendback to summary
public Future<Frame> send(byte[] data, WriterInfo writerInfo)
sendback to summary
public void send(final byte[] data, final SendHandler handler)

Deprecated

Annotations
@Deprecated
sendback to summary
public void send(final byte[] data, final SendHandler handler, WriterInfo writerInfo)
sendback to summary
public Future<Frame> send(String data)

Deprecated

Annotations
@Deprecated
sendback to summary
public Future<Frame> send(String data, WriterInfo writerInfo)
sendback to summary
public void send(final String data, final SendHandler handler)

Deprecated

Annotations
@Deprecated
sendback to summary
public void send(final String data, final SendHandler handler, WriterInfo writerInfo)
sendRawFrameback to summary
public Future<Frame> sendRawFrame(ByteBuffer data)

Raw frame is always whole (not partial).

Parameters
data:ByteBuffer

serialized frame.

Returns:Future<Frame>

send future.

setExtensionContextback to summary
public void setExtensionContext(ExtendedExtension.ExtensionContext extensionContext)

Client side. Set extension context.

Parameters
extensionContext:ExtendedExtension.ExtensionContext

extension context.

setExtensionsback to summary
public void setExtensions(List<Extension> extensions)

Client side. Set extensions negotiated for this WebSocket session/connection.

Parameters
extensions:List<Extension>

list of negotiated extensions. Can be null.

setMessageEventListenerback to summary
public void setMessageEventListener(MessageEventListener messageEventListener)

Set message event listener.

Parameters
messageEventListener:MessageEventListener

message event listener.

setWebSocketback to summary
public void setWebSocket(TyrusWebSocket webSocket)

Client side. Set WebSocket.

Parameters
webSocket:TyrusWebSocket

client WebSocket connection.

setWriterback to summary
public void setWriter(Writer writer)

Set Writer instance.

The set instance is used for "sending" all outgoing WebSocket frames.

Parameters
writer:Writer

Writer to be set.

streamback to summary
public Future<Frame> stream(boolean last, byte[] bytes, int off, int len)

Deprecated

Annotations
@Deprecated
streamback to summary
public Future<Frame> stream(boolean last, byte[] bytes, int off, int len, WriterInfo writerInfo)
streamback to summary
public Future<Frame> stream(boolean last, String fragment)

Deprecated

Annotations
@Deprecated
streamback to summary
public Future<Frame> stream(boolean last, String fragment, WriterInfo writerInfo)
unframeback to summary
public Frame unframe(ByteBuffer buffer)

TODO!

Parameters
buffer:ByteBuffer

TODO.

Returns:Frame

TODO.

validateback to summary
private void validate(final byte fragmentType, byte opcode)
writeback to summary
private Future<Frame> write(final TyrusFrame frame, final CompletionHandler<Frame> completionHandler, WriterInfo data, boolean useTimeout)
writeback to summary
private Future<Frame> write(final ByteBuffer frame, final CompletionHandler<Frame> completionHandler, WriterInfo data, boolean useTimeout)
org.glassfish.tyrus.core back to summary

private Class ProtocolHandler.CompletionHandlerWrapper

extends CompletionHandler<ByteBuffer>
Class Inheritance

Handler passed to the org.glassfish.tyrus.spi.Writer.

Field Summary

Modifier and TypeField and Description
private final Frame
private final CompletionHandler<Frame>
private final TyrusFuture<Frame>

Constructor Summary

AccessConstructor and Description
private
CompletionHandlerWrapper(CompletionHandler<Frame> frameCompletionHandler, TyrusFuture<Frame> future, Frame frame)

Method Summary

Modifier and TypeMethod and Description
public void
cancelled()

Overrides org.glassfish.tyrus.spi.CompletionHandler.cancelled.

The operation was cancelled.

public void
completed(ByteBuffer
the operation result.
result
)

Overrides org.glassfish.tyrus.spi.CompletionHandler.completed.

The operation was completed.

public void
failed(Throwable
error, which occurred during operation execution.
throwable
)

Overrides org.glassfish.tyrus.spi.CompletionHandler.failed.

The operation was failed.

public void
updated(ByteBuffer
the current result.
result
)

Overrides org.glassfish.tyrus.spi.CompletionHandler.updated.

The callback method may be called, when there is some progress in operation execution, but it is still not completed.

Field Detail

frameback to summary
private final Frame frame
frameCompletionHandlerback to summary
private final CompletionHandler<Frame> frameCompletionHandler
futureback to summary
private final TyrusFuture<Frame> future

Constructor Detail

CompletionHandlerWrapperback to summary
private CompletionHandlerWrapper(CompletionHandler<Frame> frameCompletionHandler, TyrusFuture<Frame> future, Frame frame)

Method Detail

cancelledback to summary
public void cancelled()

Overrides org.glassfish.tyrus.spi.CompletionHandler.cancelled.

Doc from org.glassfish.tyrus.spi.CompletionHandler.cancelled.

The operation was cancelled.

Annotations
@Override
completedback to summary
public void completed(ByteBuffer result)

Overrides org.glassfish.tyrus.spi.CompletionHandler.completed.

Doc from org.glassfish.tyrus.spi.CompletionHandler.completed.

The operation was completed.

Parameters
result:ByteBuffer

the operation result.

Annotations
@Override
failedback to summary
public void failed(Throwable throwable)

Overrides org.glassfish.tyrus.spi.CompletionHandler.failed.

Doc from org.glassfish.tyrus.spi.CompletionHandler.failed.

The operation was failed.

Parameters
throwable:Throwable

error, which occurred during operation execution.

Annotations
@Override
updatedback to summary
public void updated(ByteBuffer result)

Overrides org.glassfish.tyrus.spi.CompletionHandler.updated.

Doc from org.glassfish.tyrus.spi.CompletionHandler.updated.

The callback method may be called, when there is some progress in operation execution, but it is still not completed.

Parameters
result:ByteBuffer

the current result.

Annotations
@Override
org.glassfish.tyrus.core back to summary

private Class ProtocolHandler.ParsingState

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv volatile boolean
pack-priv volatile boolean
pack-priv volatile long
private volatile byte
pack-priv volatile boolean
pack-priv volatile Masker
pack-priv volatile byte
pack-priv final AtomicInteger

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

controlFrameback to summary
pack-priv volatile boolean controlFrame
finalFragmentback to summary
pack-priv volatile boolean finalFragment
lengthback to summary
pack-priv volatile long length
lengthCodeback to summary
private volatile byte lengthCode
maskedback to summary
pack-priv volatile boolean masked
maskerback to summary
pack-priv volatile Masker masker
opcodeback to summary
pack-priv volatile byte opcode
stateback to summary
pack-priv final AtomicInteger state

Constructor Detail

ParsingStateback to summary
private ParsingState()

Method Detail

recycleback to summary
pack-priv void recycle()
org.glassfish.tyrus.core back to summary

private final Enum ProtocolHandler.SendingFragmentState

extends Enum<ProtocolHandler.SendingFragmentState>
Class Inheritance

Sending state.

Field Summary

Modifier and TypeField and Description
public static final ProtocolHandler.SendingFragmentState
IDLE

Session is idle - no partial message in progress.

public static final ProtocolHandler.SendingFragmentState
SENDING_BINARY

Sending partial binary message - final frame was not yet sent.

public static final ProtocolHandler.SendingFragmentState
SENDING_TEXT

Sending partial text message - final frame was not yet sent.

Constructor Summary

AccessConstructor and Description
private

Method Summary

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

Field Detail

IDLEback to summary
public static final ProtocolHandler.SendingFragmentState IDLE

Session is idle - no partial message in progress.

SENDING_BINARYback to summary
public static final ProtocolHandler.SendingFragmentState SENDING_BINARY

Sending partial binary message - final frame was not yet sent.

SENDING_TEXTback to summary
public static final ProtocolHandler.SendingFragmentState SENDING_TEXT

Sending partial text message - final frame was not yet sent.

Constructor Detail

SendingFragmentStateback to summary
private SendingFragmentState()

Method Detail

valueOfback to summary
public static ProtocolHandler.SendingFragmentState valueOf(String name)
valuesback to summary
public static ProtocolHandler.SendingFragmentState[] values()