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

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


        this.xmppSession = xmppSession;

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

                    if (!presenceMap.containsKey(presence.getFrom().asBareJid())) {
                        // Store the user (bare JID) in the map, associated with different resources.
                        presenceMap.put(presence.getFrom().asBareJid(), new ConcurrentHashMap<String, Presence>());
                    }
                    Map<String, Presence> presencesPerResource = presenceMap.get(presence.getFrom().asBareJid());
                    // Update the contact's resource with the presence.
                    presencesPerResource.put(presence.getFrom().getResource() != null ? presence.getFrom().getResource() : "", presence);
                } else {
                    // Store the last sent presences, in order to automatically resend them, after a disconnect.
                    if (presence.getType() == null || presence.getType() == Presence.Type.UNAVAILABLE) {
                        if (presence.getTo() == null) {
                            lastSentPresences.put("", presence);
                        } else {
                            lastSentPresences.put(presence.getTo().toString(), presence);
                        }
                    }
                }
            }
        });

        xmppSession.addSessionStatusListener(new SessionStatusListener() {
            @Override
            public void sessionStatusChanged(SessionStatusEvent e) {
                // Resend the last presences, as soon as we are reconnected.
                if (e.getStatus() == XmppSession.Status.AUTHENTICATED) {
                    for (Presence presence : lastSentPresences.values()) {
                        xmppSession.send(presence);
                    }
                }
                if (e.getStatus() == XmppSession.Status.DISCONNECTED) {
                    for (Contact contact : xmppSession.getRosterManager().getContacts()) {
                        try {
                            Presence presence = new Presence(Presence.Type.UNAVAILABLE);
                            presence.setFrom(contact.getJid());
                            xmppSession.handleElement(presence);
                        } catch (Exception e1) {
                            logger.log(Level.WARNING, e1.getMessage(), e1);
                        }
                    }
View Full Code Here


                }
            }
        } else {
            Map<String, Presence> presencesPerResource = presenceMap.get(jid.asBareJid());
            if (presencesPerResource != null) {
                Presence presence = presencesPerResource.get(jid.getResource());
                if (presence != null) {
                    return presence;
                }
            }
        }

        Presence presence = new Presence(Presence.Type.UNAVAILABLE);
        presence.setFrom(jid);
        return presence;
    }
View Full Code Here

     * @param jid    The contact's JID.
     * @param status The status, which is used for additional information during the subscription request.
     * @return The id, which is used for the request.
     */
    public String requestSubscription(Jid jid, String status) {
        Presence presence = new Presence(Presence.Type.SUBSCRIBE);
        // the value of the 'to' attribute MUST be a bare JID
        presence.setTo(jid.asBareJid());
        presence.setId(UUID.randomUUID().toString());
        presence.setStatus(status);
        xmppSession.send(presence);
        return presence.getId();
    }
View Full Code Here

     *
     * @param jid The contact's JID, who has previously requested a subscription.
     * @return The id, which is used for the approval.
     */
    public String approveSubscription(Jid jid) {
        Presence presence = new Presence(Presence.Type.SUBSCRIBED);
        // For tracking purposes, a client SHOULD include an 'id' attribute in a subscription approval or subscription denial; this 'id' attribute MUST NOT mirror the 'id' attribute of the subscription request.
        presence.setId(UUID.randomUUID().toString());
        presence.setTo(jid);
        xmppSession.send(presence);
        return presence.getId();
    }
View Full Code Here

    @Test
    public void marshalBodyWithMultipleStanzas() throws XMLStreamException, JAXBException {
        IQ iq = new IQ("1", IQ.Type.GET);
        iq.setExtension(new Roster());
        Body body = Body.builder()
                .wrappedObjects(Arrays.<Object>asList(iq, new Presence())).build();

        Assert.assertEquals(marshal(body), "<body xmlns=\"http://jabber.org/protocol/httpbind\"><iq xmlns=\"jabber:client\" id=\"1\" type=\"get\"><query xmlns=\"jabber:iq:roster\"></query></iq><presence xmlns=\"jabber:client\"></presence></body>");
    }
