Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/VCardManager.java

158 lines
6.9 KiB
Java
Raw Normal View History

/**
*
* Copyright 2014-2019 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.vcardtemp;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPConnectionRegistry;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
2015-05-27 19:29:51 +02:00
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.Jid;
public final class VCardManager extends Manager {
public static final String NAMESPACE = VCard.NAMESPACE;
public static final String ELEMENT = VCard.ELEMENT;
private static final Map<XMPPConnection, VCardManager> INSTANCES = new WeakHashMap<>();
static {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
@Override
public void connectionCreated(XMPPConnection connection) {
getInstanceFor(connection);
}
});
}
/**
* Retrieves a {@link VCardManager} for the specified {@link XMPPConnection}, creating one if it doesn't already
* exist.
*
* @param connection the connection the manager is attached to.
* @return The new or existing manager.
*/
public static synchronized VCardManager getInstanceFor(XMPPConnection connection) {
VCardManager vcardManager = INSTANCES.get(connection);
if (vcardManager == null) {
vcardManager = new VCardManager(connection);
INSTANCES.put(connection, vcardManager);
}
return vcardManager;
}
/**
* Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
*
* @param jid TODO javadoc me please
* @param connection TODO javadoc me please
* @return true if the given entity understands the vCard-XML format and exchange.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
* @deprecated use {@link #isSupported(Jid)} instead.
*/
@Deprecated
public static boolean isSupported(Jid jid, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return VCardManager.getInstanceFor(connection).isSupported(jid);
}
private VCardManager(XMPPConnection connection) {
super(connection);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
}
/**
* Save this vCard for the user connected by 'connection'. XMPPConnection should be authenticated
* and not anonymous.
*
2017-12-23 20:21:19 +01:00
* @param vcard VCard.
*
* @throws XMPPErrorException thrown if there was an issue setting the VCard in the server.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
// TODO: Split VCard into VCardIq and VCardData, then create saveVCard(VCardData) and deprecate this method.
@SuppressWarnings("deprecation")
2015-02-14 09:43:44 +01:00
public void saveVCard(VCard vcard) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
// XEP-54 § 3.2 "A user may publish or update his or her vCard by sending an IQ of type "set" with no 'to' address…"
Merge branch '4.1' Conflicts: smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java smack-core/src/main/java/org/jivesoftware/smack/PacketListener.java smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java smack-core/src/main/java/org/jivesoftware/smack/debugger/SmackDebugger.java smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java smack-extensions/src/main/java/org/jivesoftware/smackx/address/provider/MultipleAddressesProvider.java smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/DataListener.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/LeafNode.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/VCardManager.java smack-extensions/src/test/java/org/jivesoftware/smackx/receipts/DeliveryReceiptTest.java smack-im/src/main/java/org/jivesoftware/smack/chat/ChatManager.java smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/JingleSession.java smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/provider/JingleProvider.java smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/UserID.java smack-legacy/src/main/java/org/jivesoftware/smackx/xroster/provider/RosterExchangeProvider.java smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java version.gradle
2015-03-04 22:42:36 +01:00
vcard.setTo((Jid) null);
vcard.setType(IQ.Type.set);
// Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't
// want to use the same stanza id again (although it wouldn't break if we did)
vcard.setStanzaId();
connection().sendIqRequestAndWaitForResponse(vcard);
}
/**
* Load the VCard of the current user.
*
2017-12-23 20:21:19 +01:00
* @return VCard.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
2015-02-14 09:43:44 +01:00
public VCard loadVCard() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return loadVCard(null);
}
/**
* Load VCard information for a given user.
*
2017-12-23 20:21:19 +01:00
* @param bareJid bareJid of the user.
*
* @return VCard.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
2015-05-27 19:29:51 +02:00
public VCard loadVCard(EntityBareJid bareJid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
VCard vcardRequest = new VCard();
vcardRequest.setTo(bareJid);
VCard result = connection().sendIqRequestAndWaitForResponse(vcardRequest);
return result;
}
/**
* Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
*
* @param jid TODO javadoc me please
* @return true if the given entity understands the vCard-XML format and exchange.
* @throws XMPPErrorException if there was an XMPP error returned.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
}
}