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