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.api.interceptor.context;
21
22
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.commons.lang3.ArrayUtils;
27 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28 import org.apache.directory.api.ldap.model.exception.LdapException;
29 import org.apache.directory.api.ldap.model.name.Dn;
30 import org.apache.directory.api.ldap.model.schema.AttributeType;
31 import org.apache.directory.api.ldap.model.schema.AttributeTypeOptions;
32 import org.apache.directory.api.ldap.model.schema.SchemaManager;
33 import org.apache.directory.api.ldap.model.schema.SchemaUtils;
34 import org.apache.directory.api.ldap.model.schema.UsageEnum;
35 import org.apache.directory.server.core.api.CoreSession;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46 public abstract class FilteringOperationContext extends AbstractOperationContext
47 {
48
49 protected static final Logger LOG = LoggerFactory.getLogger( FilteringOperationContext.class );
50
51
52 protected Set<AttributeTypeOptions> returningAttributes;
53
54
55 protected String[] returningAttributesString;
56
57
58 private boolean allOperationalAttributes;
59
60
61 private boolean allUserAttributes;
62
63
64 private boolean noAttributes;
65
66
67 protected boolean typesOnly = false;
68
69
70
71
72
73
74
75 public FilteringOperationContext( CoreSession session )
76 {
77
78 this( session, SchemaConstants.ALL_USER_ATTRIBUTES );
79 }
80
81
82
83
84
85
86
87
88 public FilteringOperationContext( CoreSession session, Dn dn )
89 {
90
91 this( session, dn, SchemaConstants.ALL_USER_ATTRIBUTES );
92 }
93
94
95
96
97
98
99
100
101 public FilteringOperationContext( CoreSession session, String... returningAttributes )
102 {
103 super( session );
104
105 setReturningAttributes( returningAttributes );
106 }
107
108
109
110
111
112
113
114
115
116 public FilteringOperationContext( CoreSession session, Dn dn, String... returningAttributes )
117 {
118 super( session, dn );
119
120 setReturningAttributes( returningAttributes );
121 }
122
123
124
125
126
127 public Set<AttributeTypeOptions> getReturningAttributes()
128 {
129 return returningAttributes;
130 }
131
132
133
134
135
136 public String[] getReturningAttributesString()
137 {
138 return returningAttributesString;
139 }
140
141
142
143
144
145
146
147
148
149 public boolean contains( SchemaManager schemaManager, String attribute )
150 {
151 if ( isNoAttributes() )
152 {
153 return false;
154 }
155
156 try
157 {
158 AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( attribute );
159
160 return contains( schemaManager, attributeType );
161 }
162 catch ( LdapException le )
163 {
164 return false;
165 }
166 }
167
168
169
170
171
172
173
174
175
176 public boolean contains( SchemaManager schemaManager, AttributeType attributeType )
177 {
178 if ( isNoAttributes() )
179 {
180 return false;
181 }
182
183 if ( ( attributeType.getUsage() == UsageEnum.USER_APPLICATIONS ) && allUserAttributes )
184 {
185 return true;
186 }
187
188 if ( ( attributeType.getUsage() != UsageEnum.USER_APPLICATIONS ) && allOperationalAttributes )
189 {
190 return true;
191 }
192
193
194 if ( returningAttributes == null )
195 {
196 return false;
197 }
198
199
200 if ( returningAttributes.contains( new AttributeTypeOptions( attributeType ) ) )
201 {
202 return true;
203 }
204
205
206 for ( AttributeTypeOptions attributeTypeOptions : returningAttributes )
207 {
208 if ( attributeTypeOptions.getAttributeType().equals( attributeType )
209 || attributeTypeOptions.getAttributeType().isAncestorOf( attributeType ) )
210 {
211 return true;
212 }
213 }
214
215 return false;
216 }
217
218
219 public void setReturningAttributes( String... attributeIds )
220 {
221 if ( ( attributeIds != null ) && ( attributeIds.length != 0 ) && ( attributeIds[0] != null ) )
222 {
223
224
225
226 returningAttributes = new HashSet<>();
227 Set<String> attributesString = new HashSet<>();
228
229 Set<AttributeTypeOptions> collectedAttributes = collectAttributeTypes( attributeIds );
230
231
232 if ( !collectedAttributes.isEmpty() || allUserAttributes || allOperationalAttributes )
233 {
234 noAttributes = false;
235 }
236
237
238
239
240 if ( !collectedAttributes.isEmpty() )
241 {
242 for ( AttributeTypeOptions attributeTypeOption : collectedAttributes )
243 {
244 if ( attributeTypeOption.getAttributeType().isUser() && !allUserAttributes )
245 {
246
247 returningAttributes.add( attributeTypeOption );
248 attributesString.add( attributeTypeOption.getAttributeType().getOid() );
249 }
250
251 if ( attributeTypeOption.getAttributeType().isOperational() && !allOperationalAttributes )
252 {
253
254 returningAttributes.add( attributeTypeOption );
255 attributesString.add( attributeTypeOption.getAttributeType().getOid() );
256 }
257 }
258 }
259
260 if ( !attributesString.isEmpty() )
261 {
262
263 returningAttributesString = attributesString.toArray( ArrayUtils.EMPTY_STRING_ARRAY );
264 }
265 else
266 {
267
268 returningAttributesString = ArrayUtils.EMPTY_STRING_ARRAY;
269 }
270 }
271 else
272 {
273
274 allUserAttributes = true;
275 returningAttributesString = ArrayUtils.EMPTY_STRING_ARRAY;
276 }
277 }
278
279
280 private Set<AttributeTypeOptions> collectAttributeTypes( String... attributesIds )
281 {
282 Set<AttributeTypeOptions> collectedAttributes = new HashSet<>();
283
284 if ( ( attributesIds != null ) && ( attributesIds.length != 0 ) )
285 {
286 for ( String returnAttribute : attributesIds )
287 {
288 if ( returnAttribute == null )
289 {
290 continue;
291 }
292
293 if ( returnAttribute.equals( SchemaConstants.NO_ATTRIBUTE ) )
294 {
295 noAttributes = true;
296 continue;
297 }
298
299 if ( returnAttribute.equals( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES ) )
300 {
301 allOperationalAttributes = true;
302 continue;
303 }
304
305 if ( returnAttribute.equals( SchemaConstants.ALL_USER_ATTRIBUTES ) )
306 {
307 allUserAttributes = true;
308 continue;
309 }
310
311 try
312 {
313 String id = SchemaUtils.stripOptions( returnAttribute );
314 Set<String> options = SchemaUtils.getOptions( returnAttribute );
315
316 AttributeType attributeType = session.getDirectoryService()
317 .getSchemaManager().lookupAttributeTypeRegistry( id );
318 AttributeTypeOptions attrOptions = new AttributeTypeOptions( attributeType, options );
319
320 collectedAttributes.add( attrOptions );
321 }
322 catch ( LdapException le )
323 {
324 LOG.warn( "Requested attribute {} does not exist in the schema, it will be ignored",
325 returnAttribute );
326
327 }
328 }
329 }
330
331 return collectedAttributes;
332 }
333
334
335
336
337
338 public void setAllOperationalAttributes( boolean allOperationalAttributes )
339 {
340 this.allOperationalAttributes = allOperationalAttributes;
341 }
342
343
344
345
346
347 public boolean isAllUserAttributes()
348 {
349 return allUserAttributes;
350 }
351
352
353
354
355
356 public void setAllUserAttributes( boolean allUserAttributes )
357 {
358 this.allUserAttributes = allUserAttributes;
359 }
360
361
362
363
364
365 public boolean isAllOperationalAttributes()
366 {
367 return allOperationalAttributes;
368 }
369
370
371
372
373
374 public boolean isNoAttributes()
375 {
376 return noAttributes;
377 }
378
379
380
381
382
383 public void setNoAttributes( boolean noAttributes )
384 {
385 this.noAttributes = noAttributes;
386 }
387
388
389
390
391
392 public boolean isTypesOnly()
393 {
394 return typesOnly;
395 }
396
397
398
399
400
401 public void setTypesOnly( boolean typesOnly )
402 {
403 this.typesOnly = typesOnly;
404 }
405
406
407
408
409
410 @Override
411 public String toString()
412 {
413 StringBuilder sb = new StringBuilder();
414
415 sb.append( "FilteringOperationContext for Dn '" );
416 sb.append( dn.getName() ).append( "'" );
417
418 if ( isTypesOnly() )
419 {
420 sb.append( ", type only" );
421 }
422
423 if ( allOperationalAttributes )
424 {
425 sb.append( ", +" );
426 }
427
428 if ( allUserAttributes )
429 {
430 sb.append( ", *" );
431 }
432
433 if ( noAttributes )
434 {
435 sb.append( ", 1.1" );
436 }
437
438 if ( ( returningAttributesString != null ) && ( returningAttributesString.length > 0 ) )
439 {
440 sb.append( ", attributes : <" );
441 boolean isFirst = true;
442
443 for ( String returningAttribute : returningAttributesString )
444 {
445 if ( isFirst )
446 {
447 isFirst = false;
448 }
449 else
450 {
451 sb.append( ", " );
452 }
453
454 sb.append( returningAttribute );
455 }
456
457 sb.append( ">" );
458 }
459
460 return sb.toString();
461 }
462 }