Announcement Announcement Module
Collapse
No announcement yet.
[Search] - How to make this request ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Search] - How to make this request ?

    Hello,

    For the connection with my directory, i use spring-ldap.
    I've got an OU: People which contains all the webmaster and i've got another OU:group which contains all the sites each webmaster can administrate.

    My problem is i don't know how to get all the sites a webmaster can administrate.

    To get a webmaster i'll do this :
    Code:
    	  public Webmaster getWebmaster(String ou,String login) {
    
    		  Name dn = buildDn(ou,login);
    		  Webmaster webmaster = null;
    		  try {
    			  webmaster = (Webmaster)ldapTemplate.lookup(dn, new WebmasterContextMapper());
    		  }
    		  catch (Exception e){
    			  log.error("getWebmaster"+e);
    		  }
    		 return webmaster;
    
    	  }
    Code:
    	  protected Name buildDn(String ou,String login) {
    		  DistinguishedName dn = new DistinguishedName();
    		  dn.add("ou", ou);
    		  dn.add("uid", login);
    
    		  return dn;
    	  }
    Code:
    	  private static class WebmasterContextMapper implements ContextMapper {
    		  public Object mapFromContext(Object ctx) {
    			  DirContextAdapter context = (DirContextAdapter)ctx;
    			  Webmaster webmaster = new Webmaster();
    			  webmaster.setLogin(context.getStringAttribute("uid"));
    			  //webmaster.setPassword(context.getStringAttribute("userPassword"));
    			  webmaster.setRoles(context.getStringAttributes("businessCategory"));
    			  return webmaster;
    		  }
    	  }
    It works fine and i'll want to do the same thing to get all the sites for the webmaster. So I have done that :

    Code:
    	  public String [] getWebmasterSites (String ou, String login){
    		  String [] sites = null;
    		  Name dn= buildDnSites (ou, login);
    		  try {
    			  sites = (String [])ldapTemplate.lookup(dn, new WebmasterContextSitesMapper());
    		  }
    		  catch (Exception e){
    			  log.error("getWebmasterSites"+e);
    		  }
    		  return sites;
    	  }
    Code:
    	  protected Name buildDnSites(String ou,String login) {
    		  DistinguishedName dn = new DistinguishedName();
    		  dn.add("ou", ou);
    		  dn.add("memberUid", login);
    
    		  return dn;
    	  }
    Code:
    	  private static class WebmasterContextSitesMapper implements ContextMapper {
    		  public Object mapFromContext(Object ctx) {
    			  DirContextAdapter context = (DirContextAdapter)ctx;
    			  String [] sites= context.getStringAttributes("businessCategory");
    			  return sites;
    		  }
    	  }
    But it doesn't work . Can somebody help me please ?

    Thanks

  • #2
    Could you please be more specific about in which way it doesn't work? It you get an exception please post the stacktrace.

    Comment


    • #3
      Thanks for your reply rasky !

      I've got this exception :
      Code:
      "2008-03-28 11:31:53,921 [http-8080-Processor25] ERROR LdapWebmasterDao  - getWebmasterSites org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - No Such Object]; nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]; remaining name 'memberuid=login, ou=group'
      But in fact, i don't know how to list all sites to find those i want ...

      Comment


      • #4
        To be more specific, what i want to do is :

        Get all the objetcs registered in the OU group
        For all objects, search those with a property memberUid ="login"
        And then return the "businessCategory" of each site found.

        I hope it's more clear now

        Thanks for your help.

        Comment


        • #5
          Ah, right. You'll want to do a search rather than a lookup. Your code would look something similar to this:
          Code:
          public List getWebmasterSites (String ou, String login){
            Filter filter = new EqualsFilter("memberUid", login);
            return ldapTemplate.search("ou=group", filter.encode(), new WebmasterContextSitesMapper());
          }
          Code:
          private static class WebmasterContextSitesMapper implements ContextMapper {
            public Object mapFromContext(Object ctx) {
              DirContextAdapter context = (DirContextAdapter)ctx;
              return context.getStringAttribute("businessCategory");
            }
          }
          This would give you a list of the matching sites.

          Comment


          • #6
            Your solution works well rasky
            Thank you very much !

            But what is the difference beetwen a lookup and a search ?

            Comment


            • #7
              The difference lies in the name: A lookup is used to lookup an entry when you know the unique identifier (Distinguished Name, or DN) of the entry in the LDAP tree. A search is used when you don't know the exact identifier but want to search for entries based on a number of criteria. The search criteria are analogous to the 'where' clause in SQL and are specified as a search filter.

              A lookup always returns just one entry (since a distinguished name must be unique in the LDAP tree), and trying to lookup a name that is not present in the tree will result in an error. A search however can return any number of entries; including no results if the search filter does not match anything.

              Comment

              Working...
              X