/**
 *
 * Copyright 2014 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;

public 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
     * @param connection
     * @return true if the given entity understands the vCard-XML format and exchange.
     * @throws XMPPErrorException 
     * @throws NoResponseException 
     * @throws NotConnectedException
     * @deprecated use {@link #isSupported(String)} instead.
     */
    @Deprecated
    public static boolean isSupported(String jid, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException  {
        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.
     *
     * @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 
     */
    public void saveVCard(VCard vcard) throws NoResponseException, XMPPErrorException, NotConnectedException {
        vcard.setType(IQ.Type.set);
        connection().createPacketCollectorAndSend(vcard).nextResultOrThrow();
    }

    /**
     * Load the VCard of the current user.
     *
     * @throws XMPPErrorException 
     * @throws NoResponseException 
     * @throws NotConnectedException 
     */
    public VCard loadVCard() throws NoResponseException, XMPPErrorException, NotConnectedException {
        return loadVCard(null);
    }

    /**
     * Load VCard information for a given user.
     *
     * @throws XMPPErrorException 
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     */
    public VCard loadVCard(String bareJid) throws NoResponseException, XMPPErrorException, NotConnectedException {
        VCard vcardRequest = new VCard();
        vcardRequest.setTo(bareJid);
        VCard result = connection().createPacketCollectorAndSend(vcardRequest).nextResultOrThrow();
        return result;
    }

    /**
     * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
     * 
     * @param jid
     * @return true if the given entity understands the vCard-XML format and exchange.
     * @throws XMPPErrorException 
     * @throws NoResponseException 
     * @throws NotConnectedException 
     */
    public boolean isSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException {
        return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
    }
}

@@ -1129,16 +1129,70 @@
 ket.IQ;%0A
+import org.jivesoftware.smack.packet.id.StanzaIdUtil;%0A
 import o
@@ -3774,33 +3774,447 @@
     
-vcard.setType(IQ.Type.set
+// XEP-54 %C2%A7 3.2 %22A user may publish or update his or her vCard by sending an IQ of type %22set%22 with no 'to' address%E2%80%A6%22%0A        vcard.setTo(null);%0A        vcard.setType(IQ.Type.set);%0A        // Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't%0A        // want to use the same stanza id again (although it wouldn't break if we did)%0A        vcard.setStanzaId(StanzaIdUtil.newStanzaId()
 );%0A 

Fix VCardManager.saveVCard(VCard)