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.ConnectionCreationListener;
  19. import org.jivesoftware.smack.SmackException.NoResponseException;
  20. import org.jivesoftware.smack.SmackException.NotConnectedException;
  21. import org.jivesoftware.smack.XMPPConnection;
  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.             @Override
  40.             public void connectionCreated(XMPPConnection connection) {
  41.                 AMPManager.setServiceEnabled(connection, true);
  42.             }
  43.         });
  44.     }

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

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

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

  74.     /**
  75.      * Check if server supports specified action.
  76.      * @param connection active xmpp connection
  77.      * @param action action to check
  78.      * @return true if this action is supported.
  79.      * @throws XMPPErrorException if there was an XMPP error returned.
  80.      * @throws NoResponseException if there was no response from the remote entity.
  81.      * @throws NotConnectedException if the XMPP connection is not connected.
  82.      * @throws InterruptedException if the calling thread was interrupted.
  83.      */
  84.     public static boolean isActionSupported(XMPPConnection connection, AMPExtension.Action action) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  85.         String featureName = AMPExtension.NAMESPACE + "?action=" + action.toString();
  86.         return isFeatureSupportedByServer(connection, featureName);
  87.     }

  88.     /**
  89.      * Check if server supports specified condition.
  90.      * @param connection active xmpp connection
  91.      * @param conditionName name of condition to check
  92.      * @return true if this condition is supported.
  93.      * @throws XMPPErrorException if there was an XMPP error returned.
  94.      * @throws NoResponseException if there was no response from the remote entity.
  95.      * @throws NotConnectedException if the XMPP connection is not connected.
  96.      * @throws InterruptedException if the calling thread was interrupted.
  97.      * @see AMPDeliverCondition
  98.      * @see AMPExpireAtCondition
  99.      * @see AMPMatchResourceCondition
  100.      */
  101.     public static boolean isConditionSupported(XMPPConnection connection, String conditionName) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  102.         String featureName = AMPExtension.NAMESPACE + "?condition=" + conditionName;
  103.         return isFeatureSupportedByServer(connection, featureName);
  104.     }

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