Package com.blackrook.small
Class SmallEndpoint
java.lang.Object
javax.websocket.Endpoint
com.blackrook.small.SmallEndpoint
public abstract class SmallEndpoint
extends javax.websocket.Endpoint
A base websocket endpoint that automatically has a connection to the Small application environment.
Endpoints that extend this class should use the
ServerEndpoint
annotation to declare itself.
You should use this class for finding components created in Small, as this provides access for retrieving
those components once this class is instantiated by the Servlet Container (see getComponent(Class)
.
- Author:
- Matthew Tropiano
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
afterOpen
(javax.websocket.EndpointConfig config) Called after the session and environment setup in theonOpen(Session, EndpointConfig)
method happens.protected <T> T
getComponent
(Class<T> componentClass) Returns a singleton component instantiated by Small.protected SmallEnvironment
getId()
Convenience method for getting this endpoint's ID.protected JSONDriver
Gets this application's JSON converter driver.getParameter
(String parameterName) Convenience method for getting a query parameter from the endpoint URI.getParameters
(String parameterName) Convenience method for getting a list of query parameter values from the endpoint URI.getPathVariable
(String variableName) Convenience method for getting a path variable from the endpoint URI.Convenience method for getting the query string from the request that created this.protected javax.websocket.Session
protected File
getURI()
Convenience method for getting the URI that created this endpoint.protected XMLDriver
Gets this application's XML converter driver.final void
onOpen
(javax.websocket.Session session, javax.websocket.EndpointConfig config) When this is called, this gets theSmallEnvironment
from the config and saves the session on this class.sendAsyncBinary
(byte[] buffer) Sends binary data, asynchronously, to the client.sendAsyncBinary
(ByteBuffer buffer) Sends binary data, asynchronously, to the client.sendAsyncJSON
(Object object) Sends a JSON Object, asynchronously, to the client.sendAsyncText
(String message) Sends a message string, asynchronously, to the client.sendAsyncXML
(Object object) Sends a JSON Object, asynchronously, to the client.void
sendBinary
(byte[] buffer) Sends binary data, synchronously, to the client.void
sendBinary
(ByteBuffer buffer) Sends binary data, synchronously, to the client.void
sendBinaryPartial
(byte[] buffer, boolean isLast) Sends binary data, synchronously, to the client, hinting that it may not be the last one in the complete message.void
sendBinaryPartial
(ByteBuffer buffer, boolean isLast) Sends binary data, synchronously, to the client, hinting that it may not be the last one in the complete message.void
sendData
(InputStream in) Sends a stream of data, synchronously, until the end of the stream.void
sendData
(InputStream in, int bufferSize) Sends a stream of data, synchronously, until the end of the stream.void
sendFileContents
(File file) Sends the contents of a file as a stream of data, synchronously, until the end of the file.void
sendFileContents
(File file, int bufferSize) Sends the contents of a file as a stream of data, synchronously, until the end of the file.void
Sends a JSON Object, synchronously, to the client.void
Sends a message string, synchronously, to the client.void
sendTextPartial
(String message, boolean isLast) Sends a message string, synchronously, to the client, hinting that it may not be the last one in the complete message.void
Sends an XML Document, synchronously, to the client.Methods inherited from class javax.websocket.Endpoint
onClose, onError
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault buffer size.- See Also:
-
-
Constructor Details
-
SmallEndpoint
public SmallEndpoint()
-
-
Method Details
-
onOpen
public final void onOpen(javax.websocket.Session session, javax.websocket.EndpointConfig config) When this is called, this gets theSmallEnvironment
from the config and saves the session on this class. This method is already annotated withOnOpen
, and callsafterOpen(EndpointConfig)
to set up configuration after theSession
and theSmallEnvironment
is set on the endpoint.- Specified by:
onOpen
in classjavax.websocket.Endpoint
-
afterOpen
protected void afterOpen(javax.websocket.EndpointConfig config) Called after the session and environment setup in theonOpen(Session, EndpointConfig)
method happens. Does nothing by default. SeegetSession()
for getting the socket session. SeegetEnvironment()
for setting up references to components.- Parameters:
config
- the endpoint configuration.
-
getSession
protected javax.websocket.Session getSession()- Returns:
- gets the session for this websocket.
-
getEnvironment
- Returns:
- the Small environment.
-
getJSONDriver
Gets this application's JSON converter driver.- Returns:
- the instantiated driver.
-
getXMLDriver
Gets this application's XML converter driver.- Returns:
- the instantiated driver.
-
getTemporaryDirectory
- Returns:
- the temporary directory to use for multipart files.
-
getComponent
Returns a singleton component instantiated by Small.- Type Parameters:
T
- the object type.- Parameters:
componentClass
- the component class.- Returns:
- a singleton component annotated with
Component
by class.
-
getId
Convenience method for getting this endpoint's ID.getSession().getId()
- Returns:
- this endpoint's unique id.
-
getURI
Convenience method for getting the URI that created this endpoint.getSession().getRequestURI()
- Returns:
- this endpoint's constructor URI.
- Since:
- 1.6.0
-
getPathVariable
Convenience method for getting a path variable from the endpoint URI.getSession().getPathParameters().get(variableName)
- Parameters:
variableName
- the variable name.- Returns:
- the corresponding value, or null if no value.
- Since:
- 1.6.0
-
getParameters
Convenience method for getting a list of query parameter values from the endpoint URI.getSession().getRequestParameterMap().get(parameterName)
- Parameters:
parameterName
- the parameter name.- Returns:
- the corresponding list of values, or null if no such parameter.
- Since:
- 1.6.0
-
getParameter
Convenience method for getting a query parameter from the endpoint URI. This only returns the last defined one.getSession().getRequestParameterMap().get(parameterName)
- Parameters:
parameterName
- the parameter name.- Returns:
- the corresponding value, or null if no such parameter.
- Since:
- 1.6.0
-
getQueryString
Convenience method for getting the query string from the request that created this.getSession().getQueryString()
- Returns:
- the query string.
- Since:
- 1.6.0
-
sendText
Sends a message string, synchronously, to the client. Execution halts until the client socket acknowledges receipt.- Parameters:
message
- the message string to pass back to the connected client.- Throws:
SmallFrameworkException
- on a send error.
-
sendTextPartial
Sends a message string, synchronously, to the client, hinting that it may not be the last one in the complete message. Execution halts until the client socket acknowledges receipt.- Parameters:
message
- the (partial) message string to pass back to the connected client.isLast
- if true, tells the client that this is the final part. if false, it is not the last part.- Throws:
SmallFrameworkException
- on a send error.
-
sendJSON
Sends a JSON Object, synchronously, to the client. Execution halts until the client socket acknowledges receipt.- Parameters:
object
- the object to pass back to the connected client (converted to JSON via theJSONDriver
).- Throws:
SmallFrameworkException
- on a send error.
-
sendXML
Sends an XML Document, synchronously, to the client. Execution halts until the client socket acknowledges receipt.- Parameters:
object
- the object to pass back to the connected client (converted to XML via theXMLDriver
).- Throws:
SmallFrameworkException
- on a send error.
-
sendBinary
Sends binary data, synchronously, to the client. Execution halts until the client socket acknowledges receipt.- Parameters:
buffer
- the buffer of data to pass back to the connected client.- Throws:
SmallFrameworkException
- on a send error.
-
sendBinaryPartial
Sends binary data, synchronously, to the client, hinting that it may not be the last one in the complete message. Execution halts until the client socket acknowledges receipt.- Parameters:
buffer
- the buffer of data to pass back to the connected client.isLast
- if true, this is the last part of the message.- Throws:
SmallFrameworkException
- on a send error.
-
sendBinary
public void sendBinary(byte[] buffer) Sends binary data, synchronously, to the client. Execution halts until the client socket acknowledges receipt. This method should not be used for repeated sends, as the array is wrapped in a ByteBuffer before it is sent.- Parameters:
buffer
- the buffer of data to pass back to the connected client.- Throws:
SmallFrameworkException
- on a send error.
-
sendBinaryPartial
public void sendBinaryPartial(byte[] buffer, boolean isLast) Sends binary data, synchronously, to the client, hinting that it may not be the last one in the complete message. Execution halts until the client socket acknowledges receipt. This method should not be used for repeated sends, as the array is wrapped in a ByteBuffer before it is sent.- Parameters:
buffer
- the buffer of data to pass back to the connected client.isLast
- if true, this is the last part of the message.- Throws:
SmallFrameworkException
- on a send error.
-
sendData
Sends a stream of data, synchronously, until the end of the stream. The stream is not closed after this completes. Execution halts until the client socket acknowledges receipt.The buffer size used for this socket is
DEFAULT_BUFFER_SIZE
.- Parameters:
in
- the input stream to read from.- Throws:
SmallFrameworkException
- on a send or read error.- Since:
- 1.6.0
-
sendData
Sends a stream of data, synchronously, until the end of the stream. The stream is not closed after this completes. Execution halts until the client socket acknowledges receipt.- Parameters:
in
- the input stream to read from.bufferSize
- the size of the buffer to use during the send.- Throws:
IllegalArgumentException
- if bufferSize is 0 or less.SmallFrameworkException
- on a send or read error.
-
sendFileContents
Sends the contents of a file as a stream of data, synchronously, until the end of the file. Execution halts until the client socket acknowledges receipt.- Parameters:
file
- the file to read.- Throws:
SmallFrameworkException
- on a send or read error.- Since:
- 1.6.0
-
sendFileContents
Sends the contents of a file as a stream of data, synchronously, until the end of the file. Execution halts until the client socket acknowledges receipt.- Parameters:
file
- the file to read.bufferSize
- the size of the buffer to use during the send.- Throws:
IllegalArgumentException
- if bufferSize is 0 or less.SmallFrameworkException
- on a send or read error.
-
sendAsyncText
Sends a message string, asynchronously, to the client.- Parameters:
message
- the message string to pass back to the connected client.- Returns:
- the
Future
object to monitor the sent request after the call. - Throws:
SmallFrameworkException
- on a send error.
-
sendAsyncJSON
Sends a JSON Object, asynchronously, to the client.- Parameters:
object
- the object to pass back to the connected client.- Returns:
- the
Future
object to monitor the sent request after the call. - Throws:
SmallFrameworkException
- on a send error.
-
sendAsyncXML
Sends a JSON Object, asynchronously, to the client.- Parameters:
object
- the object to pass back to the connected client (converted to XML via theXMLDriver
).- Returns:
- the
Future
object to monitor the sent request after the call. - Throws:
SmallFrameworkException
- on a send error.
-
sendAsyncBinary
Sends binary data, asynchronously, to the client.- Parameters:
buffer
- the buffer of data to pass back to the connected client.- Returns:
- the
Future
object to monitor the sent request after the call. - Throws:
IllegalArgumentException
- if buffer is null.
-
sendAsyncBinary
Sends binary data, asynchronously, to the client. This method should not be used for repeated sends, as the array is wrapped in a ByteBuffer before it is sent.- Parameters:
buffer
- the buffer of data to pass back to the connected client.- Returns:
- the
Future
object to monitor the sent request after the call. - Throws:
IllegalArgumentException
- if buffer is null.
-