SessionPacketRouter.java 4.26 KB
Newer Older
Alex Wenckus's avatar
Alex Wenckus committed
1 2 3 4 5
/**
 * $RCSfile: $
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Alex Wenckus's avatar
Alex Wenckus committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Alex Wenckus's avatar
Alex Wenckus committed
19
 */
20
package org.jivesoftware.openfire;
Alex Wenckus's avatar
Alex Wenckus committed
21

22
import org.dom4j.Element;
23 24
import org.jivesoftware.openfire.multiplex.UnknownStanzaException;
import org.jivesoftware.openfire.net.SASLAuthentication;
25
import org.jivesoftware.openfire.session.LocalClientSession;
Alex Wenckus's avatar
Alex Wenckus committed
26 27 28 29 30
import org.xmpp.packet.*;

import java.io.UnsupportedEncodingException;

/**
31 32
 * Handles the routing of packets to a particular session. It will invoke all of the appropriate
 * interceptors, before and after having the server process the message.
Alex Wenckus's avatar
Alex Wenckus committed
33 34 35
 *
 * @author Alexander Wenckus
 */
36
public class SessionPacketRouter implements PacketRouter {
Alex Wenckus's avatar
Alex Wenckus committed
37

38
    private LocalClientSession session;
Alex Wenckus's avatar
Alex Wenckus committed
39
    private PacketRouter router;
40
    private boolean skipJIDValidation = false;
Alex Wenckus's avatar
Alex Wenckus committed
41

42
    public SessionPacketRouter(LocalClientSession session) {
Alex Wenckus's avatar
Alex Wenckus committed
43 44 45 46
        this.session = session;
        router = XMPPServer.getInstance().getPacketRouter();
    }

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    /**
     * Sets if TO addresses of Elements being routed should be validated. Doing stringprep operations
     * is very expensive and sometimes we already validated the TO address so there is no need to
     * validate again the address. For instance, when using Connection Managers the validation
     * is done by the Connection Manager so we can just trust the TO address. On the other hand,
     * the FROM address is set by the server so there is no need to validate it.<p>
     *
     * By default validation is enabled.
     *
     * @param skipJIDValidation true if validation of TO address is enabled.
     */
    public void setSkipJIDValidation(boolean skipJIDValidation) {
        this.skipJIDValidation = skipJIDValidation;
    }

Alex Wenckus's avatar
Alex Wenckus committed
62
    public void route(Element wrappedElement)
63
            throws UnsupportedEncodingException, UnknownStanzaException {
Alex Wenckus's avatar
Alex Wenckus committed
64 65 66 67 68 69 70 71
        String tag = wrappedElement.getName();
        if ("auth".equals(tag) || "response".equals(tag)) {
            SASLAuthentication.handle(session, wrappedElement);
        }
        else if ("iq".equals(tag)) {
            route(getIQ(wrappedElement));
        }
        else if ("message".equals(tag)) {
72
            route(new Message(wrappedElement, skipJIDValidation));
Alex Wenckus's avatar
Alex Wenckus committed
73 74
        }
        else if ("presence".equals(tag)) {
75
            route(new Presence(wrappedElement, skipJIDValidation));
Alex Wenckus's avatar
Alex Wenckus committed
76 77 78 79 80 81 82 83 84 85 86 87
        }
        else {
            throw new UnknownStanzaException();
        }
    }

    private IQ getIQ(Element doc) {
        Element query = doc.element("query");
        if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
            return new Roster(doc);
        }
        else {
88
            return new IQ(doc, skipJIDValidation);
Alex Wenckus's avatar
Alex Wenckus committed
89 90 91
        }
    }

92
    public void route(Packet packet) {
93 94
        // Security: Don't allow users to send packets on behalf of other users
        packet.setFrom(session.getAddress());
95 96 97 98 99 100 101 102 103 104 105
        if(packet instanceof IQ) {
            route((IQ)packet);
        }
        else if(packet instanceof Message) {
            route((Message)packet);
        }
        else if(packet instanceof Presence) {
            route((Presence)packet);
        }
    }

Alex Wenckus's avatar
Alex Wenckus committed
106 107
    public void route(IQ packet) {
        packet.setFrom(session.getAddress());
108 109
        router.route(packet);
        session.incrementClientPacketCount();
Alex Wenckus's avatar
Alex Wenckus committed
110 111 112 113
    }

    public void route(Message packet) {
        packet.setFrom(session.getAddress());
114 115
        router.route(packet);
        session.incrementClientPacketCount();
Alex Wenckus's avatar
Alex Wenckus committed
116 117 118 119
    }

    public void route(Presence packet) {
        packet.setFrom(session.getAddress());
120 121
        router.route(packet);
        session.incrementClientPacketCount();
Alex Wenckus's avatar
Alex Wenckus committed
122 123
    }
}