1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.codec.standalone;
21
22
23 import java.lang.reflect.Constructor;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.directory.api.i18n.I18n;
29 import org.apache.directory.api.ldap.codec.StockCodecFactoryUtil;
30 import org.apache.directory.api.ldap.codec.api.ControlFactory;
31 import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
32 import org.apache.directory.api.ldap.codec.api.IntermediateOperationFactory;
33 import org.apache.directory.api.ldap.codec.api.LdapApiService;
34 import org.apache.directory.api.ldap.codec.osgi.DefaultLdapCodecService;
35 import org.apache.directory.api.ldap.extras.ExtrasCodecFactoryUtil;
36 import org.apache.directory.api.ldap.model.message.Control;
37 import org.apache.directory.api.util.Strings;
38 import org.apache.mina.filter.codec.ProtocolCodecFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class StandaloneLdapApiService extends DefaultLdapCodecService
71 {
72
73 private static final Logger LOG = LoggerFactory.getLogger( StandaloneLdapApiService.class );
74
75
76 public static final String REQUEST_CONTROLS_LIST = "apacheds.request.controls";
77
78
79 public static final String RESPONSE_CONTROLS_LIST = "apacheds.response.controls";
80
81
82 public static final String EXTENDED_OPERATIONS_LIST = "apacheds.extendedOperations";
83
84
85 public static final String INTERMEDIATE_RESPONSES_LIST = "apacheds.intermediateResponses";
86
87
88 private static final String OLD_DEFAULT_CONTROLS_LIST = "default.controls";
89
90
91 private static final String OLD_EXTRA_EXTENDED_OPERATION_LIST = "extra.extendedOperations";
92
93
94 public enum ControlType
95 {
96 REQUEST( REQUEST_CONTROLS_LIST ),
97 RESPONSE( RESPONSE_CONTROLS_LIST );
98
99 private String property;
100
101 ControlType( String property )
102 {
103 this.property = property;
104 }
105
106 private String getProperty()
107 {
108 return property;
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public StandaloneLdapApiService() throws Exception
180 {
181 this( getControlsFromSystemProperties( ControlType.REQUEST ),
182 getControlsFromSystemProperties( ControlType.RESPONSE ),
183 getExtendedOperationsFromSystemProperties(),
184 getIntermediateResponsesFromSystemProperties() );
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public StandaloneLdapApiService( List<String> requestControls,
198 List<String> responseControls, List<String> extendedOperations,
199 List<String> intermediateResponses ) throws Exception
200 {
201 StockCodecFactoryUtil.loadStockControls( this );
202 ExtrasCodecFactoryUtil.loadExtrasControls( this );
203 ExtrasCodecFactoryUtil.loadExtrasExtendedOperations( this );
204 ExtrasCodecFactoryUtil.loadExtrasIntermediateResponses( this );
205
206
207 loadControls( requestControls, getRequestControlFactories() );
208 loadControls( responseControls, getResponseControlFactories() );
209
210
211 loadExtendedOperations( extendedOperations );
212
213
214 loadIntermediateResponse( intermediateResponses );
215
216 if ( getProtocolCodecFactory() == null )
217 {
218 try
219 {
220 @SuppressWarnings("unchecked")
221 Class<? extends ProtocolCodecFactory> clazz = ( Class<? extends ProtocolCodecFactory> )
222 Class.forName( DEFAULT_PROTOCOL_CODEC_FACTORY );
223 Constructor<? extends ProtocolCodecFactory> constructor =
224 clazz.getConstructor( LdapApiService.class );
225
226 if ( constructor != null )
227 {
228 setProtocolCodecFactory( constructor.newInstance( this ) );
229 }
230 else
231 {
232 setProtocolCodecFactory( clazz.newInstance() );
233 }
234 }
235 catch ( Exception cause )
236 {
237 throw new RuntimeException( I18n.err( I18n.ERR_06000_FAILED_TO_LOAD_DEFAULT_CODEC_FACTORY ), cause );
238 }
239 }
240 }
241
242
243
244
245
246
247
248
249 private static List<String> getControlsFromSystemProperties( ControlType type )
250 {
251 List<String> controlsList = new ArrayList<>();
252
253 if ( type == ControlType.REQUEST )
254 {
255
256 String controlsString = System.getProperty( type.getProperty() );
257
258 if ( !Strings.isEmpty( controlsString ) )
259 {
260 for ( String control : controlsString.split( "," ) )
261 {
262 controlsList.add( control );
263 }
264 }
265 else
266 {
267
268 String oldDefaultControlsString = System.getProperty( OLD_DEFAULT_CONTROLS_LIST );
269
270 if ( !Strings.isEmpty( oldDefaultControlsString ) )
271 {
272 for ( String control : oldDefaultControlsString.split( "," ) )
273 {
274 controlsList.add( control );
275 }
276 }
277 }
278 }
279
280 return controlsList;
281 }
282
283
284
285
286
287
288
289
290
291 private static List<String> getExtendedOperationsFromSystemProperties()
292 {
293 List<String> extendedOperationsList = new ArrayList<>();
294
295
296 String defaultExtendedOperationsList = System.getProperty( EXTENDED_OPERATIONS_LIST );
297
298 if ( !Strings.isEmpty( defaultExtendedOperationsList ) )
299 {
300 for ( String extendedOperation : defaultExtendedOperationsList.split( "," ) )
301 {
302 extendedOperationsList.add( extendedOperation );
303 }
304 }
305 else
306 {
307
308 String oldDefaultExtendedOperationsString = System.getProperty( OLD_EXTRA_EXTENDED_OPERATION_LIST );
309
310 if ( !Strings.isEmpty( oldDefaultExtendedOperationsString ) )
311 {
312 for ( String extendedOperation : oldDefaultExtendedOperationsString.split( "," ) )
313 {
314 extendedOperationsList.add( extendedOperation );
315 }
316 }
317 }
318
319 return extendedOperationsList;
320 }
321
322
323
324
325
326
327
328
329
330 private static List<String> getIntermediateResponsesFromSystemProperties()
331 {
332 List<String> intermediateResponsesList = new ArrayList<>();
333
334
335 String defaultIntermediateResponsesList = System.getProperty( INTERMEDIATE_RESPONSES_LIST );
336
337 if ( !Strings.isEmpty( defaultIntermediateResponsesList ) )
338 {
339 for ( String intermediateResponse : defaultIntermediateResponsesList.split( "," ) )
340 {
341 intermediateResponsesList.add( intermediateResponse );
342 }
343 }
344
345 return intermediateResponsesList;
346 }
347
348
349
350
351
352
353
354
355
356 private void loadControls( List<String> controlsList, Map<String, ControlFactory<? extends Control>> controlFactories )
357 throws Exception
358 {
359
360 if ( !controlsList.isEmpty() )
361 {
362 for ( String controlFQCN : controlsList )
363 {
364 loadControl( controlFQCN, controlFactories );
365 }
366 }
367 }
368
369
370
371
372
373
374
375
376
377 private void loadControl( String controlFQCN, Map<String, ControlFactory<? extends Control>> controlFactories )
378 throws Exception
379 {
380 if ( controlFactories.containsKey( controlFQCN ) )
381 {
382 if ( LOG.isDebugEnabled() )
383 {
384 LOG.debug( I18n.msg( I18n.MSG_06003_CONTROL_FACTORY_ALREADY_LOADED, controlFQCN ) );
385 }
386
387 return;
388 }
389
390 Class<?>[] types = new Class<?>[]
391 { LdapApiService.class };
392
393
394 @SuppressWarnings("unchecked")
395 Class<? extends ControlFactory<?>> clazz = ( Class<? extends ControlFactory<?>> ) Class
396 .forName( controlFQCN.trim() );
397 Constructor<?> constructor = clazz.getConstructor( types );
398
399 ControlFactory<?> factory = ( ControlFactory<?> ) constructor.newInstance( this );
400 controlFactories.put( factory.getOid(), factory );
401
402 if ( LOG.isInfoEnabled() )
403 {
404 LOG.info( I18n.msg( I18n.MSG_06004_REGISTERED_CONTROL_FACTORY, factory.getOid() ) );
405 }
406 }
407
408
409
410
411
412
413
414
415 private void loadExtendedOperations( List<String> extendedOperationsList ) throws Exception
416 {
417
418 if ( !extendedOperationsList.isEmpty() )
419 {
420 for ( String extendedOperationFQCN : extendedOperationsList )
421 {
422 loadExtendedRequest( extendedOperationFQCN );
423 }
424 }
425 }
426
427
428
429
430
431
432
433
434 private void loadExtendedRequest( String extendedRequestFQCN ) throws Exception
435 {
436 if ( getExtendedRequestFactories().containsKey( extendedRequestFQCN ) )
437 {
438 if ( LOG.isDebugEnabled() )
439 {
440 LOG.debug( I18n.msg( I18n.MSG_06005_EXTENDED_OP_FACTORY_ALREADY_LOADED, extendedRequestFQCN ) );
441 }
442
443 return;
444 }
445
446 Class<?>[] types = new Class<?>[]
447 { LdapApiService.class };
448
449
450
451 @SuppressWarnings("unchecked")
452 Class<? extends ExtendedOperationFactory> clazz = ( Class<? extends ExtendedOperationFactory> ) Class
453 .forName( extendedRequestFQCN.trim() );
454 Constructor<?> constructor = clazz.getConstructor( types );
455
456 ExtendedOperationFactory factory = ( ExtendedOperationFactory ) constructor
457 .newInstance( this );
458 getExtendedRequestFactories().put( factory.getOid(), factory );
459
460 if ( LOG.isInfoEnabled() )
461 {
462 LOG.info( I18n.msg( I18n.MSG_06001_REGISTERED_EXTENDED_OP_FACTORY, factory.getOid() ) );
463 }
464 }
465
466
467
468
469
470
471
472
473 private void loadIntermediateResponse( List<String> intermediateResponsesList ) throws Exception
474 {
475
476 if ( !intermediateResponsesList.isEmpty() )
477 {
478 for ( String intermediateResponseFQCN : intermediateResponsesList )
479 {
480 loadIntermediateResponse( intermediateResponseFQCN );
481 }
482 }
483 }
484
485
486
487
488
489
490
491
492 private void loadIntermediateResponse( String intermediateResponseFQCN ) throws Exception
493 {
494 if ( getIntermediateResponseFactories().containsKey( intermediateResponseFQCN ) )
495 {
496 if ( LOG.isDebugEnabled() )
497 {
498 LOG.debug( I18n.msg( I18n.MSG_06006_INTERMEDIATE_FACTORY_ALREADY_LOADED, intermediateResponseFQCN ) );
499 }
500
501 return;
502 }
503
504 Class<?>[] types = new Class<?>[]
505 {};
506
507
508
509 @SuppressWarnings("unchecked")
510 Class<? extends IntermediateOperationFactory> clazz = ( Class<? extends IntermediateOperationFactory> ) Class
511 .forName( intermediateResponseFQCN.trim() );
512 Constructor<?> constructor = clazz.getConstructor( types );
513
514 IntermediateOperationFactory factory = ( IntermediateOperationFactory ) constructor
515 .newInstance();
516 getIntermediateResponseFactories().put( factory.getOid(), factory );
517
518 if ( LOG.isInfoEnabled() )
519 {
520 LOG.info( I18n.msg( I18n.MSG_06007_REGISTRED_INTERMEDIATE_RESP_FACTORY, factory.getOid() ) );
521 }
522 }
523 }