Search in sources :

Example 31 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class FromMatchesFilterTest method fullCompareMatchingEntityFullJid.

@Test
public void fullCompareMatchingEntityFullJid() {
    FromMatchesFilter filter = FromMatchesFilter.createFull(FULL_JID1_R1);
    Stanza packet = new Message();
    packet.setFrom(FULL_JID1_R1);
    assertTrue(filter.accept(packet));
    packet.setFrom(BASE_JID1);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID1_R2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID3);
    assertFalse(filter.accept(packet));
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 32 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class PacketParserUtils method parseMessage.

/**
     * Parses a message packet.
     *
     * @param parser the XML parser, positioned at the start of a message packet.
     * @return a Message packet.
     * @throws Exception 
     */
public static Message parseMessage(XmlPullParser parser) throws Exception {
    ParserUtils.assertAtStartTag(parser);
    assert (parser.getName().equals(Message.ELEMENT));
    final int initialDepth = parser.getDepth();
    Message message = new Message();
    message.setStanzaId(parser.getAttributeValue("", "id"));
    message.setTo(ParserUtils.getJidAttribute(parser, "to"));
    message.setFrom(ParserUtils.getJidAttribute(parser, "from"));
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null) {
        message.setType(Message.Type.fromString(typeString));
    }
    String language = getLanguageAttribute(parser);
    // determine message's default language
    String defaultLanguage = null;
    if (language != null && !"".equals(language.trim())) {
        message.setLanguage(language);
        defaultLanguage = language;
    } else {
        defaultLanguage = Stanza.getDefaultLanguage();
    }
    // Parse sub-elements. We include extra logic to make sure the values
    // are only read once. This is because it's possible for the names to appear
    // in arbitrary sub-elements.
    String thread = null;
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "subject":
                        String xmlLangSubject = getLanguageAttribute(parser);
                        if (xmlLangSubject == null) {
                            xmlLangSubject = defaultLanguage;
                        }
                        String subject = parseElementText(parser);
                        if (message.getSubject(xmlLangSubject) == null) {
                            message.addSubject(xmlLangSubject, subject);
                        }
                        break;
                    case Message.BODY:
                        String xmlLang = getLanguageAttribute(parser);
                        if (xmlLang == null) {
                            xmlLang = defaultLanguage;
                        }
                        String body = parseElementText(parser);
                        if (message.getBody(xmlLang) == null) {
                            message.addBody(xmlLang, body);
                        }
                        break;
                    case "thread":
                        if (thread == null) {
                            thread = parser.nextText();
                        }
                        break;
                    case "error":
                        message.setError(parseError(parser));
                        break;
                    default:
                        PacketParserUtils.addExtensionElement(message, parser, elementName, namespace);
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    message.setThread(thread);
    return message;
}
Also used : Message(org.jivesoftware.smack.packet.Message)

Example 33 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class PacketParserUtilsTest method multipleMessageBodiesTest.

