Package rocks.xmpp.core.stanza.model.client

Examples of rocks.xmpp.core.stanza.model.client.Presence


                "     <error type='auth'>\n" +
                "       <registration-required\n" +
                "           xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>\n" +
                "     </error>\n" +
                "   </presence>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertNotNull(presence.getError());
        Assert.assertEquals(presence.getError().getType(), StanzaError.Type.AUTH);
        Assert.assertTrue(presence.getError().getCondition() instanceof RegistrationRequired);
    }
View Full Code Here


        String xml = "<presence from='romeo@example.net'" +
                "              id='xk3h1v69'\n" +
                "              to='juliet@example.com'\n" +
                "              type='subscribe'/>";

        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getTo().toString(), "juliet@example.com");
        Assert.assertEquals(presence.getFrom().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getType(), Presence.Type.SUBSCRIBE);
        Assert.assertEquals(presence.getId(), "xk3h1v69");
    }
View Full Code Here

                "      <error type='modify'>\n" +
                "        <remote-server-not-found\n" +
                "            xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>\n" +
                "      </error>\n" +
                "    </presence>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getTo().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getFrom().toString(), "juliet@example.com");
        Assert.assertEquals(presence.getType(), Presence.Type.ERROR);
        Assert.assertNotNull(presence.getError());
        Assert.assertNotNull(presence.getError().getCondition() instanceof RemoteServerNotFound);
    }
