Search in sources :

Example 1 with DelayInformation

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

the class ForwardedProvider method parse.

@Override
public Forwarded parse(XmlPullParser parser, int initialDepth) throws Exception {
    DelayInformation di = null;
    Stanza packet = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(name) {
                    case DelayInformation.ELEMENT:
                        if (DelayInformation.NAMESPACE.equals(namespace)) {
                            di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth());
                        } else {
                            LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + DelayInformation.NAMESPACE + "'");
                        }
                        break;
                    case Message.ELEMENT:
                        packet = PacketParserUtils.parseMessage(parser);
                        break;
                    default:
                        LOGGER.warning("Unsupported forwarded packet type: " + name);
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    if (packet == null)
        throw new SmackException("forwarded extension must contain a packet");
    return new Forwarded(di, packet);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Stanza(org.jivesoftware.smack.packet.Stanza) SmackException(org.jivesoftware.smack.SmackException) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 2 with DelayInformation

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

the class DelayInformationTest method parsePresenceWithInvalidLegacyDelayed.

@Test
public void parsePresenceWithInvalidLegacyDelayed() throws Exception {
    String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>" + "<x xmlns='jabber:x:delay'/></presence>";
    Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
    DelayInformation delay = DelayInformationManager.getXep203DelayInformation(presence);
    assertNull(delay);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Presence(org.jivesoftware.smack.packet.Presence) Test(org.junit.Test)

Example 3 with DelayInformation

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

the class DelayInformationTest method legacyDateFormatsTest.

@Test
public void legacyDateFormatsTest() throws FactoryConfigurationError, XmlPullParserException, IOException, Exception {
    LegacyDelayInformationProvider p = new LegacyDelayInformationProvider();
    DelayInformation delayInfo;
    String control;
    // XEP-0091 date format
    control = XMLBuilder.create("x").a("xmlns", "jabber:x:delay").a("from", "capulet.com").a("stamp", "20020910T23:08:25").asString(outputProperties);
    delayInfo = p.parse(PacketParserUtils.getParserFor(control));
    assertEquals(calendar.getTime(), delayInfo.getStamp());
    // XEP-0091 date format without leading 0 in month
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMd'T'HH:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    GregorianCalendar dateInPast = new GregorianCalendar();
    if (dateInPast.get(Calendar.MONTH) >= 10) {
        dateInPast.set(Calendar.MONTH, dateInPast.get(Calendar.MONTH) - 3);
    }
    dateInPast.add(Calendar.DAY_OF_MONTH, -3);
    dateInPast.set(Calendar.MILLISECOND, 0);
    control = XMLBuilder.create("x").a("xmlns", "jabber:x:delay").a("from", "capulet.com").a("stamp", dateFormat.format(dateInPast.getTime())).asString(outputProperties);
    delayInfo = p.parse(PacketParserUtils.getParserFor(control));
    assertEquals(dateInPast.getTime(), delayInfo.getStamp());
    // XEP-0091 date format from SMACK-243
    control = XMLBuilder.create("x").a("xmlns", "jabber:x:delay").a("from", "capulet.com").a("stamp", "200868T09:16:20").asString(outputProperties);
    delayInfo = p.parse(PacketParserUtils.getParserFor(control));
    Date controlDate = XmppDateTime.parseDate("2008-06-08T09:16:20.0Z");
    assertEquals(controlDate, delayInfo.getStamp());
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) GregorianCalendar(java.util.GregorianCalendar) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 4 with DelayInformation

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

the class ForwardedTest method forwardedWithDelayTest.

@Test
public void forwardedWithDelayTest() throws Exception {
    XmlPullParser parser;
    String control;
    Forwarded fwd;
    // @formatter:off
    control = XMLBuilder.create("forwarded").a("xmlns", "urn:xmpp:forwarded:0").e("message").a("from", "romeo@montague.com").up().e("delay").ns(DelayInformation.NAMESPACE).a("stamp", "2010-07-10T23:08:25Z").asString(outputProperties);
    // @formatter:on
    parser = PacketParserUtils.getParserFor(control);
    fwd = new ForwardedProvider().parse(parser);
    // assert there is delay information in packet
    DelayInformation delay = fwd.getDelayInformation();
    assertNotNull(delay);
    // check message
    assertThat("romeo@montague.com", equalsCharSequence(fwd.getForwardedStanza().getFrom()));
    // check end of tag
    assertEquals(XmlPullParser.END_TAG, parser.getEventType());
    assertEquals("forwarded", parser.getName());
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) XmlPullParser(org.xmlpull.v1.XmlPullParser) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) ForwardedProvider(org.jivesoftware.smackx.forward.provider.ForwardedProvider) Test(org.junit.Test)

