use of org.forgerock.opendj.ldap.LdapException in project OpenAM by OpenRock.
the class DJLDAPv3Repo method getAttributes.
/**
* Returns all the requested attributes either in binary or in String format. Only the attributes defined in the
* configuration will be returned for this given identity. In case the default "inetUserStatus" attribute has been
* requested, it will be converted to the actual status attribute during query, and while processing it will be
* mapped back to standard "inetUserStatus" values as well (rather than returning the configuration/directory
* specific values). If there is an attempt to read a realm identity type's objectclass attribute, this method will
* return an empty map right away (legacy handling). If the dn attribute has been requested, and it's also defined
* in the configuration, then the attributemap will also contain the dn in the result.
*
* @param <T>
* @param type The type of the identity.
* @param name The name of the identity.
* @param attrNames The names of the requested attributes or <code>null</code> to retrieve all the attributes.
* @param function A function that can extract String or byte array values from an LDAP attribute.
* @return The requested attributes in string or binary format.
* @throws IdRepoException If there is an error while retrieving the identity attributes.
*/
private <T> Map<String, T> getAttributes(IdType type, String name, Set<String> attrNames, Function<Attribute, T, IdRepoException> function) throws IdRepoException {
Set<String> attrs = attrNames == null ? new CaseInsensitiveHashSet(0) : new CaseInsensitiveHashSet(attrNames);
if (type.equals(IdType.REALM)) {
if (attrs.contains(OBJECT_CLASS_ATTR)) {
return new HashMap(0);
}
}
Map<String, T> result = new HashMap<String, T>();
String dn = getDN(type, name);
if (type.equals(IdType.USER)) {
if (attrs.contains(DEFAULT_USER_STATUS_ATTR)) {
attrs.add(userStatusAttr);
}
}
Connection conn = null;
Set<String> definedAttributes = getDefinedAttributes(type);
if (attrs.isEmpty() || attrs.contains("*")) {
attrs.clear();
if (definedAttributes.isEmpty()) {
attrs.add("*");
} else {
attrs.addAll(definedAttributes);
}
} else {
if (!definedAttributes.isEmpty()) {
attrs.retainAll(definedAttributes);
}
if (attrs.isEmpty()) {
//there were only non-defined attributes requested, so we shouldn't return anything here.
return new HashMap<String, T>(0);
}
}
try {
conn = connectionFactory.getConnection();
SearchResultEntry entry = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn, attrs.toArray(new String[attrs.size()])));
for (Attribute attribute : entry.getAllAttributes()) {
String attrName = attribute.getAttributeDescriptionAsString();
if (!definedAttributes.isEmpty() && !definedAttributes.contains(attrName)) {
continue;
}
result.put(attribute.getAttributeDescriptionAsString(), function.apply(attribute));
if (attrName.equalsIgnoreCase(userStatusAttr) && attrs.contains(DEFAULT_USER_STATUS_ATTR)) {
String converted = helper.convertToInetUserStatus(attribute.firstValueAsString(), inactiveValue);
result.put(DEFAULT_USER_STATUS_ATTR, function.apply(new LinkedAttribute(DEFAULT_USER_STATUS_ATTR, converted)));
}
}
} catch (LdapException ere) {
DEBUG.error("An error occurred while getting user attributes", ere);
handleErrorResult(ere);
} finally {
IOUtils.closeIfNotNull(conn);
}
if (attrs.contains(DN_ATTR)) {
result.put(DN_ATTR, function.apply(new LinkedAttribute(DN_ATTR, dn)));
}
if (DEBUG.messageEnabled()) {
DEBUG.message("getAttributes returning attrMap: " + IdRepoUtils.getAttrMapWithoutPasswordAttrs(result, null));
}
return result;
}
use of org.forgerock.opendj.ldap.LdapException 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.LdapException 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.LdapException 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.LdapException 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);
}
}
Aggregations