use of javax.naming.directory.SearchResult 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.SearchResult in project nhin-d by DirectProject.
the class LDAPResearchTest method testDummy.
@SuppressWarnings("unchecked")
public void testDummy() throws Exception {
CertCacheFactory.getInstance().flushAll();
DirContext dirContext = createContext("cn=lookupTest");
Attributes attributes = dirContext.getAttributes("");
assertNotNull(attributes);
NamingEnumeration<Attribute> namingEnum = (NamingEnumeration<Attribute>) attributes.getAll();
while (namingEnum.hasMoreElements()) {
Attribute attr = namingEnum.nextElement();
System.out.println("Name: " + attr.getID() + "\r\nValue: " + attr.get() + "\r\n\r\n");
}
Set<SearchResult> results = searchDNs("(email=gm2552@cerner.com)", "", "ou=privKeys, ou=cerner, ou=com", SearchControls.SUBTREE_SCOPE, dirContext);
for (SearchResult result : results) {
System.out.println(result.getName());
// get the priv cert
String privKey = (String) result.getAttributes().get("privKeyStore").get();
System.out.println("Privkey BASE64: " + privKey);
}
}
use of javax.naming.directory.SearchResult 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;
}
use of javax.naming.directory.SearchResult in project gerrit by GerritCodeReview.
the class LdapQuery method query.
List<Result> query(final DirContext ctx, final Map<String, String> params) throws NamingException {
final SearchControls sc = new SearchControls();
final NamingEnumeration<SearchResult> res;
sc.setSearchScope(searchScope.scope());
sc.setReturningAttributes(returnAttributes);
res = ctx.search(base, pattern.getRawPattern(), pattern.bind(params), sc);
try {
final List<Result> r = new ArrayList<>();
try {
while (res.hasMore()) {
r.add(new Result(res.next()));
}
} catch (PartialResultException e) {
// Ignored
}
return r;
} finally {
res.close();
}
}
use of javax.naming.directory.SearchResult in project cloudstack by apache.
the class OpenLdapUserManagerImpl method getUserForDn.
private LdapUser getUserForDn(String userdn, LdapContext context) throws NamingException {
final SearchControls controls = new SearchControls();
controls.setSearchScope(_ldapConfiguration.getScope());
controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());
NamingEnumeration<SearchResult> result = context.search(userdn, "(objectClass=" + _ldapConfiguration.getUserObject() + ")", controls);
if (result.hasMoreElements()) {
return createUser(result.nextElement());
} else {
throw new NamingException("No user found for dn " + userdn);
}
}
Aggregations