KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > simya > ldap > beans > LdapSearchBean


1 package net.simya.ldap.beans;
2
3 import javax.naming.*;
4 import javax.naming.directory.*;
5 import java.util.StringTokenizer JavaDoc;
6 import java.lang.Integer JavaDoc;
7
8 /**
9  * This code authenticates users againts ldap
10  *
11  * assumption1: user don't set both DN and userName;
12  */

13 public class LdapSearchBean
14 {
15
16     private SearchControls ctls = new SearchControls();
17     private NamingEnumeration answer = null;
18     private Attributes attributes = null;
19     private SearchResult sr = null;
20     private String JavaDoc DN = null;
21     
22     private String JavaDoc filter = null;
23     private String JavaDoc searchBase = null;
24     private String JavaDoc scope = null;
25
26     public void search( DirContext ctx )
27     throws NamingException
28     {
29         // Set search scope
30
if (scope == null || scope.equalsIgnoreCase("sub"))
31              ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); // search whole tree
32
else if ( scope.equalsIgnoreCase("one"))
33              ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE); // search one level tree
34
else if ( scope.equalsIgnoreCase("base"))
35              ctls.setSearchScope(SearchControls.OBJECT_SCOPE); // search one level tree
36

37         // Search for objects that have those matching attributes
38
answer = ctx.search(searchBase, filter, ctls);
39     }
40
41     public boolean next()
42     throws NamingException
43     {
44         if ( answer.hasMoreElements() )
45         {
46             sr = (SearchResult)answer.next();
47             // get DN
48
DN = sr.getName();
49             attributes = sr.getAttributes();
50             return true;
51         }
52         return false;
53     }
54     
55     public void setReturningAttributes( String JavaDoc list )
56     {
57         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( list, "," );
58         String JavaDoc[] attList = new String JavaDoc[ st.countTokens() ];
59         for ( int i=0; st.hasMoreTokens(); i++ )
60             attList[i] = st.nextToken().trim();
61         ctls.setReturningAttributes( attList );
62     }
63
64     public Attribute getAttribute (String JavaDoc attrName)
65     throws NamingException
66     {
67         return attributes.get(attrName);
68     }
69
70     public String JavaDoc getStringAttribute (String JavaDoc attrName)
71     throws NamingException
72     {
73         Attribute attr = attributes.get(attrName);
74         return attr != null ? (String JavaDoc) attr.get() : "";
75     }
76
77     public String JavaDoc getStringAttribute (String JavaDoc attrName, String JavaDoc idx )
78     throws NamingException
79     {
80         Attribute attr = attributes.get(attrName);
81         return attr != null ? (String JavaDoc) attr.get(new Integer JavaDoc(idx).intValue()) : "";
82     }
83     
84     public int getAttributeSize ( String JavaDoc attrName )
85     throws NamingException
86     {
87         Attribute attr = attributes.get(attrName);
88         return attr != null ? attr.size() : 0;
89     }
90
91     public String JavaDoc getFilter() { return this.filter; }
92     public void setFilter(String JavaDoc filter) { this.filter = filter; }
93
94     public String JavaDoc getSearchBase() { return this.searchBase; }
95     public void setSearchBase(String JavaDoc searchBase) { this.searchBase = searchBase; }
96     
97     public String JavaDoc getScope() { return this.scope; }
98     public void setScope(String JavaDoc scope) { this.scope = scope; }
99     
100     public void setLimit(String JavaDoc limit) {
101        ctls.setCountLimit( new Integer JavaDoc(limit).intValue() );
102     }
103     
104     public String JavaDoc getDN () { return DN;}
105     
106     
107 }
Popular Tags