Search in sources :

Example 76 with Message

use of org.jivesoftware.smack.packet.Message in project intellij-plugins by JetBrains.

the class JabberTransport method doSendMessage.

private Message doSendMessage(XmlMessage xmlMessage, User user, String threadId) {
    Element element = new Element(xmlMessage.getTagName(), xmlMessage.getTagNamespace());
    xmlMessage.fillRequest(element);
    Message message = createBaseMessage(user, element.getText());
    message.setThread(threadId);
    message.addExtension(new JDOMExtension(element));
    myFacade.getConnection().sendPacket(message);
    return message;
}
Also used : Message(org.jivesoftware.smack.packet.Message) Element(org.jdom.Element)

Example 77 with Message

use of org.jivesoftware.smack.packet.Message in project intellij-plugins by JetBrains.

the class JabberTransport method createBaseMessage.

static Message createBaseMessage(User user, String message) {
    Message msg = new Message(user.getName(), Message.Type.CHAT);
    msg.setBody(message);
    return msg;
}
Also used : Message(org.jivesoftware.smack.packet.Message)

Example 78 with Message

use of org.jivesoftware.smack.packet.Message in project camel by apache.

the class XmppBinding method extractHeadersFromXmpp.

public Map<String, Object> extractHeadersFromXmpp(Packet xmppPacket, Exchange exchange) {
    Map<String, Object> answer = new HashMap<String, Object>();
    PacketExtension jpe = xmppPacket.getExtension(JivePropertiesExtension.NAMESPACE);
    if (jpe != null && jpe instanceof JivePropertiesExtension) {
        extractHeadersFrom((JivePropertiesExtension) jpe, exchange, answer);
    }
    if (jpe != null && jpe instanceof DefaultPacketExtension) {
        extractHeadersFrom((DefaultPacketExtension) jpe, exchange, answer);
    }
    if (xmppPacket instanceof Message) {
        Message xmppMessage = (Message) xmppPacket;
        answer.put(XmppConstants.MESSAGE_TYPE, xmppMessage.getType());
        answer.put(XmppConstants.SUBJECT, xmppMessage.getSubject());
        answer.put(XmppConstants.THREAD_ID, xmppMessage.getThread());
    } else if (xmppPacket instanceof PubSub) {
        PubSub pubsubPacket = (PubSub) xmppPacket;
        answer.put(XmppConstants.MESSAGE_TYPE, pubsubPacket.getType());
    }
    answer.put(XmppConstants.FROM, xmppPacket.getFrom());
    answer.put(XmppConstants.PACKET_ID, xmppPacket.getPacketID());
    answer.put(XmppConstants.TO, xmppPacket.getTo());
    return answer;
}
Also used : PacketExtension(org.jivesoftware.smack.packet.PacketExtension) DefaultPacketExtension(org.jivesoftware.smack.packet.DefaultPacketExtension) PubSub(org.jivesoftware.smackx.pubsub.packet.PubSub) Message(org.jivesoftware.smack.packet.Message) HashMap(java.util.HashMap) DefaultPacketExtension(org.jivesoftware.smack.packet.DefaultPacketExtension) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension)

Example 79 with Message

use of org.jivesoftware.smack.packet.Message in project camel by apache.

the class XmppGroupChatProducer method process.

public void process(Exchange exchange) {
    if (connection == null) {
        try {
            connection = endpoint.createConnection();
        } catch (Exception e) {
            throw new RuntimeExchangeException("Could not connect to XMPP server.", exchange, e);
        }
    }
    if (chat == null) {
        try {
            initializeChat();
        } catch (Exception e) {
            throw new RuntimeExchangeException("Could not initialize XMPP chat.", exchange, e);
        }
    }
    Message message = chat.createMessage();
    message.setTo(room);
    message.setFrom(endpoint.getUser());
    endpoint.getBinding().populateXmppMessage(message, exchange);
    try {
        // make sure we are connected
        if (!connection.isConnected()) {
            this.reconnect();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending XMPP message: {}", message.getBody());
        }
        chat.sendMessage(message);
        // must invoke nextMessage to consume the response from the server
        // otherwise the client local queue will fill up (CAMEL-1467)
        chat.pollMessage();
    } catch (Exception e) {
        throw new RuntimeExchangeException("Could not send XMPP message: " + message, exchange, e);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 80 with Message

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

the class PacketParserUtilsTest method singleMessageBodyTest.

@Test
public void singleMessageBodyTest() throws Exception {
    String defaultLanguage = Stanza.getDefaultLanguage();
    String otherLanguage = determineNonDefaultLanguage();
    String control;
    // message has default language, body 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("body").t(defaultLanguage).asString(outputProperties);
    Message message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertTrue(message.getBodyLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertNull(message.getBody(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has non-default language, body 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("body").t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(otherLanguage, message.getBody());
    assertTrue(message.getBodyLanguages().isEmpty());
    assertEquals(otherLanguage, message.getBody(otherLanguage));
    assertNull(message.getBody(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, body 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("body").t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertTrue(message.getBodyLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertNull(message.getBody(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has no language, body 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("body").a("xml:lang", defaultLanguage).t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertEquals(defaultLanguage, message.getBody());
    assertTrue(message.getBodyLanguages().isEmpty());
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertNull(message.getBody(otherLanguage));
    // body attribute xml:lang is unnecessary
    assertXMLNotEqual(control, message.toXML().toString());
    // message has no language, body 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("body").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getBody());
    assertFalse(message.getBodyLanguages().isEmpty());
    assertTrue(message.getBodyLanguages().contains(otherLanguage));
    assertEquals(otherLanguage, message.getBody(otherLanguage));
    assertNull(message.getBody(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has default language, body 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("body").a("xml:lang", otherLanguage).t(otherLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getBody());
    assertFalse(message.getBodyLanguages().isEmpty());
    assertTrue(message.getBodyLanguages().contains(otherLanguage));
    assertEquals(otherLanguage, message.getBody(otherLanguage));
    assertNull(message.getBody(defaultLanguage));
    assertXMLEqual(control, message.toXML().toString());
    // message has non-default language, body 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("body").a("xml:lang", defaultLanguage).t(defaultLanguage).asString(outputProperties);
    message = PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
    assertNull(message.getBody());
    assertFalse(message.getBodyLanguages().isEmpty());
    assertTrue(message.getBodyLanguages().contains(defaultLanguage));
    assertEquals(defaultLanguage, message.getBody(defaultLanguage));
    assertNull(message.getBody(otherLanguage));
    assertXMLEqual(control, message.toXML().toString());
}
Also used : Message(org.jivesoftware.smack.packet.Message) Test(org.junit.Test)

Aggregations

Message (org.jivesoftware.smack.packet.Message)142 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)17 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 Stanza (org.jivesoftware.smack.packet.Stanza)12 XMPPException (org.jivesoftware.smack.XMPPException)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Date (java.util.Date)8 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)8 AccountItem (com.xabber.android.data.account.AccountItem)7 Chat (org.jivesoftware.smack.Chat)7 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 ArrayList (java.util.ArrayList)6 Jid (org.jxmpp.jid.Jid)6 NetworkException (com.xabber.android.data.NetworkException)5 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)5 GeoLocation (org.jivesoftware.smackx.geoloc.packet.GeoLocation)5 IOException (java.io.IOException)4