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 super();
162 lines = new ArrayList<String>();
163 position = 0;
164 version = DEFAULT_VERSION;
165 }
166
167
168
169
170
171
172
173
174
175
176 private void parseAttribute( Attributes attributes, String line, String lowerLine ) throws LdapLdifException
177 {
178 int colonIndex = line.indexOf( ':' );
179
180 String attributeType = lowerLine.substring( 0, colonIndex );
181
182
183 if ( "dn".equals( attributeType ) )
184 {
185 LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS ) );
186 throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
187 }
188
189 Object attributeValue = parseValue( attributeType, line, colonIndex );
190
191
192 javax.naming.directory.Attribute attribute = attributes.get( attributeType );
193
194 if ( attribute == null )
195 {
196 attributes.put( attributeType, attributeValue );
197 }
198 else
199 {
200 attribute.add( attributeValue );
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213
214 private void parseEntryAttribute( SchemaManager schemaManager, Entry entry, String line, String lowerLine )
215 throws LdapLdifException
216 {
217 int colonIndex = line.indexOf( ':' );
218
219 String attributeName = lowerLine.substring( 0, colonIndex );
220 AttributeType attributeType = null;
221
222
223 if ( "dn".equals( attributeName ) )
224 {
225 LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS ) );
226 throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
227 }
228
229 if ( schemaManager != null )
230 {
231 attributeType = schemaManager.getAttributeType( attributeName );
232
233 if ( attributeType == null )
234 {
235 String msg = I18n.err( I18n.ERR_13475_UNKNOWN_ATTRIBUTETYPE, attributeName );
236 LOG.error( msg );
237 throw new LdapLdifException( msg );
238 }
239 }
240
241 Object attributeValue = parseValue( attributeName, line, colonIndex );
242
243
244 Attribute attribute;
245
246 if ( schemaManager == null )
247 {
248 attribute = entry.get( attributeName );
249 }
250 else
251 {
252 attribute = entry.get( attributeType );
253 }
254
255 if ( attribute == null )
256 {
257 if ( schemaManager == null )
258 {
259 if ( attributeValue instanceof String )
260 {
261 entry.put( attributeName, ( String ) attributeValue );
262 }
263 else
264 {
265 entry.put( attributeName, ( byte[] ) attributeValue );
266 }
267 }
268 else
269 {
270 try
271 {
272 if ( attributeValue instanceof String )
273 {
274 entry.put( attributeName, attributeType, ( String ) attributeValue );
275 }
276 else
277 {
278 entry.put( attributeName, attributeType, ( byte[] ) attributeValue );
279 }
280 }
281 catch ( LdapException le )
282 {
283 throw new LdapLdifException( I18n.err( I18n.ERR_13460_BAD_ATTRIBUTE ), le );
284 }
285 }
286 }
287 else
288 {
289 try
290 {
291 if ( attributeValue instanceof String )
292 {
293 attribute.add( ( String ) attributeValue );
294 }
295 else
296 {
297 attribute.add( ( byte[] ) attributeValue );
298 }
299 }
300 catch ( LdapInvalidAttributeValueException liave )
301 {
302 throw new LdapLdifException( liave.getMessage(), liave );
303 }
304 }
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 private Entry parseEntry( SchemaManager schemaManager ) throws LdapLdifException
323 {
324 if ( ( lines == null ) || lines.isEmpty() )
325 {
326 if ( LOG.isDebugEnabled() )
327 {
328 LOG.debug( I18n.msg( I18n.MSG_13408_END_OF_LDIF ) );
329 }
330
331 return null;
332 }
333
334 Entry entry = new DefaultEntry( schemaManager );
335
336
337 for ( String line : lines )
338 {
339
340
341 String lowerLine = Strings.toLowerCaseAscii( line );
342
343
344
345
346
347 if ( lowerLine.startsWith( "control:" ) )
348 {
349 LOG.error( I18n.err( I18n.ERR_13401_CHANGE_NOT_ALLOWED ) );
350 throw new LdapLdifException( I18n.err( I18n.ERR_13440_NO_CHANGE ) );
351 }
352 else if ( lowerLine.startsWith( "changetype:" ) )
353 {
354 LOG.error( I18n.err( I18n.ERR_13401_CHANGE_NOT_ALLOWED ) );
355 throw new LdapLdifException( I18n.err( I18n.ERR_13440_NO_CHANGE ) );
356 }
357 else if ( line.indexOf( ':' ) > 0 )
358 {
359 parseEntryAttribute( schemaManager, entry, line, lowerLine );
360 }
361 else
362 {
363
364 LOG.error( I18n.err( I18n.ERR_13402_EXPECTING_ATTRIBUTE_TYPE ) );
365 throw new LdapLdifException( I18n.err( I18n.ERR_13441_BAD_ATTRIBUTE ) );
366 }
367 }
368
369 if ( LOG.isDebugEnabled() )
370 {
371 LOG.debug( I18n.msg( I18n.MSG_13405_READ_ATTR, entry ) );
372 }
373
374 return entry;
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 private Attributes parseAttributes() throws LdapLdifException
394 {
395 if ( ( lines == null ) || lines.isEmpty() )
396 {
397 if ( LOG.isDebugEnabled() )
398 {
399 LOG.debug( I18n.msg( I18n.MSG_13408_END_OF_LDIF ) );
400 }
401
402 return null;
403 }
404
405 Attributes attributes = new BasicAttributes( true );
406
407
408 for ( String line : lines )
409 {
410
411
412 String lowerLine = Strings.toLowerCaseAscii( line );
413
414
415
416
417
418 if ( lowerLine.startsWith( "control:" ) )
419 {
420 LOG.error( I18n.err( I18n.ERR_13401_CHANGE_NOT_ALLOWED ) );
421 throw new LdapLdifException( I18n.err( I18n.ERR_13440_NO_CHANGE ) );
422 }
423 else if ( lowerLine.startsWith( "changetype:" ) )
424 {
425 LOG.error( I18n.err( I18n.ERR_13401_CHANGE_NOT_ALLOWED ) );
426 throw new LdapLdifException( I18n.err( I18n.ERR_13440_NO_CHANGE ) );
427 }
428 else if ( line.indexOf( ':' ) > 0 )
429 {
430 parseAttribute( attributes, line, lowerLine );
431 }
432 else
433 {
434
435 LOG.error( I18n.err( I18n.ERR_13402_EXPECTING_ATTRIBUTE_TYPE ) );
436 throw new LdapLdifException( I18n.err( I18n.ERR_13441_BAD_ATTRIBUTE ) );
437 }
438 }
439
440 if ( LOG.isDebugEnabled() )
441 {
442 LOG.debug( I18n.msg( I18n.MSG_13405_READ_ATTR, attributes ) );
443 }
444
445 return attributes;
446 }
447
448
449
450
451
452
453
454
455
456 public Attributes parseAttributes( String ldif ) throws LdapLdifException
457 {
458 lines = new ArrayList<String>();
459 position = 0;
460
461 if ( LOG.isDebugEnabled() )
462 {
463 LOG.debug( I18n.msg( I18n.MSG_13407_STARTS_PARSING_LDIF ) );
464 }
465
466 if ( Strings.isEmpty( ldif ) )
467 {
468 return new BasicAttributes( true );
469 }
470
471 StringReader strIn = new StringReader( ldif );
472 reader = new BufferedReader( strIn );
473
474 try
475 {
476 readLines();
477
478 Attributes attributes = parseAttributes();
479
480 if ( LOG.isDebugEnabled() )
481 {
482 if ( attributes == null )
483 {
484 LOG.debug( I18n.msg( I18n.MSG_13401_PARSED_NO_ENTRY ) );
485 }
486 else
487 {
488 LOG.debug( I18n.msg( I18n.MSG_13402_PARSED_ONE_ENTRY ) );
489 }
490 }
491
492 return attributes;
493 }
494 catch ( LdapLdifException ne )
495 {
496 LOG.error( I18n.err( I18n.ERR_13403_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
497 throw new LdapLdifException( I18n.err( I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER ), ne );
498 }
499 finally
500 {
501 try
502 {
503 reader.close();
504 }
505 catch ( IOException ioe )
506 {
507 throw new LdapLdifException( I18n.err( I18n.ERR_13450_CANNOT_CLOSE_FILE ), ioe );
508 }
509 }
510 }
511
512
513
514
515
516
517
518
519
520 public Entry parseEntry( String ldif ) throws LdapLdifException
521 {
522 lines = new ArrayList<String>();
523 position = 0;
524
525 if ( LOG.isDebugEnabled() )
526 {
527 LOG.debug( I18n.msg( I18n.MSG_13407_STARTS_PARSING_LDIF ) );
528 }
529
530 if ( Strings.isEmpty( ldif ) )
531 {
532 return new DefaultEntry();
533 }
534
535 StringReader strIn = new StringReader( ldif );
536 reader = new BufferedReader( strIn );
537
538 try
539 {
540 readLines();
541
542 Entry entry = parseEntry( ( SchemaManager ) null );
543
544 if ( LOG.isDebugEnabled() )
545 {
546 if ( entry == null )
547 {
548 LOG.debug( I18n.msg( I18n.MSG_13401_PARSED_NO_ENTRY ) );
549 }
550 else
551 {
552 LOG.debug( I18n.msg( I18n.MSG_13402_PARSED_ONE_ENTRY ) );
553 }
554 }
555
556 return entry;
557 }
558 catch ( LdapLdifException ne )
559 {
560 LOG.error( I18n.err( I18n.ERR_13403_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
561 throw new LdapLdifException( I18n.err( I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER ), ne );
562 }
563 finally
564 {
565 try
566 {
567 reader.close();
568 }
569 catch ( IOException ioe )
570 {
571 throw new LdapLdifException( I18n.err( I18n.ERR_13450_CANNOT_CLOSE_FILE ), ioe );
572 }
573 }
574 }
575
576
577
578
579
580
581
582
583
584
585 public Entry parseEntry( SchemaManager schemaManager, String ldif ) throws LdapLdifException
586 {
587 lines = new ArrayList<String>();
588 position = 0;
589
590 if ( LOG.isDebugEnabled() )
591 {
592 LOG.debug( I18n.msg( I18n.MSG_13407_STARTS_PARSING_LDIF ) );
593 }
594
595 if ( Strings.isEmpty( ldif ) )
596 {
597 return new DefaultEntry( schemaManager );
598 }
599
600 StringReader strIn = new StringReader( ldif );
601 reader = new BufferedReader( strIn );
602
603 try
604 {
605 readLines();
606
607 Entry entry = parseEntry( schemaManager );
608
609 if ( LOG.isDebugEnabled() )
610 {
611 if ( entry == null )
612 {
613 LOG.debug( I18n.msg( I18n.MSG_13401_PARSED_NO_ENTRY ) );
614 }
615 else
616 {
617 LOG.debug( I18n.msg( I18n.MSG_13402_PARSED_ONE_ENTRY ) );
618 }
619
620 }
621
622 return entry;
623 }
624 catch ( LdapLdifException ne )
625 {
626 LOG.error( I18n.err( I18n.ERR_13403_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage() ) );
627 throw new LdapLdifException( I18n.err( I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER ), ne );
628 }
629 finally
630 {
631 try
632 {
633 reader.close();
634 }
635 catch ( IOException ioe )
636 {
637 throw new LdapLdifException( I18n.err( I18n.ERR_13450_CANNOT_CLOSE_FILE ), ioe );
638 }
639 }
640 }
641 }