1
1
package com .iat .tpldapapachedirectory .service ;
2
2
3
- import com .iat .tpldapapachedirectory .model .User ;
4
3
import org .apache .directory .api .ldap .model .cursor .CursorException ;
5
4
import org .apache .directory .api .ldap .model .cursor .EntryCursor ;
6
5
import org .apache .directory .api .ldap .model .entry .*;
7
6
import org .apache .directory .api .ldap .model .exception .LdapException ;
8
7
import org .apache .directory .api .ldap .model .message .SearchScope ;
9
8
import org .apache .directory .ldap .client .api .LdapConnection ;
10
- import org .springframework .beans .factory .annotation .Autowired ;
11
9
import org .springframework .stereotype .Component ;
12
10
13
11
import java .io .IOException ;
14
12
import java .util .ArrayList ;
15
- import java .util .List ;
16
13
17
14
@ Component
18
15
public class LdapQueries {
19
16
20
17
/**
21
18
* Performs search using a search of all persons in a LDAP model (SearchScope.SearchScope.SUBTREE)
19
+ *
22
20
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
23
- * @param domain : a LDAP model
21
+ * @param domain : a LDAP model
24
22
*/
25
23
public void findAllPersons (LdapConnection connection , String domain ) throws LdapException , CursorException , IOException {
26
24
27
25
// Root : Dn ; parse : all trees ans subtrees
28
- EntryCursor cursor = connection .search (domain , "(objectclass=*)" , SearchScope .SUBTREE , "*" );
26
+ EntryCursor cursor = connection .search (domain , "(objectclass=*)" , SearchScope .SUBTREE , "*" );
29
27
int nbrEntries = 0 ;
30
28
31
- while (cursor .next ())
32
-
33
- {
29
+ while (cursor .next ()) {
34
30
Entry entry = cursor .get ();
35
31
System .out .println (entry );
36
- nbrEntries +=1 ;
32
+ nbrEntries += 1 ;
37
33
38
34
}
39
35
@@ -43,23 +39,23 @@ public void findAllPersons(LdapConnection connection, String domain) throws Ldap
43
39
44
40
/**
45
41
* Performs search using a search of all persons in a LDAP organizational unit (SearchScope.ONELEVEL)
42
+ *
46
43
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
47
- * @param domain : a LDAP model
44
+ * @param domain : a LDAP model
48
45
*/
49
46
public ArrayList <Entry > findAllAdm (LdapConnection connection , String domain ) throws LdapException , CursorException , IOException {
50
47
51
48
ArrayList <Entry > entries = new ArrayList <>();
52
49
53
50
// Root : ou=adm ; parse : all adm, only adm (one level)
54
- EntryCursor cursor = connection .search ( "ou=adm, " + domain + "" , "(objectclass=*)" , SearchScope .ONELEVEL , "*" );
51
+ EntryCursor cursor = connection .search ("ou=adm, " + domain + "" , "(objectclass=*)" , SearchScope .ONELEVEL , "*" );
55
52
int nbrEntries = 0 ;
56
53
57
- while (cursor .next ())
58
- {
54
+ while (cursor .next ()) {
59
55
Entry entry = cursor .get ();
60
56
System .out .println (entry );
61
57
entries .add (entry );
62
- nbrEntries +=1 ;
58
+ nbrEntries += 1 ;
63
59
64
60
}
65
61
@@ -72,48 +68,50 @@ public ArrayList<Entry> findAllAdm(LdapConnection connection, String domain) thr
72
68
73
69
/**
74
70
* Add an entry (a person) to the LDAP server
71
+ *
75
72
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
76
- * @param domain : a LDAP model
77
- * @param cn : a LDAP common name
78
- * @param sn : a LDAP surname
73
+ * @param domain : a LDAP model
74
+ * @param cn : a LDAP common name
75
+ * @param sn : a LDAP surname
79
76
*/
80
77
public void addPerson (LdapConnection connection , String domain , String cn , String sn ) throws LdapException {
81
78
connection .add (
82
79
new DefaultEntry (
83
- "cn=" + cn + ", ou=adm, " + domain + "" , // the dn
80
+ "cn=" + cn + ", ou=adm, " + domain + "" , // the dn
84
81
"ObjectClass: inetOrgPerson" ,
85
82
"ObjectClass: organizationalPerson" ,
86
83
"ObjectClass: person" ,
87
84
"ObjectClass: radiusprofile" ,
88
85
"ObjectClass: top" ,
89
- "cn: " + cn + "" ,
90
- "sn: " + sn + "" ) );
86
+ "cn: " + cn + "" ,
87
+ "sn: " + sn + "" ) );
91
88
92
- assertTrue (connection .exists ("cn=" + cn + ", ou=adm, " + domain + "" ));
89
+ assertTrue (connection .exists ("cn=" + cn + ", ou=adm, " + domain + "" ));
93
90
}
94
91
95
92
/**
96
93
* Delete the entry (a person) with the given distinguished name to the LDAP server
94
+ *
97
95
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
98
- * @param domain : a LDAP model
99
- * @param cn : a LDAP common name
96
+ * @param domain : a LDAP model
97
+ * @param cn : a LDAP common name
100
98
*/
101
99
public void deletePerson (LdapConnection connection , String domain , String cn ) throws Exception {
102
100
103
- connection .delete ("cn=" + cn + ", ou=adm, " + domain + "" );
101
+ connection .delete ("cn=" + cn + ", ou=adm, " + domain + "" );
104
102
}
105
103
106
104
/**
107
105
* Applies all the modifications (add attributes + values = ModificationOperation.ADD_ATTRIBUTE)...
108
106
* ... to the entry (a person) specified by its distinguished name
109
- * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
110
- * @param domain : a LDAP model
111
- * @param cn : a LDAP common name
107
+ *
108
+ * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
109
+ * @param domain : a LDAP model
110
+ * @param cn : a LDAP common name
112
111
* @param attributeId1 : first attribute to add
113
- * @param value1 : first value to add
112
+ * @param value1 : first value to add
114
113
* @param attributeId2 : second attribute to add
115
- * @param value2 : second value to add
116
- *
114
+ * @param value2 : second value to add
117
115
*/
118
116
public void addAttributesToPerson (LdapConnection connection , String domain , String cn , String attributeId1 ,
119
117
String value1 , String attributeId2 , String value2 ) throws LdapException {
@@ -122,38 +120,38 @@ public void addAttributesToPerson(LdapConnection connection, String domain, Stri
122
120
Modification addedInitials = new DefaultModification (ModificationOperation .ADD_ATTRIBUTE , attributeId2 ,
123
121
value2 );
124
122
125
- connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , addedGivenName , addedInitials ); // the dn
123
+ connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , addedGivenName , addedInitials ); // the dn
126
124
}
127
125
128
126
/**
129
127
* Applies all the modifications (remove attributes = ModificationOperation.REMOVE_ATTRIBUTE)...
130
128
* ... to the entry (a person) specified by its distinguished name
131
- * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
132
- * @param domain : a LDAP model
133
- * @param cn : a LDAP common name
129
+ *
130
+ * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
131
+ * @param domain : a LDAP model
132
+ * @param cn : a LDAP common name
134
133
* @param attributeId1 : first attribute to remove
135
134
* @param attributeId2 : second attribute to remove
136
- *
137
135
*/
138
136
public void removeAttributesToPerson (LdapConnection connection , String domain , String cn ,
139
137
String attributeId1 , String attributeId2 ) throws LdapException {
140
138
Modification removedGivenName = new DefaultModification (ModificationOperation .REMOVE_ATTRIBUTE , attributeId1 );
141
139
Modification removedInitials = new DefaultModification (ModificationOperation .REMOVE_ATTRIBUTE , attributeId2 );
142
140
143
- connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , removedGivenName , removedInitials ); // the dn
141
+ connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , removedGivenName , removedInitials ); // the dn
144
142
}
145
143
146
144
/**
147
145
* Applies all the modifications (replace attributes + values = ModificationOperation.REPLACE_ATTRIBUTE)...
148
146
* ... to the entry (a person) specified by its distinguished name
149
- * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
150
- * @param domain : a LDAP model
151
- * @param cn : a LDAP common name
147
+ *
148
+ * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
149
+ * @param domain : a LDAP model
150
+ * @param cn : a LDAP common name
152
151
* @param attributeId1 : first attribute to replace
153
- * @param value1 : first value to replace
152
+ * @param value1 : first value to replace
154
153
* @param attributeId2 : second attribute to replace
155
- * @param value2 : second value to replace
156
- *
154
+ * @param value2 : second value to replace
157
155
*/
158
156
public void replaceAttributesToPerson (LdapConnection connection , String domain , String cn , String attributeId1 ,
159
157
String value1 , String attributeId2 , String value2 ) throws LdapException {
@@ -162,14 +160,15 @@ public void replaceAttributesToPerson(LdapConnection connection, String domain,
162
160
Modification replacedInitials = new DefaultModification (ModificationOperation .REPLACE_ATTRIBUTE ,
163
161
attributeId2 , value2 );
164
162
165
- connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , replacedGivenName , replacedInitials ); // the dn
163
+ connection .modify ("cn=" + cn + ", ou=adm, " + domain + "" , replacedGivenName , replacedInitials ); // the dn
166
164
}
167
165
168
166
/**
169
167
* Move and rename the given entryDn. The old relative distinguished name will be deleted.
170
- * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
171
- * @param oldDn : The old relative distinguished name will be deleted
172
- * @param newDn : The new relative distinguished name
168
+ *
169
+ * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
170
+ * @param oldDn : The old relative distinguished name will be deleted
171
+ * @param newDn : The new relative distinguished name
173
172
* @param deleteOldDn : Tells if the old relative distinguished name must be removed (true = removed)
174
173
*/
175
174
public void moveAndRenamePerson (LdapConnection connection , String oldDn , String newDn ,
@@ -179,20 +178,19 @@ public void moveAndRenamePerson(LdapConnection connection, String oldDn, String
179
178
180
179
/**
181
180
* Performs search using a search of all organizational unit in a LDAP model (SearchScope.SearchScope.ONELEVEL)
181
+ *
182
182
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
183
- * @param domain : a LDAP model
183
+ * @param domain : a LDAP model
184
184
*/
185
185
public void findAllOu (LdapConnection connection , String domain ) throws LdapException , CursorException , IOException {
186
186
187
- EntryCursor cursor = connection .search (domain , "(objectclass=organizationalUnit)" , SearchScope .ONELEVEL , "*" );
187
+ EntryCursor cursor = connection .search (domain , "(objectclass=organizationalUnit)" , SearchScope .ONELEVEL , "*" );
188
188
int nbrEntries = 0 ;
189
189
190
- while (cursor .next ())
191
-
192
- {
190
+ while (cursor .next ()) {
193
191
Entry entry = cursor .get ();
194
192
System .out .println (entry );
195
- nbrEntries +=1 ;
193
+ nbrEntries += 1 ;
196
194
197
195
}
198
196
@@ -202,45 +200,48 @@ public void findAllOu(LdapConnection connection, String domain) throws LdapExcep
202
200
203
201
/**
204
202
* Add an entry (an organizational unit) to the LDAP server
203
+ *
205
204
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
206
- * @param domain : a LDAP model
207
- * @param ou : an organizational unit
205
+ * @param domain : a LDAP model
206
+ * @param ou : an organizational unit
208
207
*/
209
208
public void addOu (LdapConnection connection , String domain , String ou ) throws LdapException {
210
209
connection .add (
211
210
new DefaultEntry (
212
- "ou=" + ou + ", " + domain + "" , // the dn
211
+ "ou=" + ou + ", " + domain + "" , // the dn
213
212
"ObjectClass: organizationalUnit" ,
214
213
"ObjectClass: top" ,
215
- "ou: " + ou + "" ) );
214
+ "ou: " + ou + "" ));
216
215
217
- assertTrue (connection .exists ("ou=" + ou + ", " + domain + "" ));
216
+ assertTrue (connection .exists ("ou=" + ou + ", " + domain + "" ));
218
217
}
219
218
220
219
/**
221
220
* Delete the entry (an organizational unit) with the given distinguished name to the LDAP server
221
+ *
222
222
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
223
- * @param domain : a LDAP model
223
+ * @param domain : a LDAP model
224
224
*/
225
- public void deleteOu (LdapConnection connection , String domain ,String ou ) throws Exception {
225
+ public void deleteOu (LdapConnection connection , String domain , String ou ) throws Exception {
226
226
227
- connection .delete ("ou=" + ou + ", " + domain + "" );
227
+ connection .delete ("ou=" + ou + ", " + domain + "" );
228
228
}
229
229
230
230
/**
231
231
* Renames the given entryDn with new relative distinguished name and deletes the old relative distinguished name
232
232
* if deleteOldRdn is set to true
233
- * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
234
- * @param entryDn : The old relative distinguished name will be deleted
235
- * @param newDn : The new relative distinguished name
233
+ *
234
+ * @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
235
+ * @param entryDn : The old relative distinguished name will be deleted
236
+ * @param newDn : The new relative distinguished name
236
237
* @param deleteOldDn : Tells if the old relative distinguished name must be removed (true = removed)
237
238
*/
238
239
public void renameOu (LdapConnection connection , String entryDn , String newDn ,
239
- boolean deleteOldDn ) throws LdapException {
240
+ boolean deleteOldDn ) throws LdapException {
240
241
connection .moveAndRename (entryDn , newDn , deleteOldDn );
241
242
}
242
243
243
244
244
-
245
- private void assertTrue ( boolean exists ) { }
245
+ private void assertTrue ( boolean exists ) {
246
+ }
246
247
}
0 commit comments