1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.core.entry;
20
21
22 import java.io.IOException;
23 import java.io.ObjectInput;
24 import java.io.ObjectOutput;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import javax.naming.NamingException;
32
33 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
34 import org.apache.directory.server.schema.registries.Registries;
35 import org.apache.directory.shared.ldap.NotImplementedException;
36 import org.apache.directory.shared.ldap.constants.SchemaConstants;
37 import org.apache.directory.shared.ldap.entry.AbstractEntry;
38 import org.apache.directory.shared.ldap.entry.Entry;
39 import org.apache.directory.shared.ldap.entry.EntryAttribute;
40 import org.apache.directory.shared.ldap.entry.Value;
41 import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
42 import org.apache.directory.shared.ldap.name.LdapDN;
43 import org.apache.directory.shared.ldap.name.LdapDNSerializer;
44 import org.apache.directory.shared.ldap.schema.AttributeType;
45 import org.apache.directory.shared.ldap.util.StringTools;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52
53
54
55
56
57
58
59 public final class DefaultServerEntry extends AbstractEntry<AttributeType> implements ServerEntry
60 {
61
62 private static final long serialVersionUID = 2L;
63
64
65 private static final Logger LOG = LoggerFactory.getLogger( DefaultServerEntry.class );
66
67
68 private final transient AttributeTypeRegistry atRegistry;
69
70
71 private static transient AttributeType OBJECT_CLASS_AT;
72
73
74 private static transient Object MUTEX = new Object();
75
76
77
78
79
80
81
82
83 private AttributeType getAttributeType( String upId ) throws NamingException
84 {
85 if ( StringTools.isEmpty( StringTools.trim( upId ) ) )
86 {
87 String message = "The ID should not be null or empty";
88 LOG.error( message );
89 throw new IllegalArgumentException( message );
90 }
91
92 return atRegistry.lookup( upId );
93 }
94
95
96
97
98
99 public static String getUpId( String upId, AttributeType attributeType )
100 {
101 String normUpId = StringTools.trim( upId );
102
103 if ( ( attributeType == null ) )
104 {
105 if ( StringTools.isEmpty( normUpId ) )
106 {
107 String message = "Cannot add an attribute without an ID";
108 LOG.error( message );
109 throw new IllegalArgumentException( message );
110 }
111 }
112 else if ( StringTools.isEmpty( normUpId ) )
113 {
114 upId = attributeType.getName();
115
116 if ( StringTools.isEmpty( upId ) )
117 {
118 upId = attributeType.getOid();
119 }
120 }
121
122 return upId;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 private void initObjectClassAT( Registries registries )
138 {
139 try
140 {
141 if ( OBJECT_CLASS_AT == null )
142 {
143 synchronized ( MUTEX )
144 {
145 OBJECT_CLASS_AT = atRegistry.lookup( SchemaConstants.OBJECT_CLASS_AT );
146 }
147 }
148 }
149 catch ( NamingException ne )
150 {
151
152 }
153 }
154
155
156
157
158
159
160
161
162 private void createAttribute( String upId, AttributeType attributeType, byte[]... values )
163 {
164 ServerAttribute attribute = new DefaultServerAttribute( attributeType, values );
165 attribute.setUpId( upId, attributeType );
166 attributes.put( attributeType, attribute );
167 }
168
169
170
171
172
173
174
175
176 private void createAttribute( String upId, AttributeType attributeType, String... values )
177 {
178 ServerAttribute attribute = new DefaultServerAttribute( attributeType, values );
179 attribute.setUpId( upId, attributeType );
180 attributes.put( attributeType, attribute );
181 }
182
183
184
185
186
187
188
189
190 private void createAttribute( String upId, AttributeType attributeType, Value<?>... values )
191 {
192 ServerAttribute attribute = new DefaultServerAttribute( attributeType, values );
193 attribute.setUpId( upId, attributeType );
194 attributes.put( attributeType, attribute );
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 {
211 atRegistry = null;
212 dn = LdapDN.EMPTY_LDAPDN;
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226 public DefaultServerEntry( Registries registries )
227 {
228 atRegistry = registries.getAttributeTypeRegistry();
229 dn = LdapDN.EMPTY_LDAPDN;
230
231
232 initObjectClassAT( registries );
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public DefaultServerEntry( Registries registries, Entry entry )
249 {
250 atRegistry = registries.getAttributeTypeRegistry();
251
252
253 initObjectClassAT( registries );
254
255
256 if ( entry.getDn() != null )
257 {
258 dn = (LdapDN)entry.getDn().clone();
259 }
260 else
261 {
262 dn = LdapDN.EMPTY_LDAPDN;
263 }
264
265 if ( !dn.isNormalized( ) )
266 {
267 try
268 {
269
270 dn.normalize( registries.getAttributeTypeRegistry().getNormalizerMapping() );
271 }
272 catch ( NamingException ne )
273 {
274 LOG.warn( "The DN '" + entry.getDn() + "' cannot be normalized" );
275 }
276 }
277
278
279 attributes = new HashMap<AttributeType, EntryAttribute>( entry.size() );
280
281
282 for ( EntryAttribute attribute:entry )
283 {
284 try
285 {
286
287 AttributeType attributeType = null;
288
289 if ( attribute instanceof ServerAttribute )
290 {
291 attributeType = ((ServerAttribute)attribute).getAttributeType();
292 }
293 else
294 {
295 attributeType = registries.getAttributeTypeRegistry().lookup( attribute.getId() );
296 }
297
298
299 EntryAttribute serverAttribute = new DefaultServerAttribute( attributeType, attribute );
300
301
302 add( serverAttribute );
303 }
304 catch ( NamingException ne )
305 {
306
307 LOG.warn( "The attribute '" + attribute.getId() + "' cannot be stored" );
308 }
309 }
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public DefaultServerEntry( Registries registries, LdapDN dn )
326 {
327 if ( dn == null )
328 {
329 dn = LdapDN.EMPTY_LDAPDN;
330 }
331 else
332 {
333 this.dn = dn;
334 }
335
336 atRegistry = registries.getAttributeTypeRegistry();
337
338
339 initObjectClassAT( registries );
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360 public DefaultServerEntry( Registries registries, LdapDN dn, AttributeType... attributeTypes )
361 {
362 if ( dn == null )
363 {
364 dn = LdapDN.EMPTY_LDAPDN;
365 }
366 else
367 {
368 this.dn = dn;
369 }
370
371 atRegistry = registries.getAttributeTypeRegistry();
372
373
374 initObjectClassAT( registries );
375
376
377 set( attributeTypes );
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402 public DefaultServerEntry( Registries registries, LdapDN dn, AttributeType attributeType, String upId )
403 {
404 if ( dn == null )
405 {
406 dn = LdapDN.EMPTY_LDAPDN;
407 }
408 else
409 {
410 this.dn = dn;
411 }
412
413 atRegistry = registries.getAttributeTypeRegistry();
414
415
416
417 initObjectClassAT( registries );
418
419 try
420 {
421 put( upId, attributeType, (String)null );
422 }
423 catch ( NamingException ne )
424 {
425
426 LOG.error( "We have had an error while adding the '{}' AttributeType : {}", upId, ne.getMessage() );
427 }
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 public DefaultServerEntry( Registries registries, LdapDN dn, String... upIds )
445 {
446 if ( dn == null )
447 {
448 dn = LdapDN.EMPTY_LDAPDN;
449 }
450 else
451 {
452 this.dn = dn;
453 }
454
455 atRegistry = registries.getAttributeTypeRegistry();
456
457 initObjectClassAT( registries );
458
459 set( upIds );
460 }
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 public DefaultServerEntry( Registries registries, LdapDN dn, ServerAttribute... attributes )
477 {
478 if ( dn == null )
479 {
480 dn = LdapDN.EMPTY_LDAPDN;
481 }
482 else
483 {
484 this.dn = dn;
485 }
486
487 atRegistry = registries.getAttributeTypeRegistry();
488
489 initObjectClassAT( registries );
490
491 for ( ServerAttribute attribute:attributes )
492 {
493
494 try
495 {
496 put( attribute );
497 }
498 catch ( NamingException ne )
499 {
500 LOG.warn( "The ServerAttribute '{}' does not exist. It has been discarded", attribute );
501 }
502 }
503 }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 public void add( AttributeType attributeType, byte[]... values ) throws NamingException
528 {
529 if ( attributeType == null )
530 {
531 String message = "The attributeType should not be null";
532 LOG.error( message );
533 throw new IllegalArgumentException( message );
534 }
535
536
537 if ( attributeType.equals( OBJECT_CLASS_AT ) )
538 {
539 String message = "Only String values supported for objectClass attribute";
540 LOG.error( message );
541 throw new UnsupportedOperationException( message );
542 }
543
544 EntryAttribute attribute = attributes.get( attributeType );
545
546 if ( attribute != null )
547 {
548
549
550 attribute.add( values );
551 }
552 else
553 {
554
555
556
557 createAttribute( null, attributeType, values );
558 }
559 }
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580 public void add( AttributeType attributeType, String... values ) throws NamingException
581 {
582 if ( attributeType == null )
583 {
584 String message = "The attributeType should not be null";
585 LOG.error( message );
586 throw new IllegalArgumentException( message );
587 }
588
589 EntryAttribute attribute = attributes.get( attributeType );
590
591 if ( attribute != null )
592 {
593
594
595 attribute.add( values );
596 }
597 else
598 {
599
600
601
602 createAttribute( null, attributeType, values );
603 }
604 }
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625 public void add( AttributeType attributeType, Value<?>... values ) throws NamingException
626 {
627 if ( attributeType == null )
628 {
629 String message = "The attributeType should not be null";
630 LOG.error( message );
631 throw new IllegalArgumentException( message );
632 }
633
634 EntryAttribute attribute = attributes.get( attributeType );
635
636 if ( attribute != null )
637 {
638
639
640 attribute.add( values );
641 }
642 else
643 {
644
645
646
647 createAttribute( null, attributeType, values );
648 }
649 }
650
651
652
653
654
655
656
657
658 public void add( EntryAttribute... attributes ) throws NamingException
659 {
660 for ( EntryAttribute attribute:attributes )
661 {
662 ServerAttribute serverAttribute = (ServerAttribute)attribute;
663 AttributeType attributeType = serverAttribute.getAttributeType();
664
665 if ( this.attributes.containsKey( attributeType ) )
666 {
667
668
669 EntryAttribute oldAttribute = this.attributes.get( attributeType );
670
671 for ( Value<?> value:serverAttribute )
672 {
673 oldAttribute.add( value );
674 }
675
676
677 oldAttribute.setUpId( serverAttribute.getUpId() );
678 }
679 else
680 {
681
682 this.attributes.put( attributeType, attribute );
683 }
684 }
685 }
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707 public void add( String upId, AttributeType attributeType, byte[]... values ) throws NamingException
708 {
709
710 if ( attributeType.equals( OBJECT_CLASS_AT ) )
711 {
712 String message = "Only String values supported for objectClass attribute";
713 LOG.error( message );
714 throw new UnsupportedOperationException( message );
715 }
716
717 ServerAttribute attribute = (ServerAttribute)attributes.get( attributeType );
718
719 upId = getUpId( upId, attributeType );
720
721 if ( attribute != null )
722 {
723
724
725 attribute.add( values );
726 attribute.setUpId( upId, attributeType );
727 }
728 else
729 {
730
731
732 createAttribute( upId, attributeType, values );
733 }
734 }
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756 public void add( String upId, AttributeType attributeType, Value<?>... values ) throws NamingException
757 {
758 if ( attributeType == null )
759 {
760 String message = "The attributeType should not be null";
761 LOG.error( message );
762 throw new IllegalArgumentException( message );
763 }
764
765 upId = getUpId( upId, attributeType );
766
767 ServerAttribute attribute = (ServerAttribute)attributes.get( attributeType );
768
769 if ( attribute != null )
770 {
771
772
773 attribute.add( values );
774 attribute.setUpId( upId, attributeType );
775 }
776 else
777 {
778 createAttribute( upId, attributeType, values );
779 }
780 }
781
782
783
784
785
786
787
788
789
790
791
792 public void add( String upId, AttributeType attributeType, String... values ) throws NamingException
793 {
794 if ( attributeType == null )
795 {
796 String message = "The attributeType should not be null";
797 LOG.error( message );
798 throw new IllegalArgumentException( message );
799 }
800
801 upId = getUpId( upId, attributeType );
802
803 ServerAttribute attribute = (ServerAttribute)attributes.get( attributeType );
804
805 if ( attribute != null )
806 {
807
808
809 attribute.add( values );
810 attribute.setUpId( upId, attributeType );
811 }
812 else
813 {
814
815
816 createAttribute( upId, attributeType, values );
817 }
818 }
819
820
821
822
823
824
825
826
827
828 public void add( String upId, byte[]... values ) throws NamingException
829 {
830 add( upId, getAttributeType( upId ), values );
831 }
832
833
834
835
836
837
838
839
840
841 public void add( String upId, String... values ) throws NamingException
842 {
843 add( upId, getAttributeType( upId ), values );
844 }
845
846
847
848
849
850
851
852
853
854 public void add( String upId, Value<?>... values ) throws NamingException
855 {
856 add( upId, getAttributeType( upId ), values );
857 }
858
859
860
861
862
863
864
865
866
867
868 public boolean contains( AttributeType attributeType, byte[]... values )
869 {
870 if ( attributeType == null )
871 {
872 return false;
873 }
874
875 EntryAttribute attribute = attributes.get( attributeType );
876
877 if ( attribute != null )
878 {
879 return attribute.contains( values );
880 }
881 else
882 {
883 return false;
884 }
885 }
886
887
888
889
890
891
892
893
894
895
896 public boolean contains( AttributeType attributeType, String... values )
897 {
898 if ( attributeType == null )
899 {
900 return false;
901 }
902
903 EntryAttribute attribute = attributes.get( attributeType );
904
905 if ( attribute != null )
906 {
907 return attribute.contains( values );
908 }
909 else
910 {
911 return false;
912 }
913 }
914
915
916
917
918
919
920
921
922
923
924 public boolean contains( AttributeType attributeType, Value<?>... values )
925 {
926 if ( attributeType == null )
927 {
928 return false;
929 }
930
931 EntryAttribute attribute = attributes.get( attributeType );
932
933 if ( attribute != null )
934 {
935 return attribute.contains( values );
936 }
937 else
938 {
939 return false;
940 }
941 }
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958 public boolean contains( EntryAttribute... attributes ) throws NamingException
959 {
960 for ( EntryAttribute entryAttribute:attributes )
961 {
962 if ( entryAttribute == null )
963 {
964 return this.attributes.size() == 0;
965 }
966
967 if ( !this.attributes.containsKey( ((ServerAttribute)entryAttribute).getAttributeType() ) )
968 {
969 return false;
970 }
971 }
972
973 return true;
974 }
975
976
977
978
979
980
981
982
983
984
985 public boolean contains( String id, byte[]... values )
986 {
987 if ( id == null )
988 {
989 return false;
990 }
991
992 try
993 {
994 AttributeType attributeType = atRegistry.lookup( id );
995
996 if ( attributeType == null )
997 {
998 return false;
999 }
1000
1001 EntryAttribute attribute = attributes.get( attributeType );
1002
1003 if ( attribute != null )
1004 {
1005 return attribute.contains( values );
1006 }
1007 else
1008 {
1009 return false;
1010 }
1011 }
1012 catch ( NamingException ne )
1013 {
1014 return false;
1015 }
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027 public boolean contains( String id, String... values )
1028 {
1029 if ( id == null )
1030 {
1031 return false;
1032 }
1033
1034 try
1035 {
1036 AttributeType attributeType = atRegistry.lookup( id );
1037
1038 if ( attributeType == null )
1039 {
1040 return false;
1041 }
1042
1043 EntryAttribute attribute = attributes.get( attributeType );
1044
1045 if ( attribute != null )
1046 {
1047 return attribute.contains( values );
1048 }
1049 else
1050 {
1051 return false;
1052 }
1053 }
1054 catch ( NamingException ne )
1055 {
1056 return false;
1057 }
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 public boolean contains( String id, Value<?>... values )
1070 {
1071 if ( id == null )
1072 {
1073 return false;
1074 }
1075
1076 try
1077 {
1078 AttributeType attributeType = atRegistry.lookup( id );
1079
1080 if ( attributeType == null )
1081 {
1082 return false;
1083 }
1084
1085 EntryAttribute attribute = attributes.get( attributeType );
1086
1087 if ( attribute != null )
1088 {
1089 return attribute.contains( values );
1090 }
1091 else
1092 {
1093 return false;
1094 }
1095 }
1096 catch ( NamingException ne )
1097 {
1098 return false;
1099 }
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109 public boolean containsAttribute( AttributeType attributeType )
1110 {
1111 return attributes.containsKey( attributeType );
1112 }
1113
1114
1115
1116
1117
1118
1119
1120
1121 public boolean containsAttribute( String... attributes )
1122 {
1123 for ( String attribute:attributes )
1124 {
1125 try
1126 {
1127 if ( !this.attributes.containsKey( getAttributeType( attribute ) ) )
1128 {
1129 return false;
1130 }
1131 }
1132 catch ( NamingException ne )
1133 {
1134 return false;
1135 }
1136 }
1137
1138 return true;
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 public EntryAttribute get( AttributeType attributeType )
1158 {
1159 return attributes.get( attributeType );
1160 }
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 public EntryAttribute get( String alias )
1180 {
1181 try
1182 {
1183 return get( atRegistry.lookup( alias ) );
1184 }
1185 catch ( NamingException ne )
1186 {
1187 String message = ne.getMessage();
1188 LOG.error( message );
1189 return null;
1190 }
1191 }
1192
1193
1194
1195
1196
1197
1198
1199 public Set<AttributeType> getAttributeTypes()
1200 {
1201 return attributes.keySet();
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 public boolean hasObjectClass( String objectClass )
1213 {
1214 return contains( OBJECT_CLASS_AT, objectClass );
1215 }
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225 public boolean hasObjectClass( EntryAttribute objectClass )
1226 {
1227 if ( objectClass == null )
1228 {
1229 return false;
1230 }
1231
1232
1233 if ( ((ServerAttribute)objectClass).getAttributeType() != OBJECT_CLASS_AT )
1234 {
1235 return false;
1236 }
1237
1238 EntryAttribute attribute = attributes.get( OBJECT_CLASS_AT );
1239
1240 if ( attribute == null )
1241 {
1242
1243 return false;
1244 }
1245
1246 for ( Value<?> value:objectClass )
1247 {
1248
1249 if ( !attribute.contains( value ) )
1250 {
1251 return false;
1252 }
1253 }
1254
1255 return true;
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 public boolean isValid()
1267 {
1268
1269 throw new NotImplementedException();
1270 }
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283 public boolean isValid( EntryAttribute objectClass )
1284 {
1285
1286 throw new NotImplementedException();
1287 }
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300 public boolean isValid( String objectClass )
1301 {
1302
1303 throw new NotImplementedException();
1304 }
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 public EntryAttribute put( AttributeType attributeType, byte[]... values ) throws NamingException
1327 {
1328 return put( null, attributeType, values );
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351 public EntryAttribute put( AttributeType attributeType, String... values ) throws NamingException
1352 {
1353 return put( null, attributeType, values );
1354 }
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376 public EntryAttribute put( AttributeType attributeType, Value<?>... values ) throws NamingException
1377 {
1378 return put( null, attributeType, values );
1379 }
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397 public List<EntryAttribute> put( EntryAttribute... attributes ) throws NamingException
1398 {
1399 List<EntryAttribute> previous = new ArrayList<EntryAttribute>();
1400
1401 for ( EntryAttribute serverAttribute:attributes )
1402 {
1403 if ( serverAttribute == null )
1404 {
1405 String message = "The ServerAttribute list should not contain null elements";
1406 LOG.error( message );
1407 throw new IllegalArgumentException( message );
1408 }
1409
1410 EntryAttribute removed = this.attributes.put( ((ServerAttribute)serverAttribute).getAttributeType(), serverAttribute );
1411
1412 if ( removed != null )
1413 {
1414 previous.add( removed );
1415 }
1416 }
1417
1418 return previous;
1419 }
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445 public EntryAttribute put( String upId, AttributeType attributeType, byte[]... values ) throws NamingException
1446 {
1447 if ( attributeType == null )
1448 {
1449 String message = "The attributeType should not be null";
1450 LOG.error( message );
1451 throw new IllegalArgumentException( message );
1452 }
1453
1454 if ( !StringTools.isEmpty( upId ) )
1455 {
1456 AttributeType tempAT = getAttributeType( upId );
1457
1458 if ( !tempAT.equals( attributeType ) )
1459 {
1460 String message = "The '" + upId + "' id is not compatible with the '" + attributeType + "' attribute type";
1461 LOG.error( message );
1462 throw new IllegalArgumentException( message );
1463 }
1464 }
1465 else
1466 {
1467 upId = getUpId( upId, attributeType );
1468 }
1469
1470 if ( attributeType.equals( OBJECT_CLASS_AT ) )
1471 {
1472 String message = "Only String values supported for objectClass attribute";
1473 LOG.error( message );
1474 throw new UnsupportedOperationException( message );
1475 }
1476
1477 EntryAttribute attribute = new DefaultServerAttribute( upId, attributeType, values );
1478 return attributes.put( attributeType, attribute );
1479 }
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505 public EntryAttribute put( String upId, AttributeType attributeType, String... values ) throws NamingException
1506 {
1507 if ( attributeType == null )
1508 {
1509 try
1510 {
1511 attributeType = getAttributeType( upId );
1512 }
1513 catch ( IllegalArgumentException iae )
1514 {
1515 String message = "The attributeType should not be null";
1516 LOG.error( message );
1517 throw new IllegalArgumentException( message );
1518 }
1519 }
1520 else
1521 {
1522 if ( !StringTools.isEmpty( upId ) )
1523 {
1524 AttributeType tempAT = getAttributeType( upId );
1525
1526 if ( !tempAT.equals( attributeType ) )
1527 {
1528 String message = "The '" + upId + "' id is not compatible with the '" + attributeType + "' attribute type";
1529 LOG.error( message );
1530 throw new IllegalArgumentException( message );
1531 }
1532 }
1533 else
1534 {
1535 upId = getUpId( upId, attributeType );
1536 }
1537 }
1538
1539 EntryAttribute attribute = new DefaultServerAttribute( upId, attributeType, values );
1540 return attributes.put( attributeType, attribute );
1541 }
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567 public EntryAttribute put( String upId, AttributeType attributeType, Value<?>... values ) throws NamingException
1568 {
1569 if ( attributeType == null )
1570 {
1571 String message = "The attributeType should not be null";
1572 LOG.error( message );
1573 throw new IllegalArgumentException( message );
1574 }
1575
1576 if ( !StringTools.isEmpty( upId ) )
1577 {
1578 AttributeType tempAT = getAttributeType( upId );
1579
1580 if ( !tempAT.equals( attributeType ) )
1581 {
1582 String message = "The '" + upId + "' id is not compatible with the '" + attributeType + "' attribute type";
1583 LOG.error( message );
1584 throw new IllegalArgumentException( message );
1585 }
1586 }
1587 else
1588 {
1589 upId = getUpId( upId, attributeType );
1590 }
1591
1592 EntryAttribute attribute = new DefaultServerAttribute( upId, attributeType, values );
1593 return attributes.put( attributeType, attribute );
1594 }
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 public EntryAttribute put( String upId, byte[]... values )
1614 {
1615 try
1616 {
1617 return put( upId, getAttributeType( upId ), values );
1618 }
1619 catch ( NamingException ne )
1620 {
1621 String message = "Error while adding values into the '" + upId + "' attribute. Error : " +
1622 ne.getMessage();
1623 LOG.error( message );
1624 throw new IllegalArgumentException( message );
1625 }
1626 }
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645 public EntryAttribute put( String upId, String... values )
1646 {
1647
1648 try
1649 {
1650 return put( upId, getAttributeType( upId ), values );
1651 }
1652 catch ( NamingException ne )
1653 {
1654 String message = "Error while adding values into the '" + upId + "' attribute. Error : " +
1655 ne.getMessage();
1656 LOG.error( message );
1657 throw new IllegalArgumentException( message );
1658 }
1659 }
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678 public EntryAttribute put( String upId, Value<?>... values )
1679 {
1680 try
1681 {
1682 return put( upId, getAttributeType( upId ), values );
1683 }
1684 catch ( NamingException ne )
1685 {
1686 String message = "Error while adding values into the '" + upId + "' attribute. Error : " +
1687 ne.getMessage();
1688 LOG.error( message );
1689 throw new IllegalArgumentException( message );
1690 }
1691 }
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715 public boolean remove( AttributeType attributeType, byte[]... values ) throws NamingException
1716 {
1717 try
1718 {
1719 EntryAttribute attribute = attributes.get( attributeType );
1720
1721 if ( attribute == null )
1722 {
1723
1724 return false;
1725 }
1726
1727 int nbOldValues = attribute.size();
1728
1729
1730 attribute.remove( values );
1731
1732 if ( attribute.size() == 0 )
1733 {
1734
1735 attributes.remove( attributeType );
1736
1737 return true;
1738 }
1739
1740 if ( nbOldValues != attribute.size() )
1741 {
1742
1743 return true;
1744 }
1745 else
1746 {
1747
1748 return false;
1749 }
1750 }
1751 catch ( IllegalArgumentException iae )
1752 {
1753 LOG.error( "The removal of values for the missing '{}' attribute is not possible", attributeType );
1754 return false;
1755 }
1756 }
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780 public boolean remove( AttributeType attributeType, String... values ) throws NamingException
1781 {
1782 try
1783 {
1784 EntryAttribute attribute = attributes.get( attributeType );
1785
1786 if ( attribute == null )
1787 {
1788
1789 return false;
1790 }
1791
1792 int nbOldValues = attribute.size();
1793
1794
1795 attribute.remove( values );
1796
1797 if ( attribute.size() == 0 )
1798 {
1799
1800 attributes.remove( attributeType );
1801
1802 return true;
1803 }
1804
1805 if ( nbOldValues != attribute.size() )
1806 {
1807
1808 return true;
1809 }
1810 else
1811 {
1812
1813 return false;
1814 }
1815 }
1816 catch ( IllegalArgumentException iae )
1817 {
1818 LOG.error( "The removal of values for the missing '{}' attribute is not possible", attributeType );
1819 return false;
1820 }
1821 }
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845 public boolean remove( AttributeType attributeType, Value<?>... values ) throws NamingException
1846 {
1847 try
1848 {
1849 EntryAttribute attribute = attributes.get( attributeType );
1850
1851 if ( attribute == null )
1852 {
1853
1854 return false;
1855 }
1856
1857 int nbOldValues = attribute.size();
1858
1859
1860 attribute.remove( values );
1861
1862 if ( attribute.size() == 0 )
1863 {
1864
1865 attributes.remove( attributeType );
1866
1867 return true;
1868 }
1869
1870 if ( nbOldValues != attribute.size() )
1871 {
1872
1873 return true;
1874 }
1875 else
1876 {
1877
1878 return false;
1879 }
1880 }
1881 catch ( IllegalArgumentException iae )
1882 {
1883 LOG.error( "The removal of values for the missing '{}' attribute is not possible", attributeType );
1884 return false;
1885 }
1886 }
1887
1888
1889 public List<EntryAttribute> remove( EntryAttribute... attributes ) throws NamingException
1890 {
1891 List<EntryAttribute> removedAttributes = new ArrayList<EntryAttribute>();
1892
1893 for ( EntryAttribute serverAttribute:attributes )
1894 {
1895 if ( this.attributes.containsKey( ((ServerAttribute)serverAttribute).getAttributeType() ) )
1896 {
1897 this.attributes.remove( ((ServerAttribute)serverAttribute).getAttributeType() );
1898 removedAttributes.add( serverAttribute );
1899 }
1900 }
1901
1902 return removedAttributes;
1903 }
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927 public boolean remove( String upId, byte[]... values ) throws NamingException
1928 {
1929 try
1930 {
1931 AttributeType attributeType = getAttributeType( upId );
1932
1933 return remove( attributeType, values );
1934 }
1935 catch ( NamingException ne )
1936 {
1937 LOG.error( "The removal of values for the missing '{}' attribute is not possible", upId );
1938 return false;
1939 }
1940 catch ( IllegalArgumentException iae )
1941 {
1942 LOG.error( "The removal of values for the bad '{}' attribute is not possible", upId );
1943 return false;
1944 }
1945 }
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969 public boolean remove( String upId, String... values ) throws NamingException
1970 {
1971 try
1972 {
1973 AttributeType attributeType = getAttributeType( upId );
1974
1975 return remove( attributeType, values );
1976 }
1977 catch ( NamingException ne )
1978 {
1979 LOG.error( "The removal of values for the missing '{}' attribute is not possible", upId );
1980 return false;
1981 }
1982 catch ( IllegalArgumentException iae )
1983 {
1984 LOG.error( "The removal of values for the bad '{}' attribute is not possible", upId );
1985 return false;
1986 }
1987 }
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011 public boolean remove( String upId, Value<?>... values ) throws NamingException
2012 {
2013 try
2014 {
2015 AttributeType attributeType = getAttributeType( upId );
2016
2017 return remove( attributeType, values );
2018 }
2019 catch ( NamingException ne )
2020 {
2021 LOG.error( "The removal of values for the missing '{}' attribute is not possible", upId );
2022 return false;
2023 }
2024 catch ( IllegalArgumentException iae )
2025 {
2026 LOG.error( "The removal of values for the bad '{}' attribute is not possible", upId );
2027 return false;
2028 }
2029 }
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047 public List<EntryAttribute> removeAttributes( AttributeType... attributes )
2048 {
2049 if ( attributes.length == 0 )
2050 {
2051 return null;
2052 }
2053
2054 List<EntryAttribute> removed = new ArrayList<EntryAttribute>( attributes.length );
2055
2056 for ( AttributeType attributeType:attributes )
2057 {
2058 EntryAttribute attr = this.attributes.remove( attributeType );
2059
2060 if ( attr != null )
2061 {
2062 removed.add( attr );
2063 }
2064 }
2065
2066 if ( removed.size() == 0 )
2067 {
2068 return null;
2069 }
2070 else
2071 {
2072 return removed;
2073 }
2074 }
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092 public List<EntryAttribute> removeAttributes( String... attributes )
2093 {
2094 if ( attributes.length == 0 )
2095 {
2096 return null;
2097 }
2098
2099 List<EntryAttribute> removed = new ArrayList<EntryAttribute>( attributes.length );
2100
2101 for ( String attribute:attributes )
2102 {
2103 AttributeType attributeType = null;
2104
2105 try
2106 {
2107 attributeType = atRegistry.lookup( attribute );
2108 }
2109 catch ( NamingException ne )
2110 {
2111 String message = "The attribute '" + attribute + "' does not exist in the entry";
2112 LOG.warn( message );
2113 continue;
2114 }
2115
2116 EntryAttribute attr = this.attributes.remove( attributeType );
2117
2118 if ( attr != null )
2119 {
2120 removed.add( attr );
2121 }
2122 }
2123
2124 if ( removed.size() == 0 )
2125 {
2126 return null;
2127 }
2128 else
2129 {
2130 return removed;
2131 }
2132 }
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148 public List<EntryAttribute> set( AttributeType... attributeTypes )
2149 {
2150 List<EntryAttribute> removed = new ArrayList<EntryAttribute>();
2151
2152
2153 for ( AttributeType attributeType:attributeTypes )
2154 {
2155 if ( attributeType == null )
2156 {
2157 String message = "The AttributeType list should not contain null values";
2158 LOG.error( message );
2159 continue;
2160 }
2161
2162 EntryAttribute attribute = attributes.put( attributeType, new DefaultServerAttribute( attributeType ) );
2163
2164 if ( attribute != null )
2165 {
2166 removed.add( attribute );
2167 }
2168 }
2169
2170 if ( removed.size() == 0 )
2171 {
2172 return null;
2173 }
2174 else
2175 {
2176 return removed;
2177 }
2178 }
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194 public List<EntryAttribute> set( String... upIds )
2195 {
2196 List<EntryAttribute> removed = new ArrayList<EntryAttribute>();
2197
2198 for ( String upId:upIds )
2199 {
2200
2201 AttributeType attributeType = null;
2202
2203 try
2204 {
2205 attributeType = getAttributeType( upId );
2206 }
2207 catch ( NamingException ne )
2208 {
2209 LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, ne.getMessage() );
2210 continue;
2211 }
2212 catch ( IllegalArgumentException iae )
2213 {
2214 LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, iae.getMessage() );
2215 continue;
2216 }
2217
2218 EntryAttribute attribute = attributes.put( attributeType,
2219 new DefaultServerAttribute( upId, attributeType ));
2220
2221 if ( attribute != null )
2222 {
2223 removed.add( attribute );
2224 }
2225 }
2226
2227 if ( removed.size() == 0 )
2228 {
2229 return null;
2230 }
2231 else
2232 {
2233 return removed;
2234 }
2235 }
2236
2237
2238
2239
2240
2241
2242
2243 public Entry toClientEntry() throws NamingException
2244 {
2245
2246 Entry clientEntry = new DefaultClientEntry( dn );
2247
2248
2249 for ( EntryAttribute serverAttribute:this )
2250 {
2251 EntryAttribute clientAttribute = ((ServerAttribute)serverAttribute).toClientAttribute();
2252 clientEntry.add( clientAttribute );
2253 }
2254
2255 return clientEntry;
2256 }
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267 public Entry clone()
2268 {
2269
2270 DefaultServerEntry clone = (DefaultServerEntry)super.clone();
2271
2272
2273
2274
2275 if ( dn != null )
2276 {
2277 clone.dn = (LdapDN)dn.clone();
2278 }
2279
2280
2281 clone.attributes = (Map<AttributeType, EntryAttribute>)(((HashMap<AttributeType, EntryAttribute>)attributes).clone());
2282
2283
2284 clone.attributes.clear();
2285
2286 for ( AttributeType key:attributes.keySet() )
2287 {
2288 EntryAttribute value = (ServerAttribute)attributes.get( key ).clone();
2289 clone.attributes.put( key, value );
2290 }
2291
2292
2293 return clone;
2294 }
2295
2296
2297
2298
2299
2300
2301
2302
2303 public void writeExternal( ObjectOutput out ) throws IOException
2304 {
2305 throw new IllegalStateException( "Cannot use standard serialization for a ServerEntry" );
2306 }
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323 public void serialize( ObjectOutput out ) throws IOException
2324 {
2325
2326
2327 LdapDNSerializer.serialize( dn, out );
2328
2329
2330 out.writeInt( attributes.size() );
2331
2332
2333
2334
2335
2336 for ( AttributeType attributeType:attributes.keySet() )
2337 {
2338
2339
2340 String oid = attributeType.getOid();
2341
2342 out.writeUTF( oid );
2343
2344
2345 DefaultServerAttribute attribute = (DefaultServerAttribute)attributes.get( attributeType );
2346
2347
2348 attribute.serialize( out );
2349 }
2350 }
2351
2352
2353
2354
2355
2356
2357
2358
2359 public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
2360 {
2361 throw new IllegalStateException( "Cannot use standard serialization for a ServerAttribute" );
2362 }
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372 public void deserialize( ObjectInput in ) throws IOException, ClassNotFoundException
2373 {
2374
2375 dn = LdapDNSerializer.deserialize( in );
2376
2377
2378 int nbAttributes = in.readInt();
2379
2380
2381 for ( int i = 0; i < nbAttributes; i++ )
2382 {
2383
2384 String oid = in.readUTF();
2385
2386 try
2387 {
2388 AttributeType attributeType = atRegistry.lookup( oid );
2389
2390
2391 DefaultServerAttribute attribute = new DefaultServerAttribute( attributeType );
2392
2393
2394 attribute.deserialize( in );
2395
2396 attributes.put( attributeType, attribute );
2397 }
2398 catch ( NamingException ne )
2399 {
2400
2401 LOG.warn( "Cannot read the attribute as it's OID ('" + oid + "') does not exist" );
2402
2403 }
2404 }
2405 }
2406
2407
2408
2409
2410
2411
2412
2413
2414 public int hashCode()
2415 {
2416 int result = 37;
2417
2418 result = result*17 + dn.hashCode();
2419
2420 for ( EntryAttribute attribute:attributes.values() )
2421 {
2422 result = result*17 + attribute.hashCode();
2423 }
2424
2425 return result;
2426 }
2427
2428
2429
2430
2431
2432 public boolean equals( Object o )
2433 {
2434
2435 if ( this == o )
2436 {
2437 return true;
2438 }
2439
2440 if ( ! ( o instanceof DefaultServerEntry ) )
2441 {
2442 return false;
2443 }
2444
2445 ServerEntry other = (DefaultServerEntry)o;
2446
2447 if ( dn == null )
2448 {
2449 if ( other.getDn() != null )
2450 {
2451 return false;
2452 }
2453 }
2454 else
2455 {
2456 if ( !dn.equals( other.getDn() ) )
2457 {
2458 return false;
2459 }
2460 }
2461
2462 if ( size() != other.size() )
2463 {
2464 return false;
2465 }
2466
2467 for ( EntryAttribute attribute:other )
2468 {
2469 EntryAttribute attr = attributes.get( ((ServerAttribute)attribute).getAttributeType() );
2470
2471 if ( attr == null )
2472 {
2473 return false;
2474 }
2475
2476 if ( !attribute.equals( attr ) )
2477 {
2478 return false;
2479 }
2480 }
2481
2482 return true;
2483 }
2484
2485
2486
2487
2488 public String toString()
2489 {
2490 StringBuilder sb = new StringBuilder();
2491
2492 sb.append( "ServerEntry\n" );
2493 sb.append( " dn" );
2494
2495 if ( dn.isNormalized() )
2496 {
2497 sb.append( "[n]" );
2498 }
2499 else
2500 {
2501 sb.append( "[]" );
2502 }
2503
2504 sb.append( ": " ).append( dn ).append( '\n' );
2505
2506
2507 if ( containsAttribute( OBJECT_CLASS_AT ) )
2508 {
2509 EntryAttribute objectClass = get( OBJECT_CLASS_AT );
2510
2511 sb.append( objectClass );
2512 }
2513
2514 if ( attributes.size() != 0 )
2515 {
2516 for ( EntryAttribute attribute:attributes.values() )
2517 {
2518 if ( !((ServerAttribute)attribute).getAttributeType().equals( OBJECT_CLASS_AT ) )
2519 {
2520 sb.append( attribute );
2521 }
2522 }
2523 }
2524
2525 return sb.toString();
2526 }
2527 }