001/** 002 * 003 * Copyright 2014-2020 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.xdata; 018 019import java.util.Map; 020import java.util.WeakHashMap; 021 022import org.jivesoftware.smack.ConnectionCreationListener; 023import org.jivesoftware.smack.Manager; 024import org.jivesoftware.smack.SmackException.NoResponseException; 025import org.jivesoftware.smack.SmackException.NotConnectedException; 026import org.jivesoftware.smack.XMPPConnection; 027import org.jivesoftware.smack.XMPPConnectionRegistry; 028import org.jivesoftware.smack.XMPPException.XMPPErrorException; 029 030import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 031import org.jivesoftware.smackx.xdata.packet.DataForm; 032import org.jivesoftware.smackx.xdata.provider.DescriptionProvider; 033import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProviderManager; 034 035import org.jxmpp.jid.Jid; 036 037public final class XDataManager extends Manager { 038 039 /** 040 * The value of {@link DataForm#NAMESPACE}. 041 */ 042 public static final String NAMESPACE = DataForm.NAMESPACE; 043 044 static { 045 FormFieldChildElementProviderManager.addFormFieldChildElementProvider( 046 new DescriptionProvider() 047 ); 048 049 XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { 050 @Override 051 public void connectionCreated(XMPPConnection connection) { 052 getInstanceFor(connection); 053 } 054 }); 055 } 056 057 private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>(); 058 059 /** 060 * Get the XDataManager for the given XMPP connection. 061 * 062 * @param connection the XMPPConnection. 063 * @return the XDataManager 064 */ 065 public static synchronized XDataManager getInstanceFor(XMPPConnection connection) { 066 XDataManager xDataManager = INSTANCES.get(connection); 067 if (xDataManager == null) { 068 xDataManager = new XDataManager(connection); 069 INSTANCES.put(connection, xDataManager); 070 } 071 return xDataManager; 072 } 073 074 private XDataManager(XMPPConnection connection) { 075 super(connection); 076 ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); 077 serviceDiscoveryManager.addFeature(NAMESPACE); 078 } 079 080 /** 081 * Check if the given entity supports data forms. 082 * 083 * @param jid the JID of the entity to check. 084 * @return true if the entity supports data forms. 085 * @throws NoResponseException if there was no response from the remote entity. 086 * @throws XMPPErrorException if there was an XMPP error returned. 087 * @throws NotConnectedException if the XMPP connection is not connected. 088 * @throws InterruptedException if the calling thread was interrupted. 089 * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a> 090 * @since 4.1 091 */ 092 public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { 093 return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE); 094 } 095}