KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > component > ExternalComponentManager


1 package org.jivesoftware.messenger.component;
2
3 import org.jivesoftware.database.DbConnectionManager;
4 import org.jivesoftware.messenger.Session;
5 import org.jivesoftware.messenger.SessionManager;
6 import org.jivesoftware.messenger.XMPPServer;
7 import org.jivesoftware.messenger.component.ExternalComponentConfiguration.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 external components. When an external component is
19  * allowed to connect to this server then a special configuration for the component will be kept.
20  * The configuration holds information such as the shared secret that the component should use
21  * when authenticating with the server.
22  *
23  * @author Gaston Dombiak
24  */

25 public class ExternalComponentManager {
26
27     private static final String JavaDoc ADD_CONFIGURATION =
28         "INSERT INTO jiveExtComponentConf (subdomain,secret,permission) VALUES (?,?,?)";
29     private static final String JavaDoc DELETE_CONFIGURATION =
30         "DELETE FROM jiveExtComponentConf WHERE subdomain=?";
31     private static final String JavaDoc LOAD_CONFIGURATION =
32         "SELECT secret,permission FROM jiveExtComponentConf where subdomain=?";
33     private static final String JavaDoc LOAD_CONFIGURATIONS =
34         "SELECT subdomain,secret FROM jiveExtComponentConf where permission=?";
35
36     /**
37      * Allows an external component to connect to the local server with the specified configuration.
38      *
39      * @param configuration the configuration for the external component.
40      */

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

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

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

115     public static Collection JavaDoc<ExternalComponentConfiguration> getAllowedComponents() {
116         return getConfigurations(Permission.allowed);
117     }
118
119     /**
120      * Returns the list of external components that are NOT allowed to connect to this
121      * server.
122      *
123      * @return the configuration of the blocked external components.
124      */

125     public static Collection JavaDoc<ExternalComponentConfiguration> getBlockedComponents() {
126         return getConfigurations(Permission.blocked);
127     }
128
129     /**
130      * Removes any existing defined permission and configuration for the specified
131      * external component.
132      *
133      * @param subdomain the subdomain of the external component.
134      */

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

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

188     public static ExternalComponentConfiguration getConfiguration(String JavaDoc subdomain) {
189         ExternalComponentConfiguration configuration = null;
190         java.sql.Connection JavaDoc con = null;
191         PreparedStatement JavaDoc pstmt = null;
192         try {
193             con = DbConnectionManager.getConnection();
194             pstmt = con.prepareStatement(LOAD_CONFIGURATION);
195             pstmt.setString(1, subdomain);
196             ResultSet JavaDoc rs = pstmt.executeQuery();
197
198             while (rs.next()) {
199                 configuration = new ExternalComponentConfiguration(subdomain);
200                 configuration.setSecret(rs.getString(1));
201                 configuration.setPermission(Permission.valueOf(rs.getString(2)));
202             }
203             rs.close();
204         }
205         catch (SQLException JavaDoc sqle) {
206             Log.error(sqle);
207         }
208         finally {
209             try { if (pstmt != null) pstmt.close(); }
210             catch (Exception JavaDoc e) { Log.error(e); }
211             try { if (con != null) con.close(); }
212             catch (Exception JavaDoc e) { Log.error(e); }
213         }
214         return configuration;
215     }
216
217     private static Collection JavaDoc<ExternalComponentConfiguration> getConfigurations(
218             Permission permission) {
219         Collection JavaDoc<ExternalComponentConfiguration> answer =
220                 new ArrayList JavaDoc<ExternalComponentConfiguration>();
221         java.sql.Connection JavaDoc con = null;
222         PreparedStatement JavaDoc pstmt = null;
223         try {
224             con = DbConnectionManager.getConnection();
225             pstmt = con.prepareStatement(LOAD_CONFIGURATIONS);
226             pstmt.setString(1, permission.toString());
227             ResultSet JavaDoc rs = pstmt.executeQuery();
228             ExternalComponentConfiguration configuration;
229             while (rs.next()) {
230                 configuration = new ExternalComponentConfiguration(rs.getString(1));
231                 configuration.setSecret(rs.getString(2));
232                 configuration.setPermission(permission);
233                 answer.add(configuration);
234             }
235             rs.close();
236         }
237         catch (SQLException JavaDoc sqle) {
238             Log.error(sqle);
239         }
240         finally {
241             try { if (pstmt != null) pstmt.close(); }
242             catch (Exception JavaDoc e) { Log.error(e); }
243             try { if (con != null) con.close(); }
244             catch (Exception JavaDoc e) { Log.error(e); }
245         }
246         return answer;
247     }
248
249     /**
250      * Returns the default secret key to use for those external components that don't have an
251      * individual configuration.
252      *
253      * @return the default secret key to use for those external components that don't have an
254      * individual configuration.
255      */

256     public static String JavaDoc getDefaultSecret() {
257         return JiveGlobals.getProperty("xmpp.component.defaultSecret");
258     }
259
260     /**
261      * Sets the default secret key to use for those external components that don't have an
262      * individual configuration.
263      *
264      * @param defaultSecret the default secret key to use for those external components that
265      * don't have an individual configuration.
266      */

267     public static void setDefaultSecret(String JavaDoc defaultSecret) {
268         JiveGlobals.setProperty("xmpp.component.defaultSecret", defaultSecret);
269     }
270
271     /**
272      * Returns the shared secret with the specified external component. If no shared secret was
273      * defined then use the default shared secret.
274      *
275      * @param subdomain the subdomain of the external component to get his shared secret.
276      * (e.g. conference)
277      * @return the shared secret with the specified external component or the default shared secret.
278      */

279     public static String JavaDoc getSecretForComponent(String JavaDoc subdomain) {
280         // By default there is no shared secret defined for the XMPP entity
281
String JavaDoc secret = null;
282
283         ExternalComponentConfiguration config = getConfiguration(subdomain);
284         if (config != null) {
285             secret = config.getSecret();
286         }
287
288         secret = (secret == null ? getDefaultSecret() : secret);
289         if (secret == null) {
290             Log.error("Setup for external components is incomplete. Property " +
291                     "xmpp.component.defaultSecret does not exist.");
292         }
293         return secret;
294     }
295
296     /**
297      * Returns the permission policy being used for new XMPP entities that are trying to
298      * connect to the server. There are two types of policies: 1) blacklist: where any entity
299      * is allowed to connect to the server except for those listed in the black list and
300      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
301      * the server.
302      *
303      * @return the permission policy being used for new XMPP entities that are trying to
304      * connect to the server.
305      */

306     public static PermissionPolicy getPermissionPolicy() {
307         try {
308             return PermissionPolicy.valueOf(JiveGlobals.getProperty("xmpp.component.permission",
309                     PermissionPolicy.blacklist.toString()));
310         }
311         catch (Exception JavaDoc e) {
312             Log.error(e);
313             return PermissionPolicy.blacklist;
314         }
315     }
316
317     /**
318      * Sets the permission policy being used for new XMPP entities that are trying to
319      * connect to the server. There are two types of policies: 1) blacklist: where any entity
320      * is allowed to connect to the server except for those listed in the black list and
321      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
322      * the server.
323      *
324      * @param policy the new PermissionPolicy to use.
325      */

326     public static void setPermissionPolicy(PermissionPolicy policy) {
327         JiveGlobals.setProperty("xmpp.component.permission", policy.toString());
328         // Check if the connected component can remain connected to the server
329
for (ComponentSession session : SessionManager.getInstance().getComponentSessions()) {
330             if (!canAccess(session.getExternalComponent().getSubdomain())) {
331                 session.getConnection().close();
332             }
333         }
334     }
335
336     /**
337      * Sets the permission policy being used for new XMPP entities that are trying to
338      * connect to the server. There are two types of policies: 1) blacklist: where any entity
339      * is allowed to connect to the server except for those listed in the black list and
340      * 2) whitelist: where only the entities listed in the white list are allowed to connect to
341      * the server.
342      *
343      * @param policy the new policy to use.
344      */

345     public static void setPermissionPolicy(String JavaDoc policy) {
346         setPermissionPolicy(PermissionPolicy.valueOf(policy));
347     }
348
349     public enum PermissionPolicy {
350         /**
351          * Any XMPP entity is allowed to connect to the server except for those listed in
352          * the <b>not allowed list</b>.
353          */

354         blacklist,
355
356         /**
357          * Only the XMPP entities listed in the <b>allowed list</b> are able to connect to
358          * the server.
359          */

360         whitelist;
361     }
362 }
363
Popular Tags