KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > BaseLDAPHandler


1 /**
2  * Copyright 2001-2004 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.ldap;
6
7 import java.util.ArrayList;
8 import java.util.Enumeration;
9 import java.util.List;
10 import java.util.Vector;
11
12 import netscape.ldap.LDAPAttribute;
13 import netscape.ldap.LDAPAttributeSet;
14 import netscape.ldap.LDAPEntry;
15 import netscape.ldap.LDAPException;
16 import netscape.ldap.LDAPSearchResults;
17
18 /**
19  * Created by the eXo platform team
20  * User: Daniel Summer
21  * Date: 28 august 2004
22  *
23  */

24
25 public class BaseLDAPHandler {
26
27     protected String getutil(String field, LDAPAttributeSet attrs) {
28         if (attrs.getAttribute(field) == null) {
29             return new String("");
30         } else {
31             try {
32                 return attrs
33                     .getAttribute(field)
34                     .getStringValues()
35                     .nextElement()
36                     .toString();
37             } catch (Exception e) {
38                 return new String("");
39             }
40         }
41     }
42
43     protected String fastgetutil(String field, LDAPAttributeSet attrs)
44         throws Exception {
45         LDAPAttribute values = attrs.getAttribute(field);
46         if (values == null)
47             return new String("");
48
49         Enumeration e = values.getStringValues();
50         if (e == null || !e.hasMoreElements())
51             return new String("");
52
53         return e.nextElement().toString();
54     }
55
56     protected Vector getutil(
57         String field,
58         LDAPAttributeSet attrs,
59         boolean empty) {
60         Vector values = null;
61         String temp;
62
63         if (empty) {
64             values = new Vector();
65         }
66         values = new Vector();
67         try {
68             LDAPAttribute attr = attrs.getAttribute(field);
69             if (null != attr) {
70
71                 for (Enumeration enum = attr.getStringValues();
72                     enum.hasMoreElements();
73                     ) {
74                     temp = (String) enum.nextElement();
75                     values.add(temp);
76                 }
77
78             }
79         } catch (Exception e) {
80             //logger.warn( "getUtil(): " + e.getMessage(), e );
81
}
82
83         return values;
84     }
85
86     protected List getUtil(LDAPSearchResults res) {
87         List l = new ArrayList();
88         while (res.hasMoreElements()) {
89
90             LDAPEntry findEntry = null;
91             try {
92                 findEntry = res.next();
93                 l.add(findEntry);
94
95             } catch (LDAPException e) {
96                 continue;
97             }
98         }
99         return l;
100     }
101
102     /*
103         protected void setutil(
104             String field,
105             Object value,
106             LDAPAttributeSet attrs) {
107             attrs.remove(field);
108             attrs.add(new LDAPAttribute( field, value ));
109             updated = true;
110         }
111     */

112     protected void setutil(
113         String field,
114         String value,
115         LDAPAttributeSet attrs) {
116         attrs.remove(field);
117         if (value == null || value.length() == 0)
118             value = " ";
119         attrs.add(new LDAPAttribute(field, value));
120
121     }
122
123     protected void setutil(
124         String field,
125         Vector values,
126         LDAPAttributeSet attrs) {
127         setutil(field, values, attrs, false);
128     }
129
130     protected void setutil(
131         String field,
132         Vector values,
133         LDAPAttributeSet attrs,
134         boolean create) {
135         attrs.remove(field);
136
137         if (values == null || (values.size() == 0 && create)) {
138             return;
139         }
140
141         LDAPAttribute attr =
142             new LDAPAttribute(field, (String[]) values.toArray());
143         attrs.add(attr);
144
145     }
146
147     protected void setutil(
148         String field,
149         String[] values,
150         LDAPAttributeSet attrs,
151         boolean create) {
152         attrs.remove(field);
153
154         if (values == null || (values.length == 0 && create)) {
155             return;
156         }
157
158         LDAPAttribute attr = new LDAPAttribute(field, values);
159         attrs.add(attr);
160
161     }
162 }
Popular Tags