use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.
the class RoleDAO method findAssignedRoles.
/**
* @param userDn
* @param contextId
* @return
* @throws FinderException
*/
List<String> findAssignedRoles(String userDn, String contextId) throws FinderException {
List<String> roleNameList = new ArrayList<>();
LdapConnection ld = null;
String roleRoot = getRootDn(contextId, GlobalIds.ROLE_ROOT);
try {
String filter = GlobalIds.FILTER_PREFIX + GlobalIds.ROLE_OBJECT_CLASS_NM + ")";
filter += "(" + SchemaConstants.ROLE_OCCUPANT_AT + "=" + userDn + "))";
ld = getAdminConnection();
SearchCursor searchResults = search(ld, roleRoot, SearchScope.ONELEVEL, filter, ROLE_NM_ATR, false, GlobalIds.BATCH_SIZE);
while (searchResults.next()) {
roleNameList.add(getAttribute(searchResults.getEntry(), ROLE_NM));
}
} catch (LdapException e) {
String error = "findAssignedRoles userDn [" + userDn + "] caught LdapException=" + e.getMessage();
throw new FinderException(GlobalErrIds.ROLE_OCCUPANT_SEARCH_FAILED, error, e);
} catch (CursorException e) {
String error = "findAssignedRoles userDn [" + userDn + "] caught CursorException=" + e.getMessage();
throw new FinderException(GlobalErrIds.ROLE_OCCUPANT_SEARCH_FAILED, error, e);
} finally {
closeAdminConnection(ld);
}
return roleNameList;
}
use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.
the class RoleDAO method findRoles.
/**
* @param role
* @param limit
* @return
* @throws org.apache.directory.fortress.core.FinderException
*/
List<String> findRoles(Role role, int limit) throws FinderException {
List<String> roleList = new ArrayList<>();
LdapConnection ld = null;
String roleRoot = getRootDn(role.getContextId(), GlobalIds.ROLE_ROOT);
String filter = null;
try {
String searchVal = encodeSafeText(role.getName(), GlobalIds.ROLE_LEN);
filter = GlobalIds.FILTER_PREFIX + GlobalIds.ROLE_OBJECT_CLASS_NM + ")(" + ROLE_NM + "=" + searchVal + "*))";
ld = getAdminConnection();
SearchCursor searchResults = search(ld, roleRoot, SearchScope.ONELEVEL, filter, ROLE_NM_ATR, false, limit);
while (searchResults.next()) {
Entry entry = searchResults.getEntry();
roleList.add(getAttribute(entry, ROLE_NM));
}
} catch (LdapException e) {
String error = "findRoles filter [" + filter + "] caught LdapException=" + e.getMessage();
throw new FinderException(GlobalErrIds.ROLE_SEARCH_FAILED, error, e);
} catch (CursorException e) {
String error = "findRoles filter [" + filter + "] caught CursorException=" + e.getMessage();
throw new FinderException(GlobalErrIds.ROLE_SEARCH_FAILED, error, e);
} finally {
closeAdminConnection(ld);
}
return roleList;
}
use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.
the class LdapDataProvider method searchNode.
/**
* This method will search the directory and return at most one record. If more than one record is found
* an ldap exception will be thrown.
*
* @param connection is LdapConnection object used for all communication with host.
* @param baseDn contains address of distinguished name to begin ldap search
* @param scope indicates depth of search starting at basedn. 0 (base dn),
* 1 (one level down) or 2 (infinite) are valid values.
* @param filter contains the search criteria
* @param attrs is the requested list of attritubutes to return from directory search.
* @param attrsOnly if true pull back attribute names only.
* @return entry containing target ldap node.
* @throws LdapException thrown in the event of error in ldap client or server code.
* @throws CursorException If we weren't able to fetch an element from the search result
*/
protected Entry searchNode(LdapConnection connection, String baseDn, SearchScope scope, String filter, String[] attrs, boolean attrsOnly) throws LdapException, CursorException {
SearchRequest searchRequest = new SearchRequestImpl();
searchRequest.setBase(new Dn(baseDn));
searchRequest.setFilter(filter);
searchRequest.setScope(scope);
searchRequest.setTypesOnly(attrsOnly);
searchRequest.addAttributes(attrs);
SearchCursor result = connection.search(searchRequest);
Entry entry = result.getEntry();
if (result.next()) {
throw new LdapException("searchNode failed to return unique record for LDAP search of base DN [" + baseDn + "] filter [" + filter + "]");
}
return entry;
}
use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.
the class ExampleDAO method findExamples.
/**
* @param searchVal
* @return
* @throws org.apache.directory.fortress.core.FinderException
*/
public List<Example> findExamples(String searchVal) throws FinderException {
List<Example> exampleList = new ArrayList<>();
LdapConnection ld = null;
String exampleRoot = Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
if (LOG.isDebugEnabled()) {
LOG.debug("findExamples: " + EIds.EXAMPLE_ROOT + " [" + exampleRoot + "]");
}
try {
searchVal = encodeSafeText(searchVal, GlobalIds.ROLE_LEN);
ld = getAdminConnection();
String filter = GlobalIds.FILTER_PREFIX + Arrays.toString(EIds.EXAMPLE_OBJ_CLASS) + ")(" + EIds.EXAMPLE_NM + "=" + searchVal + "*))";
SearchCursor searchResults = search(ld, exampleRoot, SearchScope.SUBTREE, filter, EXAMPLE_ATRS, false, GlobalIds.BATCH_SIZE);
while (searchResults.next()) {
exampleList.add(getEntityFromLdapEntry(searchResults.getEntry()));
}
} catch (LdapException e) {
String error = "findExamples caught LDAPException=" + e;
LOG.warn(error);
throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error);
} catch (CursorException e) {
String error = "findExamples caught CursorException=" + e;
throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error, e);
} finally {
closeAdminConnection(ld);
}
return exampleList;
}
use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project jackrabbit-oak by apache.
the class LdapIdentityProvider method getEntry.
@CheckForNull
private Entry getEntry(@Nonnull LdapConnection connection, @Nonnull LdapProviderConfig.Identity idConfig, @Nonnull String id, @Nonnull String[] customAttributes) throws CursorException, LdapException {
String searchFilter = idConfig.getSearchFilter(id);
// Create the SearchRequest object
SearchRequest req = new SearchRequestImpl();
req.setScope(SearchScope.SUBTREE);
if (customAttributes.length == 0) {
req.addAttributes(SchemaConstants.ALL_USER_ATTRIBUTES);
} else {
req.addAttributes(customAttributes);
}
req.setTimeLimit((int) config.getSearchTimeout());
req.setBase(new Dn(idConfig.getBaseDN()));
req.setFilter(searchFilter);
if (log.isDebugEnabled()) {
log.debug("getEntry: using SearchRequest {}.", req);
}
// Process the request
SearchCursor searchCursor = null;
Entry resultEntry = null;
try {
searchCursor = connection.search(req);
while (searchCursor.next()) {
if (resultEntry != null) {
log.warn("search for {} returned more than one entry. discarding additional ones.", searchFilter);
} else {
// process the SearchResultEntry
Response response = searchCursor.get();
if (response instanceof SearchResultEntry) {
resultEntry = ((SearchResultEntry) response).getEntry();
}
}
}
} finally {
if (searchCursor != null) {
try {
searchCursor.close();
} catch (IOException e) {
log.warn("Failed to close search cursor.", e);
}
}
}
if (log.isDebugEnabled()) {
if (resultEntry == null) {
log.debug("getEntry: search below {} with {} found 0 entries.", idConfig.getBaseDN(), searchFilter);
} else {
log.debug("getEntry: search below {} with {} found {}", idConfig.getBaseDN(), searchFilter, resultEntry.getDn());
}
}
return resultEntry;
}
Aggregations