Package org.faceless.publisher.web
Class AbstractWebsocketSession
java.lang.Object
org.faceless.publisher.web.AbstractWebsocketSession
An abstraction of a Websocket sesssion.
Will be passed into a WebsocketController.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled when the Web Socket connection is closedabstract AuthorizationReturn theAuthorizationfor this Web Socket connection.Return themessage_idfield from the last message received, or null if not set.abstract StringReturn the "Origin" header from the initial HTTP exchangeabstract InetAddressReturn the remote address for the connection if known, or null if it cannot be retrievedabstract OutputStreamReturn anOutputStreamto send binary content to the connectionabstract WriterReturn aWriterto send text content to the connectionabstract ServiceEngineReturn theServiceEnginereferenced by this connectionabstract ObjectgetUserProperty(String key) Retrieve a user-defined property previously set withputUserProperty()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.booleanisBinary()Return true if the last message received was in CBOR rather than JSONabstract booleanisOpen()Return true if the connection is currently openabstract voidLog a messagevoidopen()Called when the Web Socket connection is openedabstract voidputUserProperty(String key, Object property) Store a user-defined property against this Web Socket connection.voidreceive(InputStream in) Receive a binary message from theInputStream, parse it as CBOR and call theWebsocketController.receive()method with the result.voidReceive a text message from theReader, parse it as JSON and call theWebsocketController.receive()method with the result.JsonA convenience method to create a "message" type reply.JsonA convenience method to create a "message" type replyvoidsend(Json json) Send a message as text or binary (depending in the value ifisBinary()) to the object return fromgetSendStream()orgetSendWriter()as appropriate.voidSet theAuthorizationfor this Web Socket connection.voidsystemError(Throwable err) Called to log a "system-error"
-
Field Details
-
JSON
The Media Type for JSON -
CBOR
The Media Type for CBOR
-
-
Constructor Details
-
AbstractWebsocketSession
public AbstractWebsocketSession()
-
-
Method Details
-
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
Return themessage_idfield from the last message received, or null if not set.- Returns:
- the
message_id
-
getOrigin
Return the "Origin" header from the initial HTTP exchange -
getSendWriter
Return aWriterto send text content to the connection- Returns:
- a writer
- Throws:
IOException- if the writer cannot be created
-
getSendStream
Return anOutputStreamto send binary content to the connection- Returns:
- a stream
- Throws:
IOException- if the stream cannot be created
-
getServiceEngine
Return theServiceEnginereferenced by this connection- Returns:
- the ServiceEngine
-
putUserProperty
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 keyproperty- the value to store or, ifnullthe key will be deleted
-
getUserProperty
Retrieve a user-defined property previously set withputUserProperty()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
Log a message- Parameters:
s- the message - may be nulle- the Throwable - may be null
-
getAuthorization
Return theAuthorizationfor this Web Socket connection. The returned value will never be null - at worst it will be a default Authorization- Returns:
- the Authorization
-
setAuthorization
Set theAuthorizationfor this Web Socket connection.- Parameters:
auth- the Authoization
-
receive
Receive a text message from theReader, parse it as JSON and call theWebsocketController.receive()method with the result.- Parameters:
reader- the Reader to read from
-
receive
Receive a binary message from theInputStream, parse it as CBOR and call theWebsocketController.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 ifisBinary()) to the object return fromgetSendStream()orgetSendWriter()as appropriate. If not already set, thereply_tofield will be set to the value fromgetMessageId()- Parameters:
json- the data to send.
-
open
public void open()Called when the Web Socket connection is opened -
close
Called when the Web Socket connection is closed- Parameters:
code- the code for closure if one is known, or 0 otherwisereason- the reason for closure if one is known, or null otherwise
-
systemError
Called to log a "system-error"- Parameters:
err- the error
-
reply
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 throwableok- the ok valuemsg- the message, or null to use default texte- if the message is an error message, the exception that caused it- Returns:
- a Json representation of this data
-
reply
A convenience method to create a "message" type reply. Callsreply(type, ok, msg, null)- Parameters:
type- the type of message to create, or null to use "message"ok- the ok valuemsg- the message, or null to use default text- Returns:
- a Json representation of this data
-