checkControls(controls);
PageSearchControl pageSearchControl = null;
SortByNameSearchControl sortSearchControl = null;
AttributeFilterSearchControl attributeFilterSearchControl = null;
NameFilterSearchControl nameFilterSearchControl = null;
if (controls != null)
{
for (IdentityObjectSearchControl control : controls)
{
if (control instanceof PageSearchControl)
{
pageSearchControl = (PageSearchControl)control;
}
else if (control instanceof SortByNameSearchControl)
{
sortSearchControl = (SortByNameSearchControl)control;
}
else if (control instanceof AttributeFilterSearchControl)
{
attributeFilterSearchControl = (AttributeFilterSearchControl)control;
}
else if (control instanceof NameFilterSearchControl)
{
nameFilterSearchControl = (NameFilterSearchControl)control;
}
}
}
LDAPIdentityObjectImpl ldapFromIO = getSafeLDAPIO(ctx, identity);
LDAPIdentityObjectTypeConfiguration typeConfig = getTypeConfiguration(ctx, identity.getIdentityType());
LdapContext ldapContext = getLDAPContext(ctx);
List<IdentityObject> objects = new LinkedList<IdentityObject>();
try
{
// If parent simply look for all its members
if (parent)
{
if (typeConfig.getMembershipAttributeName() == null)
{
throw new IdentityException("Membership attribute name not configured. Given IdentityObjectType cannot have" +
"members: " + identity.getIdentityType().getName());
}
Attributes attrs = ldapContext.getAttributes(ldapFromIO.getDn());
Attribute member = attrs.get(typeConfig.getMembershipAttributeName());
if (member != null)
{
NamingEnumeration memberValues = member.getAll();
while (memberValues.hasMoreElements())
{
String memberRef = memberValues.nextElement().toString();
if (typeConfig.isMembershipAttributeDN())
{
//TODO: use direct LDAP query instaed of other find method and add attributesFilter
if (nameFilterSearchControl != null)
{
String name = Tools.stripDnToName(memberRef);
String regex = Tools.wildcardToRegex(nameFilterSearchControl.getFilter());
if (Pattern.matches(regex, name))
{
objects.add(findIdentityObject(ctx, memberRef));
}
}
else
{
objects.add(findIdentityObject(ctx, memberRef));
}
}
else
{
//TODO: if relationships are not refered with DNs and only names its not possible to map
//TODO: them to proper IdentityType and keep name uniqnes per type. Workaround needed
throw new NotYetImplementedException("LDAP limitation. If relationship targets are not refered with FQDNs " +
"and only names, it's not possible to map them to proper IdentityType and keep name uniqnes per type. " +
"Workaround needed");
}
//break;
}
}
}
// if not parent then all parent entries need to be found
else
{
// Search in all other type contexts
for (IdentityObjectType parentType : configuration.getConfiguredTypes())
{
checkIOType(parentType);
LDAPIdentityObjectTypeConfiguration parentTypeConfiguration = getTypeConfiguration(ctx, parentType);
List<String> allowedTypes = Arrays.asList(parentTypeConfiguration.getAllowedMembershipTypes());
// Check if given identity type can be parent
if (!allowedTypes.contains(identity.getIdentityType().getName()))
{
continue;
}
String nameFilter = "*";
//Filter by name
if (nameFilterSearchControl != null)
{
nameFilter = nameFilterSearchControl.getFilter();
}
Control[] requestControls = null;
StringBuilder af = new StringBuilder();
// Filter by attribute values
if (attributeFilterSearchControl != null)
{
af.append("(&");
for (Map.Entry<String, String[]> stringEntry : attributeFilterSearchControl.getValues().entrySet())
{
for (String value : stringEntry.getValue())
{
af.append("(")
.append(stringEntry.getKey())
.append("=")
.append(value)
.append(")");
}
}
af.append(")");
}
// Add filter to search only parents of the given entry
af.append("(")
.append(parentTypeConfiguration.getMembershipAttributeName())
.append("=");
if (parentTypeConfiguration.isMembershipAttributeDN())
{
af.append(ldapFromIO.getDn());
}
else
{
//TODO: this doesn't make much sense unless parent/child are same identity types and resides in the same LDAP context
af.append(ldapFromIO.getName());
}
af.append(")");
String filter = parentTypeConfiguration.getEntrySearchFilter();
List<SearchResult> sr = null;
String[] entryCtxs = parentTypeConfiguration.getCtxDNs();
if (filter != null && filter.length() > 0)
{
Object[] filterArgs = {nameFilter};
sr = searchIdentityObjects(ctx,
entryCtxs,
"(&(" + filter + ")" + af.toString() + ")",
filterArgs,
new String[]{parentTypeConfiguration.getIdAttributeName()},
requestControls);
}
else
{
filter = "(".concat(parentTypeConfiguration.getIdAttributeName()).concat("=").concat(nameFilter).concat(")");
sr = searchIdentityObjects(ctx,
entryCtxs,
"(&(" + filter + ")" + af.toString() + ")",
null,
new String[]{parentTypeConfiguration.getIdAttributeName()},
requestControls);
}
for (SearchResult res : sr)
{
LdapContext ldapCtx = (LdapContext)res.getObject();
String dn = ldapCtx.getNameInNamespace();
objects.add(createIdentityObjectInstance(ctx, parentType, res.getAttributes(), dn));
}
}
}
}
catch (NamingException e)
{
throw new IdentityException("Failed to resolve relationship", e);
}
finally
{
try
{
ldapContext.close();
}
catch (NamingException e)
{
throw new IdentityException("Failed to close LDAP connection", e);
}
}
if (pageSearchControl != null)
{
objects = cutPageFromResults(objects, pageSearchControl);
}
if (sortSearchControl != null)
{
sortByName(objects, sortSearchControl.isAscending());
}
return objects;
}