Search in sources :

Example 31 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class HttpSessionDeliverable method testNamespaceOnStanza.

/**
 * Verifies that the default namespace is set on (non-empty) stanzas.
 *
 * @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
 */
@Test
public void testNamespaceOnStanza() throws Exception {
    // Setup fixture
    final Message message = new Message();
    message.setTo("unittest@example.org/test");
    message.addChildElement("unittest", "unit:test:namespace");
    final List<Packet> packets = new ArrayList<>();
    packets.add(message);
    // Execute system under test
    final HttpSession.Deliverable deliverable = new HttpSession.Deliverable(packets);
    final String result = deliverable.getDeliverable();
    // verify results
    // Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
    assertEquals("<message to=\"unittest@example.org/test\" xmlns=\"jabber:client\"><unittest xmlns=\"unit:test:namespace\"/></message>", result);
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class HttpSessionDeliverable method testNamespaceOnStanzaWithNamespace.

/**
 * Verifies that the default namespace is not set on stanzas that already have defined a default namespace.
 *
 * @see <a href="https://igniterealtime.org/issues/browse/OF-1087">OF-1087</a>
 */
@Test
public void testNamespaceOnStanzaWithNamespace() throws Exception {
    // Setup fixture
    final Message message = new Message();
    message.getElement().setQName(QName.get("message", "unit:test:preexisting:namespace"));
    message.setTo("unittest@example.org/test");
    message.addChildElement("unittest", "unit:test:namespace");
    final List<Packet> packets = new ArrayList<>();
    packets.add(message);
    // Execute system under test
    final HttpSession.Deliverable deliverable = new HttpSession.Deliverable(packets);
    final String result = deliverable.getDeliverable();
    // verify results
    // Note that this assertion depends on the Openfire XML parser-specific ordering of attributes.
    assertEquals("<message xmlns=\"unit:test:preexisting:namespace\" to=\"unittest@example.org/test\"><unittest xmlns=\"unit:test:namespace\"/></message>", result);
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with Packet

use of org.xmpp.packet.Packet in project Openfire by igniterealtime.

the class MultiUserChatServiceImpl method sendErrorPacket.

/**
 * Generate and send an error packet to indicate that something went wrong.
 *
 * @param packet  the packet to be responded to with an error.
 * @param error   the reason why the operation failed.
 * @param message an optional human-readable error message.
 */
private void sendErrorPacket(Packet packet, PacketError.Condition error, String message) {
    if (packet.getError() != null) {
        Log.debug("Avoid generating an error in response to a stanza that itself has an error (to avoid the chance of entering an endless back-and-forth of exchanging errors). Suppress sending an {} error (with message '{}') in response to: {}", error, message, packet);
        return;
    }
    if (packet instanceof IQ) {
        IQ reply = IQ.createResultIQ((IQ) packet);
        reply.setChildElement(((IQ) packet).getChildElement().createCopy());
        reply.setError(error);
        if (message != null) {
            reply.getError().setText(message);
        }
        XMPPServer.getInstance().getPacketRouter().route(reply);
    } else {
        Packet reply = packet.createCopy();
        reply.setError(error);
        if (message != null) {
            reply.getError().setText(message);
        }
        reply.setFrom(packet.getTo());
        reply.setTo(packet.getFrom());
        XMPPServer.getInstance().getPacketRouter().route(reply);
    }
}
Also used : Packet(org.xmpp.packet.Packet) IQ(org.xmpp.packet.IQ)

Aggregations

Packet (org.xmpp.packet.Packet)33 JID (org.xmpp.packet.JID)17 Element (org.dom4j.Element)16 Message (org.xmpp.packet.Message)16 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)10 IQ (org.xmpp.packet.IQ)9 Presence (org.xmpp.packet.Presence)7 NotFoundException (org.jivesoftware.util.NotFoundException)5 PacketInterceptor (org.jivesoftware.openfire.interceptor.PacketInterceptor)3 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)3 Session (org.jivesoftware.openfire.session.Session)3 Date (java.sql.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 DefaultElement (org.dom4j.tree.DefaultElement)2 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)2