Example 5 with DelayInformation

use of org.jivesoftware.smackx.delay.packet.DelayInformation in project xabber-android by redsolution.

the class AbstractChat method sendQueue.

/**
     * Requests to send messages from queue.
     *
     * @param intent can be <code>null</code>.
     */
protected void sendQueue(MessageItem intent) {
    if (!canSendMessage())
        return;
    final ArrayList<MessageItem> sentMessages = new ArrayList<MessageItem>();
    final ArrayList<MessageItem> removeMessages = new ArrayList<MessageItem>();
    for (final MessageItem messageItem : sendQuery) {
        String text = prepareText(messageItem.getText());
        if (text == null) {
            messageItem.markAsError();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    if (messageItem.getId() != null)
                        MessageTable.getInstance().markAsError(messageItem.getId());
                }
            });
        } else {
            Message message = createMessagePacket(text);
            messageItem.setPacketID(message.getPacketID());
            ChatStateManager.getInstance().updateOutgoingMessage(this, message);
            ReceiptManager.getInstance().updateOutgoingMessage(this, message, messageItem);
            CarbonManager.getInstance().updateOutgoingMessage(this, message, messageItem);
            if (messageItem != intent)
                message.addExtension(new DelayInformation(messageItem.getTimestamp()));
            try {
                ConnectionManager.getInstance().sendStanza(account, message);
            } catch (NetworkException e) {
                break;
            }
        }
        if (MessageArchiveManager.getInstance().getSaveMode(account, user, threadId) == SaveMode.fls)
            messageItem.setTag(NO_RECORD_TAG);
        if (messageItem != intent) {
            messageItem.setSentTimeStamp(new Date());
            Collections.sort(messages);
        }
        messageItem.markAsSent();
        if (AccountManager.getInstance().getArchiveMode(messageItem.getChat().getAccount()).saveLocally())
            sentMessages.add(messageItem);
        else
            removeMessages.add(messageItem);
    }
    sendQuery.removeAll(sentMessages);
    sendQuery.removeAll(removeMessages);
    MessageManager.getInstance().onChatChanged(account, user, false);
    Application.getInstance().runInBackground(new Runnable() {

        @Override
        public void run() {
            Collection<Long> sentIds = MessageManager.getMessageIds(sentMessages, false);
            Collection<Long> removeIds = MessageManager.getMessageIds(removeMessages, true);
            MessageTable.getInstance().markAsSent(sentIds);
            MessageTable.getInstance().removeMessages(removeIds);
        }
    });
}
Also used : Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) ArrayList(java.util.ArrayList) Collection(java.util.Collection) NetworkException(com.xabber.android.data.NetworkException) Date(java.util.Date)

Aggregations

DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)11 Test (org.junit.Test)7 Date (java.util.Date)6 GregorianCalendar (java.util.GregorianCalendar)4 SmackException (org.jivesoftware.smack.SmackException)3 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)3 Message (org.jivesoftware.smack.packet.Message)2 Presence (org.jivesoftware.smack.packet.Presence)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 NetworkException (com.xabber.android.data.NetworkException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 DelayInformationProvider (org.jivesoftware.smackx.delay.provider.DelayInformationProvider)1 ForwardedProvider (org.jivesoftware.smackx.forward.provider.ForwardedProvider)1 MamResultExtension (org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension)1