Search in sources :

Example 11 with Packet

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

the class MulticastRouter method sendToRemoteServer.

/**
 * Actually sends pending packets of the specified domain using the discovered multicast
 * service address. If remote server supports multicast service then a copy of the
 * orignal will be sent to the remote server. However, if no multicast service was found
 * then the local server sends a copy of the original stanza to each address.
 *
 * @param domain domain of the remote server with pending packets.
 * @param multicastService address of the discovered multicast service.
 */
private void sendToRemoteServer(String domain, String multicastService) {
    Collection<Packet> packets = null;
    // Get the packets to send to the remote entity
    synchronized (domain.intern()) {
        packets = remotePackets.remove(domain);
    }
    if (multicastService != null && multicastService.trim().length() > 0) {
        // multicast service
        for (Packet packet : packets) {
            Element addresses = getAddresses(packet);
            for (Iterator it = addresses.elementIterator("address"); it.hasNext(); ) {
                Element address = (Element) it.next();
                String jid = address.attributeValue("jid");
                if (!jid.contains("@" + domain)) {
                    if (Type.bcc.toString().equals(address.attributeValue("type"))) {
                        it.remove();
                    } else {
                        address.addAttribute("delivered", "true");
                    }
                }
            }
            // Set that the target of the packet is the multicast service
            packet.setTo(multicastService);
            // Send the packet to the remote entity
            packetRouter.route(packet);
        }
    } else {
        // to each address
        for (Packet packet : packets) {
            Element addresses = getAddresses(packet);
            List<String> targets = new ArrayList<>();
            for (Iterator it = addresses.elementIterator("address"); it.hasNext(); ) {
                Element address = (Element) it.next();
                String jid = address.attributeValue("jid");
                // Keep a list of the remote users that are going to receive the packet
                if (jid.contains("@" + domain)) {
                    targets.add(jid);
                }
                // Set as delivered
                address.addAttribute("delivered", "true");
                // Remove bcc addresses
                if (Type.bcc.toString().equals(address.attributeValue("type"))) {
                    it.remove();
                }
            }
            // Send the packet to each remote user
            for (String jid : targets) {
                packet.setTo(jid);
                packetRouter.route(packet);
            }
        }
    }
}
Also used : Packet(org.xmpp.packet.Packet) Element(org.dom4j.Element) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 12 with Packet

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

the class MulticastRouter method route.

