AMPManager.java

  1. /**
  2.  *
  3.  * Copyright 2014 Vyacheslav Blinov
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.amp;

  18. import org.jivesoftware.smack.SmackException.NoResponseException;
  19. import org.jivesoftware.smack.SmackException.NotConnectedException;
  20. import org.jivesoftware.smack.XMPPConnection;
  21. import org.jivesoftware.smack.ConnectionCreationListener;
  22. import org.jivesoftware.smack.XMPPConnectionRegistry;
  23. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  24. import org.jivesoftware.smackx.amp.packet.AMPExtension;
  25. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;

  26. /**
  27.  * Manages AMP stanzas within messages. A AMPManager provides a high level access to
  28.  * get and set AMP rules to messages.
  29.  *
  30.  * See http://xmpp.org/extensions/xep-0079.html for AMP extension details
  31.  *
  32.  * @author Vyacheslav Blinov
  33.  */
  34. public class AMPManager {


  35.     // Enable the AMP support on every established connection
  36.     // The ServiceDiscoveryManager class should have been already initialized
  37.     static {
  38.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  39.             public void connectionCreated(XMPPConnection connection) {
  40.                 AMPManager.setServiceEnabled(connection, true);
  41.             }
  42.         });
  43.     }

  44.     /**
  45.      * Enables or disables the AMP support on a given connection.<p>
  46.      *
  47.      * Before starting to send AMP messages to a user, check that the user can handle XHTML
  48.      * messages. Enable the AMP support to indicate that this client handles XHTML messages.
  49.      *
  50.      * @param connection the connection where the service will be enabled or disabled
  51.      * @param enabled indicates if the service will be enabled or disabled
  52.      */
  53.     public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) {
  54.         if (isServiceEnabled(connection) == enabled)
  55.             return;

  56.         if (enabled) {
  57.             ServiceDiscoveryManager.getInstanceFor(connection).addFeature(AMPExtension.NAMESPACE);
  58.         }
  59.         else {
  60.             ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(AMPExtension.NAMESPACE);
  61.         }
  62.     }

  63.     /**
  64.      * Returns true if the AMP support is enabled for the given connection.
  65.      *
  66.      * @param connection the connection to look for AMP support
  67.      * @return a boolean indicating if the AMP support is enabled for the given connection
  68.      */
  69.     public static boolean isServiceEnabled(XMPPConnection connection) {
  70.         connection.getServiceName();
  71.         return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(AMPExtension.NAMESPACE);
  72.     }

  73.     /**
  74.      * Check if server supports specified action
  75.      * @param connection active xmpp connection
  76.      * @param action action to check
  77.      * @return true if this action is supported.
  78.      * @throws XMPPErrorException
  79.      * @throws NoResponseException
  80.      * @throws NotConnectedException
  81.      * @throws InterruptedException
  82.      */
  83.     public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  84.         String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString();
  85.         return isFeatureSupportedByServer(connection, featureName);
  86.     }

  87.     /**
  88.      * Check if server supports specified condition
  89.      * @param connection active xmpp connection
  90.      * @param conditionName name of condition to check
  91.      * @return true if this condition is supported.
  92.      * @throws XMPPErrorException
  93.      * @throws NoResponseException
  94.      * @throws NotConnectedException
  95.      * @throws InterruptedException
  96.      * @see AMPDeliverCondition
  97.      * @see AMPExpireAtCondition
  98.      * @see AMPMatchResourceCondition
  99.      */
  100.     public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  101.         String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName;
  102.         return isFeatureSupportedByServer(connection, featureName);
  103.     }

  104.     private static boolean isFeatureSupportedByServer(XMPPConnection connection, String featureName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  105.         return ServiceDiscoveryManager.getInstanceFor(connection).serverSupportsFeature(featureName);
  106.     }
  107. }