KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jivesoftware.messenger.server;
2
3 import org.jivesoftware.database.DbConnectionManager;
4 import org.jivesoftware.messenger.Session;
5 import org.jivesoftware.messenger.SessionManager;
6 import org.jivesoftware.messenger.net.SocketAcceptThread;
7 import org.jivesoftware.messenger.server.RemoteServerConfiguration.Permission;
8 import org.jivesoftware.util.JiveGlobals;
9 import org.jivesoftware.util.Log;
10
11 import java.sql.PreparedStatement JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.SQLException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16
17 /**
18  * Manages the connection permissions for remote servers. When a remote server is allowed to
19  * connect to this server then a special configuration for the remote server will be kept.
20  * The configuration holds information such as the port to use when creating an outgoing connection.
21  *
22  * @author Gaston Dombiak
23  */

24 public class RemoteServerManager {
25
26     private static final String JavaDoc ADD_CONFIGURATION =
27         "INSERT INTO jiveRemoteServerConf (domain,remotePort,permission) VALUES (?,?,?)";
28     private static final String JavaDoc DELETE_CONFIGURATION =
29         "DELETE FROM jiveRemoteServerConf WHERE domain=?";
30     private static final String JavaDoc LOAD_CONFIGURATION =
31         "SELECT remotePort,permission FROM jiveRemoteServerConf where domain=?";
32     private static final String JavaDoc LOAD_CONFIGURATIONS =
33         "SELECT domain,remotePort FROM jiveRemoteServerConf where permission=?";
34
35     /**
36      * Allows a remote server to connect to the local server with the specified configuration.
37      *
38      * @param configuration the configuration for the remote server.
39      */

40     public static void allowAccess(RemoteServerConfiguration configuration) {
41         // Remove any previous configuration for this remote server
42
deleteConfiguration(configuration.getDomain());
43         // Update the database with the new granted permission and configuration
44
configuration.setPermission(Permission.allowed);
45         addConfiguration(configuration);
46     }
47
48     /**
49      * Blocks a remote server from connecting to the local server. If the remote server was
50      * connected when the permission was revoked then the connection of the entity will be closed.
51      *
52      * @param domain the domain of the remote server that is not allowed to connect.
53      */

54     public static void blockAccess(String JavaDoc domain) {
55         // Remove any previous configuration for this remote server
56
deleteConfiguration(domain);
57         // Update the database with the new revoked permission
58
RemoteServerConfiguration config = new RemoteServerConfiguration(domain);
59         config.setPermission(Permission.blocked);
60         addConfiguration(config);
61         // Check if the remote server was connected and proceed to close the connection
62
Session session = SessionManager.getInstance().getIncomingServerSession(domain);
63         if (session != null) {
64             session.getConnection().close();
65         }
66         session = SessionManager.getInstance().getOutgoingServerSession(domain);
67         if (session != null) {
68             session.getConnection().close();
69         }
70     }
71
72     /**
73      * Returns true if the remote server with the specified domain can connect to the
74      * local server.
75      *
76      * @param domain the domain of the remote server.
77      * @return true if the remote server with the specified domain can connect to the
78      * local server.
79      */

80     public static boolean canAccess(String JavaDoc domain) {
81         // By default there is no permission defined for the XMPP entity
82
Permission permission = null;
83
84         RemoteServerConfiguration config = getConfiguration(domain);
85         if (config != null) {
86             permission = config.getPermission();
87         }
88
89         if (PermissionPolicy.blacklist == getPermissionPolicy()) {
90             // Anyone can access except those entities listed in the blacklist
91
if (Permission.blocked == permission) {
92                 return false;
93             }
94             else {
95                 return true;
96             }
97         }
98         else {
99             // Access is limited to those present in the whitelist
100
if (Permission.allowed == permission) {
101                 return true;
102             }
103             else {
104                 return false;
105             }
106         }
107     }
108
109     /**
110      * Returns the list of registered remote servers that are allowed to connect to/from this
111      * server when using a whitelist policy. However, when using a blacklist policy (i.e. anyone
112      * may connect to the server) the returned list of configurations will be used for obtaining
113      * the specific connection configuration for each remote server.
114      *
115      * @return the configuration of the registered external components.
116      */

117     public static Collection JavaDoc<RemoteServerConfiguration> getAllowedServers() {
118         return getConfigurations(Permission.allowed);
119     }
120
121     /**
122      * Returns the list of remote servers that are NOT allowed to connect to/from this
123      * server.
124      *
125      * @return the configuration of the blocked external components.
126      */

127     public static Collection JavaDoc<RemoteServerConfiguration> getBlockedServers() {
128         return getConfigurations(Permission.blocked);
129     }
130
131     /**
132      * Removes any existing defined permission and configuration for the specified
133      * remote server.
134      *
135      * @param domain the domain of the remote server.
136      */

137     public static void deleteConfiguration(String JavaDoc domain) {
138         // Remove the permission for the entity from the database
139
java.sql.Connection JavaDoc con = null;
140         PreparedStatement JavaDoc pstmt = null;
141         try {
142             con = DbConnectionManager.getConnection();
143             pstmt = con.prepareStatement(DELETE_CONFIGURATION);
144             pstmt.setString(1, domain);
145             pstmt.executeUpdate();
146         }
147         catch (SQLException JavaDoc sqle) {
148             Log.error(sqle);
149         }
150         finally {
151             try { if (pstmt != null) pstmt.close(); }
152             catch (Exception JavaDoc e) { Log.error(e); }
153             try { if (con != null) con.close(); }
154             catch (Exception JavaDoc e) { Log.error(e); }
155         }
156     }
157
158     /**
159      * Adds a new permission for the specified remote server.
160      */

161     private static void addConfiguration(RemoteServerConfiguration configuration) {
162         // Remove the permission for the entity from the database
163
java.sql.Connection JavaDoc con = null;
164         PreparedStatement JavaDoc pstmt = null;
165         try {
166             con = DbConnectionManager.getConnection();
167             pstmt = con.prepareStatement(ADD_CONFIGURATION);
168             pstmt.setString(1, configuration.getDomain());
169             pstmt.setInt(2, configuration.getRemotePort());
170             pstmt.setString(3, configuration.getPermission().toString());
171             pstmt.executeUpdate();
172         }
173         catch (SQLException JavaDoc sqle) {
174             Log.error(sqle);
175         }
176         finally {
177             try { if (pstmt != null) pstmt.close(); }
178             catch (Exception JavaDoc e) { Log.error(e); }
179             try { if (con != null) con.close(); }
180             catch (Exception JavaDoc e) { Log.error(e); }
181         }
182     }
183
184     /**
185      * Returns the configuration for a remote server.
186      *
187      * @param domain the domain of the remote server.
188      * @return the configuration for a remote server.
189      */

190     public static RemoteServerConfiguration getConfiguration(String JavaDoc domain) {
191         RemoteServerConfiguration configuration = null;
192         java.sql.Connection JavaDoc con = null;
193         PreparedStatement JavaDoc pstmt = null;
194         try {
195             con = DbConnectionManager.getConnection();
196             pstmt = con.prepareStatement(LOAD_CONFIGURATION);
197             pstmt.setString(1, domain);
198             ResultSet JavaDoc rs = pstmt.executeQuery();
199
200             while (rs.next()) {
201                 configuration = new RemoteServerConfiguration(domain);
202                 configuration.setRemotePort(rs.getInt(1));
203                 configuration.setPermission(Permission.valueOf(rs.getString(2)));
204             }
205             rs.close();
206         }
207         catch (SQLException JavaDoc sqle) {
208             Log.error(sqle);
209         }
210         finally {
211             try { if (pstmt != null) pstmt.close(); }
212             catch (Exception JavaDoc e) { Log.error(e); }
213             try { if (con != null) con.close(); }
214             catch (Exception JavaDoc e) { Log.error(e); }
215         }
216         return configuration;
217     }
218
219     private static Collection JavaDoc<RemoteServerConfiguration> getConfigurations(
220             Permission permission) {
221         Collection JavaDoc<RemoteServerConfiguration> answer =
222                 new ArrayList JavaDoc<RemoteServerConfiguration>();
223         java.sql.Connection JavaDoc con = null;
224         PreparedStatement JavaDoc pstmt = null;
225         try {
226             con = DbConnectionManager.getConnection();
227             pstmt = con.prepareStatement(LOAD_CONFIGURATIONS);
228             pstmt.setString(1, permission.toString());
229             ResultSet JavaDoc rs = pstmt.executeQuery();
230             RemoteServerConfiguration configuration;
231             while (rs.next()) {
232                 configuration = new RemoteServerConfiguration(rs.getString(1));
233                 configuration.setRemotePort(rs.getInt(2));
234                 configuration.setPermission(permission);
235                 answer.add(configuration);
236             }
237             rs.close();
238         }
239         catch (SQLException JavaDoc sqle) {
240             Log.error(sqle);
241         }
242         finally {
243             try { if (pstmt != null) pstmt.close(); }
244             catch (Exception JavaDoc e) { Log.error(e); }
245             try { if (con != null) con.close(); }
246             catch (Exception JavaDoc e) { Log.error(e); }
247         }
248         return answer;
249     }
250
251     /**
252      * Returns the remote port to connect for the specified remote server. If no port was
253      * defined then use the default port (e.g. 5269).
254      *
255      * @param domain the domain of the remote server to get the remote port to connect to.
256      * @return the remote port to connect for the specified remote server.
257      */

258     public static int getPortForServer(String JavaDoc domain) {
259         int port = JiveGlobals.getIntProperty("xmpp.server.socket.remotePort",
260                 SocketAcceptThread.DEFAULT_SERVER_PORT);
261         RemoteServerConfiguration config = getConfiguration(domain);
262         if (config != null) {
263             port = config.getRemotePort();
264             if (port == 0) {
265                 port =
266                         JiveGlobals.getIntProperty("xmpp.server.socket.remotePort",
267                                 SocketAcceptThread.DEFAULT_SERVER_PORT);
268             }
269         }
270         return port;
271     }
272
273     /**
274      * Returns the permission policy being used for new XMPP entities that are trying to
275      * connect to the server. There are two types of policies: 1) blacklist: where any entity
276      * is allowed to connect to the server except for those listed in the black list and
277      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
278      * the server.
279      *
280      * @return the permission policy being used for new XMPP entities that are trying to
281      * connect to the server.
282      */

283     public static PermissionPolicy getPermissionPolicy() {
284         try {
285             return PermissionPolicy.valueOf(JiveGlobals.getProperty("xmpp.server.permission",
286                     PermissionPolicy.blacklist.toString()));
287         }
288         catch (Exception JavaDoc e) {
289             Log.error(e);
290             return PermissionPolicy.blacklist;
291         }
292     }
293
294     /**
295      * Sets the permission policy being used for new XMPP entities that are trying to
296      * connect to the server. There are two types of policies: 1) blacklist: where any entity
297      * is allowed to connect to the server except for those listed in the black list and
298      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
299      * the server.
300      *
301      * @param policy the new PermissionPolicy to use.
302      */

303     public static void setPermissionPolicy(PermissionPolicy policy) {
304         JiveGlobals.setProperty("xmpp.server.permission", policy.toString());
305         // Check if the connected servers can remain connected to the server
306
for (String JavaDoc hostname : SessionManager.getInstance().getIncomingServers()) {
307             if (!canAccess(hostname)) {
308                 Session session = SessionManager.getInstance().getIncomingServerSession(hostname);
309                 session.getConnection().close();
310             }
311         }
312         for (String JavaDoc hostname : SessionManager.getInstance().getOutgoingServers()) {
313             if (!canAccess(hostname)) {
314                 Session session = SessionManager.getInstance().getOutgoingServerSession(hostname);
315                 session.getConnection().close();
316             }
317         }
318     }
319
320     /**
321      * Sets the permission policy being used for new XMPP entities that are trying to
322      * connect to the server. There are two types of policies: 1) blacklist: where any entity
323      * is allowed to connect to the server except for those listed in the black list and
324      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
325      * the server.
326      *
327      * @param policy the new policy to use.
328      */

329     public static void setPermissionPolicy(String JavaDoc policy) {
330         setPermissionPolicy(PermissionPolicy.valueOf(policy));
331     }
332
333     public enum PermissionPolicy {
334         /**
335          * Any XMPP entity is allowed to connect to the server except for those listed in
336          * the <b>not allowed list</b>.
337          */

338         blacklist,
339
340         /**
341          * Only the XMPP entities listed in the <b>allowed list</b> are able to connect to
342          * the server.
343          */

344         whitelist;
345     }
346 }
347
Popular Tags