1 package net.suberic.pooka.ldap; 2 import net.suberic.pooka.*; 3 import net.suberic.util.VariableBundle; 4 5 import java.util.Hashtable ; 6 import java.util.Enumeration ; 7 import java.util.LinkedList ; 8 9 import javax.naming.*; 10 import javax.naming.directory.*; 11 12 13 16 public class LDAPAddressBook implements AddressBook, AddressMatcher { 17 18 String addressBookID; 19 20 InitialDirContext initialContext; 21 Hashtable env; 22 23 String ldapUrl; 24 String username; 25 String password; 26 27 30 public LDAPAddressBook() { 31 32 } 33 34 37 public void configureAddressBook(String newAddressBookID) { 38 addressBookID = newAddressBookID; 39 ldapUrl = Pooka.getProperty("AddressBook." + addressBookID + ".url", ""); 40 username = Pooka.getProperty("AddressBook." + addressBookID + ".username", ""); 41 password = Pooka.getProperty("AddressBook." + addressBookID + ".password", ""); 42 if (!password.equals("")) 43 password = net.suberic.util.gui.propedit.PasswordEditorPane.descrambleString(password); 44 45 47 env = new Hashtable (5, 0.75f); 48 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 49 50 51 env.put(Context.PROVIDER_URL, ldapUrl); 52 53 try { 54 55 initialContext = new InitialDirContext(env); 56 } catch (NamingException ne) { 57 Pooka.getUIFactory().showError(Pooka.getProperty("error.AddressBook.ldap.gettingInitialContext", "Failed to connect to LDAP server: ") + ne.getMessage()); 58 } 59 } 60 61 63 66 public AddressBookEntry[] match(String matchString) { 67 String searchString = "(sn=" + matchString + ")"; 69 70 try { 71 return doSearch(searchString); 72 } catch (NamingException ne) { 73 ne.printStackTrace(); 74 75 return new AddressBookEntry[0]; 76 } 77 } 78 79 82 public AddressBookEntry[] matchExactly(String matchString) { 83 return match(matchString); 84 } 85 86 90 public AddressBookEntry[] matchFirstName(String matchString) { 91 String searchString = "(sn=" + matchString + ")"; 93 94 try { 95 return doSearch(searchString); 96 } catch (NamingException ne) { 97 ne.printStackTrace(); 98 99 return new AddressBookEntry[0]; 100 } 101 } 102 103 107 public AddressBookEntry[] matchLastName(String matchString) { 108 String searchString = "(sn=" + matchString + ")"; 110 111 try { 112 return doSearch(searchString); 113 } catch (NamingException ne) { 114 ne.printStackTrace(); 115 116 return new AddressBookEntry[0]; 117 } 118 } 119 120 124 public AddressBookEntry[] matchEmailAddress(String matchString) { 125 String searchString = "(sn=" + matchString + ")"; 127 128 try { 129 return doSearch(searchString); 130 } catch (NamingException ne) { 131 ne.printStackTrace(); 132 133 return new AddressBookEntry[0]; 134 } 135 } 136 137 140 public AddressBookEntry getNextMatch(String matchString) { 141 return null; 142 } 143 144 148 public AddressBookEntry getPreviousMatch(String matchString) { 149 return null; 150 } 151 152 154 157 public AddressMatcher getAddressMatcher() { 158 return this; 159 } 160 161 164 public void addAddress(AddressBookEntry newEntry) { 165 166 } 167 168 171 public void removeAddress(AddressBookEntry removeEntry) { 172 173 } 174 175 178 public String getAddressBookID() { 179 return addressBookID; 180 } 181 182 185 public void loadAddressBook() throws java.io.IOException , java.text.ParseException { 186 187 } 188 189 192 public void saveAddressBook() throws java.io.IOException { 193 194 } 195 196 public AddressBookEntry[] doSearch(String searchString) throws NamingException { 197 198 199 SearchControls constraints = new SearchControls(); 200 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 201 202 NamingEnumeration results 203 = initialContext.search("o=Ace Industry, c=US", "(sn=" + searchString + ")", constraints); 204 205 LinkedList resultList = new LinkedList (); 206 207 while (results != null && results.hasMore()) { 208 SearchResult si = (SearchResult)results.next(); 209 210 Attributes att = si.getAttributes(); 211 212 LDAPAddressEntry entry = new LDAPAddressEntry(att); 213 214 resultList.add(entry); 215 } 216 217 AddressBookEntry[] returnValue = new AddressBookEntry[resultList.size()]; 218 219 resultList.toArray(returnValue); 220 221 return returnValue; 222 223 } 224 225 228 public AddressBookEntry newAddressBookEntry() { 229 return new LDAPAddressEntry(null); 230 } 231 232 } 233 | Popular Tags |