Class Draft_6455

java.lang.Object
org.java_websocket.drafts.Draft
org.java_websocket.drafts.Draft_6455

public class Draft_6455 extends Draft
Implementation for the RFC 6455 websocket protocol This is the recommended class for your websocket connection
  • Field Details

    • SEC_WEB_SOCKET_KEY

      private static final String SEC_WEB_SOCKET_KEY
      Handshake specific field for the key
      See Also:
    • SEC_WEB_SOCKET_PROTOCOL

      private static final String SEC_WEB_SOCKET_PROTOCOL
      Handshake specific field for the protocol
      See Also:
    • SEC_WEB_SOCKET_EXTENSIONS

      private static final String SEC_WEB_SOCKET_EXTENSIONS
      Handshake specific field for the extension
      See Also:
    • SEC_WEB_SOCKET_ACCEPT

      private static final String SEC_WEB_SOCKET_ACCEPT
      Handshake specific field for the accept
      See Also:
    • UPGRADE

      private static final String UPGRADE
      Handshake specific field for the upgrade
      See Also:
    • CONNECTION

      private static final String CONNECTION
      Handshake specific field for the connection
      See Also:
    • log

      private final org.slf4j.Logger log
      Logger instance
      Since:
      1.4.0
    • negotiatedExtension

      private IExtension negotiatedExtension
      Attribute for the used extension in this draft
    • defaultExtension

      private IExtension defaultExtension
      Attribute for the default extension
    • knownExtensions

      private List<IExtension> knownExtensions
      Attribute for all available extension in this draft
    • currentDecodingExtension

      private IExtension currentDecodingExtension
      Current active extension used to decode messages
    • protocol

      private IProtocol protocol
      Attribute for the used protocol in this draft
    • knownProtocols

      private List<IProtocol> knownProtocols
      Attribute for all available protocols in this draft
    • currentContinuousFrame

      private Framedata currentContinuousFrame
      Attribute for the current continuous frame
    • byteBufferList

      private final List<ByteBuffer> byteBufferList
      Attribute for the payload of the current continuous frame
    • incompleteframe

      private ByteBuffer incompleteframe
      Attribute for the current incomplete frame
    • reuseableRandom

      private final SecureRandom reuseableRandom
      Attribute for the reusable random instance
    • maxFrameSize

      private int maxFrameSize
      Attribute for the maximum allowed size of a frame
      Since:
      1.4.0
  • Constructor Details

    • Draft_6455

      public Draft_6455()
      Constructor for the websocket protocol specified by RFC 6455 with default extensions
      Since:
      1.3.5
    • Draft_6455

      public Draft_6455(IExtension inputExtension)
      Constructor for the websocket protocol specified by RFC 6455 with custom extensions
      Parameters:
      inputExtension - the extension which should be used for this draft
      Since:
      1.3.5
    • Draft_6455

      public Draft_6455(List<IExtension> inputExtensions)
      Constructor for the websocket protocol specified by RFC 6455 with custom extensions
      Parameters:
      inputExtensions - the extensions which should be used for this draft
      Since:
      1.3.5
    • Draft_6455

      public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtocols)
      Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
      Parameters:
      inputExtensions - the extensions which should be used for this draft
      inputProtocols - the protocols which should be used for this draft
      Since:
      1.3.7
    • Draft_6455

      public Draft_6455(List<IExtension> inputExtensions, int inputMaxFrameSize)
      Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
      Parameters:
      inputExtensions - the extensions which should be used for this draft
      inputMaxFrameSize - the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)
      Since:
      1.4.0
    • Draft_6455

      public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtocols, int inputMaxFrameSize)
      Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
      Parameters:
      inputExtensions - the extensions which should be used for this draft
      inputProtocols - the protocols which should be used for this draft
      inputMaxFrameSize - the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)
      Since:
      1.4.0
  • Method Details

    • acceptHandshakeAsServer

      public HandshakeState acceptHandshakeAsServer(ClientHandshake handshakedata) throws InvalidHandshakeException
      Specified by:
      acceptHandshakeAsServer in class Draft
      Throws:
      InvalidHandshakeException
    • containsRequestedProtocol

      private HandshakeState containsRequestedProtocol(String requestedProtocol)
      Check if the requested protocol is part of this draft
      Parameters:
      requestedProtocol - the requested protocol
      Returns:
      MATCHED if it is matched, otherwise NOT_MATCHED
    • acceptHandshakeAsClient

      public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHandshake response) throws InvalidHandshakeException
      Specified by:
      acceptHandshakeAsClient in class Draft
      Throws:
      InvalidHandshakeException
    • getExtension

      public IExtension getExtension()
      Getter for the extension which is used by this draft
      Returns:
      the extension which is used or null, if handshake is not yet done
    • getKnownExtensions

      public List<IExtension> getKnownExtensions()
      Getter for all available extensions for this draft
      Returns:
      the extensions which are enabled for this draft
    • getProtocol

      public IProtocol getProtocol()
      Getter for the protocol which is used by this draft
      Returns:
      the protocol which is used or null, if handshake is not yet done or no valid protocols
      Since:
      1.3.7
    • getMaxFrameSize

      public int getMaxFrameSize()
      Getter for the maximum allowed payload size which is used by this draft
      Returns:
      the size, which is allowed for the payload
      Since:
      1.4.0
    • getKnownProtocols

      public List<IProtocol> getKnownProtocols()
      Getter for all available protocols for this draft
      Returns:
      the protocols which are enabled for this draft
      Since:
      1.3.7
    • postProcessHandshakeRequestAsClient

      public ClientHandshakeBuilder postProcessHandshakeRequestAsClient(ClientHandshakeBuilder request)
      Specified by:
      postProcessHandshakeRequestAsClient in class Draft
    • postProcessHandshakeResponseAsServer

      public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake request, ServerHandshakeBuilder response) throws InvalidHandshakeException
      Specified by:
      postProcessHandshakeResponseAsServer in class Draft
      Throws:
      InvalidHandshakeException
    • copyInstance

      public Draft copyInstance()
      Description copied from class: Draft
      Drafts must only be by one websocket at all. To prevent drafts to be used more than once the Websocket implementation should call this method in order to create a new usable version of a given draft instance.
      The copy can be safely used in conjunction with a new websocket connection.
      Specified by:
      copyInstance in class Draft
      Returns:
      a copy of the draft
    • createBinaryFrame

      public ByteBuffer createBinaryFrame(Framedata framedata)
      Specified by:
      createBinaryFrame in class Draft
    • createByteBufferFromFramedata

      private ByteBuffer createByteBufferFromFramedata(Framedata framedata)
    • translateSingleFrame

      private Framedata translateSingleFrame(ByteBuffer buffer) throws IncompleteException, InvalidDataException
      Throws:
      IncompleteException
      InvalidDataException
    • translateSingleFramePayloadLength

      private Draft_6455.TranslatedPayloadMetaData translateSingleFramePayloadLength(ByteBuffer buffer, Opcode optcode, int oldPayloadlength, int maxpacketsize, int oldRealpacketsize) throws InvalidFrameException, IncompleteException, LimitExceededException
      Translate the buffer depending when it has an extended payload length (126 or 127)
      Parameters:
      buffer - the buffer to read from
      optcode - the decoded optcode
      oldPayloadlength - the old payload length
      maxpacketsize - the max packet size allowed
      oldRealpacketsize - the real packet size
      Returns:
      the new payload data containing new payload length and new packet size
      Throws:
      InvalidFrameException - thrown if a control frame has an invalid length
      IncompleteException - if the maxpacketsize is smaller than the realpackagesize
      LimitExceededException - if the payload length is to big
    • translateSingleFrameCheckLengthLimit

      private void translateSingleFrameCheckLengthLimit(long length) throws LimitExceededException
      Check if the frame size exceeds the allowed limit
      Parameters:
      length - the current payload length
      Throws:
      LimitExceededException - if the payload length is to big
    • translateSingleFrameCheckPacketSize

      private void translateSingleFrameCheckPacketSize(int maxpacketsize, int realpacketsize) throws IncompleteException
      Check if the max packet size is smaller than the real packet size
      Parameters:
      maxpacketsize - the max packet size
      realpacketsize - the real packet size
      Throws:
      IncompleteException - if the maxpacketsize is smaller than the realpackagesize
    • getRSVByte

      private byte getRSVByte(int rsv)
      Get a byte that can set RSV bits when OR(|)'d. 0 1 2 3 4 5 6 7 +-+-+-+-+-------+ |F|R|R|R| opcode| |I|S|S|S| (4) | |N|V|V|V| | | |1|2|3| |
      Parameters:
      rsv - Can only be {0, 1, 2, 3}
      Returns:
      byte that represents which RSV bit is set.
    • getMaskByte

      private byte getMaskByte(boolean mask)
      Get the mask byte if existing
      Parameters:
      mask - is mask active or not
      Returns:
      -128 for true, 0 for false
    • getSizeBytes

      private int getSizeBytes(ByteBuffer mes)
      Get the size bytes for the byte buffer
      Parameters:
      mes - the current buffer
      Returns:
      the size bytes
    • translateFrame

      public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException
      Specified by:
      translateFrame in class Draft
      Throws:
      InvalidDataException
    • createFrames

      public List<Framedata> createFrames(ByteBuffer binary, boolean mask)
      Specified by:
      createFrames in class Draft
    • createFrames

      public List<Framedata> createFrames(String text, boolean mask)
      Specified by:
      createFrames in class Draft
    • reset

      public void reset()
      Specified by:
      reset in class Draft
    • getServerTime

      private String getServerTime()
      Generate a date for for the date-header
      Returns:
      the server time
    • generateFinalKey

      private String generateFinalKey(String in)
      Generate a final key from a input string
      Parameters:
      in - the input string
      Returns:
      a final key
    • toByteArray

      private byte[] toByteArray(long val, int bytecount)
    • fromOpcode

      private byte fromOpcode(Opcode opcode)
    • toOpcode

      private Opcode toOpcode(byte opcode) throws InvalidFrameException
      Throws:
      InvalidFrameException
    • processFrame

      public void processFrame(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
      Description copied from class: Draft
      Handle the frame specific to the draft
      Specified by:
      processFrame in class Draft
      Parameters:
      webSocketImpl - the websocketimpl used for this draft
      frame - the frame which is supposed to be handled
      Throws:
      InvalidDataException - will be thrown on invalid data
    • processFrameContinuousAndNonFin

      private void processFrameContinuousAndNonFin(WebSocketImpl webSocketImpl, Framedata frame, Opcode curop) throws InvalidDataException
      Process the frame if it is a continuous frame or the fin bit is not set
      Parameters:
      webSocketImpl - the websocket implementation to use
      frame - the current frame
      curop - the current Opcode
      Throws:
      InvalidDataException - if there is a protocol error
    • processFrameBinary

      private void processFrameBinary(WebSocketImpl webSocketImpl, Framedata frame)
      Process the frame if it is a binary frame
      Parameters:
      webSocketImpl - the websocket impl
      frame - the frame
    • logRuntimeException

      private void logRuntimeException(WebSocketImpl webSocketImpl, RuntimeException e)
      Log the runtime exception to the specific WebSocketImpl
      Parameters:
      webSocketImpl - the implementation of the websocket
      e - the runtime exception
    • processFrameText

      private void processFrameText(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
      Process the frame if it is a text frame
      Parameters:
      webSocketImpl - the websocket impl
      frame - the frame
      Throws:
      InvalidDataException
    • processFrameIsFin

      private void processFrameIsFin(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
      Process the frame if it is the last frame
      Parameters:
      webSocketImpl - the websocket impl
      frame - the frame
      Throws:
      InvalidDataException - if there is a protocol error
    • processFrameIsNotFin

      private void processFrameIsNotFin(Framedata frame) throws InvalidDataException
      Process the frame if it is not the last frame
      Parameters:
      frame - the frame
      Throws:
      InvalidDataException - if there is a protocol error
    • processFrameClosing

      private void processFrameClosing(WebSocketImpl webSocketImpl, Framedata frame)
      Process the frame if it is a closing frame
      Parameters:
      webSocketImpl - the websocket impl
      frame - the frame
    • clearBufferList

      private void clearBufferList()
      Clear the current bytebuffer list
    • addToBufferList

      private void addToBufferList(ByteBuffer payloadData)
      Add a payload to the current bytebuffer list
      Parameters:
      payloadData - the new payload
    • checkBufferLimit

      private void checkBufferLimit() throws LimitExceededException
      Check the current size of the buffer and throw an exception if the size is bigger than the max allowed frame size
      Throws:
      LimitExceededException - if the current size is bigger than the allowed size
    • getCloseHandshakeType

      public CloseHandshakeType getCloseHandshakeType()
      Specified by:
      getCloseHandshakeType in class Draft
    • toString

      public String toString()
      Overrides:
      toString in class Draft
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getPayloadFromByteBufferList

      private ByteBuffer getPayloadFromByteBufferList() throws LimitExceededException
      Method to generate a full bytebuffer out of all the fragmented frame payload
      Returns:
      a bytebuffer containing all the data
      Throws:
      LimitExceededException - will be thrown when the totalSize is bigger then Integer.MAX_VALUE due to not being able to allocate more
    • getByteBufferListSize

      private long getByteBufferListSize()
      Get the current size of the resulting bytebuffer in the bytebuffer list
      Returns:
      the size as long (to not get an integer overflow)