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