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 ; 12 import java.sql.ResultSet ; 13 import java.sql.SQLException ; 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 17 25 public class ExternalComponentManager { 26 27 private static final String ADD_CONFIGURATION = 28 "INSERT INTO jiveExtComponentConf (subdomain,secret,permission) VALUES (?,?,?)"; 29 private static final String DELETE_CONFIGURATION = 30 "DELETE FROM jiveExtComponentConf WHERE subdomain=?"; 31 private static final String LOAD_CONFIGURATION = 32 "SELECT secret,permission FROM jiveExtComponentConf where subdomain=?"; 33 private static final String LOAD_CONFIGURATIONS = 34 "SELECT subdomain,secret FROM jiveExtComponentConf where permission=?"; 35 36 41 public static void allowAccess(ExternalComponentConfiguration configuration) { 42 deleteConfiguration(configuration.getSubdomain()); 44 configuration.setPermission(Permission.allowed); 46 addConfiguration(configuration); 47 } 48 49 55 public static void blockAccess(String subdomain) { 56 deleteConfiguration(subdomain); 58 ExternalComponentConfiguration config = new ExternalComponentConfiguration(subdomain); 60 config.setPermission(Permission.blocked); 61 addConfiguration(config); 62 String 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 78 public static boolean canAccess(String subdomain) { 79 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 if (Permission.blocked == permission) { 90 return false; 91 } 92 else { 93 return true; 94 } 95 } 96 else { 97 if (Permission.allowed == permission) { 99 return true; 100 } 101 else { 102 return false; 103 } 104 } 105 } 106 107 115 public static Collection <ExternalComponentConfiguration> getAllowedComponents() { 116 return getConfigurations(Permission.allowed); 117 } 118 119 125 public static Collection <ExternalComponentConfiguration> getBlockedComponents() { 126 return getConfigurations(Permission.blocked); 127 } 128 129 135 public static void deleteConfiguration(String subdomain) { 136 java.sql.Connection con = null; 138 PreparedStatement 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 sqle) { 146 Log.error(sqle); 147 } 148 finally { 149 try { if (pstmt != null) pstmt.close(); } 150 catch (Exception e) { Log.error(e); } 151 try { if (con != null) con.close(); } 152 catch (Exception e) { Log.error(e); } 153 } 154 } 155 156 159 private static void addConfiguration(ExternalComponentConfiguration configuration) { 160 java.sql.Connection con = null; 162 PreparedStatement 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 sqle) { 172 Log.error(sqle); 173 } 174 finally { 175 try { if (pstmt != null) pstmt.close(); } 176 catch (Exception e) { Log.error(e); } 177 try { if (con != null) con.close(); } 178 catch (Exception e) { Log.error(e); } 179 } 180 } 181 182 188 public static ExternalComponentConfiguration getConfiguration(String subdomain) { 189 ExternalComponentConfiguration configuration = null; 190 java.sql.Connection con = null; 191 PreparedStatement pstmt = null; 192 try { 193 con = DbConnectionManager.getConnection(); 194 pstmt = con.prepareStatement(LOAD_CONFIGURATION); 195 pstmt.setString(1, subdomain); 196 ResultSet 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 sqle) { 206 Log.error(sqle); 207 } 208 finally { 209 try { if (pstmt != null) pstmt.close(); } 210 catch (Exception e) { Log.error(e); } 211 try { if (con != null) con.close(); } 212 catch (Exception e) { Log.error(e); } 213 } 214 return configuration; 215 } 216 217 private static Collection <ExternalComponentConfiguration> getConfigurations( 218 Permission permission) { 219 Collection <ExternalComponentConfiguration> answer = 220 new ArrayList <ExternalComponentConfiguration>(); 221 java.sql.Connection con = null; 222 PreparedStatement pstmt = null; 223 try { 224 con = DbConnectionManager.getConnection(); 225 pstmt = con.prepareStatement(LOAD_CONFIGURATIONS); 226 pstmt.setString(1, permission.toString()); 227 ResultSet 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 sqle) { 238 Log.error(sqle); 239 } 240 finally { 241 try { if (pstmt != null) pstmt.close(); } 242 catch (Exception e) { Log.error(e); } 243 try { if (con != null) con.close(); } 244 catch (Exception e) { Log.error(e); } 245 } 246 return answer; 247 } 248 249 256 public static String getDefaultSecret() { 257 return JiveGlobals.getProperty("xmpp.component.defaultSecret"); 258 } 259 260 267 public static void setDefaultSecret(String defaultSecret) { 268 JiveGlobals.setProperty("xmpp.component.defaultSecret", defaultSecret); 269 } 270 271 279 public static String getSecretForComponent(String subdomain) { 280 String 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 306 public static PermissionPolicy getPermissionPolicy() { 307 try { 308 return PermissionPolicy.valueOf(JiveGlobals.getProperty("xmpp.component.permission", 309 PermissionPolicy.blacklist.toString())); 310 } 311 catch (Exception e) { 312 Log.error(e); 313 return PermissionPolicy.blacklist; 314 } 315 } 316 317 326 public static void setPermissionPolicy(PermissionPolicy policy) { 327 JiveGlobals.setProperty("xmpp.component.permission", policy.toString()); 328 for (ComponentSession session : SessionManager.getInstance().getComponentSessions()) { 330 if (!canAccess(session.getExternalComponent().getSubdomain())) { 331 session.getConnection().close(); 332 } 333 } 334 } 335 336 345 public static void setPermissionPolicy(String policy) { 346 setPermissionPolicy(PermissionPolicy.valueOf(policy)); 347 } 348 349 public enum PermissionPolicy { 350 354 blacklist, 355 356 360 whitelist; 361 } 362 } 363 | Popular Tags |