View Full Code Here

                        // Whenever the verification string has changed, publish the info node.
                        publishCapsNode();

                        // Resend presence. This manager will add the caps extension later.
                        PresenceManager presenceManager = xmppSession.getPresenceManager();
                        Presence lastPresence = presenceManager.getLastSentPresence();
                        Presence presence = new Presence();
                        presence.setError(lastPresence.getError());
                        presence.setFrom(lastPresence.getFrom());
                        presence.setPriority(lastPresence.getPriority());
                        presence.setLanguage(lastPresence.getLanguage());
                        presence.setShow(lastPresence.getShow());
                        presence.setStatus(lastPresence.getStatus());
                        presence.setTo(lastPresence.getTo());
                        xmppSession.send(presence);
                    }
                }
            }
        });

        xmppSession.addSessionStatusListener(new SessionStatusListener() {
            @Override
            public void sessionStatusChanged(SessionStatusEvent e) {
                if (e.getStatus() == XmppSession.Status.CLOSED) {
                    jidInfos.clear();
                    cache.clear();
                    capsSent = false;
                    currentVerificationString = null;
                }
            }
        });

        xmppSession.addPresenceListener(new PresenceListener() {
            @Override
            public void handle(PresenceEvent e) {
                Presence presence = e.getPresence();

                if (!e.isIncoming()) {
                    if (isEnabled() && presence.isAvailable() && presence.getTo() == null) {
                        try {
                            // Synchronize on sdm, to make sure no features/identities are added removed, while computing the hash.
                            synchronized (serviceDiscoveryManager) {
                                if (currentVerificationString == null) {
                                    recomputeVerificationString(HASH_ALGORITHM);
                                    publishCapsNode();
                                }
                                // a client SHOULD include entity capabilities with every presence notification it sends.
                                presence.getExtensions().add(new EntityCapabilities(getNode(), HASH_ALGORITHM, currentVerificationString));
                                capsSent = true;
                            }
                        } catch (NoSuchAlgorithmException e1) {
                            logger.log(Level.WARNING, e1.getMessage(), e1);
                        }
                    }
                } else {
                    EntityCapabilities entityCapabilities = presence.getExtension(EntityCapabilities.class);
                    if (entityCapabilities != null) {
                        synchronized (EntityCapabilitiesManager.this) {

                            Verification verification = new Verification(entityCapabilities.getHashingAlgorithm(), entityCapabilities.getVerificationString());
                            // Check if the verification string is already known.
                            if (entityCapabilities.getHashingAlgorithm() != null && cache.containsKey(verification)) {
                                InfoNode infoNode = cache.get(verification);
                                // If its known, just update the information for this entity.
                                jidInfos.put(presence.getFrom(), infoNode);
                            } else {
                                // 1. Verify that the <c/> element includes a 'hash' attribute. If it does not, ignore the 'ver'
                                String hashAlgorithm = entityCapabilities.getHashingAlgorithm();
                                if (hashAlgorithm != null) {
                                    try {
                                        // 3. If the value of the 'hash' attribute matches one of the processing application's supported hash functions, validate the verification string by doing the following:
                                        MessageDigest messageDigest = MessageDigest.getInstance(entityCapabilities.getHashingAlgorithm());

                                        try {
                                            // 3.1 Send a service discovery information request to the generating entity.
                                            // 3.2 Receive a service discovery information response from the generating entity.
                                            InfoNode infoDiscovery = serviceDiscoveryManager.discoverInformation(presence.getFrom(), entityCapabilities.getNode() + "#" + entityCapabilities.getVerificationString());
                                            // 3.3 If the response includes more than one service discovery identity with the same category/type/lang/name, consider the entire response to be ill-formed.
                                            // 3.4 If the response includes more than one service discovery feature with the same XML character data, consider the entire response to be ill-formed.
                                            // => not possible due to java.util.Set semantics and equals method.
                                            // If the response had duplicates, just check the hash.

                                            // 3.5 If the response includes more than one extended service discovery information form with the same FORM_TYPE or the FORM_TYPE field contains more than one <value/> element with different XML character data, consider the entire response to be ill-formed.
                                            List<String> ftValues = new ArrayList<>();
                                            for (DataForm dataForm : infoDiscovery.getExtensions()) {
                                                DataForm.Field formType = dataForm.findField("FORM_TYPE");
                                                // 3.6 If the response includes an extended service discovery information form where the FORM_TYPE field is not of type "hidden" or the form does not include a FORM_TYPE field, ignore the form but continue processing.
                                                if (formType != null && formType.getType() == DataForm.Field.Type.HIDDEN && !formType.getValues().isEmpty()) {
                                                    List<String> values = new ArrayList<>();
                                                    for (String value : formType.getValues()) {
                                                        if (values.contains(value)) {
                                                            // ill-formed
                                                            return;
                                                        }
                                                        values.add(value);
                                                    }
                                                    String value = formType.getValues().get(0);
                                                    if (ftValues.contains(value)) {
                                                        // ill-formed
                                                        return;
                                                    }
                                                    ftValues.add(value);
                                                }
                                            }

                                            // 3.7 If the response is considered well-formed, reconstruct the hash by using the service discovery information response to generate a local hash in accordance with the Generation Method).
                                            String verificationString = EntityCapabilities.getVerificationString(infoDiscovery, messageDigest);

                                            // 3.8 If the values of the received and reconstructed hashes match, the processing application MUST consider the result to be valid and SHOULD globally cache the result for all JabberIDs with which it communicates.
                                            if (verificationString.equals(entityCapabilities.getVerificationString())) {
                                                cache(new Verification(hashAlgorithm, verificationString), infoDiscovery);
                                            }
                                            jidInfos.put(presence.getFrom(), infoDiscovery);

                                            // 3.9 If the values of the received and reconstructed hashes do not match, the processing application MUST consider the result to be invalid and MUST NOT globally cache the verification string;

                                        } catch (XmppException e1) {
                                            logger.log(Level.WARNING, e1.getMessage(), e1);
                                        }
                                    } catch (NoSuchAlgorithmException e1) {
                                        // 2. If the value of the 'hash' attribute does not match one of the processing application's supported hash functions, do the following:
                                        try {
                                            // 2.1 Send a service discovery information request to the generating entity.
                                            // 2.2 Receive a service discovery information response from the generating entity.
                                            InfoNode infoNode = serviceDiscoveryManager.discoverInformation(presence.getFrom(), entityCapabilities.getNode());
                                            // 2.3 Do not validate or globally cache the verification string as described below; instead, the processing application SHOULD associate the discovered identity+features only with the JabberID of the generating entity.
                                            jidInfos.put(presence.getFrom(), infoNode);
                                        } catch (XmppException e2) {
                                            logger.log(Level.WARNING, e2.getMessage(), e2);
                                        }
                                    }
                                }
View Full Code Here

        xmppSession.addPresenceListener(new PresenceListener() {
            @Override
            public void handle(PresenceEvent e) {
                // If vCard based avatars are enabled.
                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

    private void resetHash() {
        // Remove our own hash and send an empty presence.
        // The lack of our own hash, will download the vCard and either broadcasts the image hash or an empty hash.
        userHashes.remove(xmppSession.getConnectedResource().asBareJid());
        Presence presence = xmppSession.getPresenceManager().getLastSentPresence();
        if (presence == null) {
            presence = new Presence();
        }
        presence.getExtensions().clear();
        xmppSession.send(presence);
    }
View Full Code Here

    public void unmarshalPresenceTypeSubscribed() throws XMLStreamException, JAXBException {
        String xml = "<presence from='juliet@example.com'\n" +
                "              id='xk3h1v69'\n" +
                "              to='romeo@example.net'\n" +
                "              type='subscribed'/>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getFrom().toString(), "juliet@example.com");
        Assert.assertEquals(presence.getTo().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getType(), Presence.Type.SUBSCRIBED);
        Assert.assertEquals(presence.getId(), "xk3h1v69");
    }
View Full Code Here

    @Test
    public void unmarshalPresenceTypeUnsubscribed() throws XMLStreamException, JAXBException {
        String xml = "<presence id='tb2m1b59'\n" +
                "              to='romeo@example.net'\n" +
                "              type='unsubscribed'/>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getTo().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getType(), Presence.Type.UNSUBSCRIBED);
        Assert.assertEquals(presence.getId(), "tb2m1b59");
    }
View Full Code Here

    @Test
    public void unmarshalPresenceTypeUnsubscribe() throws XMLStreamException, JAXBException {
        String xml = "<presence id='tb2m1b59'\n" +
                "              to='romeo@example.net'\n" +
                "              type='unsubscribe'/>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getTo().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getType(), Presence.Type.UNSUBSCRIBE);
        Assert.assertEquals(presence.getId(), "tb2m1b59");
    }
View Full Code Here

    @Test
    public void unmarshalPresenceTypeProbe() throws XMLStreamException, JAXBException {
        String xml = "<presence id='tb2m1b59'\n" +
                "              to='romeo@example.net'\n" +
                "              type='probe'/>";
        Presence presence = unmarshal(xml, Presence.class);
        Assert.assertEquals(presence.getTo().toString(), "romeo@example.net");
        Assert.assertEquals(presence.getType(), Presence.Type.PROBE);
        Assert.assertEquals(presence.getId(), "tb2m1b59");
    }
View Full Code Here

TOP

Related Classes of rocks.xmpp.core.stanza.model.client.Presence

Copyright © 2018 www.massapicom. 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.