1 11 12 package org.jivesoftware.messenger.ldap; 13 14 import org.jivesoftware.messenger.user.*; 15 import org.jivesoftware.util.JiveConstants; 16 import org.jivesoftware.util.JiveGlobals; 17 import org.jivesoftware.util.Log; 18 import org.xmpp.packet.JID; 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.directory.*; 22 import javax.naming.ldap.Control ; 23 import javax.naming.ldap.LdapContext ; 24 import javax.naming.ldap.SortControl ; 25 import java.text.MessageFormat ; 26 import java.util.*; 27 28 34 public class LdapUserProvider implements UserProvider { 35 36 private LdapManager manager; 37 private Map<String , String > searchFields; 38 private int userCount = -1; 39 private long expiresStamp = System.currentTimeMillis(); 40 41 public LdapUserProvider() { 42 manager = LdapManager.getInstance(); 43 searchFields = new LinkedHashMap<String ,String >(); 44 String fieldList = JiveGlobals.getXMLProperty("ldap.searchFields"); 45 if (fieldList == null) { 47 searchFields.put("Username", manager.getUsernameField()); 48 searchFields.put("Name", manager.getNameField()); 49 searchFields.put("Email", manager.getEmailField()); 50 } 51 else { 52 try { 53 for (StringTokenizer i=new StringTokenizer(fieldList, ","); i.hasMoreTokens(); ) { 54 String [] field = i.nextToken().split("/"); 55 searchFields.put(field[0], field[1]); 56 } 57 } 58 catch (Exception e) { 59 Log.error("Error parsing LDAP search fields: " + fieldList, e); 60 } 61 } 62 } 63 64 public User loadUser(String username) throws UserNotFoundException { 65 username = JID.unescapeNode(username); 67 DirContext ctx = null; 68 try { 69 String userDN = manager.findUserDN(username); 70 String [] attributes = new String []{ 72 manager.getUsernameField(), manager.getNameField(), 73 manager.getEmailField() 74 }; 75 ctx = manager.getContext(); 76 Attributes attrs = ctx.getAttributes(userDN, attributes); 77 String name = null; 78 String email = null; 79 Attribute nameField = attrs.get(manager.getNameField()); 80 if (nameField != null) { 81 name = (String )nameField.get(); 82 } 83 Attribute emailField = attrs.get(manager.getEmailField()); 84 if (emailField != null) { 85 email = (String )emailField.get(); 86 } 87 return new User(username, name, email, new Date(), new Date()); 88 } 89 catch (Exception e) { 90 throw new UserNotFoundException(e); 91 } 92 finally { 93 try { ctx.close(); } 94 catch (Exception ignored) { } 95 } 96 } 97 98 public User createUser(String username, String password, String name, String email) 99 throws UserAlreadyExistsException 100 { 101 throw new UnsupportedOperationException (); 102 } 103 104 public void deleteUser(String username) { 105 throw new UnsupportedOperationException (); 106 } 107 108 public int getUserCount() { 109 if (userCount != -1 && System.currentTimeMillis() < expiresStamp) { 111 return userCount; 112 } 113 int count = 0; 114 DirContext ctx = null; 115 try { 116 ctx = manager.getContext(); 117 SearchControls constraints = new SearchControls(); 119 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 120 constraints.setReturningAttributes(new String [] { manager.getUsernameField() }); 121 String filter = MessageFormat.format(manager.getSearchFilter(), "*"); 122 NamingEnumeration answer = ctx.search("", filter, constraints); 123 while (answer.hasMoreElements()) { 124 count++; 125 answer.nextElement(); 126 } 127 } 128 catch (Exception e) { 129 Log.error(e); 130 } 131 finally { 132 try { ctx.close(); } 133 catch (Exception ignored) { } 134 } 135 this.userCount = count; 136 this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5; 137 return count; 138 } 139 140 public Collection<User> getUsers() { 141 List<String > usernames = new ArrayList<String >(); 142 LdapContext ctx = null; 143 try { 144 ctx = manager.getContext(); 145 Control [] searchControl = new Control []{ 147 new SortControl (new String []{manager.getUsernameField()}, Control.NONCRITICAL) 148 }; 149 ctx.setRequestControls(searchControl); 150 151 SearchControls constraints = new SearchControls(); 153 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 154 constraints.setReturningAttributes(new String [] { manager.getUsernameField() }); 155 String filter = MessageFormat.format(manager.getSearchFilter(), "*"); 156 NamingEnumeration answer = ctx.search("", filter, constraints); 157 while (answer.hasMoreElements()) { 158 String username = (String )((SearchResult)answer.next()).getAttributes().get( 160 manager.getUsernameField()).get(); 161 usernames.add(JID.escapeNode(username)); 163 } 164 } 165 catch (Exception e) { 166 Log.error(e); 167 } 168 finally { 169 try { 170 if (ctx != null) { 171 ctx.setRequestControls(null); 172 ctx.close(); 173 } 174 } 175 catch (Exception ignored) { } 176 } 177 if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting")).booleanValue()) { 179 Collections.sort(usernames); 180 } 181 return new UserCollection((String [])usernames.toArray(new String [usernames.size()])); 182 } 183 184 public Collection<User> getUsers(int startIndex, int numResults) { 185 List<String > usernames = new ArrayList<String >(); 186 LdapContext ctx = null; 187 try { 188 ctx = manager.getContext(); 189 Control [] searchControl = new Control []{ 191 new SortControl (new String []{manager.getUsernameField()}, Control.NONCRITICAL) 192 }; 193 ctx.setRequestControls(searchControl); 194 195 SearchControls constraints = new SearchControls(); 197 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 198 constraints.setReturningAttributes(new String [] { manager.getUsernameField() }); 199 if (!Boolean.valueOf(JiveGlobals.getXMLProperty( 202 "ldap.clientSideSorting")).booleanValue()) 203 { 204 constraints.setCountLimit(startIndex+numResults); 205 } 206 String filter = MessageFormat.format(manager.getSearchFilter(), "*"); 207 NamingEnumeration answer = ctx.search("", filter, constraints); 208 if (Boolean.valueOf(JiveGlobals.getXMLProperty( 210 "ldap.clientSideSorting")).booleanValue()) 211 { 212 while (answer.hasMoreElements()) { 213 String username = (String )((SearchResult)answer.next()).getAttributes().get( 215 manager.getUsernameField()).get(); 216 usernames.add(JID.escapeNode(username)); 218 } 219 Collections.sort(usernames); 220 usernames = usernames.subList(startIndex, startIndex+numResults); 221 } 222 else { 224 for (int i=0; i < startIndex; i++) { 225 if (answer.hasMoreElements()) { 226 answer.next(); 227 } 228 else { 229 return Collections.emptyList(); 230 } 231 } 232 for (int i = 0; i < numResults; i++) { 234 if (answer.hasMoreElements()) { 235 String username = (String )((SearchResult)answer.next()).getAttributes().get( 237 manager.getUsernameField()).get(); 238 usernames.add(JID.escapeNode(username)); 240 } 241 else { 242 break; 243 } 244 } 245 } 246 } 247 catch (Exception e) { 248 Log.error(e); 249 } 250 finally { 251 try { 252 if (ctx != null) { 253 ctx.setRequestControls(null); 254 ctx.close(); 255 } 256 } 257 catch (Exception ignored) { } 258 } 259 return new UserCollection((String [])usernames.toArray(new String [usernames.size()])); 260 } 261 262 public String getPassword(String username) throws UserNotFoundException, 263 UnsupportedOperationException 264 { 265 throw new UnsupportedOperationException (); 266 } 267 268 public void setPassword(String username, String password) throws UserNotFoundException { 269 throw new UnsupportedOperationException (); 270 } 271 272 public void setName(String username, String name) throws UserNotFoundException { 273 throw new UnsupportedOperationException (); 274 } 275 276 public void setEmail(String username, String email) throws UserNotFoundException { 277 throw new UnsupportedOperationException (); 278 } 279 280 public void setCreationDate(String username, Date creationDate) throws UserNotFoundException { 281 throw new UnsupportedOperationException (); 282 } 283 284 public void setModificationDate(String username, Date modificationDate) throws UserNotFoundException { 285 throw new UnsupportedOperationException (); 286 } 287 288 public Set<String > getSearchFields() throws UnsupportedOperationException { 289 return Collections.unmodifiableSet(searchFields.keySet()); 290 } 291 292 public Collection<User> findUsers(Set<String > fields, String query) 293 throws UnsupportedOperationException 294 { 295 if (fields.isEmpty()) { 296 return Collections.emptyList(); 297 } 298 if (!searchFields.keySet().containsAll(fields)) { 299 throw new IllegalArgumentException ("Search fields " + fields + " are not valid."); 300 } 301 List<String > usernames = new ArrayList<String >(); 302 LdapContext ctx = null; 303 try { 304 ctx = manager.getContext(); 305 Control [] searchControl = new Control []{ 307 new SortControl (new String []{manager.getUsernameField()}, Control.NONCRITICAL) 308 }; 309 ctx.setRequestControls(searchControl); 310 311 SearchControls constraints = new SearchControls(); 313 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 314 constraints.setReturningAttributes(new String [] { manager.getUsernameField() }); 315 StringBuilder filter = new StringBuilder (); 316 if (fields.size() > 1) { 317 filter.append("(|"); 318 } 319 for (String field:fields) { 320 String attribute = searchFields.get(field); 321 filter.append("(").append(attribute).append("=").append(query).append(")"); 322 } 323 if (fields.size() > 1) { 324 filter.append(")"); 325 } 326 NamingEnumeration answer = ctx.search("", filter.toString(), constraints); 327 while (answer.hasMoreElements()) { 328 String username = (String )((SearchResult)answer.next()).getAttributes().get( 330 manager.getUsernameField()).get(); 331 usernames.add(JID.escapeNode(username)); 333 } 334 if (Boolean.valueOf(JiveGlobals.getXMLProperty( 336 "ldap.clientSideSorting")).booleanValue()) 337 { 338 Collections.sort(usernames); 339 } 340 } 341 catch (Exception e) { 342 Log.error(e); 343 } 344 finally { 345 try { 346 if (ctx != null) { 347 ctx.setRequestControls(null); 348 ctx.close(); 349 } 350 } 351 catch (Exception ignored) { } 352 } 353 return new UserCollection((String [])usernames.toArray(new String [usernames.size()])); 354 } 355 356 public Collection<User> findUsers(Set<String > fields, String query, int startIndex, 357 int numResults) throws UnsupportedOperationException 358 { 359 if (fields.isEmpty()) { 360 return Collections.emptyList(); 361 } 362 if (!searchFields.keySet().containsAll(fields)) { 363 throw new IllegalArgumentException ("Search fields " + fields + " are not valid."); 364 } 365 List<String > usernames = new ArrayList<String >(); 366 LdapContext ctx = null; 367 try { 368 ctx = manager.getContext(); 369 Control [] searchControl = new Control []{ 371 new SortControl (new String []{manager.getUsernameField()}, Control.NONCRITICAL) 372 }; 373 ctx.setRequestControls(searchControl); 374 375 SearchControls constraints = new SearchControls(); 377 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 378 constraints.setReturningAttributes(new String [] { manager.getUsernameField() }); 379 StringBuilder filter = new StringBuilder (); 380 if (fields.size() > 1) { 381 filter.append("(|"); 382 } 383 for (String field:fields) { 384 String attribute = searchFields.get(field); 385 filter.append("(").append(attribute).append("=").append(query).append(")"); 386 } 387 if (fields.size() > 1) { 388 filter.append(")"); 389 } 390 NamingEnumeration answer = ctx.search("", filter.toString(), constraints); 392 for (int i=0; i < startIndex; i++) { 393 if (answer.hasMoreElements()) { 394 answer.next(); 395 } 396 else { 397 return Collections.emptyList(); 398 } 399 } 400 for (int i = 0; i < numResults; i++) { 402 if (answer.hasMoreElements()) { 403 String username = (String )((SearchResult)answer.next()).getAttributes().get( 405 manager.getUsernameField()).get(); 406 usernames.add(JID.escapeNode(username)); 408 } 409 else { 410 break; 411 } 412 } 413 414 if (Boolean.valueOf(JiveGlobals.getXMLProperty( 416 "ldap.clientSideSorting")).booleanValue()) 417 { 418 Collections.sort(usernames); 419 } 420 } 421 catch (Exception e) { 422 Log.error(e); 423 } 424 finally { 425 try { 426 if (ctx != null) { 427 ctx.setRequestControls(null); 428 ctx.close(); 429 } 430 } 431 catch (Exception ignored) { } 432 } 433 return new UserCollection((String [])usernames.toArray(new String [usernames.size()])); 434 } 435 436 public boolean isReadOnly() { 437 return true; 438 } 439 } | Popular Tags |