VCardManager.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.vcardtemp;

  18. import java.util.Map;
  19. import java.util.WeakHashMap;

  20. import org.jivesoftware.smack.ConnectionCreationListener;
  21. import org.jivesoftware.smack.Manager;
  22. import org.jivesoftware.smack.SmackException.NoResponseException;
  23. import org.jivesoftware.smack.SmackException.NotConnectedException;
  24. import org.jivesoftware.smack.XMPPConnection;
  25. import org.jivesoftware.smack.XMPPConnectionRegistry;
  26. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  27. import org.jivesoftware.smack.packet.IQ;
  28. import org.jivesoftware.smack.packet.id.StanzaIdUtil;
  29. import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
  30. import org.jivesoftware.smackx.vcardtemp.packet.VCard;
  31. import org.jxmpp.jid.BareJid;
  32. import org.jxmpp.jid.Jid;

  33. public class VCardManager extends Manager {
  34.     public static final String NAMESPACE = VCard.NAMESPACE;
  35.     public static final String ELEMENT = VCard.ELEMENT;

  36.     private static final Map<XMPPConnection, VCardManager> INSTANCES = new WeakHashMap<>();

  37.     static {
  38.         XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
  39.             @Override
  40.             public void connectionCreated(XMPPConnection connection) {
  41.                 getInstanceFor(connection);
  42.             }
  43.         });
  44.     }

  45.     /**
  46.      * Retrieves a {@link VCardManager} for the specified {@link XMPPConnection}, creating one if it doesn't already
  47.      * exist.
  48.      *
  49.      * @param connection the connection the manager is attached to.
  50.      * @return The new or existing manager.
  51.      */
  52.     public static synchronized VCardManager getInstanceFor(XMPPConnection connection) {
  53.         VCardManager vcardManager = INSTANCES.get(connection);
  54.         if (vcardManager == null) {
  55.             vcardManager = new VCardManager(connection);
  56.             INSTANCES.put(connection, vcardManager);
  57.         }
  58.         return vcardManager;
  59.     }

  60.     /**
  61.      * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
  62.      *
  63.      * @param jid
  64.      * @param connection
  65.      * @return true if the given entity understands the vCard-XML format and exchange.
  66.      * @throws XMPPErrorException
  67.      * @throws NoResponseException
  68.      * @throws NotConnectedException
  69.      * @throws InterruptedException
  70.      * @deprecated use {@link #isSupported(Jid)} instead.
  71.      */
  72.     @Deprecated
  73.     public static boolean isSupported(Jid jid, XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  74.         return VCardManager.getInstanceFor(connection).isSupported(jid);
  75.     }

  76.     private VCardManager(XMPPConnection connection) {
  77.         super(connection);
  78.         ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
  79.     }

  80.     /**
  81.      * Save this vCard for the user connected by 'connection'. XMPPConnection should be authenticated
  82.      * and not anonymous.
  83.      *
  84.      * @throws XMPPErrorException thrown if there was an issue setting the VCard in the server.
  85.      * @throws NoResponseException if there was no response from the server.
  86.      * @throws NotConnectedException
  87.      * @throws InterruptedException
  88.      */
  89.     public void saveVCard(VCard vcard) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  90.         // 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…"
  91.         vcard.setTo((Jid) null);
  92.         vcard.setType(IQ.Type.set);
  93.         // Also make sure to generate a new stanza id (the given vcard could be a vcard result), in which case we don't
  94.         // want to use the same stanza id again (although it wouldn't break if we did)
  95.         vcard.setStanzaId(StanzaIdUtil.newStanzaId());
  96.         connection().createPacketCollectorAndSend(vcard).nextResultOrThrow();
  97.     }

  98.     /**
  99.      * Load the VCard of the current user.
  100.      *
  101.      * @throws XMPPErrorException
  102.      * @throws NoResponseException
  103.      * @throws NotConnectedException
  104.      * @throws InterruptedException
  105.      */
  106.     public VCard loadVCard() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  107.         return loadVCard(null);
  108.     }

  109.     /**
  110.      * Load VCard information for a given user.
  111.      *
  112.      * @throws XMPPErrorException
  113.      * @throws NoResponseException if there was no response from the server.
  114.      * @throws NotConnectedException
  115.      * @throws InterruptedException
  116.      */
  117.     public VCard loadVCard(BareJid bareJid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  118.         VCard vcardRequest = new VCard();
  119.         vcardRequest.setTo(bareJid);
  120.         VCard result = connection().createPacketCollectorAndSend(vcardRequest).nextResultOrThrow();
  121.         return result;
  122.     }

  123.     /**
  124.      * Returns true if the given entity understands the vCard-XML format and allows the exchange of such.
  125.      *
  126.      * @param jid
  127.      * @return true if the given entity understands the vCard-XML format and exchange.
  128.      * @throws XMPPErrorException
  129.      * @throws NoResponseException
  130.      * @throws NotConnectedException
  131.      * @throws InterruptedException
  132.      */
  133.     public boolean isSupported(Jid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  134.         return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, NAMESPACE);
  135.     }
  136. }