Announcement Announcement Module
Collapse
No announcement yet.
LDAP query / configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LDAP query / configuration

    Hello,

    I have some problem with my first LDAP prototype.

    An (external) LDAP server works well und connecting / browsing via Jxplorer is no problem.

    I try a simple search/query in a java application.
    First of all I set up a new context:
    Code:
    		LdapContextSource ctx = new LdapContextSource();
    		ctx.setUrl(url);
    		ctx.setUserDn(userDn);
    		ctx.setPassword(password);
    		ctx.setBase(base);
    		ctx.setPooled(pooled);
    (It should be a very simple application.. up to now, I do not need a xml configuration file..)

    For a comfortable search, I want to use LdapTemplate.
    Code:
    ldapTemplate = new LdapTemplate(ctx);
    For a simple test, I copied a code from http://static.springsource.org/sprin...roduction.html
    Code:
    	  public static List getAllPersonNames() {
    	      return ldapTemplate.search(
    	         base, "(objectclass=person)",
    	         new AttributesMapper() {
    	            public Object mapFromAttributes(Attributes attrs)
    	               throws NamingException {
    	               return attrs.get("cn").get();
    	            }
    	         });
    	   }
    If I try to execute the code, I get a Exception:
    Exception in thread "main" java.lang.NullPointerException
    at org.springframework.ldap.core.support.AbstractCont extSource.getReadOnlyContext(AbstractContextSource .java:125)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:287)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:259)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:571)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:556)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:411)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:431)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:451)
    ...
    I need help.. what else I have to configurate for a simple query?




    Edit:
    Some additional information:
    Code:
    	public DirContext getReadOnlyContext() {
    		if (!anonymousReadOnly) {
    			return getContext(authenticationSource.getPrincipal(), authenticationSource.getCredentials());
    		}
    		else {
    			return createContext(getAnonymousEnv());
    		}
    	}
    This is AbstractContextSource.getReadOnlyContext(AbstractC ontextSource.java:125).
    The exception occurs, because the authenticationSource is null. I do not understand why: all needed information are present in the LdapContextSource, aren't they?

    If I make a new authenticationSource I just get another exception.:
    Code:
    		ctx.setAuthenticationSource(new AuthenticationSource() {
    			
    			@Override
    			public String getPrincipal() {
    				// TODO Auto-generated method stub
    				return userDn;
    			}
    			
    			@Override
    			public String getCredentials() {
    				// TODO Auto-generated method stub
    				return password;
    			}
    		});
    New exception
    Exception in thread "main" java.lang.NullPointerException
    at java.util.Hashtable.<init>(Unknown Source)
    at org.springframework.ldap.core.support.AbstractCont extSource.getAuthenticatedEnv(AbstractContextSourc e.java:480)
    at org.springframework.ldap.core.support.AbstractCont extSource.getContext(AbstractContextSource.java:10 6)
    at org.springframework.ldap.core.support.AbstractCont extSource.getReadOnlyContext(AbstractContextSource .java:125)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:287)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:259)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:571)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:556)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:411)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:431)
    at org.springframework.ldap.core.LdapTemplate.search( LdapTemplate.java:451)
    ..
    at
    Code:
    Hashtable env = new Hashtable(getAnonymousEnv());

    I just want to connect to the ldap server and run simple queries. It seems I missed something. Could someone give me some hints?
    Last edited by florlp; May 9th, 2011, 12:37 PM.
Working...
X