Examples of AvatarUpdate


Examples of rocks.xmpp.extensions.vcard.avatar.model.AvatarUpdate

        super(AvatarUpdate.class);
    }

    @Test
    public void marshalVCardUpdateWithEmptyPhotoElement() throws JAXBException, XMLStreamException {
        AvatarUpdate avatarUpdate = new AvatarUpdate("");
        String xml = marshal(avatarUpdate);
        Assert.assertEquals("<x xmlns=\"vcard-temp:x:update\"><photo></photo></x>", xml);
    }
View Full Code Here

Examples of rocks.xmpp.extensions.vcard.avatar.model.AvatarUpdate

        Assert.assertEquals("<x xmlns=\"vcard-temp:x:update\"><photo></photo></x>", xml);
    }

    @Test
    public void marshalVCardUpdateWithNoPhotoElement() throws JAXBException, XMLStreamException {
        AvatarUpdate avatarUpdate = new AvatarUpdate();
        String xml = marshal(avatarUpdate);
        Assert.assertEquals("<x xmlns=\"vcard-temp:x:update\"></x>", xml);
    }
View Full Code Here

Examples of rocks.xmpp.extensions.vcard.avatar.model.AvatarUpdate

                if (isEnabled()) {
                    final Presence presence = e.getPresence();
                    if (e.isIncoming()) {

                        // If the presence has an avatar update information.
                        final AvatarUpdate avatarUpdate = presence.getExtension(AvatarUpdate.class);

                        // 4.3 Multiple Resources
                        if (presence.getFrom().asBareJid().equals(xmppSession.getConnectedResource().asBareJid()) && presence.getFrom().getResource() != null && !presence.getFrom().getResource().equals(xmppSession.getConnectedResource().getResource())) {
                            // We received a presence stanza from another resource of our own JID.

                            if (avatarUpdate == null) {
                                // 1. If the presence stanza received from the other resource does not contain the update child element, then the other resource does not support vCard-based avatars.
                                // That resource could modify the contents of the vCard (including the photo element);
                                // because polling for vCard updates is not allowed, the client MUST stop advertising the avatar image hash.
                                if (presence.isAvailable()) {
                                    nonConformingResources.add(presence.getFrom().getResource());
                                }
                                // However, the client MAY reset its hash if all instances of non-conforming resources have gone offline.
                                else if (presence.getType() == Presence.Type.UNAVAILABLE && nonConformingResources.remove(presence.getFrom().getResource()) && nonConformingResources.isEmpty()) {
                                    resetHash();
                                }
                            } else {
                                // If the presence stanza received from the other resource contains the update child element, then the other resource conforms to the protocol for vCard-based avatars. There are three possible scenarios.
                                // If the update child element contains a non-empty photo element, then the client MUST compare the image hashes.
                                if (avatarUpdate.getHash() != null && !avatarUpdate.getHash().equals(userHashes.get(xmppSession.getConnectedResource().asBareJid()))) {
                                    // If the hashes are different, then the client MUST NOT attempt to resolve the conflict by uploading its avatar image again. Instead, it MUST defer to the content of the retrieved vCard by resetting its image hash
                                    resetHash();
                                }
                            }
                        }

                        if (avatarUpdate != null && avatarUpdate.getHash() != null) {
                            final Jid contact;
                            MucUser mucUser = presence.getExtension(MucUser.class);
                            if (mucUser != null) {
                                if (mucUser.getItem() != null && mucUser.getItem().getJid() != null) {
                                    contact = mucUser.getItem().getJid().asBareJid();
                                } else {
                                    // Ignore presence received from anonymous MUC room.
                                    return;
                                }
                            } else {
                                contact = presence.getFrom().asBareJid();
                            }
                            // If the user sends the same hash as we already know, it's the same avatar. Therefore do nothing.
                            if (!avatarUpdate.getHash().equals(userHashes.put(contact, avatarUpdate.getHash()))) {
                                // When the recipient's client receives the hash of the avatar image, it SHOULD check the hash to determine if it already has a cached copy of that avatar image.
                                byte[] imageData = loadFromCache(avatarUpdate.getHash());
                                byte[] avatar = null;
                                if (imageData != null) {
                                    avatar = imageData;
                                }
                                if (avatar != null) {
                                    notifyListeners(contact, avatar);
                                } else {
                                    // If not, it retrieves the sender's full vCard
                                    avatarRequester.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                // If the avatar was either known before or could be successfully retrieved from the vCard.
                                                notifyListeners(contact, getAvatarByVCard(contact));
                                            } catch (XmppException e1) {
                                                logger.log(Level.WARNING, e1.getMessage(), e1);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    } else if (presence.isAvailable() && nonConformingResources.isEmpty()) {
                        // 1. If a client supports the protocol defined herein, it MUST include the update child element in every presence broadcast it sends and SHOULD also include the update child in directed presence stanzas.

                        String myHash = userHashes.get(xmppSession.getConnectedResource().asBareJid());

                        if (myHash == null) {
                            // 2. If a client is not yet ready to advertise an image, it MUST send an empty update child element:
                            presence.getExtensions().add(new AvatarUpdate());

                            // Load my own avatar in order to advertise an image.
                            avatarRequester.execute(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        getAvatarByVCard(xmppSession.getConnectedResource().asBareJid());
                                        // If the client subsequently obtains an avatar image (e.g., by updating or retrieving the vCard), it SHOULD then publish a new <presence/> stanza with character data in the <photo/> element.
                                        Presence lastSentPresence = xmppSession.getPresenceManager().getLastSentPresence();
                                        Presence presence = new Presence();
                                        if (lastSentPresence != null) {
                                            presence.setPriority(lastSentPresence.getPriority());
                                            presence.getStatuses().addAll(lastSentPresence.getStatuses());
                                            presence.setShow(lastSentPresence.getShow());
                                            presence.setLanguage(lastSentPresence.getLanguage());
                                        }
                                        // Send out a presence, which will be filled with the extension later, because we now know or own avatar and have the hash for it.
                                        xmppSession.send(presence);
                                    } catch (XmppException e1) {
                                        logger.log(Level.WARNING, e1.getMessage(), e1);
                                    }
                                }
                            });

                        } else if (presence.getExtension(AvatarUpdate.class) == null) {
                            presence.getExtensions().add(new AvatarUpdate(myHash));
                        }
                    }
                }
            }
        });
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.