1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.interceptor.context;
21
22
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import javax.naming.NamingException;
28 import javax.naming.directory.NoSuchAttributeException;
29 import javax.naming.directory.SearchControls;
30
31 import org.apache.directory.server.core.CoreSession;
32 import org.apache.directory.shared.ldap.constants.SchemaConstants;
33 import org.apache.directory.shared.ldap.filter.SearchScope;
34 import org.apache.directory.shared.ldap.message.AliasDerefMode;
35 import org.apache.directory.shared.ldap.name.LdapDN;
36 import org.apache.directory.shared.ldap.schema.AttributeType;
37 import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
38 import org.apache.directory.shared.ldap.schema.SchemaUtils;
39 import org.apache.directory.shared.ldap.util.ArrayUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import static org.apache.directory.shared.ldap.filter.SearchScope.ONELEVEL;
44
45
46
47
48
49
50
51
52
53 public abstract class SearchingOperationContext extends AbstractOperationContext
54 {
55
56 private static Logger LOG = LoggerFactory.getLogger( SearchingOperationContext.class );
57
58 private static String[] EMPTY_STR_ARRAY = new String[0];
59
60
61 protected AliasDerefMode aliasDerefMode = AliasDerefMode.DEREF_ALWAYS;
62
63
64 protected long sizeLimit = 0;
65
66
67 protected int timeLimit = 0;
68
69
70 protected SearchScope scope = ONELEVEL;
71
72
73 protected boolean allOperationalAttributes = false;
74
75
76 protected boolean allUserAttributes = false;
77
78
79 protected boolean noAttributes = false;
80
81
82 protected Set<AttributeTypeOptions> returningAttributes;
83
84
85 protected boolean abandoned = false;
86
87
88
89
90
91 public SearchingOperationContext( CoreSession session )
92 {
93 super( session );
94 }
95
96
97
98
99
100
101
102 public SearchingOperationContext( CoreSession session, LdapDN dn )
103 {
104 super( session, dn );
105 }
106
107
108
109
110
111
112
113
114 public SearchingOperationContext( CoreSession session, LdapDN dn, AliasDerefMode aliasDerefMode )
115 {
116 super( session, dn );
117 this.aliasDerefMode = aliasDerefMode;
118 }
119
120
121 protected void setReturningAttributes( Collection<String> attributesIds )
122 throws Exception
123 {
124 setReturningAttributes( attributesIds.toArray( EMPTY_STR_ARRAY ) );
125 }
126
127
128 protected void setReturningAttributes( String[] attributesIds ) throws Exception
129 {
130 if ( attributesIds != null && attributesIds.length != 0 )
131 {
132 returningAttributes = new HashSet<AttributeTypeOptions>();
133
134 for ( String returnAttribute : attributesIds )
135 {
136 if ( returnAttribute.equals( SchemaConstants.NO_ATTRIBUTE ) )
137 {
138 noAttributes = true;
139 continue;
140 }
141
142 if ( returnAttribute.equals( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES ) )
143 {
144 allOperationalAttributes = true;
145 continue;
146 }
147
148 if ( returnAttribute.equals( SchemaConstants.ALL_USER_ATTRIBUTES ) )
149 {
150 allUserAttributes = true;
151 continue;
152 }
153
154 try
155 {
156 String id = SchemaUtils.stripOptions( returnAttribute );
157 Set<String> options = SchemaUtils.getOptions( returnAttribute );
158
159 AttributeType attributeType = session.getDirectoryService()
160 .getRegistries().getAttributeTypeRegistry().lookup( id );
161 AttributeTypeOptions attrOptions = new AttributeTypeOptions( attributeType, options );
162
163 returningAttributes.add( attrOptions );
164 }
165 catch ( NoSuchAttributeException nsae )
166 {
167 LOG.warn( "Requested attribute {} does not exist in the schema, it will be ignored", returnAttribute );
168
169 }
170 }
171 }
172 }
173
174
175
176
177
178
179
180
181
182 public SearchingOperationContext( CoreSession session, LdapDN dn, AliasDerefMode aliasDerefMode,
183 SearchControls searchControls ) throws Exception
184 {
185 super( session, dn );
186 this.aliasDerefMode = aliasDerefMode;
187 this.scope = SearchScope.getSearchScope( searchControls );
188 this.timeLimit = searchControls.getTimeLimit();
189 this.sizeLimit = searchControls.getCountLimit();
190
191 if ( searchControls.getReturningAttributes() != null )
192 {
193 setReturningAttributes( searchControls.getReturningAttributes() );
194 }
195 }
196
197
198
199
200
201
202
203
204
205
206 public SearchingOperationContext( CoreSession session, LdapDN dn, AliasDerefMode aliasDerefMode,
207 Set<AttributeTypeOptions> returningAttributes )
208 {
209 super( session, dn );
210 this.aliasDerefMode = aliasDerefMode;
211 this.returningAttributes = returningAttributes;
212 }
213
214
215
216
217
218 public String toString()
219 {
220 return "ListOperationContext with DN '" + getDn().getUpName() + "'";
221 }
222
223
224 public AliasDerefMode getAliasDerefMode()
225 {
226 return aliasDerefMode;
227 }
228
229
230
231
232
233 public void setSizeLimit( long sizeLimit )
234 {
235 this.sizeLimit = sizeLimit;
236 }
237
238
239
240
241
242 public long getSizeLimit()
243 {
244 return sizeLimit;
245 }
246
247
248
249
250
251 public void setTimeLimit( int timeLimit )
252 {
253 this.timeLimit = timeLimit;
254 }
255
256
257
258
259
260 public int getTimeLimit()
261 {
262 return timeLimit;
263 }
264
265
266
267
268
269 public void setScope( SearchScope scope )
270 {
271 this.scope = scope;
272 }
273
274
275
276
277
278 public SearchScope getScope()
279 {
280 return scope;
281 }
282
283
284
285
286
287 public void setAllOperationalAttributes( boolean allOperationalAttribute )
288 {
289 this.allOperationalAttributes = allOperationalAttribute;
290 }
291
292
293
294
295
296 public boolean isAllOperationalAttributes()
297 {
298 return allOperationalAttributes;
299 }
300
301
302
303
304
305 public void setAllUserAttributes( boolean allUserAttributes )
306 {
307 this.allUserAttributes = allUserAttributes;
308 }
309
310
311
312
313
314 public boolean isAllUserAttributes()
315 {
316 return allUserAttributes;
317 }
318
319
320
321
322
323 public void setNoAttributes( boolean noAttributes )
324 {
325 this.noAttributes = noAttributes;
326 }
327
328
329
330
331
332 public boolean isNoAttributes()
333 {
334 return noAttributes;
335 }
336
337
338
339
340
341 public void setReturningAttributes( Set<AttributeTypeOptions> returningAttributes )
342 {
343 this.returningAttributes = returningAttributes;
344 }
345
346
347
348
349
350 public Set<AttributeTypeOptions> getReturningAttributes()
351 {
352 return returningAttributes;
353 }
354
355
356
357
358
359
360
361
362 public SearchControls getSearchControls()
363 {
364 return getSearchControls( false );
365 }
366
367
368
369
370
371
372
373
374
375 public SearchControls getSearchControls( boolean denormalized )
376 {
377 SearchControls controls = new SearchControls();
378 controls.setCountLimit( sizeLimit );
379 controls.setSearchScope( scope.getJndiScope() );
380 controls.setTimeLimit( timeLimit );
381
382 Set<String> allReturningAttributes = new HashSet<String>();
383
384 if ( noAttributes )
385 {
386 allReturningAttributes.add( SchemaConstants.NO_ATTRIBUTE );
387 }
388
389 if ( allUserAttributes )
390 {
391 allReturningAttributes.add( SchemaConstants.ALL_USER_ATTRIBUTES );
392 }
393
394 if ( allOperationalAttributes )
395 {
396 allReturningAttributes.add( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES );
397 }
398
399 if ( returningAttributes != null )
400 {
401 for ( AttributeTypeOptions at : returningAttributes )
402 {
403 if ( denormalized )
404 {
405 allReturningAttributes.add( at.getAttributeType().getName() );
406 }
407 else
408 {
409 allReturningAttributes.add( at.getAttributeType().getOid() );
410 }
411 }
412 }
413
414 if ( allReturningAttributes.size() > 0 )
415 {
416 controls.setReturningAttributes( allReturningAttributes.toArray( ArrayUtils.EMPTY_STRING_ARRAY ) );
417 }
418
419 return controls;
420 }
421
422
423
424
425
426 public void setAbandoned( boolean abandoned )
427 {
428 this.abandoned = abandoned;
429 }
430
431
432
433
434
435 public boolean isAbandoned()
436 {
437 return abandoned;
438 }
439 }