Commit 3076f778 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Improved exception handling logic to make origin of errors more apparent. JM-597

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3571 b35dd754-fafc-0310-a699-88a17e54d16e
parent 299ac226
...@@ -22,14 +22,15 @@ import org.jivesoftware.wildfire.user.UserNotFoundException; ...@@ -22,14 +22,15 @@ import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import javax.naming.NamingEnumeration; import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*; import javax.naming.directory.*;
import javax.naming.ldap.LdapName; import javax.naming.ldap.LdapName;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
/** /**
* LDAP implementation of the GroupProvider interface. All data in the directory is * LDAP implementation of the GroupProvider interface. All data in the directory is treated as read-only so any set
* treated as read-only so any set operations will result in an exception. * operations will result in an exception.
* *
* @author Greg Ferguson and Cameron Moore * @author Greg Ferguson and Cameron Moore
*/ */
...@@ -42,8 +43,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -42,8 +43,7 @@ public class LdapGroupProvider implements GroupProvider {
private String[] standardAttributes; private String[] standardAttributes;
/** /**
* Constructor of the LdapGroupProvider class. * Constructor of the LdapGroupProvider class. Gets an LdapManager instance from the LdapManager class.
* Gets an LdapManager instance from the LdapManager class.
*/ */
public LdapGroupProvider() { public LdapGroupProvider() {
manager = LdapManager.getInstance(); manager = LdapManager.getInstance();
...@@ -57,8 +57,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -57,8 +57,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* LDAP groups are read-only.
* *
* @param name the name of the group to create. * @param name the name of the group to create.
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
...@@ -68,8 +67,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -68,8 +67,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* LDAP groups are read-only.
* *
* @param name the name of the group to delete * @param name the name of the group to delete
* @throws UnsupportedOperationException when called. * @throws UnsupportedOperationException when called.
...@@ -82,8 +80,14 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -82,8 +80,14 @@ public class LdapGroupProvider implements GroupProvider {
String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*"); String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
String searchFilter = "(&" + filter + "(" + String searchFilter = "(&" + filter + "(" +
manager.getGroupNameField() + "=" + group + "))"; manager.getGroupNameField() + "=" + group + "))";
Collection<Group> groups = Collection<Group> groups;
populateGroups(searchForGroups(searchFilter, standardAttributes)); try {
groups = populateGroups(searchForGroups(searchFilter, standardAttributes));
}
catch (NamingException e) {
Log.error("Error populating groups from LDAP", e);
throw new GroupNotFoundException("Error populating groups from LDAP", e);
}
if (groups.size() > 1) { if (groups.size() > 1) {
// If multiple groups found, throw exception. // If multiple groups found, throw exception.
throw new GroupNotFoundException("Too many groups with name " + group + " were found."); throw new GroupNotFoundException("Too many groups with name " + group + " were found.");
...@@ -97,8 +101,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -97,8 +101,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* LDAP groups are read-only.
* *
* @param oldName the current name of the group. * @param oldName the current name of the group.
* @param newName the desired new name of the group. * @param newName the desired new name of the group.
...@@ -109,8 +112,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -109,8 +112,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* LDAP groups are read-only.
* *
* @param name the group name. * @param name the group name.
* @param description the group description. * @param description the group description.
...@@ -134,6 +136,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -134,6 +136,7 @@ public class LdapGroupProvider implements GroupProvider {
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*"); String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
String returningAttributes[] = {manager.getGroupNameField()}; String returningAttributes[] = {manager.getGroupNameField()};
try {
NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter, returningAttributes); NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter, returningAttributes);
for (; answer.hasMoreElements(); count++) { for (; answer.hasMoreElements(); count++) {
try { try {
...@@ -146,18 +149,34 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -146,18 +149,34 @@ public class LdapGroupProvider implements GroupProvider {
this.groupCount = count; this.groupCount = count;
this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE * 5; this.expiresStamp = System.currentTimeMillis() + JiveConstants.MINUTE * 5;
}
catch (NamingException ex) {
Log.error("Error searching for groups in LDAP", ex);
}
return count; return count;
} }
public Collection<Group> getGroups() { public Collection<Group> getGroups() {
String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*"); String filter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
try {
return populateGroups(searchForGroups(filter, standardAttributes)); return populateGroups(searchForGroups(filter, standardAttributes));
} }
catch (NamingException ex) {
return Collections.emptyList();
}
}
public Collection<Group> getGroups(int start, int num) { public Collection<Group> getGroups(int start, int num) {
// Get an enumeration of all groups in the system // Get an enumeration of all groups in the system
String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*"); String searchFilter = MessageFormat.format(manager.getGroupSearchFilter(), "*");
NamingEnumeration<SearchResult> answer = searchForGroups(searchFilter, standardAttributes); NamingEnumeration<SearchResult> answer;
try {
answer = searchForGroups(searchFilter, standardAttributes);
}
catch (NamingException e) {
Log.error("Error searching for groups in LDAP", e);
return Collections.emptyList();
}
// Place all groups that are wanted into an enumeration // Place all groups that are wanted into an enumeration
Vector<SearchResult> v = new Vector<SearchResult>(); Vector<SearchResult> v = new Vector<SearchResult>();
...@@ -173,8 +192,14 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -173,8 +192,14 @@ public class LdapGroupProvider implements GroupProvider {
} }
} }
try {
return populateGroups(v.elements()); return populateGroups(v.elements());
} }
catch (NamingException e) {
Log.error("Error populating groups recieved from LDAP", e);
return Collections.emptyList();
}
}
public Collection<Group> getGroups(JID user) { public Collection<Group> getGroups(JID user) {
XMPPServer server = XMPPServer.getInstance(); XMPPServer server = XMPPServer.getInstance();
...@@ -190,12 +215,17 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -190,12 +215,17 @@ public class LdapGroupProvider implements GroupProvider {
} }
String filter = MessageFormat.format(manager.getGroupSearchFilter(), username); String filter = MessageFormat.format(manager.getGroupSearchFilter(), username);
try {
return populateGroups(searchForGroups(filter, standardAttributes)); return populateGroups(searchForGroups(filter, standardAttributes));
} }
catch (NamingException e) {
Log.error("Error populating groups recieved from LDAP", e);
return Collections.emptyList();
}
}
/** /**
* Always throws an UnsupportedOperationException because LDAP groups * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* are read-only.
* *
* @param groupName name of a group. * @param groupName name of a group.
* @param user the JID of the user to add * @param user the JID of the user to add
...@@ -208,8 +238,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -208,8 +238,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because LDAP groups * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* are read-only.
* *
* @param groupName the naame of a group. * @param groupName the naame of a group.
* @param user the JID of the user with new privileges * @param user the JID of the user with new privileges
...@@ -222,8 +251,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -222,8 +251,7 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* Always throws an UnsupportedOperationException because LDAP groups * Always throws an UnsupportedOperationException because LDAP groups are read-only.
* are read-only.
* *
* @param groupName the name of a group. * @param groupName the name of a group.
* @param user the JID of the user to delete. * @param user the JID of the user to delete.
...@@ -243,14 +271,14 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -243,14 +271,14 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* An auxilary method used to perform LDAP queries based on a * An auxilary method used to perform LDAP queries based on a provided LDAP search filter.
* provided LDAP search filter.
* *
* @param searchFilter LDAP search filter used to query. * @param searchFilter LDAP search filter used to query.
* @return an enumeration of SearchResult. * @return an enumeration of SearchResult.
*/ */
private NamingEnumeration<SearchResult> searchForGroups(String searchFilter, private NamingEnumeration<SearchResult> searchForGroups(String searchFilter,
String[] returningAttributes) { String[] returningAttributes) throws NamingException
{
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Trying to find all groups in the system."); Log.debug("Trying to find all groups in the system.");
} }
...@@ -275,12 +303,6 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -275,12 +303,6 @@ public class LdapGroupProvider implements GroupProvider {
return answer; return answer;
} }
catch (Exception e) {
if (manager.isDebugEnabled()) {
Log.debug("Error while searching for groups.", e);
}
return null;
}
finally { finally {
if (ctx != null) { if (ctx != null) {
try { try {
...@@ -292,13 +314,12 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -292,13 +314,12 @@ public class LdapGroupProvider implements GroupProvider {
} }
/** /**
* An auxilary method used to populate LDAP groups based on a * An auxilary method used to populate LDAP groups based on a provided LDAP search result.
* provided LDAP search result.
* *
* @param answer LDAP search result. * @param answer LDAP search result.
* @return a collection of groups. * @return a collection of groups.
*/ */
private Collection<Group> populateGroups(Enumeration<SearchResult> answer) { private Collection<Group> populateGroups(Enumeration<SearchResult> answer) throws NamingException {
if (manager.isDebugEnabled()) { if (manager.isDebugEnabled()) {
Log.debug("Starting to populate groups with users."); Log.debug("Starting to populate groups with users.");
} }
...@@ -306,12 +327,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -306,12 +327,7 @@ public class LdapGroupProvider implements GroupProvider {
try { try {
TreeMap<String, Group> groups = new TreeMap<String, Group>(); TreeMap<String, Group> groups = new TreeMap<String, Group>();
try {
ctx = manager.getContext(); ctx = manager.getContext();
}
catch (Exception e) {
return new ArrayList<Group>();
}
SearchControls ctrls = new SearchControls(); SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes(new String[]{manager.getUsernameField()}); ctrls.setReturningAttributes(new String[]{manager.getUsernameField()});
...@@ -327,9 +343,9 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -327,9 +343,9 @@ public class LdapGroupProvider implements GroupProvider {
Attributes a = answer.nextElement().getAttributes(); Attributes a = answer.nextElement().getAttributes();
String description; String description;
try { try {
name = ((String) ((a.get(manager.getGroupNameField())).get())); name = ((String)((a.get(manager.getGroupNameField())).get()));
description = description =
((String) ((a.get(manager.getGroupDescriptionField())).get())); ((String)((a.get(manager.getGroupDescriptionField())).get()));
} }
catch (Exception e) { catch (Exception e) {
description = ""; description = "";
...@@ -338,7 +354,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -338,7 +354,7 @@ public class LdapGroupProvider implements GroupProvider {
Attribute member = a.get(manager.getGroupMemberField()); Attribute member = a.get(manager.getGroupMemberField());
NamingEnumeration ne = member.getAll(); NamingEnumeration ne = member.getAll();
while (ne.hasMore()) { while (ne.hasMore()) {
String username = (String) ne.next(); String username = (String)ne.next();
if (!manager.isPosixMode()) { //userName is full dn if not posix if (!manager.isPosixMode()) { //userName is full dn if not posix
try { try {
// Get the CN using LDAP // Get the CN using LDAP
...@@ -351,7 +367,7 @@ public class LdapGroupProvider implements GroupProvider { ...@@ -351,7 +367,7 @@ public class LdapGroupProvider implements GroupProvider {
"(&(" + ldapcn + ")" + userSearchFilter + ")"; "(&(" + ldapcn + ")" + userSearchFilter + ")";
NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls); NamingEnumeration usrAnswer = ctx.search("", combinedFilter, ctrls);
if (usrAnswer.hasMoreElements()) { if (usrAnswer.hasMoreElements()) {
username = (String) ((SearchResult) usrAnswer.next()) username = (String)((SearchResult)usrAnswer.next())
.getAttributes().get( .getAttributes().get(
manager.getUsernameField()).get(); manager.getUsernameField()).get();
} }
......
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