KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > server > IncomingServerSession


1 /**
2  * $RCSfile: IncomingServerSession.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/06/15 22:29:18 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.server;
13
14 import org.jivesoftware.messenger.*;
15 import org.jivesoftware.messenger.net.SocketConnection;
16 import org.jivesoftware.messenger.auth.UnauthorizedException;
17 import org.xmpp.packet.Packet;
18 import org.dom4j.io.XPPPacketReader;
19 import org.dom4j.Element;
20 import org.xmlpull.v1.XmlPullParserException;
21 import org.xmlpull.v1.XmlPullParser;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 /**
29  * Server-to-server communication is done using two TCP connections between the servers. One
30  * connection is used for sending packets while the other connection is used for receiving packets.
31  * The <tt>IncomingServerSession</tt> represents the connection to a remote server that will only
32  * be used for receiving packets.<p>
33  *
34  * Currently only the Server Dialback method is being used for authenticating the remote server.
35  * Once the remote server has been authenticated incoming packets will be processed by this server.
36  * It is also possible for remote servers to authenticate more domains once the session has been
37  * established. For optimization reasons the existing connection is used between the servers.
38  * Therefore, the incoming server session holds the list of authenticated domains which are allowed
39  * to send packets to this server.<p>
40  *
41  * Using the Server Dialback method it is possible that this server may also act as the
42  * Authoritative Server. This implies that an incoming connection will be established with this
43  * server for authenticating a domain. This incoming connection will only last for a brief moment
44  * and after the domain has been authenticated the connection will be closed and no session will
45  * exist.
46  *
47  * @author Gaston Dombiak
48  */

49 public class IncomingServerSession extends Session {
50
51     private Collection JavaDoc<String JavaDoc> validatedDomains = new ArrayList JavaDoc<String JavaDoc>();
52
53     /**
54      * Creates a new session that will receive packets. The new session will be authenticated
55      * before being returned. If the authentication process fails then the answer will be
56      * <tt>null</tt>.<p>
57      *
58      * Currently the Server Dialback method is the only way to authenticate a remote server. Since
59      * Server Dialback requires an Authoritative Server, it is possible for this server to receive
60      * an incoming connection that will only exist until the requested domain has been validated.
61      * In this case, this method will return <tt>null</tt> since the connection is closed after
62      * the domain was validated. See
63      * {@link ServerDialback#createIncomingSession(org.dom4j.io.XPPPacketReader)} for more
64      * information.
65      *
66      * @param serverName hostname of this server.
67      * @param reader reader on the new established connection with the remote server.
68      * @param connection the new established connection with the remote server.
69      * @return a new session that will receive packets or null if a problem occured while
70      * authenticating the remote server or when acting as the Authoritative Server during
71      * a Server Dialback authentication process.
72      * @throws XmlPullParserException if an error occurs while parsing the XML.
73      * @throws IOException if an input/output error occurs while using the connection.
74      */

75     public static Session createSession(String JavaDoc serverName, XPPPacketReader reader,
76             SocketConnection connection) throws XmlPullParserException, IOException JavaDoc {
77         XmlPullParser xpp = reader.getXPPParser();
78         if (xpp.getNamespace("db") != null) {
79             ServerDialback method = new ServerDialback(connection, serverName);
80             return method.createIncomingSession(reader);
81         }
82         // Close the connection since we only support server dialback for s2s communication
83
connection.close();
84         return null;
85     }
86
87     public IncomingServerSession(String JavaDoc serverName, Connection connection, StreamID streamID) {
88         super(serverName, connection, streamID);
89     }
90
91     public void process(Packet packet) throws UnauthorizedException, PacketException {
92         //TODO Should never be called? Should be passed to the outgoing connection?
93
}
94
95     /**
96      * Returns true if the request of a new domain was valid. Sessions may receive subsequent
97      * domain validation request. If the validation of the new domain fails then the session and
98      * the underlying TCP connection will be closed.<p>
99      *
100      * For optimization reasons, the same session may be servicing several domains of a
101      * remote server.
102      *
103      * @param dbResult the DOM stanza requesting the domain validation.
104      * @return true if the requested domain was valid.
105      */

106     public boolean validateSubsequentDomain(Element dbResult) {
107         ServerDialback method = new ServerDialback(getConnection(), getServerName());
108         if (method.validateRemoteDomain(dbResult, getStreamID())) {
109             addValidatedDomain(dbResult.attributeValue("from"));
110             return true;
111         }
112         return false;
113     }
114
115     /**
116      * Returns true if the specified domain has been validated for this session. The remote
117      * server should send a "db:result" packet for registering new subdomains or even
118      * virtual hosts.<p>
119      *
120      * In the spirit of being flexible we allow remote servers to not register subdomains
121      * and even so consider subdomains that include the server domain in their domain part
122      * as valid domains.
123      *
124      * @param domain the domain to validate.
125      * @return true if the specified domain has been validated for this session.
126      */

127     public boolean isValidDomain(String JavaDoc domain) {
128         // Check if the specified domain is contained in any of the validated domains
129
for (String JavaDoc validatedDomain : getValidatedDomains()) {
130             if (domain.contains(validatedDomain)) {
131                 return true;
132             }
133         }
134         return false;
135     }
136
137     /**
138      * Returns a collection with all the domains, subdomains and virtual hosts that where
139      * validated. The remote server is allowed to send packets from any of these domains,
140      * subdomains and virtual hosts.
141      *
142      * @return domains, subdomains and virtual hosts that where validated.
143      */

144     public Collection JavaDoc<String JavaDoc> getValidatedDomains() {
145         return Collections.unmodifiableCollection(validatedDomains);
146     }
147
148     /**
149      * Adds a new validated domain, subdomain or virtual host to the list of
150      * validated domains for the remote server.
151      *
152      * @param domain the new validated domain, subdomain or virtual host to add.
153      */

154     public void addValidatedDomain(String JavaDoc domain) {
155         if (validatedDomains.add(domain)) {
156             // Register the new validated domain for this server session in SessionManager
157
SessionManager.getInstance().registerIncomingServerSession(domain, this);
158         }
159     }
160
161     /**
162      * Removes the previously validated domain from the list of validated domains. The remote
163      * server will no longer be able to send packets from the removed domain, subdomain or
164      * virtual host.
165      *
166      * @param domain the domain, subdomain or virtual host to remove from the list of
167      * validated domains.
168      */

169     public void removeValidatedDomain(String JavaDoc domain) {
170         validatedDomains.remove(domain);
171         // Unregister the validated domain for this server session in SessionManager
172
SessionManager.getInstance().unregisterIncomingServerSession(domain);
173     }
174
175     /**
176      * Verifies the received key sent by the remote server. This server is trying to generate
177      * an outgoing connection to the remote server and the remote server is reusing an incoming
178      * connection for validating the key.
179      *
180      * @param doc the received Element that contains the key to verify.
181      */

182     public void verifyReceivedKey(Element doc) {
183         ServerDialback.verifyReceivedKey(doc, getConnection());
184     }
185 }
186
Popular Tags