XDataManager.java

  1. /**
  2.  *
  3.  * Copyright 2014-2020 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.jivesoftware.smackx.xdata.provider.DescriptionProvider;
  30. import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProviderManager;

  31. import org.jxmpp.jid.Jid;

  32. public final class XDataManager extends Manager {

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

  37.     static {
  38.         FormFieldChildElementProviderManager.addFormFieldChildElementProvider(
  39.                         new DescriptionProvider()
  40.         );

  41.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  42.             @Override
  43.             public void connectionCreated(XMPPConnection connection) {
  44.                 getInstanceFor(connection);
  45.             }
  46.         });
  47.     }

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

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

  63.     private XDataManager(XMPPConnection connection) {
  64.         super(connection);
  65.         ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
  66.         serviceDiscoveryManager.addFeature(NAMESPACE);
  67.     }

  68.     /**
  69.      * Check if the given entity supports data forms.
  70.      *
  71.      * @param jid the JID of the entity to check.
  72.      * @return true if the entity supports data forms.
  73.      * @throws NoResponseException if there was no response from the remote entity.
  74.      * @throws XMPPErrorException if there was an XMPP error returned.
  75.      * @throws NotConnectedException if the XMPP connection is not connected.
  76.      * @throws InterruptedException if the calling thread was interrupted.
  77.      * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a>
  78.      * @since 4.1
  79.      */
  80.     public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  81.         return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
  82.     }
  83. }