1 package com.ca.commons.jndi; 2 3 import javax.naming.NamingException ; 4 import javax.naming.Context ; 5 import java.util.*; 6 7 12 13 public class ConnectionData 14 { 15 20 21 public String baseDN = ""; 22 23 26 27 public int version = 3; 29 32 33 public static final String LDAP = "ldap"; 34 public static final String DSML = "dsml"; 35 public String protocol = LDAP; 37 40 41 public String url; 42 43 46 47 public String userDN; 48 49 52 53 public char[] pwd; 54 55 58 59 public String referralType = "follow"; 60 61 64 65 public String aliasType = "searching"; 66 67 70 71 public boolean useSSL; 72 73 76 77 public String cacerts; 79 80 83 84 public String clientcerts; 85 86 89 90 public char[] caKeystorePwd; 91 92 95 96 public char[] clientKeystorePwd; 97 98 101 102 public String caKeystoreType; 103 104 107 108 public String clientKeystoreType; 109 110 113 114 public String sslSocketFactory = "com.ca.commons.jndi.JndiSocketFactory"; 115 116 120 121 public boolean tracing; 122 123 127 128 public boolean sslTracing; 129 130 131 private static final String DEFAULT_CTX = "com.sun.jndi.ldap.LdapCtxFactory"; 132 133 private static final String DEFAULT_DSML_CTX = "com.ca.jndiproviders.dsml.DsmlCtxFactory"; 135 136 137 139 142 143 public boolean useGSSAPI; 144 145 148 public Properties extraProperties; 149 150 151 155 156 public ConnectionData() 157 { 158 } 159 160 187 public ConnectionData(int version, 188 String url, 189 String userDN, 190 char[] pwd, 191 boolean tracing, 192 String referralType, 193 String aliasType, 194 boolean useSSL, 195 String cacerts, 196 String clientcerts, 197 char[] caKeystorePwd, 198 char[] clientKeystorePwd, 199 String caKeystoreType, 200 String clientKeystoreType, 201 boolean useGSSAPI, 202 Properties extraProperties) 203 { 204 this.version = version; 205 this.url = url; 206 this.userDN = userDN; 207 this.pwd = pwd; 208 this.referralType = referralType; 209 this.aliasType = aliasType; 210 this.useSSL = useSSL; 211 this.cacerts = cacerts; 212 this.clientcerts = clientcerts; 213 this.caKeystorePwd = caKeystorePwd; 214 this.clientKeystorePwd = clientKeystorePwd; 215 this.caKeystoreType = caKeystoreType; 216 this.clientKeystoreType = clientKeystoreType; 217 this.tracing = tracing; 218 219 this.sslTracing = tracing; 221 this.useGSSAPI = useGSSAPI; 222 this.extraProperties = extraProperties; 223 } 224 225 236 public ConnectionData(int version, 237 String url, 238 String userDN, 239 char[] pwd, 240 boolean tracing, 241 String referralType, 242 String aliasType) 243 { 244 this.version = version; 245 this.url = url; 246 this.userDN = userDN; 247 this.pwd = pwd; 248 this.referralType = referralType; 249 this.aliasType = aliasType; 250 this.sslTracing = tracing; } 252 253 254 public void setProtocol(String newProtocol) 255 { 256 if (newProtocol.equalsIgnoreCase(LDAP)) 257 protocol = LDAP; 258 else if (newProtocol.equalsIgnoreCase(DSML)) 259 protocol = DSML; 260 else 261 System.err.println("Unknown Protocol " + newProtocol); 262 } 263 264 274 275 public void clearPasswords() 276 { 277 if (pwd != null) for (int i = 0; i < pwd.length; i++) pwd[i] = ' '; if (caKeystorePwd != null) for (int i = 0; i < caKeystorePwd.length; i++) caKeystorePwd[i] = ' '; 279 if (clientKeystorePwd != null) for (int i = 0; i < clientKeystorePwd.length; i++) clientKeystorePwd[i] = ' '; 280 281 pwd = null; 282 caKeystorePwd = null; 283 clientKeystorePwd = null; 284 } 285 286 287 294 295 public void setURL(String host, int port) 296 { 297 if (protocol == LDAP) 298 url = "ldap://" + host + ":" + port; 299 else if (protocol == DSML) 300 url = "http://" + host + ":" + port; 301 302 } 303 304 310 311 public void setURL(String URL) 312 { 313 if (protocol == LDAP) 314 { 315 if (URL.toLowerCase().startsWith("ldap://")) 316 url = URL; 317 else 318 url = "ldap://" + URL; 319 } 320 else if (protocol == DSML) 321 { 322 if (URL.toLowerCase().startsWith("http://")) 323 url = URL; 324 else if (URL.toLowerCase().startsWith("dsml://")) 325 url = "http://" + URL.substring(7); 326 else 327 url = "http://" + URL; 328 } 329 else { 331 if (URL.toLowerCase().startsWith("ldap:")) 332 { 333 protocol = LDAP; 334 url = URL; 335 } 336 else if (URL.toLowerCase().startsWith("http:")) 337 { 338 protocol = DSML; 339 url = URL; 340 } 341 else if (URL.toLowerCase().startsWith("dsml:")) 342 { 343 protocol = DSML; 344 url = "http:" + URL.substring(5); 345 } 346 } 347 } 348 349 public String getURL() 350 { 351 return url; 352 } 353 354 359 360 362 public String getHost() 363 { 364 if (url == null) 365 return null; 366 367 int protocolSeparator = url.indexOf("://") + 3; 368 int portSeparator = url.indexOf(":", protocolSeparator); 369 return url.substring(protocolSeparator, portSeparator); 370 } 371 372 373 378 379 public int getPort() 380 { 381 if (url == null) 382 return -1; 383 384 try 385 { 386 int protocolSeparator = url.indexOf("://") + 3; 387 int portSeparator = url.indexOf(":", protocolSeparator) + 1; 388 int serverDetails = url.indexOf("/", portSeparator); 389 390 String port = (serverDetails == -1) ? url.substring(portSeparator) : url.substring(portSeparator, serverDetails); 391 int portNumber = Integer.parseInt(port); 392 if (portNumber > 65536 || portNumber <= 0) 393 return -1; 394 395 return portNumber; 396 } 397 catch (NumberFormatException nfe) 398 { 399 return -1; 400 } 401 } 402 403 404 409 410 public String toString() 411 { 412 return new String ("baseDN: " + baseDN + 413 "\nversion: " + Integer.toString(version) + 414 "\nurl: " + url + 415 "\nuserDN: " + userDN + 416 "\nreferralType: " + referralType + 417 "\naliasType: " + aliasType + 418 "\nuseSSL: " + String.valueOf(useSSL) + 419 "\ncacerts: " + cacerts + 420 "\nclientcerts: " + clientcerts + 421 "\ncaKeystoreType: " + caKeystoreType + 422 "\nclientKeystoreType: " + clientKeystoreType + 423 "\ncaKeystorePwd; " + new String (caKeystorePwd) + 424 "\nclientKeystorePwd: " + new String (clientKeystorePwd) + 425 "\ntracing: " + String.valueOf(tracing) + 426 "\nprotocol: " + protocol + 427 "\nsslSocketFactory: " + sslSocketFactory + 428 "\nuseGSSAPI: " + String.valueOf(useGSSAPI)); 429 } 430 431 439 public Hashtable getJNDIEnvironment() 440 throws NamingException 441 { 442 464 checkData(); 466 Hashtable env = new Hashtable(); 468 if (protocol == DSML) env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_DSML_CTX); 470 else if (protocol == LDAP) 471 env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_CTX); 472 473 JNDIOps.setupBasicProperties(env, url, tracing, referralType, aliasType); 475 476 if (pwd != null && userDN != null) 480 { 481 JNDIOps.setupSimpleSecurityProperties(env, userDN, pwd); 482 } 483 if (useSSL) 485 { 486 487 if (tracing) 488 sslTracing = true; 490 JNDIOps.setupSSLProperties(env, cacerts, clientcerts, 491 caKeystorePwd, clientKeystorePwd, 492 caKeystoreType, clientKeystoreType, 493 sslTracing, sslSocketFactory); 494 } 495 496 498 if (useGSSAPI) 499 { 500 env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI"); 501 } 503 504 if (extraProperties!=null && extraProperties.size()>0) 506 { 507 Enumeration extraKeys = extraProperties.keys(); 508 while (extraKeys.hasMoreElements()) 509 { 510 try 511 { 512 String key = (String )extraKeys.nextElement(); 513 String value = (String )extraProperties.getProperty(key); 514 if (value != null) 515 env.put(key, value); 516 } 517 catch (ClassCastException e) {} } 519 } 520 521 return env; 522 } 523 524 530 public void checkData() throws NamingException 531 { 532 if (url == null) 534 throw new NamingException ("URL not specified in openContext()!"); 535 536 if (version < 2 || version > 3) 537 throw new NamingException ("Incorrect ldap Version! (was " + version + ")"); 538 539 if (useSSL && (cacerts == null)) 540 throw new NamingException ("Cannot use SSL without a trusted CA certificates JKS file."); 541 542 if (referralType == null) referralType = "follow"; 544 if (aliasType == null) aliasType = "finding"; 546 if ("followthrowignore".indexOf(referralType) == -1) 547 throw new NamingException ("unknown referral type: " + referralType + " (setting to 'follow')"); 548 } 549 550 public void putExtraProperty(String key, String property) 551 { 552 if (extraProperties==null) 553 extraProperties = new Properties(); 554 555 extraProperties.put(key, property); 556 } 557 } | Popular Tags |