use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method getSchema.
protected Schema getSchema() throws IdRepoException {
if (schema == null) {
synchronized (this) {
if (schema == null) {
Connection conn = null;
try {
conn = connectionFactory.getConnection();
schema = Schema.readSchemaForEntry(conn, DN.valueOf(rootSuffix)).asStrictSchema();
} catch (LdapException ere) {
DEBUG.error("Unable to read the directory schema", ere);
throw new IdRepoException("Unable to read the directory schema");
} finally {
IOUtils.closeIfNotNull(conn);
}
}
}
}
return schema;
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method delete.
/**
* Deletes the identity from the directory.
*
* @param token Not used.
* @param type The type of the identity.
* @param name The name of the identity.
* @throws IdRepoException If the identity cannot be found, or there is an error while deleting the identity.
*/
@Override
public void delete(SSOToken token, IdType type, String name) throws IdRepoException {
if (DEBUG.messageEnabled()) {
DEBUG.message("delete invoked");
}
String dn = getDN(type, name);
Connection conn = null;
try {
conn = connectionFactory.getConnection();
conn.delete(LDAPRequests.newDeleteRequest(dn));
} catch (LdapException ere) {
DEBUG.error("Unable to delete entry: " + dn, ere);
handleErrorResult(ere);
} finally {
IOUtils.closeIfNotNull(conn);
}
if (dnCacheEnabled) {
dnCache.remove(generateDNCacheKey(name, type));
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method getDN.
private String getDN(IdType type, String name, boolean shouldGenerate, String searchAttr) throws IdRepoException {
Object cachedDn = null;
if (dnCacheEnabled) {
cachedDn = dnCache.get(generateDNCacheKey(name, type));
}
if (cachedDn != null) {
return cachedDn.toString();
}
String dn = null;
DN searchBase = getBaseDN(type);
if (shouldGenerate) {
return searchBase.child(getSearchAttribute(type), name).toString();
}
if (searchAttr == null) {
searchAttr = getSearchAttribute(type);
}
Filter filter = Filter.and(Filter.equality(searchAttr, name), getObjectClassFilter(type));
SearchRequest searchRequest = LDAPRequests.newSearchRequest(searchBase, defaultScope, filter, DN_ATTR);
Connection conn = null;
try {
conn = connectionFactory.getConnection();
ConnectionEntryReader reader = conn.search(searchRequest);
SearchResultEntry entry = null;
while (reader.hasNext()) {
if (reader.isEntry()) {
if (entry != null) {
throw newIdRepoException(ResultCode.CLIENT_SIDE_UNEXPECTED_RESULTS_RETURNED, IdRepoErrorCode.LDAP_EXCEPTION_OCCURRED, CLASS_NAME, ResultCode.CLIENT_SIDE_UNEXPECTED_RESULTS_RETURNED.intValue());
}
entry = reader.readEntry();
} else {
//ignore references
reader.readReference();
}
}
if (entry == null) {
DEBUG.message("Unable to find entry with name: " + name + " under searchbase: " + searchBase + " with scope: " + defaultScope);
throw new IdentityNotFoundException(IdRepoBundle.BUNDLE_NAME, IdRepoErrorCode.TYPE_NOT_FOUND, ResultCode.CLIENT_SIDE_NO_RESULTS_RETURNED, new Object[] { name, type.getName() });
}
dn = entry.getName().toString();
} catch (LdapException ere) {
DEBUG.error("An error occurred while querying entry DN", ere);
handleErrorResult(ere);
} catch (SearchResultReferenceIOException srrioe) {
//should never ever happen...
DEBUG.error("Got reference instead of entry", srrioe);
throw newIdRepoException(IdRepoErrorCode.SEARCH_FAILED, CLASS_NAME);
} finally {
IOUtils.closeIfNotNull(conn);
}
if (dnCacheEnabled && !shouldGenerate) {
dnCache.put(generateDNCacheKey(name, type), dn);
}
return dn;
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method modifyRoleMembership.
/**
* Modifies role membership data in the directory. This will add/remove the corresponding nsRoleDN attribute from
* the user entry.
*
* @param roleDN The DN of the role.
* @param memberDNs The DNs of the role members.
* @param operation Whether the members needs to be added or removed from the group. Use {@link IdRepo#ADDMEMBER}
* or {@link IdRepo#REMOVEMEMBER}.
* @throws IdRepoException If there was an error while modifying the membership data.
*/
private void modifyRoleMembership(String roleDN, Set<String> memberDNs, int operation) throws IdRepoException {
Attribute attr = new LinkedAttribute(roleDNAttr, roleDN);
Modification mod;
if (ADDMEMBER == operation) {
mod = new Modification(ModificationType.ADD, attr);
} else {
mod = new Modification(ModificationType.DELETE, attr);
}
Connection conn = null;
try {
conn = connectionFactory.getConnection();
for (String memberDN : memberDNs) {
ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(memberDN);
modifyRequest.addModification(mod);
conn.modify(modifyRequest);
}
} catch (LdapException ere) {
DEBUG.error("An error occurred while trying to modify role membership. Name: " + roleDN + " memberDNs: " + memberDNs, ere);
handleErrorResult(ere);
} finally {
IOUtils.closeIfNotNull(conn);
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class DJLDAPv3Repo method getFilteredRoleMembers.
/**
* Returns the DNs of the members of this filtered role. To do that this will execute a read on the filtered role
* entry to get the values of the nsRoleFilter attribute, and then it will perform searches using the retrieved
* filters.
*
* @param dn The DN of the filtered role to query.
* @return The DNs of the members.
* @throws IdRepoException If there is an error while trying to retrieve the filtered role members.
*/
private Set<String> getFilteredRoleMembers(String dn) throws IdRepoException {
Set<String> results = new HashSet<String>();
Connection conn = null;
try {
conn = connectionFactory.getConnection();
SearchResultEntry entry = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn, roleFilterAttr));
Attribute filterAttr = entry.getAttribute(roleFilterAttr);
if (filterAttr != null) {
for (ByteString byteString : filterAttr) {
Filter filter = Filter.valueOf(byteString.toString());
//TODO: would it make sense to OR these filters and run a single search?
SearchRequest searchRequest = LDAPRequests.newSearchRequest(rootSuffix, defaultScope, filter.toString(), DN_ATTR);
searchRequest.setTimeLimit(defaultTimeLimit);
searchRequest.setSizeLimit(defaultSizeLimit);
ConnectionEntryReader reader = conn.search(searchRequest);
while (reader.hasNext()) {
if (reader.isEntry()) {
results.add(reader.readEntry().getName().toString());
} else {
//ignore search result references
reader.readReference();
}
}
}
}
} catch (LdapException ere) {
DEBUG.error("An error occurred while trying to retrieve filtered role members for " + dn, ere);
handleErrorResult(ere);
} catch (SearchResultReferenceIOException srrioe) {
//should never ever happen...
DEBUG.error("Got reference instead of entry", srrioe);
throw newIdRepoException(IdRepoErrorCode.SEARCH_FAILED, CLASS_NAME);
} finally {
IOUtils.closeIfNotNull(conn);
}
return results;
}
Aggregations