Package tigase.xmpp.impl

## Class OfflineMessages

- java.lang.Object
- - tigase.xmpp.XMPPProcessor
  - - tigase.xmpp.impl.OfflineMessages

- All Implemented Interfaces:
    `java.lang.Comparable<XMPPImplIfc>`, `XMPPImplIfc`, `XMPPPostprocessorIfc`, `XMPPProcessorConcurrencyAwareIfc`, `XMPPProcessorIfc`

      @Bean(name="msgoffline",
            parent=SessionManager.class,
            active=false)
      public class OfflineMessages
      extends XMPPProcessor
      implements XMPPPostprocessorIfc, XMPPProcessorIfc

  OfflineMessages plugin implementation which follows XEP-0160: Best Practices for Handling Offline Messages specification. Responsible for storing messages send to offline users - either as a standalone plugin or as a processor for other plugins (e.g. AMP). Is registered to handle packets of type `<presence>`.
  Created: Mon Oct 16 13:28:53 2006
  Author:
    Artur Hefczyc

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `OfflineMessages.Notifier` |
    `static interface` | `OfflineMessages.OfflineMsgRepositoryIfc` |
    `static class` | `OfflineMessages.PubSubPublisherNotifier` |
    `static class` | `OfflineMessages.StampComparator` | `Comparator` interface implementation for the purpose of sorting Elements retrieved from the repository by the timestamp stored in `delay` element.

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `protected static java.lang.String` | `ID` | Field holds identification string for the plugin.
    `static java.lang.String[]` | `MESSAGE_EVENT_PATH` | Field holds an array for element paths for which the plugin offers message saving capabilities.
    `static java.lang.String[]` | `MESSAGE_HEADER_PATH` | Field holds an array for element paths for which the plugin offers processing capabilities.
    `static java.lang.String[]` | `MESSAGE_HINTS_NO_STORE` |
    `static java.lang.String` | `MESSAGE_HINTS_XMLNS` |
    `static java.lang.String[]` | `MESSAGE_RECEIVED_PATH` |
    `static java.lang.String` | `MESSAGE_RECEIVED_XMLNS` |
    `static java.lang.String` | `PUBSUB_NODE_KEY` |
    `static java.lang.String[]` | `PUBSUB_NODE_PATH` |
    `protected static java.lang.String` | `XMLNS` | Field holds default client namespace for stanzas.

    - ### Fields inherited from class tigase.xmpp.XMPPProcessor

      `ALL_NAMES, ALL_PATHS, cmpInfo`

    - ### Fields inherited from interface tigase.xmpp.XMPPImplIfc

      `CLIENT_XMLNS`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `OfflineMessages()` |

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `protected OfflineMessages.OfflineMsgRepositoryIfc` | `getMsgRepoImpl​(NonAuthUserRepository repo, XMPPResourceConnection conn)` | Method allows obtaining instance of `MsgRepositoryIfc` interface implementation.
    `java.lang.String[]` | `getOfflineStorageMatchers()` |
    `java.lang.String` | `id()` | Method `id` returns a unique ID of the plugin.
    `protected boolean` | `isAllowedForOfflineStorage​(Packet pac)` | Method determines whether packet sent to offline user should be stored in offline storage or not
    `protected boolean` | `isAllowedForOfflineStorageDefaults​(Packet pac)` |
    `protected boolean` | `loadOfflineMessages​(Packet packet, XMPPResourceConnection conn)` | Method determines whether offline messages should be loaded - the process should be run only once per user session and only for available/null presence with priority greater than 0.
    `protected void` | `notifyNewOfflineMessage​(Packet packet, XMPPResourceConnection conn, java.util.Queue<Packet> queue, java.util.Map<java.lang.String,​java.lang.Object> settings)` |
    `protected void` | `notifyOfflineMessagesRetrieved​(XMPPResourceConnection conn, java.util.Queue<Packet> queue)` |
    `void` | `postProcess​(Packet packet, XMPPResourceConnection conn, NonAuthUserRepository repo, java.util.Queue<Packet> queue, java.util.Map<java.lang.String,​java.lang.Object> settings)` | Performs processing of `packet` for which there was no processor.
    `void` | `process​(Packet packet, XMPPResourceConnection conn, NonAuthUserRepository repo, java.util.Queue<Packet> results, java.util.Map<java.lang.String,​java.lang.Object> settings)` | `OfflineMessages` processor is triggered by `<presence>` stanza.
    `void` | `processIq​(Packet packet, XMPPResourceConnection conn, NonAuthUserRepository repo, java.util.Queue<Packet> results)` |
    `java.util.Queue<Packet>` | `restorePacketForOffLineUser​(XMPPResourceConnection conn, OfflineMsgRepositoryIfc repo)` | Method restores all messages from repository for the JID of the current session.
    `Authorization` | `savePacketForOffLineUser​(Packet packet, OfflineMsgRepositoryIfc repo, NonAuthUserRepository userRepo)` | Method stores messages to offline repository with the following rules applied, i.e. saves only: message stanza with either nonempty `<body>`, `<event>` or `<header>` child element and only messages of type normal, chat. presence stanza of type subscribe, subscribed, unsubscribe and unsubscribed. Processed messages are stamped with the `delay` element and appropriate timestamp.
    `void` | `setOfflineStorageMatchers​(java.lang.String[] matcherStrs)` |
    `Element[]` | `supDiscoFeatures​(XMPPResourceConnection session)` | Method `supDiscoFeatures` returns an array of XML `Element`s with service discovery features which have to be returned to the client uppon request.
    `java.lang.String[][]` | `supElementNamePaths()` | Method `supElementNamePaths` returns an array of element names in form of a full path to the XML element for stanzas which can be processed by this plugin.
    `java.lang.String[]` | `supNamespaces()` | Method `supNamespaces` returns an array of name-spaces for stanzas which can be processed by this plugin.

    - ### Methods inherited from class tigase.xmpp.XMPPProcessor

      `canHandle, compareTo, concurrentQueuesNo, getComponentInfo, getInstance, getQueueSize, getStatistics, getThreadsNo, init, supStreamFeatures, supTypes, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface java.lang.Comparable

      `compareTo`

    - ### Methods inherited from interface tigase.xmpp.XMPPImplIfc

      `canHandle, concurrentQueuesNo, getComponentInfo, getStatistics, init, supStreamFeatures, supTypes`

- - ### Field Detail

    - #### MESSAGE_EVENT_PATH

          public static final java.lang.String[] MESSAGE_EVENT_PATH

      Field holds an array for element paths for which the plugin offers message saving capabilities. In case of `msgoffline` plugin it is presence stanza

    - #### MESSAGE_HEADER_PATH

          public static final java.lang.String[] MESSAGE_HEADER_PATH

      Field holds an array for element paths for which the plugin offers processing capabilities. In case of `msgoffline` plugin it is presence stanza

    - #### MESSAGE_HINTS_NO_STORE

          public static final java.lang.String[] MESSAGE_HINTS_NO_STORE

    - #### MESSAGE_HINTS_XMLNS

          public static final java.lang.String MESSAGE_HINTS_XMLNS

      See Also:
        Constant Field Values

    - #### MESSAGE_RECEIVED_PATH

          public static final java.lang.String[] MESSAGE_RECEIVED_PATH

    - #### MESSAGE_RECEIVED_XMLNS

          public static final java.lang.String MESSAGE_RECEIVED_XMLNS

      See Also:
        Constant Field Values

    - #### PUBSUB_NODE_PATH

          public static final java.lang.String[] PUBSUB_NODE_PATH

    - #### PUBSUB_NODE_KEY

          public static final java.lang.String PUBSUB_NODE_KEY

      See Also:
        Constant Field Values

    - #### XMLNS

          protected static final java.lang.String XMLNS

      Field holds default client namespace for stanzas. In case of `msgoffline` plugin it is jabber:client
      See Also:
        Constant Field Values

    - #### ID

          protected static final java.lang.String ID

      Field holds identification string for the plugin. In case of `msgoffline` plugin it is msgoffline
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### OfflineMessages

          public OfflineMessages()

  - ### Method Detail

    - #### id

          public java.lang.String id()

      Description copied from interface: `XMPPImplIfc`
      Method `id` returns a unique ID of the plugin. Each plugin has own, unique ID which is used in the configuration file to determine whether it needs to be loaded or not. In most cases the ID can be equal to XMLNS of the packages processed by the plugin.
      Specified by:
        `id` in interface `XMPPImplIfc`
      Returns:
        a `String` value

    - #### postProcess

          public void postProcess​(Packet packet,
                                  XMPPResourceConnection conn,
                                  NonAuthUserRepository repo,
                                  java.util.Queue<Packet> queue,
                                  java.util.Map<java.lang.String,​java.lang.Object> settings)

      Performs processing of `packet` for which there was no processor.
      OfflineMessages postprocessor simply calls `savePacketForOffLineUser` method to store packet to offline repository.
      Specified by:
        `postProcess` in interface `XMPPPostprocessorIfc`
      Parameters:
        `packet` - packet is which being processed. This parameter may never be null. Even though this is not immutable object it mustn't be altered. None of it's fields or attributes can be changed during processing.
        `conn` - user session which keeps all the user session data and also gives an access to the user's repository data. It allows for storing information in a permanent storage or in memory only during the live of the online session. This parameter can be null if there is no online user session at the time of the packet processing.
        `repo` - this is a user data storage which is normally used when the user session (parameter above) is null. This is repository allows for a very restricted access only. It allows for storing some user private data (doesn't allow overwriting existing data) like messages for offline users and it also allows for reading user public data like VCard.
        `queue` - this a collection with packets which have been generated as input packet processing results. Regardless a response to a user request is sent or the packet is forwarded to it's destination it is always required that a copy of the input packet is created and stored in the results queue.
        `settings` - this map keeps plugin specific settings loaded from the Tigase server configuration. In most cases it is unused, however if the plugin needs to access an external database that this is a way to pass database connection string to the plugin.

    - #### process

          public void process​(Packet packet,
                              XMPPResourceConnection conn,
                              NonAuthUserRepository repo,
                              java.util.Queue<Packet> results,
                              java.util.Map<java.lang.String,​java.lang.Object> settings)
                       throws NotAuthorizedException

      `OfflineMessages` processor is triggered by `<presence>` stanza. Upon receiving it plugin tries to load messages from repository and, if the result is not empty, sends them to the user
      Specified by:
        `process` in interface `XMPPProcessorIfc`
      Parameters:
        `packet` - packet is which being processed. This parameter may never be null. Even though this is not immutable object it mustn't be altered. None of it's fields or attributes can be changed during processing.
        `conn` - user session which keeps all the user session data and also gives an access to the user's repository data. It allows for storing information in a permanent storage or in memory only during the live of the online session. This parameter can be null if there is no online user session at the time of the packet processing.
        `repo` - this is a user data storage which is normally used when the user session (parameter above) is null. This is repository allows for a very restricted access only. It allows for storing some user private data (doesn't allow overwriting existing data) like messages for offline users and it also allows for reading user public data like VCard.
        `results` - this a collection with packets which have been generated as input packet processing results. Regardless a response to a user request is sent or the packet is forwarded to it's destination it is always required that a copy of the input packet is created and stored in the results queue.
        `settings` - this map keeps plugin specific settings loaded from the Tigase server configuration. In most cases it is unused, however if the plugin needs to access an external database that this is a way to pass database connection string to the plugin.
      Throws:
        `NotAuthorizedException`

    - #### processIq

          public void processIq​(Packet packet,
                                XMPPResourceConnection conn,
                                NonAuthUserRepository repo,
                                java.util.Queue<Packet> results)
                         throws NotAuthorizedException

      Throws:
        `NotAuthorizedException`

    - #### restorePacketForOffLineUser

          public java.util.Queue<Packet> restorePacketForOffLineUser​(XMPPResourceConnection conn,
                                                                     OfflineMsgRepositoryIfc repo)
                                                              throws UserNotFoundException,
                                                                     NotAuthorizedException

      Method restores all messages from repository for the JID of the current session. All retrieved elements are then instantiated as `Packet` objects added to `LinkedList` collection and, if possible, sorted by timestamp.
      Parameters:
        `conn` - user session which keeps all the user session data and also gives an access to the user's repository data.
        `repo` - an implementation of `MsgRepositoryIfc` interface
      Returns:
        a `Queue` of `Packet` objects based on all stored payloads for the JID of the current session.
      Throws:
        `UserNotFoundException`
        `NotAuthorizedException`

    - #### savePacketForOffLineUser

          public Authorization savePacketForOffLineUser​(Packet packet,
                                                        OfflineMsgRepositoryIfc repo,
                                                        NonAuthUserRepository userRepo)
                                                 throws UserNotFoundException

      Method stores messages to offline repository with the following rules applied, i.e. saves only:
      - message stanza with either nonempty `<body>`, `<event>` or `<header>` child element and only messages of type normal, chat.
      - presence stanza of type subscribe, subscribed, unsubscribe and unsubscribed.
      Processed messages are stamped with the `delay` element and appropriate timestamp.
      Parameters:
        `packet` - a `Packet` object containing packet that should be verified and saved
        `repo` - a `MsgRepositoryIfc` repository handler responsible for storing messages
      Returns:
        `true` if the packet was correctly saved to repository, `false` otherwise.
      Throws:
        `UserNotFoundException`

    - #### getOfflineStorageMatchers

          public java.lang.String[] getOfflineStorageMatchers()

    - #### setOfflineStorageMatchers

          public void setOfflineStorageMatchers​(java.lang.String[] matcherStrs)

    - #### supDiscoFeatures

          public Element[] supDiscoFeatures​(XMPPResourceConnection session)

      Description copied from interface: `XMPPImplIfc`
      Method `supDiscoFeatures` returns an array of XML `Element`s with service discovery features which have to be returned to the client uppon request. Service discovery features returned by this method correspond to services supported by this plugin.
      Specified by:
        `supDiscoFeatures` in interface `XMPPImplIfc`
      Overrides:
        `supDiscoFeatures` in class `XMPPProcessor`
      Parameters:
        `session` - a `XMPPResourceConnection` value
      Returns:
        an `Element[]` value

    - #### supElementNamePaths

          public java.lang.String[][] supElementNamePaths()

      Description copied from interface: `XMPPImplIfc`
      Method `supElementNamePaths` returns an array of element names in form of a full path to the XML element for stanzas which can be processed by this plugin. Each element name path corresponds to XMLNS returned in array by `supNamespaces()` method. The element path itself is represented by a String array with each path element as a separate String.
      Specified by:
        `supElementNamePaths` in interface `XMPPImplIfc`
      Overrides:
        `supElementNamePaths` in class `XMPPProcessor`
      Returns:
        a `String[][]` value is an array for element paths for which the plugin offers processing capabilities. Each path is in form of a String array in order to reduce parsing overhead.

    - #### supNamespaces

          public java.lang.String[] supNamespaces()

      Description copied from interface: `XMPPImplIfc`
      Method `supNamespaces` returns an array of name-spaces for stanzas which can be processed by this plugin. Each namespace corresponds to element name returned in array by `supElemenets()` method.
      Specified by:
        `supNamespaces` in interface `XMPPImplIfc`
      Overrides:
        `supNamespaces` in class `XMPPProcessor`
      Returns:
        a `String[]` value

    - #### getMsgRepoImpl

          protected OfflineMessages.OfflineMsgRepositoryIfc getMsgRepoImpl​(NonAuthUserRepository repo,
                                                                           XMPPResourceConnection conn)

      Method allows obtaining instance of `MsgRepositoryIfc` interface implementation.
      Parameters:
        `conn` - user session which keeps all the user session data and also gives an access to the user's repository data.
        `repo` - an implementation of `MsgRepositoryIfc` interface
      Returns:
        instance of `MsgRepositoryIfc` interface implementation.

    - #### isAllowedForOfflineStorage

          protected boolean isAllowedForOfflineStorage​(Packet pac)

      Method determines whether packet sent to offline user should be stored in offline storage or not

    - #### isAllowedForOfflineStorageDefaults

          protected boolean isAllowedForOfflineStorageDefaults​(Packet pac)

    - #### loadOfflineMessages

          protected boolean loadOfflineMessages​(Packet packet,
                                                XMPPResourceConnection conn)

      Method determines whether offline messages should be loaded - the process should be run only once per user session and only for available/null presence with priority greater than 0.
      Parameters:
        `packet` - a `Packet` object containing packet that should be verified and saved
        `conn` - user session which keeps all the user session data and also gives an access to the user's repository data.
      Returns:
        `true` if the messages should be loaded, `false` otherwise.

    - #### notifyNewOfflineMessage

          protected void notifyNewOfflineMessage​(Packet packet,
                                                 XMPPResourceConnection conn,
                                                 java.util.Queue<Packet> queue,
                                                 java.util.Map<java.lang.String,​java.lang.Object> settings)

    - #### notifyOfflineMessagesRetrieved

          protected void notifyOfflineMessagesRetrieved​(XMPPResourceConnection conn,
                                                        java.util.Queue<Packet> queue)