Search in sources :

Example 41 with NamingEnumeration

use of javax.naming.NamingEnumeration in project uPortal by Jasig.

the class LDAPGroupStore method searchForEntities.

public EntityIdentifier[] searchForEntities(String query, int method, Class type) throws GroupsException {
    if (type != group && type != iperson)
        return new EntityIdentifier[0];
    // Guarantee that LDAP injection is prevented by replacing LDAP special characters
    // with escaped versions of the character
    query = LdapEncoder.filterEncode(query);
    ArrayList ids = new ArrayList();
    switch(method) {
        case STARTS_WITH:
            query = query + "*";
            break;
        case ENDS_WITH:
            query = "*" + query;
            break;
        case CONTAINS:
            query = "*" + query + "*";
            break;
    }
    query = namefield + "=" + query;
    DirContext context = getConnection();
    NamingEnumeration userlist = null;
    SearchControls sc = new SearchControls();
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
    sc.setReturningAttributes(new String[] { keyfield });
    try {
        userlist = context.search(usercontext, query, sc);
        ArrayList keys = new ArrayList();
        processLdapResults(userlist, keys);
        String[] k = (String[]) keys.toArray(new String[0]);
        for (int i = 0; i < k.length; i++) {
            ids.add(new EntityIdentifier(k[i], iperson));
        }
        return (EntityIdentifier[]) ids.toArray(new EntityIdentifier[0]);
    } catch (NamingException nex) {
        throw new GroupsException("LDAPGroupStore: Unable to perform filter " + query, nex);
    }
}
Also used : GroupsException(org.apereo.portal.groups.GroupsException) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) EntityIdentifier(org.apereo.portal.EntityIdentifier)

Example 42 with NamingEnumeration

use of javax.naming.NamingEnumeration in project uPortal by Jasig.

the class SimpleLdapSecurityContext method getAttributeValue.

/*--------------------- Helper methods ---------------------*/
/**
     * Return a single value of an attribute from possibly multiple values, grossly ignoring
     * anything else. If there are no values, then return an empty string.
     *
     * @param attrs LDAP query results
     * @param attribute LDAP attribute we are interested in
     * @return a single value of the attribute
     */
private String getAttributeValue(Attributes attrs, int attribute) throws NamingException {
    NamingEnumeration values = null;
    String aValue = "";
    if (!isAttribute(attribute))
        return aValue;
    Attribute attrib = attrs.get(attributes[attribute]);
    if (attrib != null) {
        for (values = attrib.getAll(); values.hasMoreElements(); ) {
            aValue = (String) values.nextElement();
            // take only the first attribute value
            break;
        }
    }
    return aValue;
}
Also used : Attribute(javax.naming.directory.Attribute) NamingEnumeration(javax.naming.NamingEnumeration)

Example 43 with NamingEnumeration

use of javax.naming.NamingEnumeration in project geode by apache.

the class SocketCreator method reverseDNS.

/**
   * This method uses JNDI to look up an address in DNS and return its name
   * 
   * @param addr
   *
   * @return the host name associated with the address or null if lookup isn't possible or there is
   *         no host name for this address
   */
public static String reverseDNS(InetAddress addr) {
    byte[] addrBytes = addr.getAddress();
    // reverse the address suitable for reverse lookup
    String lookup = "";
    for (int index = addrBytes.length - 1; index >= 0; index--) {
        lookup = lookup + (addrBytes[index] & 0xff) + '.';
    }
    lookup += "in-addr.arpa";
    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
        DirContext ctx = new InitialDirContext(env);
        Attributes attrs = ctx.getAttributes(lookup, new String[] { "PTR" });
        for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements(); ) {
            Attribute attr = (Attribute) ae.next();
            for (Enumeration vals = attr.getAll(); vals.hasMoreElements(); ) {
                Object elem = vals.nextElement();
                if ("PTR".equals(attr.getID()) && elem != null) {
                    return elem.toString();
                }
            }
        }
        ctx.close();
    } catch (Exception e) {
    // ignored
    }
    return null;
}
Also used : Enumeration(java.util.Enumeration) NamingEnumeration(javax.naming.NamingEnumeration) Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) InitialDirContext(javax.naming.directory.InitialDirContext) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BindException(java.net.BindException) SocketException(java.net.SocketException) SystemConnectException(org.apache.geode.SystemConnectException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) GemFireConfigException(org.apache.geode.GemFireConfigException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 44 with NamingEnumeration

use of javax.naming.NamingEnumeration in project geode by apache.

the class ContextJUnitTest method clearContext.

/**
   * Removes all entries from the specified context, including subcontexts.
   * 
   * @param context context to clear
   */
private void clearContext(Context context) throws NamingException {
    for (NamingEnumeration e = context.listBindings(""); e.hasMoreElements(); ) {
        Binding binding = (Binding) e.nextElement();
        if (binding.getObject() instanceof Context) {
            clearContext((Context) binding.getObject());
        }
        context.unbind(binding.getName());
    }
}
Also used : Binding(javax.naming.Binding) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NamingEnumeration(javax.naming.NamingEnumeration)

Example 45 with NamingEnumeration

use of javax.naming.NamingEnumeration in project geode by apache.

the class ContextJUnitTest method verifyListBindings.

private void verifyListBindings(Context c, String name, Object obj1, Object obj2) throws NamingException {
    boolean datasourceFoundFlg = false;
    boolean o2FoundFlg = false;
    boolean datasourceO1FoundFlg = false;
    boolean datasourceNullFoundFlg = false;
    // List bindings for the specified context
    for (NamingEnumeration en = c.listBindings(name); en.hasMore(); ) {
        Binding b = (Binding) en.next();
        if (b.getName().equals("datasource")) {
            assertEquals(b.getObject(), dataSourceContext);
            datasourceFoundFlg = true;
            Context nextCon = (Context) b.getObject();
            for (NamingEnumeration en1 = nextCon.listBindings(""); en1.hasMore(); ) {
                Binding b1 = (Binding) en1.next();
                if (b1.getName().equals("sub41")) {
                    assertEquals(b1.getObject(), obj1);
                    datasourceO1FoundFlg = true;
                } else if (b1.getName().equals("sub43")) {
                    // check for null object
                    assertNull(b1.getObject());
                    datasourceNullFoundFlg = true;
                }
            }
        } else if (b.getName().equals("sub42")) {
            assertEquals(b.getObject(), obj2);
            o2FoundFlg = true;
        }
    }
    if (!(datasourceFoundFlg && o2FoundFlg && datasourceO1FoundFlg && datasourceNullFoundFlg)) {
        fail();
    }
}
Also used : Binding(javax.naming.Binding) InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NamingEnumeration(javax.naming.NamingEnumeration)

Aggregations

NamingEnumeration (javax.naming.NamingEnumeration)48 Attribute (javax.naming.directory.Attribute)22 SearchResult (javax.naming.directory.SearchResult)20 NamingException (javax.naming.NamingException)19 Attributes (javax.naming.directory.Attributes)19 SearchControls (javax.naming.directory.SearchControls)18 DirContext (javax.naming.directory.DirContext)14 ArrayList (java.util.ArrayList)11 LdapContext (javax.naming.ldap.LdapContext)10 Test (org.junit.Test)9 InitialContext (javax.naming.InitialContext)7 Hashtable (java.util.Hashtable)6 Context (javax.naming.Context)6 InitialDirContext (javax.naming.directory.InitialDirContext)6 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Binding (javax.naming.Binding)3 NameClassPair (javax.naming.NameClassPair)3 NameNotFoundException (javax.naming.NameNotFoundException)3