1 package com.ca.commons.jndi; 2 3 4 5 6 import javax.naming.*; 7 import javax.naming.directory.*; 8 import javax.security.auth.login.LoginContext ; 9 import javax.security.auth.login.LoginException ; 10 import javax.security.auth.Subject ; 11 import java.util.*; 12 import java.util.logging.Logger ; 13 14 26 27 public class JNDIOps 28 { 29 30 private static final String DEFAULT_CTX = "com.sun.jndi.ldap.LdapCtxFactory"; 31 32 36 private SearchControls existanceConstraints; 37 38 39 42 public static final String DEFAULT_REFERRAL_HANDLING = "ignore"; 43 44 47 public static final String DEFAULT_ALIAS_HANDLING = "finding"; 48 49 50 53 private NameParser nameParser; 54 55 private DirContext ctx = null; 58 59 private static Logger log = Logger.getLogger(JNDIOps.class.getName()); 61 { 63 existanceConstraints = new SearchControls(); 64 existanceConstraints.setSearchScope(SearchControls.OBJECT_SCOPE); 65 existanceConstraints.setCountLimit(0); 66 existanceConstraints.setTimeLimit(0); 67 existanceConstraints.setReturningAttributes(new String []{"1.1"}); } 69 70 73 74 public JNDIOps(DirContext c) 75 { 76 setContext(c); 77 } 78 79 85 86 92 93 public JNDIOps(Hashtable env) throws NamingException 94 { 95 96 if ((env.get(Context.SECURITY_AUTHENTICATION)).equals("GSSAPI")) 100 { 101 setupKerberosContext(env); 102 } 103 else 104 { 105 setContext(openContext(env)); } 107 } 108 109 117 protected void setupKerberosContext(Hashtable env) 118 throws NamingException 119 { 120 log.finest("dumping kerberos environment keys"); 122 Enumeration keys = env.keys(); 123 while (keys.hasMoreElements()) 124 { 125 String key = (String )keys.nextElement(); 126 log.finest(key + " : " + env.get(key)); 127 } 128 129 130 132 LoginContext lc = null; 133 try 134 { 135 lc = new LoginContext (JNDIOps.class.getName(), 136 new JXCallbackHandler()); 137 lc.login(); 138 } 139 catch (LoginException ex) 140 { 141 ex.printStackTrace(); 142 throw new NamingException("login problem: " + ex); 143 } 144 145 DirContext newCtx = (DirContext) Subject.doAs(lc.getSubject(), new JndiAction(env)); 146 147 148 if (newCtx == null) 149 { 150 throw new NamingException("a problem with GSSAPI occurred - couldn't create a GSSAPI directory context"); 151 } 153 setContext(newCtx); 154 155 156 if (ctx == null) 157 { 158 throw new NamingException("another problem with GSSAPI occurred"); } } 161 162 168 169 public JNDIOps(String url) 170 throws NamingException 171 { 172 Hashtable env = new Hashtable(); 174 setupBasicProperties(env, url); 176 setContext(openContext(env)); } 178 179 189 190 public JNDIOps(String url, String userDN, char[] pwd) 191 throws NamingException 192 { 193 194 Hashtable env = new Hashtable(); 196 setupBasicProperties(env, url); 198 setupSimpleSecurityProperties(env, userDN, pwd); 200 setContext(openContext(env)); } 202 203 219 220 public JNDIOps(String url, 221 String cacerts, String clientcerts, 222 char[] caKeystorePwd, char[] clientKeystorePwd, 223 String caKeystoreType, String clientKeystoreType, 224 boolean tracing, boolean sslTracing, String sslSocketFactory) 225 throws NamingException 226 { 227 228 Hashtable env = new Hashtable(); 230 231 setupBasicProperties(env, url, tracing, DEFAULT_REFERRAL_HANDLING, DEFAULT_ALIAS_HANDLING); 233 setupSSLProperties(env, cacerts, clientcerts, 235 caKeystorePwd, clientKeystorePwd, 236 caKeystoreType, clientKeystoreType, 237 sslTracing, sslSocketFactory); 238 239 setContext(openContext(env)); } 241 242 247 public static void setupBasicProperties(Hashtable env, String url) 248 throws NamingException 249 { 250 setupBasicProperties(env, url, false, DEFAULT_REFERRAL_HANDLING, DEFAULT_ALIAS_HANDLING); 251 } 252 253 254 266 267 285 public static String makeServerURL(String serverURL, String baseDN) 286 { 287 if (baseDN != null && baseDN.length() > 0) 288 { 289 if (serverURL.length()>7 && serverURL.endsWith("/")) 291 serverURL = serverURL.substring(0, serverURL.length()-1); 292 293 baseDN = baseDN.replaceAll("[%]", "%25"); 295 296 baseDN = baseDN.replaceAll(" ", "%20"); 297 baseDN = baseDN.replaceAll("[<]", "%3c"); 298 baseDN = baseDN.replaceAll("[>]", "%3e"); 299 baseDN = baseDN.replaceAll("[\"]", "%3f"); 300 baseDN = baseDN.replaceAll("[#]", "%23"); 301 baseDN = baseDN.replaceAll("[{]", "%7b"); 302 baseDN = baseDN.replaceAll("[}]", "%7d"); 303 baseDN = baseDN.replaceAll("[|]", "%7c"); 304 baseDN = baseDN.replaceAll("[\\\\]", "%5c"); baseDN = baseDN.replaceAll("[\\^]", "%5e"); 306 baseDN = baseDN.replaceAll("[~]", "%7e"); 307 baseDN = baseDN.replaceAll("[\\[]", "%5b"); 308 baseDN = baseDN.replaceAll("[\\]]", "%5d"); 309 baseDN = baseDN.replaceAll("[']", "%27"); 310 311 baseDN = baseDN.replaceAll("[?]", "%3f"); 312 313 serverURL = serverURL + "/" + baseDN; 314 } 315 316 return serverURL; 317 318 319 } 320 321 333 334 335 public static void setupBasicProperties(Hashtable env, String url, boolean tracing, String referralType, String aliasType) 336 throws NamingException 337 { 338 if (url == null) 340 throw new NamingException("URL not specified in openContext()!"); 341 342 if (tracing) 344 env.put("com.sun.jndi.ldap.trace.ber", System.err); 345 346 env.put("java.naming.ldap.version", "3"); 348 if (env.get(Context.INITIAL_CONTEXT_FACTORY) == null) 349 env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_CTX); 351 env.put("java.naming.ldap.deleteRDN", "false"); 353 env.put(Context.REFERRAL, referralType); 355 env.put("java.naming.ldap.attributes.binary", "photo jpegphoto jpegPhoto"); 357 env.put("java.naming.ldap.derefAliases", aliasType); 359 env.put(Context.SECURITY_AUTHENTICATION, "none"); 361 env.put(Context.PROVIDER_URL, url); } 363 364 376 public static void setupSimpleSecurityProperties(Hashtable env, String userDN, char[] pwd) 377 { 378 env.put(Context.SECURITY_AUTHENTICATION, "simple"); 380 env.put(Context.SECURITY_PRINCIPAL, userDN); 382 env.put(Context.SECURITY_CREDENTIALS, new String (pwd)); } 385 386 400 public static void setupSSLProperties(Hashtable env, 401 String cacerts, String clientcerts, 402 char[] caKeystorePwd, char[] clientKeystorePwd, 403 String caKeystoreType, String clientKeystoreType, 404 boolean tracing, boolean sslTracing, 405 String sslSocketFactory) 406 throws NamingException 407 { 408 setupSSLProperties(env, cacerts, clientcerts, caKeystorePwd, clientKeystorePwd, caKeystoreType, clientKeystoreType, sslTracing, sslSocketFactory); 409 } 410 411 432 433 public static void setupSSLProperties(Hashtable env, 434 String cacerts, String clientcerts, 435 char[] caKeystorePwd, char[] clientKeystorePwd, 436 String caKeystoreType, String clientKeystoreType, 437 boolean sslTracing, 438 String sslSocketFactory) 439 throws NamingException 440 { 441 442 if (cacerts == null) 444 throw new NamingException("Cannot use SSL without a trusted CA certificates JKS file."); 445 446 env.put(Context.SECURITY_PROTOCOL, "ssl"); 448 449 452 if (sslSocketFactory.equals("com.ca.commons.jndi.JndiSocketFactory")) 453 { 454 JndiSocketFactory.init(cacerts, clientcerts, 455 caKeystorePwd, clientKeystorePwd, 456 caKeystoreType, clientKeystoreType); 457 } 458 459 env.put("java.naming.ldap.factory.socket", sslSocketFactory); 461 462 if (clientcerts != null && (clientKeystorePwd != null && clientKeystorePwd.length > 0)) 464 { 465 env.put(Context.SECURITY_AUTHENTICATION, "EXTERNAL"); } 467 468 if (sslTracing) 469 { 470 System.setProperty("javax.net.debug", "ssl handshake verbose"); 472 } 473 } 474 475 476 485 486 public static DirContext openContext(Hashtable env) 487 throws NamingException 488 { 489 502 503 504 DirContext ctx = new InitialDirContext(env); 505 506 if (ctx == null) 507 throw new NamingException("Internal Error with jndi connection: No Context was returned, however no exception was reported by jndi."); 508 509 return ctx; 510 } 511 512 513 525 526 public void renameEntry(Name oldDN, Name newDN) 527 throws NamingException 528 { 529 Name rdn = newDN.getSuffix(newDN.size() - 1); 530 Name oldRdn = oldDN.getSuffix(oldDN.size() - 1); 531 532 if (oldRdn.toString().equals(rdn.toString()) == false) ctx.rename(oldDN, rdn); 534 } 535 536 537 544 545 public void copyEntry(Name fromDN, Name toDN) 546 throws NamingException 547 { 548 addEntry(toDN, read(fromDN)); 549 } 550 551 552 559 560 public void addEntry(Name dn, Attributes atts) 561 throws NamingException 562 { 563 ctx.createSubcontext(dn, atts); 564 } 565 566 571 572 public void deleteEntry(Name dn) 573 throws NamingException 574 { 575 ctx.destroySubcontext(dn); 576 } 577 578 579 586 587 public boolean exists(Name nodeDN) 588 throws NamingException 589 { 590 591 try 592 { 593 ctx.search(nodeDN, "(objectclass=*)", existanceConstraints); 594 return true; 595 } 596 catch (NoSuchAttributeException e) { return true; 599 } 600 catch (NameNotFoundException e) { 602 return false; 603 } 604 610 catch (NullPointerException e) 611 { 612 if ((ctx != null) && (ctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY).toString().indexOf("dsml") > 0)) 613 return false; 614 else 615 throw e; 616 } 617 } 618 619 620 627 628 public boolean exists(String nodeDN) 629 throws NamingException 630 { 631 try 632 { 633 ctx.search(nodeDN, "(objectclass=*)", existanceConstraints); 634 return true; 635 } 636 catch (NameNotFoundException e) { 638 return false; 639 } 640 646 catch (NullPointerException e) 647 { 648 if ((ctx != null) && (ctx.getEnvironment().get(Context.INITIAL_CONTEXT_FACTORY).toString().indexOf("dsml") > 0)) 649 return false; 650 else 651 throw e; 652 } 653 } 654 655 662 663 public synchronized Attributes read(Name dn) 664 throws NamingException 665 { 666 return read(dn, null); 667 } 668 669 670 678 679 public synchronized Attributes read(Name dn, String [] returnAttributes) 680 throws NamingException 681 { 682 return ctx.getAttributes(dn, returnAttributes); 683 } 684 685 695 696 public void modifyAttributes(Name dn, int mod_type, Attributes attr) 697 throws NamingException 698 { 699 ctx.modifyAttributes(dn, mod_type, attr); 700 } 701 702 703 710 711 public void modifyAttributes(Name dn, ModificationItem[] modList) 712 throws NamingException 713 { 714 ctx.modifyAttributes(dn, modList); 715 } 716 717 723 724 public void updateEntry(Name dn, Attributes atts) 725 throws NamingException 726 { 727 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, atts); 728 } 729 730 731 737 738 public void deleteAttribute(Name dn, Attribute a) 739 throws NamingException 740 { 741 BasicAttributes atts = new BasicAttributes(); 742 atts.put(a); 743 modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, atts); 744 } 745 746 753 754 755 public void deleteAttributes(Name dn, Attributes a) 756 throws NamingException 757 { 758 modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, a); 759 } 760 761 767 768 public void updateAttribute(Name dn, Attribute a) 769 throws NamingException 770 { 771 BasicAttributes atts = new BasicAttributes(); 772 atts.put(a); 773 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, atts); 774 } 775 776 782 783 public void updateAttributes(Name dn, Attributes a) 784 throws NamingException 785 { 786 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, a); 787 } 788 789 795 796 public void addAttribute(Name dn, Attribute a) 797 throws NamingException 798 { 799 BasicAttributes atts = new BasicAttributes(); 800 atts.put(a); 801 modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, atts); 802 } 803 804 810 811 public void addAttributes(Name dn, Attributes a) 812 throws NamingException 813 { 814 modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, a); 815 } 816 817 818 826 827 public NamingEnumeration list(Name Searchbase) 828 throws NamingException 829 { 830 833 836 return rawSearchOneLevel(Searchbase, "(objectclass=*)", 0, 0, new String []{"objectclass"}); 838 } 839 840 841 851 852 public NamingEnumeration searchOneLevel(String searchbase, String filter, int limit, int timeout) 853 throws NamingException 854 { 855 return searchOneLevel(searchbase, filter, limit, timeout, new String []{"1.1"}); 856 } 857 858 868 869 870 public NamingEnumeration searchOneLevel(String searchbase, String filter, int limit, 871 int timeout, String [] returnAttributes) 872 throws NamingException 873 { 874 return rawSearchOneLevel(nameParser.parse(searchbase), filter, limit, timeout, returnAttributes); 875 } 876 877 878 888 889 public NamingEnumeration searchOneLevel(Name searchbase, String filter, int limit, int timeout) 890 throws NamingException 891 { 892 return rawSearchOneLevel(searchbase, filter, limit, timeout, new String []{"1.1"}); 893 } 894 895 905 906 907 public NamingEnumeration searchOneLevel(Name searchbase, String filter, int limit, 908 int timeout, String [] returnAttributes) 909 throws NamingException 910 { 911 return rawSearchOneLevel(searchbase, filter, limit, timeout, returnAttributes); 912 } 913 914 925 protected NamingEnumeration rawSearchOneLevel(Name searchbase, String filter, int limit, 926 int timeout, String [] returnAttributes) throws NamingException 927 { 928 929 SearchControls constraints = new SearchControls(); 930 931 constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE); 932 constraints.setCountLimit(limit); 933 constraints.setTimeLimit(timeout); 934 935 constraints.setReturningAttributes(returnAttributes); 936 937 NamingEnumeration results = ctx.search(searchbase, filter, constraints); 939 940 return results; 941 942 } 943 944 954 public NamingEnumeration searchSubTree(Name searchbase, String filter, int limit, int timeout) 955 throws NamingException 956 { 957 return searchSubTree(searchbase, filter, limit, timeout, new String []{"1.1"}); 958 } 959 960 970 public NamingEnumeration searchSubTree(String searchbase, String filter, int limit, int timeout) 971 throws NamingException 972 { 973 return searchSubTree((searchbase), filter, limit, timeout, new String []{"1.1"}); 974 } 975 976 986 987 public NamingEnumeration searchSubTree(String searchbase, String filter, int limit, 988 int timeout, String [] returnAttributes) 989 throws NamingException 990 { 991 return rawSearchSubTree(nameParser.parse(searchbase), filter, limit, timeout, returnAttributes); 992 993 995 } 997 998 1008 1009 public NamingEnumeration searchSubTree(Name searchbase, String filter, int limit, 1010 int timeout, String [] returnAttributes) 1011 throws NamingException 1012 { 1013 return rawSearchSubTree(searchbase, filter, limit, timeout, returnAttributes); 1014 } 1015 1016 protected NamingEnumeration rawSearchSubTree(Name searchbase, String filter, int limit, 1017 int timeout, String [] returnAttributes) throws NamingException 1018 { 1019 if (returnAttributes != null && returnAttributes.length == 0) 1020 returnAttributes = new String []{"objectClass"}; 1021 1022 1023 SearchControls constraints1 = new SearchControls(); 1024 1025 constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE); 1026 constraints1.setCountLimit(limit); 1027 constraints1.setTimeLimit(timeout); 1028 1029 constraints1.setReturningAttributes(returnAttributes); 1030 SearchControls constraints = constraints1; 1031 1032 return ctx.search(searchbase, filter, constraints); 1033 } 1034 1035 1036 1046 1047 public NamingEnumeration searchBaseEntry(Name searchbase, String filter, int limit, int timeout) 1048 throws NamingException 1049 { 1050 return rawSearchBaseEntry(searchbase, filter, limit, timeout, new String []{"objectClass"}); 1051 } 1052 1053 1054 1064 1065 public NamingEnumeration searchBaseEntry(Name searchbase, String filter, int limit, 1066 int timeout, String [] returnAttributes) 1067 throws NamingException 1068 { 1069 return rawSearchBaseEntry(searchbase, filter, limit, timeout, returnAttributes); 1070 } 1071 1072 1082 1083 protected NamingEnumeration rawSearchBaseEntry(Name searchbase, String filter, int limit, 1084 int timeout, String [] returnAttributes) 1085 throws NamingException 1086 { 1087 NamingEnumeration result = null; 1088 1089 if (returnAttributes != null && returnAttributes.length == 0) 1090 returnAttributes = new String []{"objectClass"}; 1091 1092 1093 SearchControls constraints = new SearchControls(); 1094 1095 constraints.setSearchScope(SearchControls.OBJECT_SCOPE); 1096 constraints.setCountLimit(limit); 1097 constraints.setTimeLimit(timeout); 1098 1099 constraints.setReturningAttributes(returnAttributes); 1100 1101 result = ctx.search(searchbase, filter, constraints); 1102 1103 return result; 1104 } 1105 1106 1107 1117 1118 public NamingEnumeration searchBaseEntry(String searchbase, String filter, int limit, int timeout) 1119 throws NamingException 1120 { 1121 return rawSearchBaseEntry(nameParser.parse(searchbase), filter, limit, timeout, new String []{"objectClass"}); 1122 } 1123 1124 1125 1135 1136 public NamingEnumeration searchBaseEntry(String searchbase, String filter, int limit, 1137 int timeout, String [] returnAttributes) 1138 throws NamingException 1139 { 1140 return rawSearchBaseEntry(nameParser.parse(searchbase), filter, limit, timeout, returnAttributes); 1141 1160 } 1161 1162 1163 1172 1173 public void renameEntry(Name OldDN, Name NewDN, boolean deleteOldRDN) 1174 throws NamingException 1175 { 1176 String value = (deleteOldRDN) ? "true" : "false"; 1177 try 1178 { 1179 ctx.addToEnvironment("java.naming.ldap.deleteRDN", value); 1180 1181 renameEntry(OldDN, NewDN); 1182 1183 ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false"); } 1185 catch (NamingException e) 1186 { 1187 ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false"); throw e; 1189 } 1190 } 1191 1192 1193 1194 1195 1196 1198 1199 1210 1211 public void renameEntry(String oldDN, String newDN) 1212 throws NamingException 1213 { 1214 ctx.rename(oldDN, newDN); 1215 } 1216 1217 1218 1225 1226 public void copyEntry(String fromDN, String toDN) 1227 throws NamingException 1228 { 1229 addEntry(toDN, read(fromDN)); 1230 } 1231 1232 1233 1240 1241 public void addEntry(String dn, Attributes atts) 1242 throws NamingException 1243 { 1244 ctx.createSubcontext(dn, atts); 1245 } 1246 1247 1252 1253 public void deleteEntry(String dn) 1254 throws NamingException 1255 { 1256 ctx.destroySubcontext(dn); 1257 } 1258 1259 1260 1267 1268 public synchronized Attributes read(String dn) 1269 throws NamingException 1270 { 1271 return read(dn, null); 1272 } 1273 1274 1275 1283 1284 public synchronized Attributes read(String dn, String [] returnAttributes) 1285 throws NamingException 1286 { 1287 return ctx.getAttributes(dn, returnAttributes); 1288 } 1289 1290 1300 1301 public void modifyAttributes(String dn, int mod_type, Attributes attr) 1302 throws NamingException 1303 { 1304 ctx.modifyAttributes(dn, mod_type, attr); 1305 } 1306 1307 1308 1315 1316 public void modifyAttributes(String dn, ModificationItem[] modList) 1317 throws NamingException 1318 { 1319 ctx.modifyAttributes(dn, modList); 1320 } 1321 1322 1328 1329 public void updateEntry(String dn, Attributes atts) 1330 throws NamingException 1331 { 1332 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, atts); 1333 } 1334 1335 1336 1342 1343 public void deleteAttribute(String dn, Attribute a) 1344 throws NamingException 1345 { 1346 BasicAttributes atts = new BasicAttributes(); 1347 atts.put(a); 1348 modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, atts); 1349 } 1350 1351 1358 1359 1360 public void deleteAttributes(String dn, Attributes a) 1361 throws NamingException 1362 { 1363 modifyAttributes(dn, DirContext.REMOVE_ATTRIBUTE, a); 1364 } 1365 1366 1372 1373 public void updateAttribute(String dn, Attribute a) 1374 throws NamingException 1375 { 1376 BasicAttributes atts = new BasicAttributes(); 1377 atts.put(a); 1378 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, atts); 1379 } 1380 1381 1387 1388 public void updateAttributes(String dn, Attributes a) 1389 throws NamingException 1390 { 1391 modifyAttributes(dn, DirContext.REPLACE_ATTRIBUTE, a); 1392 } 1393 1394 1400 1401 public void addAttribute(String dn, Attribute a) 1402 throws NamingException 1403 { 1404 BasicAttributes atts = new BasicAttributes(); 1405 atts.put(a); 1406 modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, atts); 1407 } 1408 1409 1415 1416 public void addAttributes(String dn, Attributes a) 1417 throws NamingException 1418 { 1419 modifyAttributes(dn, DirContext.ADD_ATTRIBUTE, a); 1420 } 1421 1422 1423 1433 1434 public NamingEnumeration list(String searchbase) 1435 throws NamingException 1436 { 1437 1440 return rawSearchOneLevel(nameParser.parse(searchbase), "(objectclass=*)", 0, 0, new String []{"1.1"}); 1441 } 1442 1443 1454 1473 1474 1475 1476 1480 1481 public void close() 1482 throws NamingException 1483 { 1484 if (ctx == null) return; nameParser = null; 1486 ctx.close(); 1487 } 1489 1490 1499 1500 public void renameEntry(String OldDN, String NewDN, boolean deleteOldRDN) 1501 throws NamingException 1502 { 1503 String value = (deleteOldRDN) ? "true" : "false"; 1504 try 1505 { 1506 ctx.addToEnvironment("java.naming.ldap.deleteRDN", value); 1507 1508 renameEntry(OldDN, NewDN); 1509 1510 ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false"); } 1512 catch (NamingException e) 1513 { 1514 ctx.addToEnvironment("java.naming.ldap.deleteRDN", "false"); throw e; } 1517 } 1518 1519 1522 private static Properties nameParserSyntax = null; 1523 1524 1529 private static void setupLDAPSyntax() 1530 { 1531 nameParserSyntax = new Properties(); 1532 1533 nameParserSyntax.put("jndi.syntax.direction", "right_to_left"); 1534 nameParserSyntax.put("jndi.syntax.separator", ","); 1535 nameParserSyntax.put("jndi.syntax.escape", "\\"); 1536 1537 1542 nameParserSyntax.put("jndi.syntax.trimblanks", "true"); 1543 nameParserSyntax.put("jndi.syntax.separator.typeval", "="); 1544 } 1545 1546 1554 public static Name getNameFromString(String iDN) 1556 throws NamingException 1557 { 1558 1564 1566 String DN = iDN; 1567 Name CompositeFormDN = null; 1568 CompoundName CompoundFormDN = null; 1569 1570 if (iDN.indexOf("ldap://") != -1) 1571 { 1572 CompositeFormDN = new CompositeName(iDN); 1576 if (CompositeFormDN.size() != 0) 1577 DN = CompositeFormDN.get(CompositeFormDN.size() - 1); 1578 } 1579 1580 if (nameParserSyntax == null) 1581 setupLDAPSyntax(); 1582 1583 CompoundFormDN = new CompoundName(DN, nameParserSyntax); 1584 1585 return CompoundFormDN; 1586 } 1587 1588 1597 public static Name getNameFromSearchResult(SearchResult iDirectoryEntry, Name iBaseDN) 1599 throws InvalidNameException, NamingException 1600 { 1601 1606 String RDN = applyJNDIRDNBugWorkAround(iDirectoryEntry.getName()); 1608 1609 Name JNDIRDN = getNameFromString(RDN); 1610 1611 if (JNDIRDN != null) 1612 { if (iDirectoryEntry.isRelative()) 1614 JNDIRDN.addAll(0, iBaseDN); 1615 } 1616 else 1617 JNDIRDN = (Name) iBaseDN.clone(); 1619 return JNDIRDN; 1620 } 1621 1622 1630 private static String applyJNDIRDNBugWorkAround(String iRDN) 1631 { 1632 1633 1641 int SlashPos = iRDN.lastIndexOf("\\\\"); String ReturnString; 1644 1645 if (SlashPos == iRDN.length() - 2) 1646 ReturnString = iRDN.substring(0, SlashPos); 1647 else 1648 ReturnString = iRDN; 1649 1650 return ReturnString; 1651 } 1652 1653 1654 public DirContext getContext() 1655 { 1656 return ctx; 1657 } 1658 1659 public void setContext(DirContext ctx) 1660 { 1661 this.ctx = ctx; 1662 1663 try 1664 { 1665 nameParser = ctx.getNameParser(""); 1666 } 1667 catch (NamingException e) 1668 { 1669 System.out.println("Error initialising name parser " + e); 1671 } 1672 } 1673 1674 1675} 1676 | Popular Tags |