1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package es.accv.arangi.base.ldap;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.apache.log4j.Logger;
29
30 import com.novell.ldap.LDAPAttribute;
31 import com.novell.ldap.LDAPConnection;
32 import com.novell.ldap.LDAPEntry;
33 import com.novell.ldap.LDAPModification;
34 import com.novell.ldap.LDAPSearchResults;
35
36 import es.accv.arangi.base.exception.ldap.LDAPException;
37
38
39
40
41
42
43
44
45
46 public class LDAP {
47
48
49
50
51 public static final int LDAP_DEFAULT_PORT = 389;
52
53
54
55
56
57 Logger logger = Logger.getLogger(LDAP.class);
58
59
60
61
62 LDAPConnection lc;
63
64
65
66
67
68
69
70
71
72
73 public LDAP (String host, int port, String user, String password) throws LDAPException {
74
75 logger.debug("[LDAP]::Entrada:: " + Arrays.asList(new Object[] { host, port, user }));
76
77 lc = new LDAPConnection();
78 try {
79 lc.connect(host, port);
80 } catch (com.novell.ldap.LDAPException e) {
81 logger.info ("[LDAP]::No ha sido posible conectarse al host " + host + ":" + port, e);
82 throw new LDAPException ("No ha sido posible conectarse al host " + host + ":" + port, e);
83 }
84 try {
85 lc.bind( LDAPConnection.LDAP_V3, user, password.getBytes() );
86 } catch (com.novell.ldap.LDAPException e) {
87 logger.info ("[LDAP]::No ha sido posible autenticarse en el host " + host + ":" + port, e);
88 throw new LDAPException ("No ha sido posible autenticarse en el al host " + host + ":" + port, e);
89 }
90
91 }
92
93
94
95
96
97
98
99
100
101 public LDAP (String host, String user, String password) throws LDAPException {
102 this(host, LDAP_DEFAULT_PORT, user, password);
103 }
104
105
106
107
108
109
110
111
112
113 public List<String> getDNs (String branch, String filter) throws LDAPException {
114
115 logger.debug("[LDAP.getDNs]::Entrada::" + Arrays.asList(new Object[] { branch, filter }));
116
117 LDAPSearchResults results;
118 try {
119 results = lc.search(branch, LDAPConnection.SCOPE_ONE, filter, null, false);
120 } catch (com.novell.ldap.LDAPException e) {
121 logger.info ("[LDAP.getDNs]::Ha ocurrido un error realizando la búsqueda", e);
122 throw new LDAPException ("Ha ocurrido un error realizando la búsqueda", e);
123 }
124 List<String> result = new ArrayList<String>();
125 while (results.hasMore()) {
126 LDAPEntry entry;
127 try {
128 entry = results.next();
129 } catch (com.novell.ldap.LDAPException e) {
130 logger.info ("[LDAP.getDNs]::Ha ocurrido un error recuperando una de las entradas resultado de la búsqueda", e);
131 throw new LDAPException ("Ha ocurrido un error recuperando una de las entradas resultado de la búsqueda", e);
132 }
133 result.add(entry.getDN());
134 }
135
136 logger.debug("[LDAP.getDNs]::Se devolverán " + result.size() + " elementos.");
137
138 return result;
139 }
140
141
142
143
144
145
146
147
148
149 public void copy (String dnOrigin, String dnDestination) throws LDAPException {
150
151 logger.debug("[LDAP.copy]::Entrada::" + Arrays.asList(new Object[] { dnOrigin, dnDestination }));
152
153
154 LDAPEntry entryOrigen;
155 try {
156 entryOrigen = getEntry(dnOrigin);
157 } catch (com.novell.ldap.LDAPException e1) {
158 logger.info("[LDAP.copy]::Se ha producido un error buscando la entrada origen: " + dnOrigin);
159 throw new LDAPException ("Se ha producido un error buscando la entrada origen: " + dnOrigin);
160 }
161 if (entryOrigen == null) {
162 logger.info("[LDAP.copy]::No se ha encontrado la entrada origen: " + dnOrigin);
163 throw new LDAPException ("No se ha encontrado la entrada para el DN '" + dnOrigin + "'");
164 }
165 logger.debug("[LDAP.copy]::Encontrada entrada origen: " + dnOrigin);
166
167
168 LDAPEntry entryDestino;
169 try {
170 entryDestino = getEntry (dnDestination);
171 } catch (com.novell.ldap.LDAPException e1) {
172 logger.info("[LDAP.copy]::Se ha producido un error buscando la entrada destino: " + dnDestination);
173 throw new LDAPException ("Se ha producido un error buscando la entrada destino: " + dnDestination);
174 }
175 if (entryDestino != null) {
176 logger.debug("[LDAP.copy]::Encontrada entrada destino: " + dnDestination);
177
178
179 List<LDAPModification> lModificaciones = new ArrayList<LDAPModification>();
180 for (Iterator iterator = entryOrigen.getAttributeSet().iterator(); iterator.hasNext();) {
181
182 LDAPAttribute attrOrigen = (LDAPAttribute)iterator.next();
183
184 if (attrOrigen.hasSubtype("binary")) {
185
186
187 logger.debug("[LDAP.copy]::Añadir atributo '" + attrOrigen.getBaseName() + "' al destino: " + dnDestination);
188 LDAPModification modification = new LDAPModification(LDAPModification.ADD, attrOrigen);
189
190 lModificaciones.add (modification);
191 }
192 }
193
194
195 try {
196 lc.modify(dnDestination, lModificaciones.toArray(new LDAPModification[0]));
197 } catch (com.novell.ldap.LDAPException e) {
198 logger.info ("[LDAP.getDNs]::No se ha podido modificar la entrada con DN=" + dnDestination, e);
199 throw new LDAPException ("No se ha podido modificar la entrada con DN=" + dnDestination, e);
200 }
201
202 } else {
203
204 logger.debug("[LDAP.copy]::No se ha encontrado la entrada destino: " + dnDestination);
205 entryDestino = new LDAPEntry(dnDestination, entryOrigen.getAttributeSet());
206 try {
207 lc.add (entryDestino);
208 } catch (com.novell.ldap.LDAPException e) {
209 logger.info ("[LDAP.getDNs]::No se ha podido añadir la entrada con DN=" + dnDestination, e);
210 throw new LDAPException ("No se ha podido añadir la entrada con DN=" + dnDestination, e);
211 }
212 }
213 }
214
215
216
217
218
219
220
221
222
223 public void move (String dnOrigin, String dnDestination) throws LDAPException {
224
225 logger.debug("[LDAP.move]::Entrada::" + Arrays.asList(new Object[] { dnOrigin, dnDestination }));
226
227
228 copy (dnOrigin, dnDestination);
229 logger.debug("[LDAP.move]::Se ha realizado la copia::" + Arrays.asList(new Object[] { dnOrigin, dnDestination }));
230
231
232 try {
233 lc.delete(dnOrigin);
234 } catch (com.novell.ldap.LDAPException e) {
235 logger.info ("[LDAP.getDNs]::No se ha podido eliminar la entrada con DN=" + dnOrigin, e);
236 throw new LDAPException ("No se ha podido eliminar la entrada con DN=" + dnOrigin, e);
237 }
238 logger.debug("[LDAP.move]::Se ha eliminado la entrada: " + dnOrigin);
239 }
240
241
242
243
244
245
246 public void close () throws LDAPException {
247
248 logger.debug("[LDAP.close]::Entrada");
249
250 try {
251 lc.disconnect();
252 } catch (com.novell.ldap.LDAPException e) {
253 logger.info ("[LDAP.getDNs]::No se ha podido cerrar la conexión con el servidor LDAP", e);
254 throw new LDAPException ("No se ha podido cerrar la conexión con el servidor LDAP", e);
255 }
256 logger.debug("[LDAP.close]::Se ha cerrado la conexión con el servidor LDAP");
257 }
258
259
260
261
262
263 private LDAPEntry getEntry (String dn) throws com.novell.ldap.LDAPException {
264 LDAPSearchResults results = lc.search(dn, LDAPConnection.SCOPE_BASE, null, null, false);
265 Object object;
266 try {
267 object = results.next();
268 } catch (com.novell.ldap.LDAPException e) {
269 return null;
270 }
271 if (!(object instanceof LDAPEntry)) {
272 return null;
273 }
274
275 return (LDAPEntry) object;
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 }