Java Code Examples for org.xmpp.packet.Message#setType()

The following examples show how to use org.xmpp.packet.Message#setType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreGroupChatMessages() {
    // XEP-0160: "groupchat" message types SHOULD NOT be stored offline
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 2
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStoreEmptyChatMessagesWithOtherExtensions() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    PacketExtension chatState = new PacketExtension("composing", "http://jabber.org/protocol/chatstates");
    message.addExtension(chatState);
    PacketExtension packetExtension2 = new PacketExtension("received", "urn:xmpp:receipts");
    message.addExtension(packetExtension2);
    assertTrue(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 3
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreEmptyChatMessagesWithOnlyChatStatesAndThread() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    message.setThread("1234");
    PacketExtension chatState = new PacketExtension("composing", "http://jabber.org/protocol/chatstates");
    message.addExtension(chatState);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 4
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreEmptyChatMessagesWithOnlyChatStates() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    PacketExtension chatState = new PacketExtension("composing", "http://jabber.org/protocol/chatstates");
    message.addExtension(chatState);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 5
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStoreNonEmptyChatMessages() {
    // XEP-0160: "chat" message types SHOULD be stored offline unless they only contain chat state notifications
    Message message = new Message();
    message.setType(Message.Type.chat);
    message.setBody(" ");
    assertTrue(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 6
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreEmptyChatMessages() {
    // XEP-0160: "chat" message types SHOULD be stored offline unless they only contain chat state notifications
    Message message = new Message();
    message.setType(Message.Type.chat);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 7
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStoreNormalMessages() {
    // XEP-0160: Messages with a 'type' attribute whose value is "normal" (or messages with no 'type' attribute) SHOULD be stored offline.
    Message message = new Message();
    message.setType(Message.Type.normal);
    assertTrue(OfflineMessageStore.shouldStoreMessage(message));

    Message message2 = new Message();
    assertTrue(OfflineMessageStore.shouldStoreMessage(message2));
}
 
Example 8
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreErrorMessages() {
    // XEP-0160: "error" message types SHOULD NOT be stored offline,
    Message message = new Message();
    message.setType(Message.Type.error);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 9
Source File: OfflineMessageStoreTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotStoreHeadlineMessages() {
    // XEP-0160: "headline" message types SHOULD NOT be stored offline
    Message message = new Message();
    message.setType(Message.Type.headline);
    assertFalse(OfflineMessageStore.shouldStoreMessage(message));
}
 
Example 10
Source File: ForwardTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void testForwarded() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    message.setBody("Tests");
    message.addExtension(new DataForm(DataForm.Type.submit));

    Forwarded forwarded = new Forwarded(message);
    Forwarded forwarded2 = new Forwarded(message);
    String xml1 = forwarded.getElement().asXML();
    String xml2 = forwarded2.getElement().asXML();
    assertEquals("<forwarded xmlns=\"urn:xmpp:forward:0\"><message xmlns=\"jabber:client\" type=\"chat\"><body>Tests</body><x xmlns=\"jabber:x:data\" type=\"submit\"/></message></forwarded>", xml1);
    assertEquals("<forwarded xmlns=\"urn:xmpp:forward:0\"><message xmlns=\"jabber:client\" type=\"chat\"><body>Tests</body><x xmlns=\"jabber:x:data\" type=\"submit\"/></message></forwarded>", xml2);
}
 
Example 11
Source File: MessageCarbonsTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void testReceived() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    message.setBody("Tests");

    Forwarded forwarded = new Forwarded(message);

    Received received = new Received(forwarded);
    String xml = received.getElement().asXML();
    assertEquals("<received xmlns=\"urn:xmpp:carbons:2\"><forwarded xmlns=\"urn:xmpp:forward:0\"><message xmlns=\"jabber:client\" type=\"chat\"><body>Tests</body></message></forwarded></received>", xml);
}
 
Example 12
Source File: MessageCarbonsTest.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Test
public void testSent() {
    Message message = new Message();
    message.setType(Message.Type.chat);
    message.setBody("Tests");

    Forwarded forwarded = new Forwarded(message);

    Sent sent = new Sent(forwarded);
    String xml = sent.getElement().asXML();
    assertEquals("<sent xmlns=\"urn:xmpp:carbons:2\"><forwarded xmlns=\"urn:xmpp:forward:0\"><message xmlns=\"jabber:client\" type=\"chat\"><body>Tests</body></message></forwarded></sent>", xml);
}
 
Example 13
Source File: LocalMUCRoom.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public void serverBroadcast(String msg) {
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    message.setBody(msg);
    message.setFrom(role.getRoleAddress());
    broadcast(message, role);
}
 
Example 14
Source File: IQMUCvCardHandler.java    From Openfire with Apache License 2.0 5 votes vote down vote up
private void sendConfigChangeNotification( final MUCRoom room )
{
    Log.debug("Sending configuration change notification to all occupants of room {}", room.getName());
    final Message notification = new Message();
    notification.setType(Message.Type.groupchat);
    notification.setFrom(room.getJID());
    final Element x = notification.addChildElement("x", "http://jabber.org/protocol/muc#user");
    final Element status = x.addElement("status");
    status.addAttribute("code", "104");

    for ( final MUCRole occupant : room.getOccupants() )
    {
        occupant.send(notification);
    }
}
 
Example 15
Source File: WeatherComponent.java    From Whack with Apache License 2.0 4 votes vote down vote up
/**
 * Handle the receied packet and answer the weather information of the requested station id.
 * The request must be made using Message packets where the body of the message should be the
 * station id.<p>
 *
 * Note: I don't know the list of valid station ids so if you find the list please send it to me
 * so I can add it to this example.
 *
 * @param packet the Message requesting information about a certain station id.
 */
public void processPacket(Packet packet) {
    System.out.println("Received package:"+packet.toXML());
    // Only process Message packets
    if (packet instanceof Message) {
        // Get the requested station to obtain it's weather information
        Message message = (Message) packet;
        String station = message.getBody();
        // Send the request and get the weather information
        Metar metar = Weather.getMetar(station, 5000);

        // Build the answer
        Message reply = new Message();
        reply.setTo(message.getFrom());
        reply.setFrom(message.getTo());
        reply.setType(message.getType());
        reply.setThread(message.getThread());

        // Append the discovered information if something was found
        if (metar != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("station id : " + metar.getStationID());
            sb.append("\rwind dir   : " + metar.getWindDirection() + " degrees");
            sb.append("\rwind speed : " + metar.getWindSpeedInMPH() + " mph, " +
                                                 metar.getWindSpeedInKnots() + " knots");
            if (!metar.getVisibilityLessThan()) {
                sb.append("\rvisibility : " + metar.getVisibility() + " mile(s)");
            } else {
                sb.append("\rvisibility : < " + metar.getVisibility() + " mile(s)");
            }

            sb.append("\rpressure   : " + metar.getPressure() + " in Hg");
            sb.append("\rtemperaturePrecise: " +
                               metar.getTemperaturePreciseInCelsius() + " C, " +
                               metar.getTemperaturePreciseInFahrenheit() + " F");
            sb.append("\rtemperature: " +
                               metar.getTemperatureInCelsius() + " C, " +
                               metar.getTemperatureInFahrenheit() + " F");
            sb.append("\rtemperatureMostPrecise: " +
                               metar.getTemperatureMostPreciseInCelsius() + " C, " +
                               metar.getTemperatureMostPreciseInFahrenheit() + " F");
            reply.setBody(sb.toString());
        }
        else {
            // Answer that the requested station id does not exist
            reply.setBody("Unknown station ID");
        }

        // Send the response to the sender of the request
        try {
            ComponentManagerFactory.getComponentManager().sendPacket(this, reply);
        } catch (ComponentException e) {
            e.printStackTrace();
        }
    }
}
 
Example 16
Source File: RoutingTableImpl.java    From Openfire with Apache License 2.0 4 votes vote down vote up
private void ccMessage(JID originalRecipient, Message message) {
    // We don't want to CC a message that is already a CC
    final Element receivedElement = message.getChildElement(Received.NAME, Received.NAMESPACE);
    final boolean isCC = receivedElement != null;
    if (message.getType() == Message.Type.chat && !isCC) {
        List<JID> routes = getRoutes(originalRecipient.asBareJID(), null);
        for (JID ccJid : routes) {
            // The receiving server MUST NOT send a forwarded copy to the full JID the original <message/> stanza was addressed to, as that recipient receives the original <message/> stanza.
            if (!ccJid.equals(originalRecipient)) {
                ClientSession clientSession = getClientRoute(ccJid);
                if (clientSession.isMessageCarbonsEnabled()) {
                    Message carbon = new Message();
                    // The wrapping message SHOULD maintain the same 'type' attribute value;
                    carbon.setType(message.getType());
                    // the 'from' attribute MUST be the Carbons-enabled user's bare JID
                    carbon.setFrom(ccJid.asBareJID());
                    // and the 'to' attribute MUST be the full JID of the resource receiving the copy
                    carbon.setTo(ccJid);
                    // The content of the wrapping message MUST contain a <received/> element qualified by the namespace "urn:xmpp:carbons:2", which itself contains a <forwarded/> element qualified by the namespace "urn:xmpp:forward:0" that contains the original <message/>.
                    carbon.addExtension(new Received(new Forwarded(message)));

                    try {
                        final RoutableChannelHandler localRoute = localRoutingTable.getRoute(ccJid);
                        if (localRoute != null) {
                            // This session is on a local cluster node
                            localRoute.process(carbon);
                        } else {
                            // The session is not on a local cluster node, so try a remote
                            final ClientRoute remoteRoute = getClientRouteForLocalUser(ccJid);
                            if (remotePacketRouter != null // If we're in a cluster
                                && remoteRoute != null // and we've found a route to the other node
                                && !remoteRoute.getNodeID().equals(XMPPServer.getInstance().getNodeID())) { // and it really is a remote node
                                // Try and route the packet to the remote session
                                remotePacketRouter.routePacket(remoteRoute.getNodeID().toByteArray(), ccJid, carbon);
                            } else {
                                Log.warn("Unable to find route to CC remote user {}", ccJid);
                            }
                        }
                    } catch (UnauthorizedException e) {
                        Log.error("Unable to route packet {}", message, e);
                    }
                }
            }
        }
    }
}
 
Example 17
Source File: MUCRoomHistory.java    From Openfire with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new message and adds it to the history. The new message will be created based on
 * the provided information. This information will likely come from the database when loading
 * the room history from the database.
 *
 * @param senderJID the sender's JID of the message to add to the history.
 * @param nickname the sender's nickname of the message to add to the history.
 * @param sentDate the date when the message was sent to the room.
 * @param subject the subject included in the message.
 * @param body the body of the message.
 * @param stanza the stanza to add
 */
public void addOldMessage(String senderJID, String nickname, Date sentDate, String subject,
        String body, String stanza)
{
    Message message = new Message();
    message.setType(Message.Type.groupchat);
    if (stanza != null) {
        // payload initialized as XML string from DB
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        try {
            Element element = xmlReader.read(new StringReader(stanza)).getRootElement();
            for (Element child : (List<Element>)element.elements()) {
                Namespace ns = child.getNamespace();
                if (ns == null || ns.getURI().equals("jabber:client") || ns.getURI().equals("jabber:server")) {
                    continue;
                }
                Element added = message.addChildElement(child.getName(), child.getNamespaceURI());
                if (!child.getText().isEmpty()) {
                    added.setText(child.getText());
                }
                for (Attribute attr : (List<Attribute>)child.attributes()) {
                    added.addAttribute(attr.getQName(), attr.getValue());
                }
                for (Element el : (List<Element>)child.elements()) {
                    added.add(el.createCopy());
                }
            }
            if (element.attribute("id") != null) {
                message.setID(element.attributeValue("id"));
            }
        } catch (Exception ex) {
            Log.error("Failed to parse payload XML", ex);
        }
    }
    message.setSubject(subject);
    message.setBody(body);
    // Set the sender of the message
    if (nickname != null && nickname.trim().length() > 0) {
        JID roomJID = room.getRole().getRoleAddress();
        // Recreate the sender address based on the nickname and room's JID
        message.setFrom(new JID(roomJID.getNode(), roomJID.getDomain(), nickname, true));
    }
    else {
        // Set the room as the sender of the message
        message.setFrom(room.getRole().getRoleAddress());
    }

    // Add the delay information to the message
    Element delayInformation = message.addChildElement("delay", "urn:xmpp:delay");
    delayInformation.addAttribute("stamp", XMPPDateTimeFormat.format(sentDate));
    if (room.canAnyoneDiscoverJID()) {
        // Set the Full JID as the "from" attribute
        delayInformation.addAttribute("from", senderJID);
    }
    else {
        // Set the Room JID as the "from" attribute
        delayInformation.addAttribute("from", room.getRole().getRoleAddress().toString());
    }
    historyStrategy.addMessage(message);
}
 
Example 18
Source File: WeatherComponent.java    From Whack with Apache License 2.0 4 votes vote down vote up
/**
 * Handle a receied message and answer the weather information of the requested station id.
 * The request must be made using Message packets where the body of the message should be the
 * station id.<p>
 *
 * Note: I don't know the list of valid station ids so if you find the list please send it to me
 * so I can add it to this example.
 *
 * @param message the Message requesting information about a certain station id.
 */
@Override
protected void handleMessage(Message message) {
    System.out.println("Received message:"+message.toXML());
    // Get the requested station to obtain it's weather information
    String station = message.getBody();
    // Send the request and get the weather information
    Metar metar = Weather.getMetar(station, 5000);

    // Build the answer
    Message reply = new Message();
    reply.setTo(message.getFrom());
    reply.setFrom(message.getTo());
    reply.setType(message.getType());
    reply.setThread(message.getThread());

    // Append the discovered information if something was found
    if (metar != null) {
        StringBuilder sb = new StringBuilder();
        sb.append("station id : " + metar.getStationID());
        sb.append("\rwind dir   : " + metar.getWindDirection() + " degrees");
        sb.append("\rwind speed : " + metar.getWindSpeedInMPH() + " mph, " +
                                             metar.getWindSpeedInKnots() + " knots");
        if (!metar.getVisibilityLessThan()) {
            sb.append("\rvisibility : " + metar.getVisibility() + " mile(s)");
        }
        else {
            sb.append("\rvisibility : < " + metar.getVisibility() + " mile(s)");
        }

        sb.append("\rpressure   : " + metar.getPressure() + " in Hg");
        sb.append("\rtemperaturePrecise: " +
                           metar.getTemperaturePreciseInCelsius() + " C, " +
                           metar.getTemperaturePreciseInFahrenheit() + " F");
        sb.append("\rtemperature: " +
                           metar.getTemperatureInCelsius() + " C, " +
                           metar.getTemperatureInFahrenheit() + " F");
        sb.append("\rtemperatureMostPrecise: " +
                           metar.getTemperatureMostPreciseInCelsius() + " C, " +
                           metar.getTemperatureMostPreciseInFahrenheit() + " F");
        reply.setBody(sb.toString());
    }
    else {
        // Answer that the requested station id does not exist
        reply.setBody("Unknown station ID");
    }

    // Send the response to the sender of the request
    send(reply);
}