Search in sources :

Example 1 with DelayInformation

use of org.jivesoftware.smackx.packet.DelayInformation in project Smack by igniterealtime.

the class MultiUserChatTest method testDiscussionHistory.

public void testDiscussionHistory() {
    try {
        // User1 sends some messages to the room
        muc.sendMessage("Message 1");
        muc.sendMessage("Message 2");
        // Wait 5 seconds before sending the last message
        Thread.sleep(5000);
        muc.sendMessage("Message 3");
        // User2 joins the room requesting to receive the messages of the last 2 seconds.
        MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
        DiscussionHistory history = new DiscussionHistory();
        history.setSeconds(2);
        muc2.join("testbot2", null, history, SmackConfiguration.getPacketReplyTimeout());
        Message msg;
        // Get first historic message
        msg = muc2.nextMessage(1000);
        assertNotNull("First message is null", msg);
        DelayInformation delay = DelayInformationManager.getDelayInformation(msg);
        assertNotNull("Message contains no delay information", delay);
        SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
        UTC_FORMAT.setTimeZone(TimeZone.getDefault());
        System.out.println(UTC_FORMAT.format(delay.getStamp()));
        assertEquals("Body of first message is incorrect", "Message 3", msg.getBody());
        // Try to get second historic message 
        msg = muc2.nextMessage(1000);
        assertNull("Second message is not null", msg);
        // User3 joins the room requesting to receive the last 2 messages.
        MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
        history = new DiscussionHistory();
        history.setMaxStanzas(2);
        muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout());
        // Get first historic message 
        msg = muc3.nextMessage(1000);
        assertNotNull("First message is null", msg);
        assertEquals("Body of first message is incorrect", "Message 2", msg.getBody());
        // Get second historic message 
        msg = muc3.nextMessage(1000);
        assertNotNull("Second message is null", msg);
        assertEquals("Body of second message is incorrect", "Message 3", msg.getBody());
        // Try to get third historic message 
        msg = muc3.nextMessage(1000);
        assertNull("Third message is not null", msg);
        // User2 leaves the room
        muc2.leave();
        // User3 leaves the room
        muc3.leave();
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.packet.DelayInformation) SimpleDateFormat(java.text.SimpleDateFormat) XMPPException(org.jivesoftware.smack.XMPPException)

Example 2 with DelayInformation

use of org.jivesoftware.smackx.packet.DelayInformation in project Openfire by igniterealtime.

the class XMPPListener method processMessage.

/**
     * Handles incoming messages.
     *
     * @param chat Chat instance this message is associated with.
     * @param message Message received.
     */
public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
    Log.debug("Received " + getSession().getTransport().getType().name() + " message: " + message.toXML());
    try {
        final BaseTransport<XMPPBuddy> transport = getSession().getTransport();
        final JID legacyJID = transport.convertIDToJID(message.getFrom());
        final JID localJID = getSession().getJID();
        final PacketExtension pe = message.getExtension("x", NameSpace.X_DELAY);
        final PacketExtension attExt = message.getExtension(AttentionExtension.ELEMENT_NAME, AttentionExtension.NAMESPACE);
        if (pe != null && pe instanceof DelayInformation) {
            DelayInformation di = (DelayInformation) pe;
            transport.sendOfflineMessage(localJID, legacyJID, message.getBody(), di.getStamp(), di.getReason());
        } else if (attExt != null && (attExt instanceof AttentionExtension)) {
            transport.sendAttentionNotification(localJID, legacyJID, message.getBody());
        } else {
            // see if we got sent chat state notifications
            final PacketExtension cse = message.getExtension("http://jabber.org/protocol/chatstates");
            if (cse != null && cse instanceof ChatStateExtension) {
                final String chatState = cse.getElementName();
                try {
                    final ChatStateType cst = ChatStateType.valueOf(ChatStateType.class, chatState);
                    switch(cst) {
                        case active:
                            // included with that chat message below.
                            if (message.getBody() == null || message.getBody().trim().length() == 0) {
                                transport.sendChatActiveNotification(localJID, legacyJID);
                            }
                            break;
                        case composing:
                            transport.sendComposingNotification(localJID, legacyJID);
                            break;
                        case gone:
                            transport.sendChatGoneNotification(localJID, legacyJID);
                            break;
                        case inactive:
                            transport.sendChatInactiveNotification(localJID, legacyJID);
                            break;
                        case paused:
                            transport.sendComposingPausedNotification(localJID, legacyJID);
                            break;
                        default:
                            Log.debug("Unexpected chat state recieved: " + cst);
                            break;
                    }
                } catch (IllegalArgumentException ex) {
                    Log.warn("Illegal chat state notification " + "received from legacy domain: " + chatState);
                }
            }
            if (message.getType() == Type.error) {
                Log.debug("Received an error message! Message: " + message.toXML());
                transport.sendMessage(localJID, legacyJID, message.getBody(), Message.Type.error);
            } else {
                transport.sendMessage(localJID, legacyJID, message.getBody());
            }
        }
    //            if (message.getProperty("time") == null || message.getProperty("time").equals("")) {
    //            }
    //            else {
    //                getSession().getTransport().sendOfflineMessage(
    //                        getSession().getJID(),
    //                        getSession().getTransport().convertIDToJID(message.getFrom()),
    //                        message.getBody(),
    //                        Message.Type.chat,
    //                        message.getProperty("time").toString()
    //                );
    //            }
    } catch (Exception ex) {
        Log.debug("E001:" + ex.getMessage(), ex);
    }
}
Also used : PacketExtension(org.jivesoftware.smack.packet.PacketExtension) IQWithPacketExtension(net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension) ChatStateType(net.sf.kraken.type.ChatStateType) JID(org.xmpp.packet.JID) DelayInformation(org.jivesoftware.smackx.packet.DelayInformation) ChatStateExtension(org.jivesoftware.smackx.packet.ChatStateExtension) AttentionExtension(net.sf.kraken.protocols.xmpp.packet.AttentionExtension)

Aggregations

DelayInformation (org.jivesoftware.smackx.packet.DelayInformation)2 SimpleDateFormat (java.text.SimpleDateFormat)1 AttentionExtension (net.sf.kraken.protocols.xmpp.packet.AttentionExtension)1 IQWithPacketExtension (net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension)1 ChatStateType (net.sf.kraken.type.ChatStateType)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Message (org.jivesoftware.smack.packet.Message)1 PacketExtension (org.jivesoftware.smack.packet.PacketExtension)1 ChatStateExtension (org.jivesoftware.smackx.packet.ChatStateExtension)1 JID (org.xmpp.packet.JID)1