1 11 12 package org.jivesoftware.messenger.ldap; 13 14 import org.jivesoftware.util.*; 15 import org.jivesoftware.messenger.user.*; 16 import org.jivesoftware.messenger.group.*; 17 import org.xmpp.packet.JID; 18 19 import java.util.ArrayList ; 20 import java.util.TreeMap ; 21 import java.util.TreeSet ; 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 import java.text.MessageFormat ; 27 28 import javax.naming.Name ; 29 import javax.naming.NamingEnumeration ; 30 import javax.naming.directory.*; 31 import javax.naming.ldap.LdapName ; 32 33 39 public class LdapGroupProvider implements GroupProvider 40 { 41 42 private LdapManager manager; 43 private UserManager userManager; 44 private int groupCount; 45 private long expiresStamp; 46 private String [] standardAttributes; 47 48 53 public LdapGroupProvider() { 54 manager = LdapManager.getInstance(); 55 userManager = UserManager.getInstance(); 56 groupCount = -1; 57 expiresStamp = System.currentTimeMillis(); 58 standardAttributes = new String [3]; 59 standardAttributes[0] = manager.getGroupNameField(); 60 standardAttributes[1] = manager.getGroupDescriptionField(); 61 standardAttributes[2] = manager.getGroupMemberField(); 62 } 63 64 71 public Group createGroup (String group) 72 throws UnsupportedOperationException 73 { 74 throw new UnsupportedOperationException (); 75 } 76 77 84 public void deleteGroup (Group group) 85 throws UnsupportedOperationException 86 { 87 throw new UnsupportedOperationException (); 88 } 89 90 97 public void deleteGroup (String group) 98 throws UnsupportedOperationException 99 { 100 throw new UnsupportedOperationException (); 101 } 102 103 110 public Group getGroup (String group) 111 { 112 String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); 113 String searchFilter = "(&"+filter+"("+ 114 manager.getGroupNameField()+"="+group+"))"; 115 Collection <Group> groups = populateGroups(searchForGroups(searchFilter,standardAttributes)); 116 if (groups.size() > 1) 117 return null; for (Group g : groups) 119 return g; return null; 121 } 122 123 131 public void setName(String name, String s) 132 throws UnsupportedOperationException 133 { 134 throw new UnsupportedOperationException (); 135 } 136 137 145 public void setDescription(String description,String s) 146 throws UnsupportedOperationException 147 { 148 throw new UnsupportedOperationException (); 149 } 150 151 157 public int getGroupCount() 158 { 159 if (groupCount != -1 && System.currentTimeMillis() < expiresStamp) { 161 return groupCount; 162 } 163 int count = 0; 164 165 if (manager.isDebugEnabled()) { 166 Log.debug("Trying to get the number of groups in the system."); 167 } 168 169 String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); 170 String returningAttributes[]= { manager.getGroupNameField() }; 171 NamingEnumeration <SearchResult> answer = searchForGroups(searchFilter,returningAttributes); 172 for (; answer.hasMoreElements(); count++) 173 { 174 try 175 { 176 answer.next(); 177 } 178 catch (Exception e) { } 179 } 180 181 this.groupCount = count; 182 this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5; 183 return count; 184 } 185 186 191 public Collection <Group> getGroups() 192 { 193 String filter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); 194 return populateGroups(searchForGroups(filter,standardAttributes)); 195 } 196 197 207 public Collection <Group> getGroups(int start, int num) 208 { 209 ArrayList <Group> returnCollection = new ArrayList <Group>(); 210 211 213 String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(),"*"); 214 NamingEnumeration <SearchResult> answer = searchForGroups(searchFilter,standardAttributes); 215 216 218 Vector <SearchResult> v = new Vector <SearchResult>(); 219 for (int i = 1; answer.hasMoreElements() && i <= (start+num); i++) 220 { 221 try 222 { 223 SearchResult sr = answer.next(); 224 if (i >= start) 225 v.add(sr); 226 } 227 catch (Exception e) { } 228 } 229 230 return populateGroups(v.elements()); 231 } 232 233 240 public Collection <Group> getGroups(User user) 241 { 242 String username = JID.unescapeNode(user.getUsername()); 243 if (!manager.getPosixEnabled()) 244 { 245 try 246 { 247 username = manager.findUserDN(username) + "," + 248 manager.getBaseDN(); 249 } 250 catch (Exception e) 251 { 252 return new ArrayList <Group>(); 253 } 254 } 255 256 String filter = MessageFormat.format(manager.getGroupSearchFilter(),username); 257 return populateGroups(searchForGroups(filter,standardAttributes)); 258 } 259 260 269 public void addMember(String groupName, String username, boolean administrator) 270 throws UnsupportedOperationException 271 { 272 throw new UnsupportedOperationException (); 273 } 274 275 284 public void updateMember(String groupName, String username, boolean administrator) 285 throws UnsupportedOperationException 286 { 287 throw new UnsupportedOperationException (); 288 } 289 290 298 public void deleteMember(String groupName, String username) 299 throws UnsupportedOperationException 300 { 301 throw new UnsupportedOperationException (); 302 } 303 304 310 public boolean isReadOnly() 311 { 312 return true; 313 } 314 315 316 323 private NamingEnumeration <SearchResult> searchForGroups (String searchFilter, 324 String [] returningAttributes) 325 { 326 if (manager.isDebugEnabled()) { 327 Log.debug("Trying to find all groups in the system."); 328 } 329 DirContext ctx = null; 330 NamingEnumeration <SearchResult> answer = null; 331 try 332 { 333 ctx = manager.getContext(); 334 if (manager.isDebugEnabled()) { 335 Log.debug("Starting LDAP search..."); 336 Log.debug("Using groupSearchFilter: "+searchFilter); 337 } 338 339 SearchControls searchControls = new SearchControls(); 341 searchControls.setReturningAttributes(returningAttributes); 342 searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); 343 answer = ctx.search("",searchFilter,searchControls); 344 345 if (manager.isDebugEnabled()) { 346 Log.debug("... search finished"); 347 } 348 } 349 catch (Exception e) 350 { 351 if (manager.isDebugEnabled()) 352 Log.debug("Error while searching for groups.",e); 353 } 354 return answer; 355 } 356 357 364 private Collection <Group> populateGroups (Enumeration <SearchResult> answer) 365 { 366 if (manager.isDebugEnabled()) { 367 Log.debug("Starting to populate groups with users."); 368 } 369 370 TreeMap <String ,Group> groups = new TreeMap <String ,Group>(); 371 372 DirContext ctx = null; 373 try 374 { 375 ctx = manager.getContext(); 376 } 377 catch (Exception e) 378 { 379 return new ArrayList <Group>(); 380 } 381 382 SearchControls ctrls = new SearchControls(); 383 ctrls.setReturningAttributes( new String []{manager.getUsernameField()} ); 384 ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE); 385 386 String userSearchFilter = MessageFormat.format(manager.getSearchFilter(),"*"); 387 388 while (answer.hasMoreElements()) 389 { 390 String name = ""; 391 try 392 { 393 Attributes a = (((SearchResult)answer.nextElement()).getAttributes()); 394 String description; 395 try 396 { 397 name = ((String )((a.get(manager.getGroupNameField())).get())); 398 description = ((String )((a.get(manager.getGroupDescriptionField())).get())); 399 } 400 catch (Exception e) 401 { 402 description = ""; 403 } 404 TreeSet <String > members = new TreeSet <String >(); 405 Attribute member = a.get(manager.getGroupMemberField()); 406 NamingEnumeration ne = member.getAll(); 407 while (ne.hasMore()) 408 { 409 String userName = (String )ne.next(); 410 if (!manager.getPosixEnabled()) 411 { try 413 { 414 LdapName ldapname = new LdapName (userName); 416 String ldapcn = ldapname.get(ldapname.size()-1); 417 418 420 String combinedFilter = "(&("+ldapcn+")"+userSearchFilter+")"; 421 NamingEnumeration usrAnswer = ctx.search("",combinedFilter,ctrls); 422 if (usrAnswer.hasMoreElements()) 423 { 424 userName = (String )((SearchResult)usrAnswer.next()).getAttributes().get( 425 manager.getUsernameField()).get(); 426 } 427 else 428 throw new UserNotFoundException(); 429 } 430 catch (Exception e) 431 { 432 if (manager.isDebugEnabled()) 433 Log.debug("Error populating user with DN: "+userName,e); 434 } 435 } 436 try 437 { 438 User user = userManager.getUser(JID.escapeNode(userName)); 439 members.add(user.getUsername()); 440 } 441 catch (UserNotFoundException e) 442 { 443 if (manager.isDebugEnabled()) 444 Log.debug("User not found: "+userName); 445 } 446 } 447 if (manager.isDebugEnabled()) 448 Log.debug("Adding group \""+name+"\" with "+members.size()+" members."); 449 Group g = new Group(this,name,description,members,new ArrayList <String >()); 450 groups.put(name,g); 451 } 452 catch (Exception e) 453 { 454 if (manager.isDebugEnabled()) 455 Log.debug("Error while populating group, "+name+".",e); 456 } 457 } 458 if (manager.isDebugEnabled()) 459 Log.debug("Finished populating group(s) with users."); 460 461 try 462 { 463 ctx.close(); 464 } 465 catch (Exception e) { } 466 467 return groups.values(); 468 } 469 } | Popular Tags |