Record Class WebSocketMessage

java.lang.Object
java.lang.Record
org.microproxy.http.WebSocketMessage
Record Components:
type - whether the message is text or binary
payload - the application data (UTF-8 for a text message)
fromClient - whether the message travels from the client towards the server

public record WebSocketMessage(WebSocketMessage.Type type, byte[] payload, boolean fromClient) extends Record
A whole WebSocket message (RFC 6455 section 5.4): the payloads of a text or binary frame and its continuation frames, joined, and inflated when the connection uses permessage-deflate (RFC 7692). Messages relayed after an upgrade are shown to HttpFilters.filterWebSocketMessage(WebSocketMessage, boolean), which may return one built with text(String, boolean), binary(byte[], boolean), withText(String) and the like to send instead.

Messages are immutable: the payload is copied in and out.

  • Constructor Details

    • WebSocketMessage

      public WebSocketMessage(WebSocketMessage.Type type, byte[] payload, boolean fromClient)
      A message, with a copy of payload.
      Parameters:
      type - whether the message is text or binary
      payload - the application data, copied
      fromClient - whether the message travels from the client towards the server
  • Method Details

    • text

      public static WebSocketMessage text(String text, boolean fromClient)
      A text message.
      Parameters:
      text - the text, encoded as UTF-8
      fromClient - whether the message travels towards the server
      Returns:
      a text message
    • binary

      public static WebSocketMessage binary(byte[] data, boolean fromClient)
      A binary message.
      Parameters:
      data - the payload, copied
      fromClient - whether the message travels towards the server
      Returns:
      a binary message
    • payload

      public byte[] payload()
      Returns a copy of the application data.
      Returns:
      the payload bytes
    • length

      public int length()
      Returns the payload length.
      Returns:
      the number of payload bytes
    • isText

      public boolean isText()
      Checks the message type.
      Returns:
      whether this is a text message
    • isBinary

      public boolean isBinary()
      Checks the message type.
      Returns:
      whether this is a binary message
    • text

      public String text()
      The payload decoded as UTF-8, with malformed input replaced (meaningful for text messages).
      Returns:
      the payload as text
    • textOrNull

      public String textOrNull()
      The payload decoded as UTF-8, or null if it is not valid UTF-8.
      Returns:
      the payload as text, or null
    • withPayload

      public WebSocketMessage withPayload(byte[] data)
      This message with another payload, keeping its type and direction.
      Parameters:
      data - the new payload, copied
      Returns:
      a new message
    • withText

      public WebSocketMessage withText(String text)
      This message with another payload, encoded as UTF-8, keeping its type and direction.
      Parameters:
      text - the new payload text
      Returns:
      a new message
    • withType

      public WebSocketMessage withType(WebSocketMessage.Type newType)
      This message as another type, keeping its payload and direction.
      Parameters:
      newType - the new type
      Returns:
      a new message
    • withFromClient

      public WebSocketMessage withFromClient(boolean fromClient)
      This message travelling the other way, or the same way: a hook that returns it with fromClient changed sends it back to where the original came from.
      Parameters:
      fromClient - whether the new message travels from the client towards the server
      Returns:
      a new message
    • equals

      public boolean equals(Object other)
      Whether other has the same type, direction and payload.
      Specified by:
      equals in class Record
      Parameters:
      other - the object to compare with
      Returns:
      whether the messages are equal
    • hashCode

      public int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • type

      public WebSocketMessage.Type type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • fromClient

      public boolean fromClient()
      Returns the value of the fromClient record component.
      Returns:
      the value of the fromClient record component