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.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.directory.api.ldap.model.entry.Entry;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.message.Control;
31 import org.apache.directory.api.ldap.model.name.Dn;
32 import org.apache.directory.server.core.api.CoreSession;
33 import org.apache.directory.server.core.api.LdapPrincipal;
34 import org.apache.directory.server.core.api.partition.Partition;
35 import org.apache.directory.server.core.api.partition.PartitionTxn;
36
37
38
39
40
41
42
43
44 public abstract class AbstractOperationContext implements OperationContext
45 {
46 protected static final Control[] EMPTY_CONTROLS = new Control[0];
47
48
49 protected Dn dn;
50
51
52 protected Entry entry;
53
54
55 protected Entry originalEntry;
56
57
58 protected Map<String, Control> requestControls = new HashMap<>( 4 );
59
60
61 protected Map<String, Control> responseControls = new HashMap<>( 4 );
62
63
64 protected Collection<String> byPassed;
65
66
67 protected List<String> interceptors;
68
69
70 protected int currentInterceptor;
71
72 protected LdapPrincipal authorizedPrincipal;
73
74
75 protected CoreSession session;
76
77
78 protected boolean throwReferral;
79
80
81 protected PartitionTxn transaction;
82
83
84 protected Partition partition;
85
86
87
88
89
90
91
92 public AbstractOperationContext( CoreSession session )
93 {
94 this.session = session;
95 currentInterceptor = 0;
96 }
97
98
99
100
101
102
103
104
105 public AbstractOperationContext( CoreSession session, Dn dn )
106 {
107 this.dn = dn;
108 this.session = session;
109
110
111
112 ignoreReferral();
113 }
114
115
116
117
118
119 @Override
120 public CoreSession getSession()
121 {
122 return session;
123 }
124
125
126
127
128
129 public void setSession( CoreSession session )
130 {
131 this.session = session;
132 }
133
134
135
136
137
138 protected void setAuthorizedPrincipal( LdapPrincipal authorizedPrincipal )
139 {
140 this.authorizedPrincipal = authorizedPrincipal;
141 }
142
143
144
145
146
147 @Override
148 public Dn getDn()
149 {
150 return dn;
151 }
152
153
154
155
156
157
158
159 @Override
160 public void setDn( Dn dn )
161 {
162 this.dn = dn;
163 }
164
165
166
167
168
169 @Override
170 public void addRequestControl( Control requestControl )
171 {
172 requestControls.put( requestControl.getOid(), requestControl );
173 }
174
175
176
177
178
179 @Override
180 public Control getRequestControl( String numericOid )
181 {
182 return requestControls.get( numericOid );
183 }
184
185
186
187
188
189 @Override
190 public boolean hasRequestControl( String numericOid )
191 {
192 return requestControls.containsKey( numericOid );
193 }
194
195
196
197
198
199 @Override
200 public boolean hasRequestControls()
201 {
202 return !requestControls.isEmpty();
203 }
204
205
206
207
208
209 @Override
210 public void addResponseControl( Control responseControl )
211 {
212 responseControls.put( responseControl.getOid(), responseControl );
213 }
214
215
216
217
218
219 @Override
220 public Control getResponseControl( String numericOid )
221 {
222 return responseControls.get( numericOid );
223 }
224
225
226
227
228
229 @Override
230 public boolean hasResponseControl( String numericOid )
231 {
232 return responseControls.containsKey( numericOid );
233 }
234
235
236
237
238
239 @Override
240 public Control[] getResponseControls()
241 {
242 if ( responseControls.isEmpty() )
243 {
244 return EMPTY_CONTROLS;
245 }
246
247 return responseControls.values().toArray( EMPTY_CONTROLS );
248 }
249
250
251
252
253
254 @Override
255 public boolean hasResponseControls()
256 {
257 return !responseControls.isEmpty();
258 }
259
260
261
262
263
264 @Override
265 public int getResponseControlCount()
266 {
267 return responseControls.size();
268 }
269
270
271
272
273
274 @Override
275 public void addRequestControls( Control[] requestControls )
276 {
277 for ( Control c : requestControls )
278 {
279 this.requestControls.put( c.getOid(), c );
280 }
281 }
282
283
284
285
286
287 public void setRequestControls( Map<String, Control> requestControls )
288 {
289 this.requestControls = requestControls;
290 }
291
292
293
294
295
296 public Entry getOriginalEntry()
297 {
298 return originalEntry;
299 }
300
301
302
303
304
305 public void setOriginalEntry( Entry originalEntry )
306 {
307 this.originalEntry = originalEntry;
308 }
309
310
311
312
313
314 @Override
315 public final void setInterceptors( List<String> interceptors )
316 {
317 this.interceptors = interceptors;
318 }
319
320
321
322
323
324 @Override
325 public final String getNextInterceptor()
326 {
327 if ( currentInterceptor == interceptors.size() )
328 {
329 return "FINAL";
330 }
331
332 String interceptor = interceptors.get( currentInterceptor );
333 currentInterceptor++;
334
335 return interceptor;
336 }
337
338
339
340
341
342 @Override
343 public int getCurrentInterceptor()
344 {
345 return currentInterceptor;
346 }
347
348
349
350
351
352
353
354 @Override
355 public void setCurrentInterceptor( int currentInterceptor )
356 {
357 this.currentInterceptor = currentInterceptor;
358 }
359
360
361 private void setup( AbstractOperationContext opContext )
362 {
363 opContext.setAuthorizedPrincipal( authorizedPrincipal );
364 }
365
366
367
368
369
370 @Override
371 public void delete( Dn dn ) throws LdapException
372 {
373 DeleteOperationContextterceptor/context/DeleteOperationContext.html#DeleteOperationContext">DeleteOperationContext deleteContext = new DeleteOperationContext( session, dn );
374 setup( deleteContext );
375 session.getDirectoryService().getOperationManager().delete( deleteContext );
376 }
377
378
379
380
381
382 @Override
383 public Entry lookup( LookupOperationContext lookupContext ) throws LdapException
384 {
385 return session.getDirectoryService().getOperationManager().lookup( lookupContext );
386 }
387
388
389
390
391
392
393 @Override
394 public LookupOperationContext newLookupContext( Dn dn, String... attributes )
395 {
396 LookupOperationContextterceptor/context/LookupOperationContext.html#LookupOperationContext">LookupOperationContext lookupContext = new LookupOperationContext( session, dn, attributes );
397 setup( lookupContext );
398
399 return lookupContext;
400 }
401
402
403
404
405
406 @Override
407 public LdapPrincipal getEffectivePrincipal()
408 {
409 if ( authorizedPrincipal != null )
410 {
411 return authorizedPrincipal;
412 }
413
414 return session.getEffectivePrincipal();
415 }
416
417
418
419
420
421
422
423
424 @Override
425 public void setEntry( Entry entry )
426 {
427 this.entry = entry;
428 }
429
430
431
432
433
434 @Override
435 public Entry getEntry()
436 {
437 return entry;
438 }
439
440
441
442
443
444 @Override
445 public void throwReferral()
446 {
447 throwReferral = true;
448 }
449
450
451
452
453
454 @Override
455 public boolean isReferralThrown()
456 {
457 return throwReferral;
458 }
459
460
461
462
463
464 @Override
465 public void ignoreReferral()
466 {
467 throwReferral = false;
468 }
469
470
471
472
473
474 @Override
475 public boolean isReferralIgnored()
476 {
477 return !throwReferral;
478 }
479
480
481
482
483
484 @Override
485 public PartitionTxn getTransaction()
486 {
487 return transaction;
488 }
489
490
491
492
493
494 @Override
495 public void setTransaction( PartitionTxn transaction )
496 {
497 this.transaction = transaction;
498 }
499
500
501
502
503
504 @Override
505 public Partition getPartition()
506 {
507 return partition;
508 }
509
510
511
512
513
514 @Override
515 public void setPartition( Partition partition )
516 {
517 this.partition = partition;
518 }
519 }