Search in sources :

Example 1 with Sent

use of org.jivesoftware.openfire.carbons.Sent in project Openfire by igniterealtime.

the class MessageRouter method route.

/**
 * <p>Performs the actual packet routing.</p>
 * <p>You routing is considered 'quick' and implementations may not take
 * excessive amounts of time to complete the routing. If routing will take
 * a long amount of time, the actual routing should be done in another thread
 * so this method returns quickly.</p>
 * <h2>Warning</h2>
 * <p>Be careful to enforce concurrency DbC of concurrent by synchronizing
 * any accesses to class resources.</p>
 *
 * @param packet The packet to route
 * @throws NullPointerException If the packet is null
 */
public void route(Message packet) {
    if (packet == null) {
        throw new NullPointerException();
    }
    ClientSession session = sessionManager.getSession(packet.getFrom());
    try {
        // Invoke the interceptors before we process the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);
        if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
            JID recipientJID = packet.getTo();
            // If the server receives a message stanza with no 'to' attribute, it MUST treat the message as if the 'to' address were the bare JID <localpart@domainpart> of the sending entity.
            if (recipientJID == null) {
                recipientJID = packet.getFrom().asBareJID();
            }
            // Check if the message was sent to the server hostname
            if (recipientJID.getNode() == null && recipientJID.getResource() == null && serverName.equals(recipientJID.getDomain())) {
                if (packet.getElement().element("addresses") != null) {
                    // Message includes multicast processing instructions. Ask the multicastRouter
                    // to route this packet
                    multicastRouter.route(packet);
                } else {
                    // Message was sent to the server hostname so forward it to a configurable
                    // set of JID's (probably admin users)
                    sendMessageToAdmins(packet);
                }
                return;
            }
            boolean isAcceptable = true;
            if (session instanceof LocalClientSession) {
                // Check if we could process messages from the recipient.
                // If not, return a not-acceptable error as per XEP-0016:
                // If the user attempts to send an outbound stanza to a contact and that stanza type is blocked, the user's server MUST NOT route the stanza to the contact but instead MUST return a <not-acceptable/> error
                Message dummyMessage = packet.createCopy();
                dummyMessage.setFrom(packet.getTo());
                dummyMessage.setTo(packet.getFrom());
                if (!((LocalClientSession) session).canProcess(dummyMessage)) {
                    packet.setTo(session.getAddress());
                    packet.setFrom((JID) null);
                    packet.setError(PacketError.Condition.not_acceptable);
                    session.process(packet);
                    isAcceptable = false;
                }
            }
            if (isAcceptable) {
                boolean isPrivate = packet.getElement().element(QName.get("private", "urn:xmpp:carbons:2")) != null;
                try {
                    // Deliver stanza to requested route
                    routingTable.routePacket(recipientJID, packet, false);
                } catch (Exception e) {
                    log.error("Failed to route packet: " + packet.toXML(), e);
                    routingFailed(recipientJID, packet);
                }
                // When a client sends a <message/> of type "chat"
                if (packet.getType() == Message.Type.chat && !isPrivate && session != null) {
                    // && session.isMessageCarbonsEnabled() ??? // must the own session also be carbon enabled?
                    List<JID> routes = routingTable.getRoutes(packet.getFrom().asBareJID(), null);
                    for (JID route : routes) {
                        // The sending server SHOULD NOT send a forwarded copy to the sending full JID if it is a Carbons-enabled resource.
                        if (!route.equals(session.getAddress())) {
                            ClientSession clientSession = sessionManager.getSession(route);
                            if (clientSession != null && clientSession.isMessageCarbonsEnabled()) {
                                Message message = new Message();
                                // The wrapping message SHOULD maintain the same 'type' attribute value
                                message.setType(packet.getType());
                                // the 'from' attribute MUST be the Carbons-enabled user's bare JID
                                message.setFrom(packet.getFrom().asBareJID());
                                // and the 'to' attribute SHOULD be the full JID of the resource receiving the copy
                                message.setTo(route);
                                // The content of the wrapping message MUST contain a <sent/> element qualified by the namespace "urn:xmpp:carbons:2", which itself contains a <forwarded/> qualified by the namespace "urn:xmpp:forward:0" that contains the original <message/> stanza.
                                message.addExtension(new Sent(new Forwarded(packet)));
                                clientSession.process(message);
                            }
                        }
                    }
                }
            }
        } else {
            packet.setTo(session.getAddress());
            packet.setFrom((JID) null);
            packet.setError(PacketError.Condition.not_authorized);
            session.process(packet);
        }
        // Invoke the interceptors after we have processed the read packet
        InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);
    } catch (PacketRejectedException e) {
        // An interceptor rejected this packet
        if (session != null && e.getRejectionMessage() != null && e.getRejectionMessage().trim().length() > 0) {
            // A message for the rejection will be sent to the sender of the rejected packet
            Message reply = new Message();
            reply.setID(packet.getID());
            reply.setTo(session.getAddress());
            reply.setFrom(packet.getTo());
            reply.setType(packet.getType());
            reply.setThread(packet.getThread());
            reply.setBody(e.getRejectionMessage());
            session.process(reply);
        }
    }
}
Also used : LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) JID(org.xmpp.packet.JID) Message(org.xmpp.packet.Message) LocalClientSession(org.jivesoftware.openfire.session.LocalClientSession) ClientSession(org.jivesoftware.openfire.session.ClientSession) Forwarded(org.jivesoftware.openfire.forward.Forwarded) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) Sent(org.jivesoftware.openfire.carbons.Sent)

Aggregations

Sent (org.jivesoftware.openfire.carbons.Sent)1 Forwarded (org.jivesoftware.openfire.forward.Forwarded)1 PacketRejectedException (org.jivesoftware.openfire.interceptor.PacketRejectedException)1 ClientSession (org.jivesoftware.openfire.session.ClientSession)1 LocalClientSession (org.jivesoftware.openfire.session.LocalClientSession)1 JID (org.xmpp.packet.JID)1 Message (org.xmpp.packet.Message)1