Commit 1de1f19a authored by Matt Tucker's avatar Matt Tucker Committed by matt

Code cleanup, include wildcard in LDAP user search by default (JM-458).

git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@3055 b35dd754-fafc-0310-a699-88a17e54d16e
parent d9d6afba
...@@ -92,8 +92,14 @@ public class LdapUserProvider implements UserProvider { ...@@ -92,8 +92,14 @@ public class LdapUserProvider implements UserProvider {
throw new UserNotFoundException(e); throw new UserNotFoundException(e);
} }
finally { finally {
try { ctx.close(); } try {
catch (Exception ignored) { } if (ctx != null) {
ctx.close();
}
}
catch (Exception ignored) {
// Ignore.
}
} }
} }
...@@ -131,8 +137,14 @@ public class LdapUserProvider implements UserProvider { ...@@ -131,8 +137,14 @@ public class LdapUserProvider implements UserProvider {
Log.error(e); Log.error(e);
} }
finally { finally {
try { ctx.close(); } try {
catch (Exception ignored) { } if (ctx != null) {
ctx.close();
}
}
catch (Exception ignored) {
// Ignore.
}
} }
this.userCount = count; this.userCount = count;
this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5; this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE *5;
...@@ -174,13 +186,15 @@ public class LdapUserProvider implements UserProvider { ...@@ -174,13 +186,15 @@ public class LdapUserProvider implements UserProvider {
ctx.close(); ctx.close();
} }
} }
catch (Exception ignored) { } catch (Exception ignored) {
// Ignore.
}
} }
// If client-side sorting is enabled, do it. // If client-side sorting is enabled, do it.
if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting")).booleanValue()) { if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting"))) {
Collections.sort(usernames); Collections.sort(usernames);
} }
return new UserCollection((String[])usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
public Collection<User> getUsers(int startIndex, int numResults) { public Collection<User> getUsers(int startIndex, int numResults) {
...@@ -200,17 +214,13 @@ public class LdapUserProvider implements UserProvider { ...@@ -200,17 +214,13 @@ public class LdapUserProvider implements UserProvider {
constraints.setReturningAttributes(new String[] { manager.getUsernameField() }); constraints.setReturningAttributes(new String[] { manager.getUsernameField() });
// Limit results to those we'll need to process unless client-side sorting // Limit results to those we'll need to process unless client-side sorting
// is turned on. // is turned on.
if (!Boolean.valueOf(JiveGlobals.getXMLProperty( if (!(Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting")))) {
"ldap.clientSideSorting")).booleanValue())
{
constraints.setCountLimit(startIndex+numResults); constraints.setCountLimit(startIndex+numResults);
} }
String filter = MessageFormat.format(manager.getSearchFilter(), "*"); String filter = MessageFormat.format(manager.getSearchFilter(), "*");
NamingEnumeration answer = ctx.search("", filter, constraints); NamingEnumeration answer = ctx.search("", filter, constraints);
// If client-side sorting is enabled, read in all results, sort, then get a sublist. // If client-side sorting is enabled, read in all results, sort, then get a sublist.
if (Boolean.valueOf(JiveGlobals.getXMLProperty( if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting"))) {
"ldap.clientSideSorting")).booleanValue())
{
while (answer.hasMoreElements()) { while (answer.hasMoreElements()) {
// Get the next userID. // Get the next userID.
String username = (String)((SearchResult)answer.next()).getAttributes().get( String username = (String)((SearchResult)answer.next()).getAttributes().get(
...@@ -256,9 +266,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -256,9 +266,11 @@ public class LdapUserProvider implements UserProvider {
ctx.close(); ctx.close();
} }
} }
catch (Exception ignored) { } catch (Exception ignored) {
// Ignore.
}
} }
return new UserCollection((String[])usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
public String getPassword(String username) throws UserNotFoundException, public String getPassword(String username) throws UserNotFoundException,
...@@ -294,12 +306,17 @@ public class LdapUserProvider implements UserProvider { ...@@ -294,12 +306,17 @@ public class LdapUserProvider implements UserProvider {
public Collection<User> findUsers(Set<String> fields, String query) public Collection<User> findUsers(Set<String> fields, String query)
throws UnsupportedOperationException throws UnsupportedOperationException
{ {
if (fields.isEmpty()) { if (fields.isEmpty() || query == null || "".equals(query)) {
return Collections.emptyList(); return Collections.emptyList();
} }
if (!searchFields.keySet().containsAll(fields)) { if (!searchFields.keySet().containsAll(fields)) {
throw new IllegalArgumentException("Search fields " + fields + " are not valid."); throw new IllegalArgumentException("Search fields " + fields + " are not valid.");
} }
// Make the query be a wildcard search by default. So, if the user searches for
// "John", make the search be "John*" instead.
if (!query.endsWith("*")) {
query = query + "*";
}
List<String> usernames = new ArrayList<String>(); List<String> usernames = new ArrayList<String>();
LdapContext ctx = null; LdapContext ctx = null;
try { try {
...@@ -334,9 +351,7 @@ public class LdapUserProvider implements UserProvider { ...@@ -334,9 +351,7 @@ public class LdapUserProvider implements UserProvider {
usernames.add(JID.escapeNode(username)); usernames.add(JID.escapeNode(username));
} }
// If client-side sorting is enabled, sort. // If client-side sorting is enabled, sort.
if (Boolean.valueOf(JiveGlobals.getXMLProperty( if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting"))) {
"ldap.clientSideSorting")).booleanValue())
{
Collections.sort(usernames); Collections.sort(usernames);
} }
} }
...@@ -350,9 +365,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -350,9 +365,11 @@ public class LdapUserProvider implements UserProvider {
ctx.close(); ctx.close();
} }
} }
catch (Exception ignored) { } catch (Exception ignored) {
// Ignore.
}
} }
return new UserCollection((String[])usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
public Collection<User> findUsers(Set<String> fields, String query, int startIndex, public Collection<User> findUsers(Set<String> fields, String query, int startIndex,
...@@ -414,9 +431,7 @@ public class LdapUserProvider implements UserProvider { ...@@ -414,9 +431,7 @@ public class LdapUserProvider implements UserProvider {
} }
// If client-side sorting is enabled, sort. // If client-side sorting is enabled, sort.
if (Boolean.valueOf(JiveGlobals.getXMLProperty( if (Boolean.valueOf(JiveGlobals.getXMLProperty("ldap.clientSideSorting"))) {
"ldap.clientSideSorting")).booleanValue())
{
Collections.sort(usernames); Collections.sort(usernames);
} }
} }
...@@ -430,9 +445,11 @@ public class LdapUserProvider implements UserProvider { ...@@ -430,9 +445,11 @@ public class LdapUserProvider implements UserProvider {
ctx.close(); ctx.close();
} }
} }
catch (Exception ignored) { } catch (Exception ignored) {
// Ignore.
}
} }
return new UserCollection((String[])usernames.toArray(new String[usernames.size()])); return new UserCollection(usernames.toArray(new String[usernames.size()]));
} }
public boolean isReadOnly() { public boolean isReadOnly() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment