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     * @throws XMPPErrorException thrown if there was an issue setting the VCard in the server.
096     * @throws NoResponseException if there was no response from the server.
097     * @throws NotConnectedException 
098     * @throws InterruptedException 
099     */
100    public void saveVCard(VCard vcard) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
101        // 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…"
102        vcard.setTo((Jid) null);
103        vcard.setType(IQ.Type.set);
104        // Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't
105        // want to use the same stanza id again (although it wouldn't break if we did)
106        vcard.setStanzaId(StanzaIdUtil.newStanzaId());
107        connection().createStanzaCollectorAndSend(vcard).nextResultOrThrow();
108    }
109
110    /**
111     * Load the VCard of the current user.
112     *
113     * @throws XMPPErrorException 
114     * @throws NoResponseException 
115     * @throws NotConnectedException 
116     * @throws InterruptedException 
117     */
118    public VCard loadVCard() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
119        return loadVCard(null);
120    }
121
122    /**
123     * Load VCard information for a given user.
124     *
125     * @throws XMPPErrorException 
126     * @throws NoResponseException if there was no response from the server.
127     * @throws NotConnectedException 
128     * @throws InterruptedException 
129     */
130    public VCard loadVCard(EntityBareJid bareJid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
131        VCard vcardRequest = new VCard();
132        vcardRequest.setTo(bareJid);
133        VCard result = connection().createStanzaCollectorAndSend(vcardRequest).nextResultOrThrow();
134        return result;
135    }
136
137    /**
138     * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
139     * 
140     * @param jid
141     * @return true if the given entity understands the vCard-XML format and exchange.
142     * @throws XMPPErrorException 
143     * @throws NoResponseException 
144     * @throws NotConnectedException 
145     * @throws InterruptedException 
146     */
147    public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
148        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
149    }
150}