Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving E-mail address from LDAP context object?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I need to modify a function that gets passed a username and returns the E-mail address (from LDAP) associated with that user.

Currently the function has the LDAP Host, Port and RootDN hard-coded in, and I need to remove those and just get the E-mail address from an LDAP context object returned by Tomcat...

I know nothing about web or database programming. I've only done simple stand-alone Java apps so far.

Any help would be much appreciated.
 
Code:
    private String getEmailAddress (String user) throws QueueException {
    	
    	// Connect to LDAP server.
    	Hashtable<String,String> env = new Hashtable<String, String>(11);
    	env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    	env.put(Context.PROVIDER_URL, "ldap://"+ldapHost+":"+ldapPort);
    	
    	InitialDirContext ctx = null;
        try {
			ctx = new InitialDirContext(env);
        } catch (Exception e) {
        	throw new QueueException ("Problem connecting to LDAP server:" + e.getLocalizedMessage());
        }
        
		String[] searchAttrs = {ldapSearchAttribs};
		SearchControls constraints1 = new SearchControls();
		constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE);
        constraints1.setCountLimit(0);
        constraints1.setTimeLimit(0);

        constraints1.setReturningAttributes(searchAttrs);
        
        StringBuffer s = new StringBuffer(ldapSearchTmpl);
        int idx = s.toString().indexOf("?");
        s.replace(idx, idx + 1, user);

        // Search for user's email given matching user ID.
        String email = null;
	    try {
	        NamingEnumeration<SearchResult> answer = ctx.search(ldapRootDN, s.toString(), constraints1);
	        
	        while (answer.hasMore()) {
	        	SearchResult ldapResult = answer.next();
                Attributes attrs = ldapResult.getAttributes();
                Attribute emailAttr = attrs.get(ldapSearchAttribs);
                email = (String) emailAttr.get();
	        }
	        ctx.close();

		} catch (Exception e) {
			// default to simply appending the domain if LDAP failed
			RequestQueue.logWarn("getEmailAddress: Unable to query email from LDAP for user: " + user);
			RequestQueue.logWarn("getEmailAddress: ", e);
			RequestQueue.logWarn("getEmailAddress: Appending @" + sDefaultEmailDomain + " instead.");
			email = user + "@" + sDefaultEmailDomain;
		}
		
		return email;
    }
I think I need to use the code from this site:

and do something like this:
Code:
Server server = ServerFactory.getServer();
//Note, this assumes the Container is "Catalina"
Service service = server.findService("Catalina");
Container engine = service.getContainer();
//Note, this assumes your context is "myContext"
JNDIRealm realm = (JNDIRealm) engine.getRealm();
But I'm not sure what to do with the JNDIRealm object after that?
 
OK, I was able to replace these lines:
Code:
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://"+ldapHost+":"+ldapPort);
with this:
Code:
env.put( Context.INITIAL_CONTEXT_FACTORY, realm.getContextFactory() );
env.put( Context.PROVIDER_URL, realm.getConnectionURL() );
But I still have to replace the ldapRootDN variable (which is currently hard-coded) in this line:
Code:
NamingEnumeration<SearchResult> answer = ctx.search([b]ldapRootDN[/b], s.toString(), constraints1);
Any ideas on how to get the RootDN (whatever that means) from the JNDIRealm object?
 
I tried using the JNDIRealm.getUserPattern() function, but it returns something like:
Code:
"uid={0},ou=People,dc=mycompany,dc=com"
instead of the
Code:
"dc=mycompany,dc=com"
that was hard-coded before.
 
Thanks. I'm not sure if that will work or not, but I just took the easy way out and simply chopped off the "uid={0}," from the string that JNDIRealm.getUserPattern() returns.

Everything seems to be working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top