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.model.ldif;
21
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.ArrayList;
27
28 import javax.naming.directory.Attributes;
29 import javax.naming.directory.BasicAttributes;
30
31 import org.apache.directory.api.i18n.I18n;
32 import org.apache.directory.api.ldap.model.entry.Attribute;
33 import org.apache.directory.api.ldap.model.entry.DefaultEntry;
34 import org.apache.directory.api.ldap.model.entry.Entry;
35 import org.apache.directory.api.ldap.model.exception.LdapException;
36 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
37 import org.apache.directory.api.ldap.model.schema.AttributeType;
38 import org.apache.directory.api.ldap.model.schema.SchemaManager;
39 import org.apache.directory.api.util.Strings;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 public class LdifAttributesReader extends LdifReader
151 {
152
153 private static final Logger LOG = LoggerFactory.getLogger( LdifAttributesReader.class );
154
155
156
157
158
159 public LdifAttributesReader()
160 {
161 lines = new ArrayList<String>();
162 position = 0;
163 version = DEFAULT_VERSION;
164 }
165
166
167
168
169
170
171
172
173
174
175 private void parseAttribute( Attributes attributes, String line, String lowerLine ) throws LdapLdifException
176 {
177 int colonIndex = line.indexOf( ':' );
178
179 String attributeType = lowerLine.substring( 0, colonIndex );
180
181
182 if ( attributeType.equals( "dn" ) )
183 {
184 LOG.error( I18n.err( I18n.ERR_12002_ENTRY_WITH_TWO_DNS ) );
185 throw new LdapLdifException( I18n.err( I18n.ERR_12003_LDIF_ENTRY_WITH_TWO_DNS ) );
186 }
187
188 Object attributeValue = parseValue( line, colonIndex );
189
190
191 javax.naming.directory.Attribute attribute = attributes.get( attributeType );
192
193 if ( attribute == null )
194 {
195 attributes.put( attributeType, attributeValue );
196 }
197 else
198 {
199 attribute.add( attributeValue );
200 }
201 }
202
203
204
205
206
207
208
209
210
211
212
213 private void parseEntryAttribute( SchemaManager schemaManager, Entry entry, String line, String lowerLine )
214 throws LdapLdifException
215 {
216 int colonIndex = line.indexOf( ':' );
217
218 String attributeName = lowerLine.substring( 0, colonIndex );
219 AttributeType attributeType = null;
220
221
222 if ( attributeName.equals( "dn" ) )
223 {
224 LOG.error( I18n.err( I18n.ERR_12002_ENTRY_WITH_TWO_DNS ) );
225 throw new LdapLdifException( I18n.err( I18n.ERR_12003_LDIF_ENTRY_WITH_TWO_DNS ) );
226 }
227
228 if ( schemaManager != null )
229 {
230 attributeType = schemaManager.getAttributeType( attributeName );
231
232 if ( attributeType == null )
233 {
234 LOG.error( "" );
235 throw new LdapLdifException( "" );
236 }
237 }
238
239 Object attributeValue = parseValue( line, colonIndex );
240
241
242 Attribute attribute = null;
243
244 if ( schemaManager == null )
245 {
246 attribute = entry.get( attributeName );
247 }
248 else
249 {
250 attribute = entry.get( attributeType );
251 }
252
253 if ( attribute == null )
254 {
255 if ( schemaManager == null )
256 {
257 if ( attributeValue instanceof String )
258 {
259 entry.put( attributeName, ( String ) attributeValue );
260 }
261 else
262 {
263 entry.put( attributeName, ( byte[] ) attributeValue );
264 }
265 }
266 else
267 {
268 try
269 {
270 if ( attributeValue instanceof String )
271 {
272 entry.put( attributeName, attributeType, ( String ) attributeValue );
273 }
274 else
275 {
276 entry.put( attributeName, attributeType, ( byte[] ) attributeValue );
277 }
278 }
279 catch ( LdapException le )
280 {
281 throw new LdapLdifException( I18n.err( I18n.ERR_12057_BAD_ATTRIBUTE ), le );
282 }
283 }
284 }
285 else
286 {
287 try
288 {
289 if ( attributeValue instanceof String )
290 {
291 attribute.add( ( String ) attributeValue );
292 }
293 else
294 {
295 attribute.add( ( byte[] ) attributeValue );
296 }
297 }
298 catch ( LdapInvalidAttributeValueException liave )
299 {
300 throw new LdapLdifException( liave.getMessage(), liave );
301 }
302 }
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 private Entry parseEntry( SchemaManager schemaManager ) throws LdapLdifException
321 {
322 if ( ( lines == null ) || ( lines.size() == 0 ) )
323 {
324 LOG.debug( "The entry is empty : end of ldif file" );
325 return null;
326 }
327
328 Entry entry = new DefaultEntry( schemaManager );
329
330
331 for ( String line : lines )
332 {
333
334
335 String lowerLine = Strings.toLowerCase( line );
336
337
338
339
340
341 if ( lowerLine.startsWith( "control:" ) )
342 {
343 LOG.error( I18n.err( I18n.ERR_12004_CHANGE_NOT_ALLOWED ) );
344 throw new LdapLdifException( I18n.err( I18n.ERR_12005_NO_CHANGE ) );
345 }
346 else if ( lowerLine.startsWith( "changetype:" ) )
347 {
348 LOG.error( I18n.err( I18n.ERR_12004_CHANGE_NOT_ALLOWED ) );
349 throw new LdapLdifException( I18n.err( I18n.ERR_12005_NO_CHANGE ) );
350 }
351 else if ( line.indexOf( ':' ) > 0 )
352 {
353 parseEntryAttribute( schemaManager, entry, line, lowerLine );
354 }
355 else
356 {
357
358 LOG.error( I18n.err( I18n.ERR_12006_EXPECTING_ATTRIBUTE_TYPE ) );
359 throw new LdapLdifException( I18n.err( I18n.ERR_12007_BAD_ATTRIBUTE ) );
360 }
361 }
362
363 LOG.debug( "Read an attributes : {}", entry );
364
365 return entry;
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 private Attributes parseAttributes() throws LdapLdifException
383 {
384 if ( ( lines == null ) || ( lines.size() == 0 ) )
385 {
386 LOG.debug( "The entry is empty : end of ldif file" );
387 return null;
388 }
389
390 Attributes attributes = new BasicAttributes( true );
391
392
393 for ( String line : lines )
394 {
395
396
397 String lowerLine = Strings.toLowerCase( line );
398
399
400
401
402
403 if ( lowerLine.startsWith( "control:" ) )
404 {
405 LOG.error( I18n.err( I18n.ERR_12004_CHANGE_NOT_ALLOWED ) );
406 throw new LdapLdifException( I18n.err( I18n.ERR_12005_NO_CHANGE ) );
407 }
408 else if ( lowerLine.startsWith( "changetype:" ) )
409 {
410 LOG.error( I18n.err( I18n.ERR_12004_CHANGE_NOT_ALLOWED ) );
411 throw new LdapLdifException( I18n.err( I18n.ERR_12005_NO_CHANGE ) );
412 }
413 else if ( line.indexOf( ':' ) > 0 )
414 {
415 parseAttribute( attributes, line, lowerLine );
416 }
417 else
418 {
419
420 LOG.error( I18n.err( I18n.ERR_12006_EXPECTING_ATTRIBUTE_TYPE ) );
421 throw new LdapLdifException( I18n.err( I18n.ERR_12007_BAD_ATTRIBUTE ) );
422 }
423 }
424
425 LOG.debug( "Read an attributes : {}", attributes );
426
427 return attributes;
428 }
429
430
431
432
433
434
435
436
437
438 public Attributes parseAttributes( String ldif ) throws LdapLdifException
439 {
440 lines = new ArrayList<String>();
441 position = 0;
442
443 LOG.debug( "Starts parsing ldif buffer" );
444
445 if ( Strings.isEmpty( ldif ) )
446 {
447 return new BasicAttributes( true );
448 }
449
450 StringReader strIn = new StringReader( ldif );
451 reader = new BufferedReader( strIn );
452
453 try
454 {
455 readLines();
456
457 Attributes attributes = parseAttributes();
458
459 if ( LOG.isDebugEnabled() )
460 {
461 if ( attributes == null )
462 {
463 LOG.debug( "Parsed no entry." );
464 }
465 else
466 {
467 LOG.debug( "Parsed one entry." );
468 }
469 }
470
471 return attributes;
472 }
473 catch ( LdapLdifException ne )
474 {
475 LOG.error( I18n.err( I18n.ERR_12008_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
476 throw new LdapLdifException( I18n.err( I18n.ERR_12009_ERROR_PARSING_LDIF_BUFFER ), ne );
477 }
478 finally
479 {
480 try
481 {
482 reader.close();
483 }
484 catch ( IOException ioe )
485 {
486 throw new LdapLdifException( I18n.err( I18n.ERR_12024_CANNOT_CLOSE_FILE ), ioe );
487 }
488 }
489 }
490
491
492
493
494
495
496
497
498
499 public Entry parseEntry( String ldif ) throws LdapLdifException
500 {
501 lines = new ArrayList<String>();
502 position = 0;
503
504 LOG.debug( "Starts parsing ldif buffer" );
505
506 if ( Strings.isEmpty( ldif ) )
507 {
508 return new DefaultEntry();
509 }
510
511 StringReader strIn = new StringReader( ldif );
512 reader = new BufferedReader( strIn );
513
514 try
515 {
516 readLines();
517
518 Entry entry = parseEntry( ( SchemaManager ) null );
519
520 if ( LOG.isDebugEnabled() )
521 {
522 if ( entry == null )
523 {
524 LOG.debug( "Parsed no entry." );
525 }
526 else
527 {
528 LOG.debug( "Parsed one entry." );
529 }
530
531 }
532
533 return entry;
534 }
535 catch ( LdapLdifException ne )
536 {
537 LOG.error( I18n.err( I18n.ERR_12008_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
538 throw new LdapLdifException( I18n.err( I18n.ERR_12009_ERROR_PARSING_LDIF_BUFFER ), ne );
539 }
540 finally
541 {
542 try
543 {
544 reader.close();
545 }
546 catch ( IOException ioe )
547 {
548 throw new LdapLdifException( I18n.err( I18n.ERR_12024_CANNOT_CLOSE_FILE ), ioe );
549 }
550 }
551 }
552
553
554
555
556
557
558
559
560
561
562 public Entry parseEntry( SchemaManager schemaManager, String ldif ) throws LdapLdifException
563 {
564 lines = new ArrayList<String>();
565 position = 0;
566
567 LOG.debug( "Starts parsing ldif buffer" );
568
569 if ( Strings.isEmpty( ldif ) )
570 {
571 return new DefaultEntry( schemaManager );
572 }
573
574 StringReader strIn = new StringReader( ldif );
575 reader = new BufferedReader( strIn );
576
577 try
578 {
579 readLines();
580
581 Entry entry = parseEntry( schemaManager );
582
583 if ( LOG.isDebugEnabled() )
584 {
585 if ( entry == null )
586 {
587 LOG.debug( "Parsed no entry." );
588 }
589 else
590 {
591 LOG.debug( "Parsed one entry." );
592 }
593
594 }
595
596 return entry;
597 }
598 catch ( LdapLdifException ne )
599 {
600 LOG.error( I18n.err( I18n.ERR_12008_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
601 throw new LdapLdifException( I18n.err( I18n.ERR_12009_ERROR_PARSING_LDIF_BUFFER ), ne );
602 }
603 finally
604 {
605 try
606 {
607 reader.close();
608 }
609 catch ( IOException ioe )
610 {
611 throw new LdapLdifException( I18n.err( I18n.ERR_12024_CANNOT_CLOSE_FILE ), ioe );
612 }
613 }
614 }
615 }