use of javax.naming.directory.SearchControls in project perun by CESNET.
the class ExtSourceLdap method querySource.
/**
* Query LDAP using query in defined base. Results can be limited to the maxResults.
*
* @param query
* @param base
* @param maxResults
* @return List of Map of the LDAP attribute names and theirs values
* @throws InternalErrorException
*/
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
NamingEnumeration<SearchResult> results = null;
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
try {
// If query is null, then we are finding object by the base
if (query == null) {
log.trace("search base [{}]", base);
// TODO jmena atributu spise prijimiat pres vstupni parametr metody
Attributes ldapAttributes = getContext().getAttributes(base);
if (ldapAttributes.size() > 0) {
Map<String, String> attributes = this.getSubjectAttributes(ldapAttributes);
if (!attributes.isEmpty()) {
subjects.add(attributes);
}
}
} else {
log.trace("search string [{}]", query);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Set timeout to 5s
controls.setTimeLimit(5000);
if (maxResults > 0) {
controls.setCountLimit(maxResults);
}
if (base == null)
base = "";
results = getContext().search(base, query, controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Map<String, String> subjectAttributes = this.getSubjectAttributes(attributes);
if (!subjectAttributes.isEmpty()) {
subjects.add(subjectAttributes);
}
}
}
log.trace("Returning [{}] subjects", subjects.size());
return subjects;
} catch (NamingException e) {
log.error("LDAP exception during running query '{}'", query);
throw new InternalErrorException("LDAP exception during running query: " + query + ".", e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (Exception e) {
log.error("LDAP exception during closing result, while running query '{}'", query);
throw new InternalErrorException(e);
}
}
}
use of javax.naming.directory.SearchControls in project Activiti by Activiti.
the class LDAPGroupManager method createSearchControls.
protected SearchControls createSearchControls() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
use of javax.naming.directory.SearchControls in project Activiti by Activiti.
the class LDAPQueryBuilder method createSearchControls.
protected SearchControls createSearchControls(LDAPConfigurator ldapConfigurator) {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setTimeLimit(ldapConfigurator.getSearchTimeLimit());
return searchControls;
}
use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.
the class LdapPublicCertUtilImpl method getBaseNamingContexts.
/**
* Gets the base DNs for a connected LDAP context
* @param ctx The LDAP connection context.
* @return List of string representing the base DNs of the LDAP server.
*/
protected List<String> getBaseNamingContexts(InitialDirContext ctx) {
List<String> dNs = new ArrayList<String>();
try {
SearchControls ctls = new SearchControls();
ctls.setReturningObjFlag(true);
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
ctls.setReturningAttributes(new String[] { BASE_DN_ATTRIBUTE });
NamingEnumeration<SearchResult> objResults = ctx.search("", "objectclass=*", ctls);
while (objResults != null && objResults.hasMore()) {
final SearchResult objEntry = objResults.nextElement();
final Attributes objAttributes = objEntry.getAttributes();
if (objAttributes != null) {
final Attribute objAttribute = objAttributes.get(BASE_DN_ATTRIBUTE);
NamingEnumeration<? extends Object> allValues = objAttribute.getAll();
while (allValues.hasMoreElements()) dNs.add((String) allValues.nextElement());
}
}
if (dNs.isEmpty())
LOGGER.warn("No base DNs could be located for LDAP context");
} catch (Exception e) {
// no naming contexts could be located or query error
LOGGER.warn("ERROR looking up base DNs for LDAP context", e);
}
return dNs;
}
use of javax.naming.directory.SearchControls in project nhin-d by DirectProject.
the class LDAPResearchTest method searchDNs.
private Set<SearchResult> searchDNs(String filter, String partition, String base, int scope, DirContext appRoot) throws Exception {
if (appRoot == null)
appRoot = createContext(partition);
SearchControls controls = new SearchControls();
controls.setSearchScope(scope);
NamingEnumeration<SearchResult> result = appRoot.search(base, filter, controls);
// collect all results
Set<SearchResult> entries = new HashSet<SearchResult>();
while (result.hasMore()) {
SearchResult entry = (SearchResult) result.next();
entries.add(entry);
}
return entries;
}
Aggregations