Search in sources :

Example 16 with SearchResult

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;
}
Also used : Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 17 with SearchResult

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);
    }
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) DirContext(javax.naming.directory.DirContext)

Example 18 with SearchResult

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;
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) HashSet(java.util.HashSet)

Example 19 with SearchResult

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();
    }
}
Also used : ArrayList(java.util.ArrayList) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) PartialResultException(javax.naming.PartialResultException) SearchResult(javax.naming.directory.SearchResult)

Example 20 with SearchResult

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);
    }
}
Also used : SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

Aggregations

SearchResult (javax.naming.directory.SearchResult)248 SearchControls (javax.naming.directory.SearchControls)146 NamingException (javax.naming.NamingException)113 Attributes (javax.naming.directory.Attributes)96 Attribute (javax.naming.directory.Attribute)86 ArrayList (java.util.ArrayList)75 LdapContext (javax.naming.ldap.LdapContext)39 NamingEnumeration (javax.naming.NamingEnumeration)36 DirContext (javax.naming.directory.DirContext)35 Test (org.junit.Test)32 BasicAttributes (javax.naming.directory.BasicAttributes)30 HashSet (java.util.HashSet)28 InitialDirContext (javax.naming.directory.InitialDirContext)27 InitialLdapContext (javax.naming.ldap.InitialLdapContext)23 PagedResultsControl (javax.naming.ldap.PagedResultsControl)22 HashMap (java.util.HashMap)20 IOException (java.io.IOException)19 BasicAttribute (javax.naming.directory.BasicAttribute)19 Control (javax.naming.ldap.Control)16 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)15