001/** 002 * 003 * Copyright 2014-2015 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; 029import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 030import org.jivesoftware.smackx.xdata.packet.DataForm; 031 032public class XDataManager extends Manager { 033 034 /** 035 * The value of {@link DataForm#NAMESPACE}. 036 */ 037 public static final String NAMESPACE = DataForm.NAMESPACE; 038 039 static { 040 XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() { 041 @Override 042 public void connectionCreated(XMPPConnection connection) { 043 getInstanceFor(connection); 044 } 045 }); 046 } 047 048 private static final Map<XMPPConnection, XDataManager> INSTANCES = new WeakHashMap<>(); 049 050 /** 051 * Get the XDataManager for the given XMPP connection. 052 * 053 * @param connection 054 * @return the XDataManager 055 */ 056 public static synchronized XDataManager getInstanceFor(XMPPConnection connection) { 057 XDataManager xDataManager = INSTANCES.get(connection); 058 if (xDataManager == null) { 059 xDataManager = new XDataManager(connection); 060 INSTANCES.put(connection, xDataManager); 061 } 062 return xDataManager; 063 } 064 065 private XDataManager(XMPPConnection connection) { 066 super(connection); 067 ServiceDiscoveryManager serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection); 068 serviceDiscoveryManager.addFeature(NAMESPACE); 069 } 070 071 /** 072 * Check if the given entity supports data forms. 073 * 074 * @param jid the JID of the entity to check. 075 * @return true if the entity supports data forms. 076 * @throws NoResponseException 077 * @throws XMPPErrorException 078 * @throws NotConnectedException 079 * @see <a href="http://xmpp.org/extensions/xep-0004.html#disco">XEP-0004: Data Forms ยง 6. Service Discovery</a> 080 * @since 4.1 081 */ 082 public boolean isSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException { 083 return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE); 084 } 085}