Class UiMessageHandler


  • public abstract class UiMessageHandler
    extends Object
    Abstraction of an entity capable of processing JSON messages from the user interface client.

    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.

    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method 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 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​(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​(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.
      Set<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.
      Set<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.
    • Constructor Detail

      • UiMessageHandler

        public UiMessageHandler()
    • Method Detail

      • createRequestHandlers

        protected abstract Collection<RequestHandler> createRequestHandlers()
        Subclasses must create and return the collection of request handlers for the message types they handle.

        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.

        Returns:
        the message handler instances
      • messageTypes

        public Set<String> messageTypes()
        Returns the set of message types which this handler is capable of processing.
        Returns:
        set of message types
      • process

        public void process​(com.fasterxml.jackson.databind.node.ObjectNode message)
        Processes a JSON message from the user interface client.
        Parameters:
        message - JSON message
      • init

        public void init​(UiConnection connection,
                         ServiceDirectory directory)
        Initializes the handler with the user interface connection and service directory context.
        Parameters:
        connection - user interface connection
        directory - service directory
      • destroy

        public void destroy()
        Destroys the message handler context.
      • connection

        public UiConnection connection()
        Returns the user interface connection with which this handler was primed.
        Returns:
        user interface connection
      • directory

        public ServiceDirectory directory()
        Returns the service directory with which this handler was primed.
        Returns:
        service directory
      • get

        protected <T> T get​(Class<T> serviceClass)
        Returns an implementation of the specified service class.
        Type Parameters:
        T - type of service
        Parameters:
        serviceClass - service class
        Returns:
        implementation class
        Throws:
        ServiceNotFoundException - if no implementation found
      • requiredLionBundles

        public Set<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.

        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.

        Returns:
        the set of identifiers of required localization bundles
      • cacheLionBundle

        public 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.
        Parameters:
        bundle - the bundle to cache
      • getLionBundle

        protected LionBundle getLionBundle​(String id)
        Returns the localization bundle with the given identifier, if we requested to have it cached during initialization; null otherwise.
        Parameters:
        id - the lion bundle identifier
        Returns:
        the associated lion bundle
      • objectNode

        protected com.fasterxml.jackson.databind.node.ObjectNode objectNode()
        Returns a freshly minted object node.
        Returns:
        new object node
      • arrayNode

        protected com.fasterxml.jackson.databind.node.ArrayNode arrayNode()
        Returns a freshly minted array node.
        Returns:
        new array node
      • sendMessage

        protected void sendMessage​(com.fasterxml.jackson.databind.node.ObjectNode data)
        Sends the specified data to the client. It is expected that the data is in the prescribed JSON format for events to the client.
        Parameters:
        data - data to be sent
      • getJsonCodecContext

        protected CodecContext getJsonCodecContext()
        Obtain a CodecContext to be used in encoding and decoding objects that have a registered JsonCodec for their class. This method instantiates a private inner class which is returned on subsequent calls.
        Returns:
        a CodecContext.