Announcement Announcement Module
Collapse
No announcement yet.
Using SearchControls and Filters in ContextMapper Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using SearchControls and Filters in ContextMapper

    Hello,

    Can anyone plz let me know how SearchControls and Filters can be used in ContextMapper?

    Thanks in advance,

    Mercury.

  • #2
    I'm sorry if this sounds rude, but we have spent quite a lot of time producing pretty good reference documentation and api documentation. We'd be more than happy to get feedback on the documentation so that it might be improved to help getting future users up to speed.

    As documented in the api docs there is a search method in LdapTemplate that takes base DN, filter, SearchControls and a ContextMapper.

    Comment


    • #3
      Sorry, May be am not clear. I was supposed to know how searchcontrols/filters can be implemented in LOOKUP operation. (Just like using using wildcard search ... )

      Comment


      • #4
        As per your suggestion, I have changed my mind to SEARCH operation, instead of LOOKUP.

        Am passing basepath, filter, search, attributesmapper to SEARCH method.

        Below is the code ...

        Code:
        public User getContactDetails(String commonName){
        	System.out.println("Given name = " + commonName);
        	System.out.println("Base path = " + basePath);
        	AndFilter andFilter = new AndFilter();		
        	andFilter.and(new WhitespaceWildcardsFilter("objectclass"," ")); << I should get the users where available in the directory, So I am using WhitespaceWildcardsFilter here. Am I right in doing this ? >>
        	andFilter.and(new EqualsFilter("sAMAccountName",commonName)); << Userid would be entered which is sAMAccountName in LDAP Terms. >>
        	System.out.println("LDAP Query = " + andFilter.encode());
        	
        /*	SearchControls searchControls = new SearchControls();
        	searchControls.setCountLimit(0);
        	searchControls.setReturningObjFlag(true);
        	System.out.println("LDAP SearchControls = " + searchControls);
        */
        		
        	List<User> list = ldapTemplate.search(basePath, andFilter.encode(), SearchControls.SUBTREE_SCOPE, new PersonAttributeMapper());
        		
        << As I need only record, I am expecting the same with Filters. However SEARCH will return me a list and so I am using the IF conditions here as .. >>
        
        	if(list.size()==0){
        		System.out.println("No object found !!!");
        		return null;
        	}
        	else if(list.size()==1){
        		System.out.println("Yo !! Only one object found, so that is ours.");
        		User user = list.get(0);
        		return user;
        	}else{
        		System.out.println("Ahhh !! Size is more than 1.");
        		return null;
        	}
        }
        	
        public class PersonAttributeMapper implements AttributesMapper{
        
        	public Object mapFromAttributes(Attributes attributes) throws NamingException {
        		
        << Mapping the basic details which I need for now. >>	
        		User user = new User();
        			
        		String commonName = (String)attributes.get("cn").get();
        		if(commonName != null)
        			user.setFullName(commonName);
        			
        		String lastName = (String)attributes.get("sn").get();
        		if(lastName != null)
        			user.setLastName(lastName);
        			
        		Attribute description = attributes.get("description");
        		if(description != null)
        			user.setDescription((String)description.get());
        			
        		Attribute managerName = attributes.get("manager");
        		if(managerName != null)
        			user.setManagerName((String)managerName.get());
        			
        		return user;
        	}
        
        }
        All the time I am getting a NullPointerException on getting the User details, as the list size is always zero. Dont know why?

        Please suggest.

        Thanks,
        Mercury.

        Comment

        Working...
        X