View Full Code Here

     *
     * @param jid The contact's JID, whose subscription is denied or canceled.
     * @return The id, which is used for the subscription denial.
     */
    public String denySubscription(Jid jid) {
        Presence presence = new Presence(Presence.Type.UNSUBSCRIBED);
        // For tracking purposes, a client SHOULD include an 'id' attribute in a subscription approval or subscription denial; this 'id' attribute MUST NOT mirror the 'id' attribute of the subscription request.
        presence.setId(UUID.randomUUID().toString());
        presence.setTo(jid);
        xmppSession.send(presence);
        return presence.getId();
    }
View Full Code Here

     *
     * @param jid The contact's JID.
     * @return The id, which is used for the unsubscription.
     */
    public String unsubscribe(Jid jid) {
        Presence presence = new Presence(Presence.Type.UNSUBSCRIBE);
        // For tracking purposes, a client SHOULD include an 'id' attribute in a subscription approval or subscription denial; this 'id' attribute MUST NOT mirror the 'id' attribute of the subscription request.
        presence.setId(UUID.randomUUID().toString());
        presence.setTo(jid);
        xmppSession.send(presence);
        return presence.getId();
    }
View Full Code Here

        };

        presenceListener = new PresenceListener() {
            @Override
            public void handle(PresenceEvent e) {
                Presence presence = e.getPresence();
                // If the presence came from the room.
                if (presence.getFrom() != null && presence.getFrom().asBareJid().equals(roomJid)) {
                    if (e.isIncoming()) {
                        MucUser mucUser = presence.getExtension(MucUser.class);
                        if (mucUser != null) {
                            String nick = presence.getFrom().getResource();

                            if (nick != null) {
                                boolean isSelfPresence = isSelfPresence(presence);
                                if (presence.isAvailable()) {
                                    Occupant occupant = new Occupant(presence, isSelfPresence);
                                    Occupant previousOccupant = occupantMap.put(nick, occupant);
                                    // A new occupant entered the room.
                                    if (previousOccupant == null) {
                                        // Only notify about "joins", if it's not our own join and we are already in the room.
                                        if (!isSelfPresence && entered) {
                                            notifyOccupantListeners(new OccupantEvent(ChatRoom.this, occupant, OccupantEvent.Type.ENTERED, null, null, null));
                                        }
                                    } else {
                                        notifyOccupantListeners(new OccupantEvent(ChatRoom.this, occupant, OccupantEvent.Type.STATUS_CHANGED, null, null, null));
                                    }
                                } else if (presence.getType() == Presence.Type.UNAVAILABLE) {
                                    // Occupant has exited the room.
                                    Occupant occupant = occupantMap.remove(nick);
                                    if (occupant != null) {
                                        if (mucUser.getItem() != null) {
                                            Actor actor = mucUser.getItem().getActor();
View Full Code Here

        try {
            xmppSession.addMessageListener(messageListener);
            xmppSession.addPresenceListener(presenceListener);

            final Presence enterPresence = new Presence();
            enterPresence.setTo(roomJid.withResource(nick));
            enterPresence.getExtensions().add(new Muc(password, history));
            this.nick = nick;
            xmppSession.sendAndAwaitPresence(enterPresence, new StanzaFilter<Presence>() {
                @Override
                public boolean accept(Presence presence) {
                    Jid room = presence.getFrom().asBareJid();
View Full Code Here

     */
    public synchronized void changeNickname(String newNickname) throws XmppException {
        if (!entered) {
            throw new IllegalStateException("You must have entered the room to change your nickname.");
        }
        final Presence changeNickNamePresence = new Presence();
        changeNickNamePresence.setTo(roomJid.withResource(newNickname));
        xmppSession.sendAndAwaitPresence(changeNickNamePresence, new StanzaFilter<Presence>() {
            @Override
            public boolean accept(Presence presence) {
                return presence.getFrom().equals(changeNickNamePresence.getTo());
            }
        });
    }
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.