public void route(Packet packet) {
    Set<String> remoteServers = new HashSet<>();
    List<String> targets = new ArrayList<>();
    Packet localBroadcast = packet.createCopy();
    Element addresses = getAddresses(localBroadcast);
    String localDomain = "@" + server.getServerInfo().getXMPPDomain();
    // remote domains that should receive the packet too
    for (Iterator it = addresses.elementIterator("address"); it.hasNext(); ) {
        Element address = (Element) it.next();
        // Skip addresses of type noreply since they don't have any address
        if (Type.noreply.toString().equals(address.attributeValue("type"))) {
            continue;
        }
        String jid = address.attributeValue("jid");
        // Only send to local users and if packet has not already been delivered
        if (jid.contains(localDomain) && address.attributeValue("delivered") == null) {
            targets.add(jid);
        } else if (!jid.contains(localDomain)) {
            remoteServers.add(new JID(jid).getDomain());
        }
        // Set as delivered
        address.addAttribute("delivered", "true");
        // Remove bcc addresses
        if (Type.bcc.toString().equals(address.attributeValue("type"))) {
            it.remove();
        }
    }
    // Send the packet to local target users
    for (String jid : targets) {
        localBroadcast.setTo(jid);
        packetRouter.route(localBroadcast);
    }
    // Keep a registry of packets that should be sent to remote domains.
    for (String domain : remoteServers) {
        boolean shouldDiscover = false;
        synchronized (domain.intern()) {
            Collection<Packet> packets = remotePackets.get(domain);
            if (packets == null) {
                packets = new ArrayList<>();
                remotePackets.put(domain, packets);
                shouldDiscover = true;
            }
            // Add that this packet should be sent to the requested remote server
            packets.add(packet);
        }
        if (shouldDiscover) {
            // First time a packet is sent to this remote server so start the extra work
            // of discovering if remote server supports multicast service and actually send
            // the packet to the remote server
            sendToRemoteEntity(domain);
        }
    }
// TODO Add thread that checks every 5 minutes if packets to remote servers were not
// TODO sent because no disco response was received. So assume that remote server does
// TODO not support JEP-33 and send pending packets
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 13 with Packet

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

the class IQRosterHandler method removeItem.

/**
 * Remove the roster item from the sender's roster (and possibly the recipient's).
 * Actual roster removal is done in the removeItem(Roster,RosterItem) method.
 *
 * @param roster The sender's roster.
 * @param sender The JID of the sender of the removal request
 * @param item   The removal item element
 * @return The removed item or null, if not item has been removed.
 */
private RosterItem removeItem(org.jivesoftware.openfire.roster.Roster roster, JID sender, org.xmpp.packet.Roster.Item item) throws SharedGroupException {
    JID recipient = item.getJID();
    // Remove recipient from the sender's roster
    RosterItem removedItem = roster.deleteRosterItem(item.getJID(), true);
    // Forward set packet to the subscriber
    if (localServer.isLocal(recipient)) {
        // Recipient is local so let's handle it here
        try {
            Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster();
            // Instead of deleting the sender in the recipient's roster, update it.
            // https://igniterealtime.atlassian.net/browse/OF-720
            RosterItem rosterItem = recipientRoster.getRosterItem(sender);
            // If the receiver doesn't have subscribed yet, delete the sender from the receiver's roster, too.
            if (rosterItem.getRecvStatus().equals(RosterItem.RECV_SUBSCRIBE)) {
                recipientRoster.deleteRosterItem(sender, true);
            } else // Otherwise only update it, so that the sender is not deleted from the receivers roster.
            {
                rosterItem.setAskStatus(RosterItem.ASK_NONE);
                rosterItem.setRecvStatus(RosterItem.RECV_NONE);
                rosterItem.setSubStatus(RosterItem.SUB_NONE);
                recipientRoster.updateRosterItem(rosterItem);
            }
        } catch (UserNotFoundException e) {
        // Do nothing
        }
    } else {
        // Recipient is remote so we just forward the packet to them
        String serverDomain = localServer.getServerInfo().getXMPPDomain();
        // Check if the recipient may be hosted by this server
        if (!recipient.getDomain().contains(serverDomain)) {
        // TODO Implete when s2s is implemented
        } else {
            Packet removePacket = createRemoveForward(sender, recipient);
            router.route(removePacket);
        }
    }
    return removedItem;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster)

Example 14 with Packet

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

the class TransportHandler method process.

@Override
public void process(Packet packet) throws UnauthorizedException, PacketException {
    boolean handled = false;
    String host = packet.getTo().getDomain();
    for (Channel<Packet> channel : transports.values()) {
        if (channel.getName().equalsIgnoreCase(host)) {
            channel.add(packet);
            handled = true;
        }
    }
    if (!handled) {
        JID recipient = packet.getTo();
        JID sender = packet.getFrom();
        packet.setError(PacketError.Condition.remote_server_timeout);
        packet.setFrom(recipient);
        packet.setTo(sender);
        try {
            deliverer.deliver(packet);
        } catch (PacketException e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }
}
Also used : Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) PacketException(org.jivesoftware.openfire.PacketException)

Example 15 with Packet

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

the class StanzaIDUtilTest method testDontOverwriteStanzaIDElement.

/**
 * Test if {@link StanzaIDUtil#ensureUniqueAndStableStanzaID(Packet, JID)} does not overwrites
 * a stanza-id element when another is present with a different 'by' value.
 */
@Test
public void testDontOverwriteStanzaIDElement() throws Exception {
    // Setup fixture.
    final Packet input = new Message();
    final JID self = new JID("foobar");
    final String notExpected = "de305d54-75b4-431b-adb2-eb6b9e546013";
    final Element toOverwrite = input.getElement().addElement("stanza-id", "urn:xmpp:sid:0");
    toOverwrite.addAttribute("by", new JID("someoneelse").toString());
    toOverwrite.addAttribute("id", notExpected);
    // Execute system under test.
    final Packet result = StanzaIDUtil.ensureUniqueAndStableStanzaID(input, self);
    // Verify results.
    Assert.assertNotNull(result);
    final List<Element> elements = result.getElement().elements(QName.get("stanza-id", "urn:xmpp:sid:0"));
    assertEquals(2, elements.size());
}
Also used : Packet(org.xmpp.packet.Packet) Message(org.xmpp.packet.Message) JID(org.xmpp.packet.JID) Element(org.dom4j.Element) Test(org.junit.Test)

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