Search in sources :

Example 1 with VCardManager

use of org.jivesoftware.openfire.vcard.VCardManager in project Openfire by igniterealtime.

the class IQvCardHandler method handleIQ.

@Override
public IQ handleIQ(IQ packet) throws UnauthorizedException, PacketException {
    IQ result = IQ.createResultIQ(packet);
    IQ.Type type = packet.getType();
    if (type.equals(IQ.Type.set)) {
        try {
            User user = userManager.getUser(packet.getFrom().getNode());
            Element vcard = packet.getChildElement();
            if (vcard != null) {
                VCardManager.getInstance().setVCard(user.getUsername(), vcard);
            }
        } catch (UserNotFoundException e) {
            result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.item_not_found);
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
            result.setError(PacketError.Condition.internal_server_error);
        }
    } else if (type.equals(IQ.Type.get)) {
        JID recipient = packet.getTo();
        // If no TO was specified then get the vCard of the sender of the packet
        if (recipient == null) {
            recipient = packet.getFrom();
        }
        // By default return an empty vCard
        result.setChildElement("vCard", "vcard-temp");
        // Only try to get the vCard values of non-anonymous users
        if (recipient != null) {
            if (recipient.getNode() != null && server.isLocal(recipient)) {
                VCardManager vManager = VCardManager.getInstance();
                Element userVCard = vManager.getVCard(recipient.getNode());
                if (userVCard != null) {
                    // Check if the requester wants to ignore some vCard's fields
                    Element filter = packet.getChildElement().element(QName.get("filter", "vcard-temp-filter"));
                    if (filter != null) {
                        // Create a copy so we don't modify the original vCard
                        userVCard = userVCard.createCopy();
                        // Ignore fields requested by the user
                        for (Iterator toFilter = filter.elementIterator(); toFilter.hasNext(); ) {
                            Element field = (Element) toFilter.next();
                            Element fieldToRemove = userVCard.element(field.getName());
                            if (fieldToRemove != null) {
                                fieldToRemove.detach();
                            }
                        }
                    }
                    result.setChildElement(userVCard);
                }
            } else {
                result = IQ.createResultIQ(packet);
                result.setChildElement(packet.getChildElement().createCopy());
                result.setError(PacketError.Condition.item_not_found);
            }
        } else {
            result = IQ.createResultIQ(packet);
            result.setChildElement(packet.getChildElement().createCopy());
            result.setError(PacketError.Condition.item_not_found);
        }
    } else {
        result.setChildElement(packet.getChildElement().createCopy());
        result.setError(PacketError.Condition.not_acceptable);
    }
    return result;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) IQ(org.xmpp.packet.IQ) Iterator(java.util.Iterator) VCardManager(org.jivesoftware.openfire.vcard.VCardManager) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 2 with VCardManager

use of org.jivesoftware.openfire.vcard.VCardManager in project Openfire by igniterealtime.

the class JustMarriedController method copyVCard.

/**
	 * Copy v card.
	 *
	 * @param currentUserName
	 *            the current user name
	 * @param newUserName
	 *            the new user name
	 * @throws ServiceException
	 *             the service exception
	 */
private static void copyVCard(String currentUserName, String newUserName) throws ServiceException {
    VCardManager vcardManager = VCardManager.getInstance();
    Element vcard = vcardManager.getVCard(currentUserName);
    if (vcard != null) {
        try {
            vcardManager.setVCard(newUserName, vcard);
        } catch (Exception e) {
            throw new ServiceException("Could not copy vcard to new user", newUserName, ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST, e);
        }
    }
}
Also used : ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) Element(org.dom4j.Element) VCardManager(org.jivesoftware.openfire.vcard.VCardManager) ServiceException(org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 3 with VCardManager

use of org.jivesoftware.openfire.vcard.VCardManager in project Openfire by igniterealtime.

the class JustMarriedPlugin method copyVCard.

private static void copyVCard(String currentUserName, String newUserName) {
    VCardManager vcardManager = VCardManager.getInstance();
    Element vcard = vcardManager.getVCard(currentUserName);
    if (vcard != null) {
        try {
            vcardManager.setVCard(newUserName, vcard);
        } catch (Exception e) {
            Log.error("Could not copy vcard to " + newUserName, e);
        }
    }
}
Also used : Element(org.dom4j.Element) VCardManager(org.jivesoftware.openfire.vcard.VCardManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Aggregations

Element (org.dom4j.Element)3 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)3 VCardManager (org.jivesoftware.openfire.vcard.VCardManager)3 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)2 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)2 Iterator (java.util.Iterator)1 PacketException (org.jivesoftware.openfire.PacketException)1 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)1 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)1 User (org.jivesoftware.openfire.user.User)1 IQ (org.xmpp.packet.IQ)1 JID (org.xmpp.packet.JID)1