XDataManager.java

  1. /**
  2.  *
  3.  * Copyright 2014-2015 Florian Schmaus
  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.xdata;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.ConnectionCreationListener;
  21. import org.jivesoftware.smack.Manager;
  22. import org.jivesoftware.smack.SmackException.NoResponseException;
  23. import org.jivesoftware.smack.SmackException.NotConnectedException;
  24. import org.jivesoftware.smack.XMPPConnection;
  25. import org.jivesoftware.smack.XMPPConnectionRegistry;
  26. import org.jivesoftware.smack.XMPPException.XMPPErrorException;

  27. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  28. import org.jivesoftware.smackx.xdata.packet.DataForm;

  29. import org.jxmpp.jid.Jid;

  30. public final class XDataManager extends Manager {

  31.     /**
  32.      * The value of {@link DataForm#NAMESPACE}.
  33.      */
  34.     public static final String NAMESPACE = DataForm.NAMESPACE;

  35.     static {
  36.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  37.             @Override
  38.             public void connectionCreated(XMPPConnection connection) {
  39.                 getInstanceFor(connection);
  40.             }
  41.         });
  42.     }

  43.     private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>();

  44.     /**
  45.      * Get the XDataManager for the given XMPP connection.
  46.      *
  47.      * @param connection the XMPPConnection.
  48.      * @return the XDataManager
  49.      */
  50.     public static synchronized XDataManager getInstanceFor(XMPPConnection connection) {
  51.         XDataManager xDataManager = INSTANCES.get(connection);
  52.         if (xDataManager == null) {
  53.             xDataManager = new XDataManager(connection);
  54.             INSTANCES.put(connection, xDataManager);
  55.         }
  56.         return xDataManager;
  57.     }

  58.     private XDataManager(XMPPConnection connection) {
  59.         super(connection);
  60.         ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
  61.         serviceDiscoveryManager.addFeature(NAMESPACE);
  62.     }

  63.     /**
  64.      * Check if the given entity supports data forms.
  65.      *
  66.      * @param jid the JID of the entity to check.
  67.      * @return true if the entity supports data forms.
  68.      * @throws NoResponseException
  69.      * @throws XMPPErrorException
  70.      * @throws NotConnectedException
  71.      * @throws InterruptedException
  72.      * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a>
  73.      * @since 4.1
  74.      */
  75.     public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  76.         return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
  77.     }
  78. }