Class AbstractWebsocketSession

java.lang.Object
org.faceless.publisher.web.AbstractWebsocketSession

public abstract class AbstractWebsocketSession extends Object
An abstraction of a Websocket sesssion. Will be passed into a WebsocketController.
  • Field Details

    • JSON

      public static final MediaType JSON
      The Media Type for JSON
    • CBOR

      public static final MediaType CBOR
      The Media Type for CBOR
  • Constructor Details

    • AbstractWebsocketSession

      public AbstractWebsocketSession()
  • Method Details

    • getRemoteAddress

      public abstract InetAddress getRemoteAddress()
      Return the remote address for the connection if known, or null if it cannot be retrieved
      Returns:
      the remote address
    • isOpen

      public abstract boolean isOpen()
      Return true if the connection is currently open
      Returns:
      the connection open state
    • isBinary

      public boolean isBinary()
      Return true if the last message received was in CBOR rather than JSON
      Returns:
      the binary flag
    • getMessageId

      public Object getMessageId()
      Return the message_id field from the last message received, or null if not set.
      Returns:
      the message_id
    • getSendWriter

      public abstract Writer getSendWriter() throws IOException
      Return a Writer to send text content to the connection
      Returns:
      a writer
      Throws:
      IOException - if the writer cannot be created
    • getSendStream

      public abstract OutputStream getSendStream() throws IOException
      Return an OutputStream to send binary content to the connection
      Returns:
      a stream
      Throws:
      IOException - if the stream cannot be created
    • getServiceEngine

      public abstract ServiceEngine getServiceEngine()
      Return the ServiceEngine referenced by this connection
      Returns:
      the ServiceEngine
    • putUserProperty

      public abstract void putUserProperty(String key, Object property)
      Store a user-defined property against this Web Socket connection. Multiple threads may be processing multiple messages from a single logical session at once, so implementations must be synchronized and callers must be prepared for this.
      Parameters:
      key - the key
      property - the value to store or, if null the key will be deleted
    • getUserProperty

      public abstract Object getUserProperty(String key)
      Retrieve a user-defined property previously set with putUserProperty() Multiple threads may be processing multiple messages from a single logical session at once, so implementations must be synchronized and callers must be prepared for this.
      Parameters:
      key - the key
      Returns:
      the value or null if not set
    • log

      public abstract void log(String s, Throwable e)
      Log a message
      Parameters:
      s - the message - may be null
      e - the Throwable - may be null
    • getAuthorization

      public abstract Authorization getAuthorization()
      Return the Authorization for this Web Socket connection. The returned value will never be null - at worst it will be a default Authorization
      Returns:
      the Authorization
    • setAuthorization

      public void setAuthorization(Authorization auth)
      Set the Authorization for this Web Socket connection.
      Parameters:
      auth - the Authoization
    • receive

      public void receive(Reader reader)
      Receive a text message from the Reader, parse it as JSON and call the WebsocketController.receive() method with the result.
      Parameters:
      reader - the Reader to read from
    • receive

      public void receive(InputStream in)
      Receive a binary message from the InputStream, parse it as CBOR and call the WebsocketController.receive() method with the result.
      Parameters:
      in - the InputStream to read from
    • send

      public void send(Json json)
      Send a message as text or binary (depending in the value if isBinary()) to the object return from getSendStream() or getSendWriter() as appropriate. If not already set, the reply_to field will be set to the value from getMessageId()
      Parameters:
      json - the data to send.
    • open

      public void open()
      Called when the Web Socket connection is opened
    • close

      public void close(int code, String reason)
      Called when the Web Socket connection is closed
      Parameters:
      code - the code for closure if one is known, or 0 otherwise
      reason - the reason for closure if one is known, or null otherwise
    • systemError

      public void systemError(Throwable err)
      Called to log a "system-error"
      Parameters:
      err - the error
    • reply

      public Json reply(String type, boolean ok, String msg, Throwable e)
      A convenience method to create a "message" type reply
      Parameters:
      type - the type of message to create, or null to use "message" or "error" depending on the presence of the throwable
      ok - the ok value
      msg - the message, or null to use default text
      e - if the message is an error message, the exception that caused it
      Returns:
      a Json representation of this data
    • reply

      public Json reply(String type, boolean ok, String msg)
      A convenience method to create a "message" type reply. Calls reply(type, ok, msg, null)
      Parameters:
      type - the type of message to create, or null to use "message"
      ok - the ok value
      msg - the message, or null to use default text
      Returns:
      a Json representation of this data