001/**
002 *
003 * Copyright 2014-2019 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.vcardtemp;
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.smack.packet.IQ;
030
031import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
032import org.jivesoftware.smackx.vcardtemp.packet.VCard;
033
034import org.jxmpp.jid.EntityBareJid;
035import org.jxmpp.jid.Jid;
036
037public final class VCardManager extends Manager {
038    public static final String NAMESPACE = VCard.NAMESPACE;
039    public static final String ELEMENT = VCard.ELEMENT;
040
041    private static final Map<XMPPConnection, VCardManager> INSTANCES = new WeakHashMap<>();
042
043    static {
044        XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
045            @Override
046            public void connectionCreated(XMPPConnection connection) {
047                getInstanceFor(connection);
048            }
049        });
050    }
051
052    /**
053     * Retrieves a {@link VCardManager} for the specified {@link XMPPConnection}, creating one if it doesn't already
054     * exist.
055     *
056     * @param connection the connection the manager is attached to.
057     * @return The new or existing manager.
058     */
059    public static synchronized VCardManager getInstanceFor(XMPPConnection connection) {
060        VCardManager vcardManager = INSTANCES.get(connection);
061        if (vcardManager == null) {
062            vcardManager = new VCardManager(connection);
063            INSTANCES.put(connection, vcardManager);
064        }
065        return vcardManager;
066    }
067
068    /**
069     * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
070     *
071     * @param jid TODO javadoc me please
072     * @param connection TODO javadoc me please
073     * @return true if the given entity understands the vCard-XML format and exchange.
074     * @throws XMPPErrorException if there was an XMPP error returned.
075     * @throws NoResponseException if there was no response from the remote entity.
076     * @throws NotConnectedException if the XMPP connection is not connected.
077     * @throws InterruptedException if the calling thread was interrupted.
078     * @deprecated use {@link #isSupported(Jid)} instead.
079     */
080    @Deprecated
081    public static boolean isSupported(Jid jid, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
082        return VCardManager.getInstanceFor(connection).isSupported(jid);
083    }
084
085    private VCardManager(XMPPConnection connection) {
086        super(connection);
087        ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
088    }
089
090    /**
091     * Save this vCard for the user connected by 'connection'. XMPPConnection should be authenticated
092     * and not anonymous.
093     *
094     * @param vcard VCard.
095     *
096     * @throws XMPPErrorException thrown if there was an issue setting the VCard in the server.
097     * @throws NoResponseException if there was no response from the server.
098     * @throws NotConnectedException if the XMPP connection is not connected.
099     * @throws InterruptedException if the calling thread was interrupted.
100     */
101    // TODO: Split VCard into VCardIq and VCardData, then create saveVCard(VCardData) and deprecate this method.
102    @SuppressWarnings("deprecation")
103    public void saveVCard(VCard vcard) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
104        // 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…"
105        vcard.setTo((Jid) null);
106        vcard.setType(IQ.Type.set);
107        // Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't
108        // want to use the same stanza id again (although it wouldn't break if we did)
109        vcard.setStanzaId();
110        connection().sendIqRequestAndWaitForResponse(vcard);
111    }
112
113    /**
114     * Load the VCard of the current user.
115     *
116     * @return VCard.
117     * @throws XMPPErrorException if there was an XMPP error returned.
118     * @throws NoResponseException if there was no response from the remote entity.
119     * @throws NotConnectedException if the XMPP connection is not connected.
120     * @throws InterruptedException if the calling thread was interrupted.
121     */
122    public VCard loadVCard() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
123        return loadVCard(null);
124    }
125
126    /**
127     * Load VCard information for a given user.
128     *
129     * @param bareJid bareJid of the user.
130     *
131     * @return VCard.
132     * @throws XMPPErrorException if there was an XMPP error returned.
133     * @throws NoResponseException if there was no response from the server.
134     * @throws NotConnectedException if the XMPP connection is not connected.
135     * @throws InterruptedException if the calling thread was interrupted.
136     */
137    public VCard loadVCard(EntityBareJid bareJid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
138        VCard vcardRequest = new VCard();
139        vcardRequest.setTo(bareJid);
140        VCard result = connection().sendIqRequestAndWaitForResponse(vcardRequest);
141        return result;
142    }
143
144    /**
145     * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
146     *
147     * @param jid TODO javadoc me please
148     * @return true if the given entity understands the vCard-XML format and exchange.
149     * @throws XMPPErrorException if there was an XMPP error returned.
150     * @throws NoResponseException if there was no response from the remote entity.
151     * @throws NotConnectedException if the XMPP connection is not connected.
152     * @throws InterruptedException if the calling thread was interrupted.
153     */
154    public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
155        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
156    }
157}