public abstract class UiMessageHandler
extends java.lang.Object
The message structure is:
{ "event": "event-type", "payload": { arbitrary JSON object structure } }On
initialization
the handler will create and cache
RequestHandler
instances, each of which are bound to a particular
event-type. On arrival
of a new message,
the event-type is determined, and the message dispatched to the
corresponding RequestHandler's
process
method.
For convenience the implementation includes methods to obtain JSON generating objects (mapper, objectNode, arrayNode) as well as a JsonCodecContext for preparing and digesting messages to the UI client.
Constructor and Description |
---|
UiMessageHandler() |
Modifier and Type | Method and Description |
---|---|
protected com.fasterxml.jackson.databind.node.ArrayNode |
arrayNode()
Returns a freshly minted array node.
|
void |
cacheLionBundle(LionBundle bundle)
Invoked during initialization to cache any requested localization
bundles in the handler's context, so that it may subsequently look
up localization strings when composing data for the client.
|
UiConnection |
connection()
Returns the user interface connection with which this handler was primed.
|
protected abstract java.util.Collection<RequestHandler> |
createRequestHandlers()
Subclasses must create and return the collection of request handlers
for the message types they handle.
|
void |
destroy()
Destroys the message handler context.
|
ServiceDirectory |
directory()
Returns the service directory with which this handler was primed.
|
protected <T> T |
get(java.lang.Class<T> serviceClass)
Returns an implementation of the specified service class.
|
protected CodecContext |
getJsonCodecContext()
Obtain a CodecContext to be used in encoding and decoding objects
that have a registered JsonCodec for their class.
|
protected LionBundle |
getLionBundle(java.lang.String id)
Returns the localization bundle with the given identifier, if we
requested to have it cached during initialization; null otherwise.
|
void |
init(UiConnection connection,
ServiceDirectory directory)
Initializes the handler with the user interface connection and
service directory context.
|
java.util.Set<java.lang.String> |
messageTypes()
Returns the set of message types which this handler is capable of
processing.
|
protected com.fasterxml.jackson.databind.node.ObjectNode |
objectNode()
Returns a freshly minted object node.
|
void |
process(com.fasterxml.jackson.databind.node.ObjectNode message)
Processes a JSON message from the user interface client.
|
java.util.Set<java.lang.String> |
requiredLionBundles()
Returns the set of identifiers for localization bundles that the
message handler would like injected into itself, so that it can use
those bundles in composing localized data to ship to the client.
|
protected void |
sendMessage(com.fasterxml.jackson.databind.node.ObjectNode data)
Sends the specified data to the client.
|
protected abstract java.util.Collection<RequestHandler> createRequestHandlers()
Note that request handlers should be stateless. When we are
destroyed
, we will simply drop our references to them
and allow them to be garbage collected.
public java.util.Set<java.lang.String> messageTypes()
public void process(com.fasterxml.jackson.databind.node.ObjectNode message)
message
- JSON messagepublic void init(UiConnection connection, ServiceDirectory directory)
connection
- user interface connectiondirectory
- service directorypublic void destroy()
public UiConnection connection()
public ServiceDirectory directory()
protected <T> T get(java.lang.Class<T> serviceClass)
T
- type of serviceserviceClass
- service classServiceNotFoundException
- if no implementation foundpublic java.util.Set<java.lang.String> requiredLionBundles()
This default implementation returns an empty set.
Subclasses that wish to have localization bundles injected should override this method and return the set of bundle identifiers.
public void cacheLionBundle(LionBundle bundle)
bundle
- the bundle to cacheprotected LionBundle getLionBundle(java.lang.String id)
id
- the lion bundle identifierprotected com.fasterxml.jackson.databind.node.ObjectNode objectNode()
protected com.fasterxml.jackson.databind.node.ArrayNode arrayNode()
protected void sendMessage(com.fasterxml.jackson.databind.node.ObjectNode data)
data
- data to be sentprotected CodecContext getJsonCodecContext()