@Test
public void multipleMessageBodiesTest() throws Exception {
    String defaultLanguage = Stanza.getDefaultLanguage();
    String otherLanguage = determineNonDefaultLanguage();
    String control;
    Message message;
    // message has default language, first body no language, second body other language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", defaultLanguage).e("body").t(defaultLanguage).up().e("body").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertEquals(otherLanguage, message.getBody(otherLanguage));
    assertEquals(2, message.getBodies().size());
    assertEquals(1, message.getBodyLanguages().size());
    assertTrue(message.getBodyLanguages().contains(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has default language, first body no language, second body default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", defaultLanguage).e("body").t(defaultLanguage).up().e("body").a("xml:lang", defaultLanguage).t(defaultLanguage + "2").asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertEquals(1, message.getBodies().size());
    assertEquals(0, message.getBodyLanguages().size());
    assertXMLNotEqual(control, message.toXML().toString());
    // message has non-default language, first body no language, second body default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", otherLanguage).e("body").t(otherLanguage).up().e("body").a("xml:lang", defaultLanguage).t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(otherLanguage, message.getBody());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertEquals(2, message.getBodies().size());
    assertEquals(1, message.getBodyLanguages().size());
    assertTrue(message.getBodyLanguages().contains(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, first body no language, second body default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("body").t(defaultLanguage).up().e("body").a("xml:lang", defaultLanguage).t(defaultLanguage + "2").asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertEquals(1, message.getBodies().size());
    assertEquals(0, message.getBodyLanguages().size());
    assertXMLNotEqual(control, message.toXML().toString());
    // message has no language, first body no language, second body other language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("body").t(defaultLanguage).up().e("body").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertEquals(otherLanguage, message.getBody(otherLanguage));
    assertEquals(2, message.getBodies().size());
    assertEquals(1, message.getBodyLanguages().size());
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, first body no language, second body no language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("body").t(defaultLanguage).up().e("body").t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertEquals(1, message.getBodies().size());
    assertEquals(0, message.getBodyLanguages().size());
    assertXMLNotEqual(control, message.toXML().toString());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Example 34 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class PacketParserUtilsTest method singleMessageSubjectTest.

@Test
public void singleMessageSubjectTest() throws Exception {
    String defaultLanguage = Stanza.getDefaultLanguage();
    String otherLanguage = determineNonDefaultLanguage();
    String control;
    // message has default language, subject has no language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", defaultLanguage).e("subject").t(defaultLanguage).asString(outputProperties);
    Message message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getSubject());
    assertTrue(message.getSubjectLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
    assertNull(message.getSubject(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has non-default language, subject has no language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", otherLanguage).e("subject").t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(otherLanguage, message.getSubject());
    assertTrue(message.getSubjectLanguages().isEmpty());
    assertEquals(otherLanguage, message.getSubject(otherLanguage));
    assertNull(message.getSubject(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, subject has no language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("subject").t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getSubject());
    assertTrue(message.getSubjectLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
    assertNull(message.getSubject(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, subject has default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("subject").a("xml:lang", defaultLanguage).t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getSubject());
    assertTrue(message.getSubjectLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
    assertNull(message.getSubject(otherLanguage));
    // subject attribute xml:lang is unnecessary
    assertXMLNotEqual(control, message.toXML().toString());
    // message has no language, subject has non-default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").e("subject").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getSubject());
    assertFalse(message.getSubjectLanguages().isEmpty());
    assertTrue(message.getSubjectLanguages().contains(otherLanguage));
    assertEquals(otherLanguage, message.getSubject(otherLanguage));
    assertNull(message.getSubject(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has default language, subject has non-default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", defaultLanguage).e("subject").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getSubject());
    assertFalse(message.getSubjectLanguages().isEmpty());
    assertTrue(message.getSubjectLanguages().contains(otherLanguage));
    assertEquals(otherLanguage, message.getSubject(otherLanguage));
    assertNull(message.getSubject(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has non-default language, subject has default language
    control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", otherLanguage).e("subject").a("xml:lang", defaultLanguage).t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getSubject());
    assertFalse(message.getSubjectLanguages().isEmpty());
    assertTrue(message.getSubjectLanguages().contains(defaultLanguage));
    assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
    assertNull(message.getSubject(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Example 35 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class PacketParserUtilsTest method invalidMessageBodyContainingTagTest.

/**
     * RFC6121 5.2.3 explicitly disallows mixed content in <body/> elements. Make sure that we throw
     * an exception if we encounter such an element.
     * 
     * @throws Exception
     */
@Test(expected = XmlPullParserException.class)
public void invalidMessageBodyContainingTagTest() throws Exception {
    String control = XMLBuilder.create("message").a("from", "romeo@montague.lit/orchard").a("to", "juliet@capulet.lit/balcony").a("id", "zid615d9").a("type", "chat").a("xml:lang", "en").e("body").a("xmlns", "http://www.w3.org/1999/xhtml").e("span").a("style", "font-weight: bold;").t("Bad Message Body").asString(outputProperties);
    Message message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
    fail("Should throw exception. Instead got message: " + message.toXML().toString());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Aggregations

Message (org.jivesoftware.smack.packet.Message)166 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)21 XMPPException (org.jivesoftware.smack.XMPPException)15 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 NetworkException (com.xabber.android.data.NetworkException)13 Stanza (org.jivesoftware.smack.packet.Stanza)13 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)13 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)12 AccountItem (com.xabber.android.data.account.AccountItem)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)11 Date (java.util.Date)10 Jid (org.jxmpp.jid.Jid)10 Chat (org.jivesoftware.smack.Chat)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 ArrayList (java.util.ArrayList)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 InputStream (java.io.InputStream)6 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)6