| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
package org.ietf.ldap; |
| 39 |
|
| 40 |
import java.io.Serializable; |
| 41 |
import java.util.ArrayList; |
| 42 |
import java.util.Arrays; |
| 43 |
import java.util.Comparator; |
| 44 |
import java.util.Enumeration; |
| 45 |
import java.util.Hashtable; |
| 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 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
public class LDAPSchema implements Serializable { |
| 183 |
|
| 184 |
static final long serialVersionUID = -3911737419783579398L; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public LDAPSchema() { |
| 208 |
} |
| 209 |
|
| 210 |
public LDAPSchema( LDAPEntry entry ) { |
| 211 |
initialize( entry ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void add( LDAPSchemaElement el ) { |
| 223 |
add( el, true ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private void add( LDAPSchemaElement el, |
| 237 |
boolean isChange ) { |
| 238 |
String ID = el.getID(); |
| 239 |
if ( ID != null ) { |
| 240 |
LDAPSchemaElement oldEl = _IDToElement.get( ID ); |
| 241 |
if ( (oldEl != null) && namesTheSame( oldEl, el ) ) { |
| 242 |
throw new IllegalArgumentException( "Schema element already " + |
| 243 |
"defined for " + ID + |
| 244 |
"; old: " + |
| 245 |
oldEl + |
| 246 |
" ; new: " + el ); |
| 247 |
} |
| 248 |
_IDToElement.put( ID, el ); |
| 249 |
} |
| 250 |
if ( isChange ) { |
| 251 |
_changeList.add( getModificationForElement( el, |
| 252 |
LDAPModification.ADD, |
| 253 |
null ) ); |
| 254 |
} |
| 255 |
String name; |
| 256 |
try { |
| 257 |
name = (el.getNames().length > 0) ? |
| 258 |
el.getNames()[0].toLowerCase() : ""; |
| 259 |
} catch ( Exception e ) { |
| 260 |
System.err.println( el ); |
| 261 |
e.printStackTrace(); |
| 262 |
return; |
| 263 |
} |
| 264 |
if ( el instanceof LDAPAttributeSchema ) { |
| 265 |
_attributes.put( name, (LDAPAttributeSchema) el ); |
| 266 |
} else if ( el instanceof LDAPObjectClassSchema ) { |
| 267 |
_objectClasses.put( name, (LDAPObjectClassSchema) el ); |
| 268 |
} else if ( el instanceof LDAPMatchingRuleSchema ) { |
| 269 |
_matchingRules.put( name, (LDAPMatchingRuleSchema) el ); |
| 270 |
} else if ( el instanceof LDAPMatchingRuleUseSchema ) { |
| 271 |
_matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el ); |
| 272 |
} else if ( el instanceof LDAPNameFormSchema ) { |
| 273 |
_nameForms.put( name, (LDAPNameFormSchema) el ); |
| 274 |
} else if ( el instanceof LDAPSyntaxSchema ) { |
| 275 |
if ( name.length() < 1 ) { |
| 276 |
name = el.getID(); |
| 277 |
} |
| 278 |
_syntaxes.put( name, (LDAPSyntaxSchema) el ); |
| 279 |
} else if ( el instanceof LDAPDITContentRuleSchema ) { |
| 280 |
_contentRules.put( name, (LDAPDITContentRuleSchema) el ); |
| 281 |
} else if ( el instanceof LDAPDITStructureRuleSchema ) { |
| 282 |
_structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el ); |
| 283 |
_structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el ); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
public void fetchSchema( LDAPConnection ld ) throws LDAPException { |
| 294 |
fetchSchema( ld, "" ); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public void fetchSchema( LDAPConnection ld, String dn ) |
| 305 |
throws LDAPException { |
| 306 |
clear(); |
| 307 |
|
| 308 |
String entryName = getSchemaDN( ld, dn ); |
| 309 |
|
| 310 |
|
| 311 |
LDAPEntry entry = readSchema( ld, entryName ); |
| 312 |
initialize( entry ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public Enumeration<String> getAttributeNames() { |
| 320 |
return _attributes.keys(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public LDAPAttributeSchema getAttributeSchema( String name ) { |
| 330 |
return _attributes.get( name.toLowerCase() ); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public Enumeration<LDAPAttributeSchema> getAttributeSchemas() { |
| 338 |
return _attributes.elements(); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public Enumeration<String> getDITContentRuleNames() { |
| 346 |
return _contentRules.keys(); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public LDAPDITContentRuleSchema getDITContentRuleSchema( String name ) { |
| 356 |
return _contentRules.get( name.toLowerCase() ); |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public Enumeration<LDAPDITContentRuleSchema> getDITContentRuleSchemas() { |
| 364 |
return _contentRules.elements(); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public Enumeration<String> getDITStructureRuleNames() { |
| 372 |
return _structureRulesByName.keys(); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public LDAPDITStructureRuleSchema getDITStructureRuleSchema( String name ) { |
| 382 |
return _structureRulesByName.get(name.toLowerCase() ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
public LDAPDITStructureRuleSchema getDITStructureRuleSchema( int ID ) { |
| 392 |
return _structureRulesById.get( ID ); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public Enumeration<LDAPDITStructureRuleSchema> getDITStructureRuleSchemas() { |
| 400 |
return _structureRulesByName.elements(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public Enumeration<String> getMatchingRuleNames() { |
| 408 |
return _matchingRules.keys(); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public LDAPMatchingRuleSchema getMatchingRuleSchema( String name ) { |
| 418 |
return _matchingRules.get( name.toLowerCase() ); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public Enumeration<LDAPMatchingRuleSchema> getMatchingRuleSchemas() { |
| 426 |
return _matchingRules.elements(); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public Enumeration<String> getMatchingRuleUseNames() { |
| 434 |
return _matchingRuleUses.keys(); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public LDAPMatchingRuleUseSchema getMatchingRuleUseSchema( String name ) { |
| 444 |
return _matchingRuleUses.get( name.toLowerCase() ); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public Enumeration<LDAPMatchingRuleUseSchema> getMatchingRuleUseSchemas() { |
| 452 |
return _matchingRuleUses.elements(); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
public Enumeration<String> getNameFormNames() { |
| 460 |
return _nameForms.keys(); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public LDAPNameFormSchema getNameFormSchema( String name ) { |
| 470 |
return _nameForms.get( name.toLowerCase() ); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public Enumeration<LDAPNameFormSchema> getNameFormSchemas() { |
| 478 |
return _nameForms.elements(); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public Enumeration<String> getObjectClassNames() { |
| 486 |
return _objectClasses.keys(); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public LDAPObjectClassSchema getObjectClassSchema( String name ) { |
| 496 |
return _objectClasses.get( name.toLowerCase() ); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
public Enumeration<LDAPObjectClassSchema> getObjectClassSchemas() { |
| 504 |
return _objectClasses.elements(); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
public Enumeration<String> getSyntaxNames() { |
| 512 |
return _syntaxes.keys(); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public LDAPSyntaxSchema getSyntaxSchema( String name ) { |
| 522 |
return _syntaxes.get( name.toLowerCase() ); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public Enumeration<LDAPSyntaxSchema> getSyntaxSchemas() { |
| 530 |
return _syntaxes.elements(); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public void modify( LDAPSchemaElement el ) { |
| 542 |
modify( el, true ); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
private void modify( LDAPSchemaElement el, |
| 556 |
boolean isChange ) { |
| 557 |
String ID = el.getID(); |
| 558 |
if ( isChange ) { |
| 559 |
|
| 560 |
LDAPSchemaElement oldEl = ( ID != null ) ? |
| 561 |
(LDAPSchemaElement)_IDToElement.get( ID ) : null; |
| 562 |
if ( oldEl != null ) { |
| 563 |
_changeList.add( |
| 564 |
getModificationForElement( oldEl, |
| 565 |
LDAPModification.DELETE, |
| 566 |
null ) ); |
| 567 |
} |
| 568 |
_changeList.add( |
| 569 |
getModificationForElement( el, |
| 570 |
LDAPModification.ADD, |
| 571 |
null ) ); |
| 572 |
} |
| 573 |
if ( ID != null ) { |
| 574 |
_IDToElement.put( ID, el ); |
| 575 |
} |
| 576 |
String name = (el.getNames().length > 0) ? |
| 577 |
el.getNames()[0].toLowerCase() : ""; |
| 578 |
if ( el instanceof LDAPAttributeSchema ) { |
| 579 |
_attributes.put( name, (LDAPAttributeSchema) el ); |
| 580 |
} else if ( el instanceof LDAPObjectClassSchema ) { |
| 581 |
_objectClasses.put( name, (LDAPObjectClassSchema) el ); |
| 582 |
} else if ( el instanceof LDAPMatchingRuleSchema ) { |
| 583 |
_matchingRules.put( name, (LDAPMatchingRuleSchema) el ); |
| 584 |
} else if ( el instanceof LDAPMatchingRuleUseSchema ) { |
| 585 |
_matchingRuleUses.put( name, (LDAPMatchingRuleUseSchema) el ); |
| 586 |
} else if ( el instanceof LDAPNameFormSchema ) { |
| 587 |
_nameForms.put( name, (LDAPNameFormSchema) el ); |
| 588 |
} else if ( el instanceof LDAPSyntaxSchema ) { |
| 589 |
if ( name.length() < 1 ) { |
| 590 |
name = el.getID(); |
| 591 |
} |
| 592 |
_syntaxes.put( name, (LDAPSyntaxSchema) el ); |
| 593 |
} else if ( el instanceof LDAPDITContentRuleSchema ) { |
| 594 |
_contentRules.put( name, (LDAPDITContentRuleSchema) el ); |
| 595 |
} else if ( el instanceof LDAPDITStructureRuleSchema ) { |
| 596 |
_structureRulesByName.put( name, (LDAPDITStructureRuleSchema) el ); |
| 597 |
_structureRulesById.put( ((LDAPDITStructureRuleSchema)el).getRuleID(), (LDAPDITStructureRuleSchema) el ); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
public void remove( LDAPSchemaElement el ) { |
| 610 |
remove( el, true ); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
private void remove( LDAPSchemaElement el, boolean isChange ) { |
| 624 |
String ID = el.getID(); |
| 625 |
if ( isChange ) { |
| 626 |
|
| 627 |
if ( (ID != null) && _IDToElement.containsKey( ID ) ) { |
| 628 |
_changeList.add( |
| 629 |
getModificationForElement( el, |
| 630 |
LDAPModification.DELETE, |
| 631 |
null ) ); |
| 632 |
} |
| 633 |
} |
| 634 |
if ( ID != null ) { |
| 635 |
_IDToElement.remove( ID ); |
| 636 |
} |
| 637 |
String name = (el.getNames().length > 0) ? |
| 638 |
el.getNames()[0].toLowerCase() : ""; |
| 639 |
if ( el instanceof LDAPAttributeSchema ) { |
| 640 |
_attributes.remove( name ); |
| 641 |
} else if ( el instanceof LDAPObjectClassSchema ) { |
| 642 |
_objectClasses.remove( name ); |
| 643 |
} else if ( el instanceof LDAPMatchingRuleSchema ) { |
| 644 |
_matchingRules.remove( name ); |
| 645 |
} else if ( el instanceof LDAPMatchingRuleUseSchema ) { |
| 646 |
_matchingRuleUses.remove( name ); |
| 647 |
} else if ( el instanceof LDAPNameFormSchema ) { |
| 648 |
_nameForms.remove( name ); |
| 649 |
} else if ( el instanceof LDAPSyntaxSchema ) { |
| 650 |
if ( name.length() < 1 ) { |
| 651 |
name = el.getID(); |
| 652 |
} |
| 653 |
_syntaxes.remove( name ); |
| 654 |
} else if ( el instanceof LDAPDITContentRuleSchema ) { |
| 655 |
_contentRules.remove( name ); |
| 656 |
} else if ( el instanceof LDAPDITStructureRuleSchema ) { |
| 657 |
_structureRulesByName.remove( name ); |
| 658 |
_structureRulesById.remove( ((LDAPDITStructureRuleSchema)el).getRuleID() ); |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
public void saveSchema( LDAPConnection ld ) throws LDAPException { |
| 669 |
saveSchema( ld, "" ); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public void saveSchema( LDAPConnection ld, String dn ) |
| 680 |
throws LDAPException { |
| 681 |
|
| 682 |
|
| 683 |
LDAPModification[] mods = |
| 684 |
_changeList.toArray( new LDAPModification[0] ); |
| 685 |
if ( mods.length < 1 ) { |
| 686 |
return; |
| 687 |
} |
| 688 |
|
| 689 |
|
| 690 |
String entryName = getSchemaDN( ld, dn ); |
| 691 |
|
| 692 |
ld.modify( entryName, mods ); |
| 693 |
|
| 694 |
|
| 695 |
_changeList.clear(); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public String toString() { |
| 706 |
String s = "Object classes:\n"; |
| 707 |
Enumeration<LDAPObjectClassSchema> en1 = getObjectClassSchemas(); |
| 708 |
while( en1.hasMoreElements() ) { |
| 709 |
s += en1.nextElement().toString(); |
| 710 |
s += '\n'; |
| 711 |
} |
| 712 |
s += "Attributes:\n"; |
| 713 |
Enumeration<LDAPAttributeSchema> en2 = getAttributeSchemas(); |
| 714 |
while( en2.hasMoreElements() ) { |
| 715 |
s += en2.nextElement().toString(); |
| 716 |
s += '\n'; |
| 717 |
} |
| 718 |
s += "Matching rules:\n"; |
| 719 |
Enumeration<LDAPMatchingRuleSchema> en3 = getMatchingRuleSchemas(); |
| 720 |
while( en3.hasMoreElements() ) { |
| 721 |
s += en3.nextElement().toString(); |
| 722 |
s += '\n'; |
| 723 |
} |
| 724 |
s += "Syntaxes:\n"; |
| 725 |
Enumeration<LDAPSyntaxSchema> en4 = getSyntaxSchemas(); |
| 726 |
while( en4.hasMoreElements() ) { |
| 727 |
s += en4.nextElement().toString(); |
| 728 |
s += '\n'; |
| 729 |
} |
| 730 |
return s; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
void clear() { |
| 737 |
_attributes.clear(); |
| 738 |
_objectClasses.clear(); |
| 739 |
_matchingRules.clear(); |
| 740 |
_matchingRuleUses.clear(); |
| 741 |
_nameForms.clear(); |
| 742 |
_syntaxes.clear(); |
| 743 |
_contentRules.clear(); |
| 744 |
_structureRulesByName.clear(); |
| 745 |
_structureRulesById.clear(); |
| 746 |
_IDToElement.clear(); |
| 747 |
_changeList.clear(); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
private static LDAPModification getModificationForElement( |
| 759 |
LDAPSchemaElement el, |
| 760 |
int op, |
| 761 |
LDAPConnection ld ) { |
| 762 |
boolean quotingBug; |
| 763 |
try { |
| 764 |
quotingBug = (ld != null) ? |
| 765 |
!LDAPSchema.isAttributeSyntaxStandardsCompliant( ld ) : false; |
| 766 |
} catch ( LDAPException e ) { |
| 767 |
quotingBug = false; |
| 768 |
} |
| 769 |
LDAPAttribute attr = new LDAPAttribute( el.getAttributeName(), |
| 770 |
el.getValue( quotingBug ) ); |
| 771 |
return new LDAPModification( op, attr ); |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
private static String getSchemaDN( LDAPConnection ld, |
| 781 |
String dn ) |
| 782 |
throws LDAPException { |
| 783 |
if ( (ld == null) || !ld.isConnected() ) { |
| 784 |
throw new LDAPException( "No connection", LDAPException.OTHER ); |
| 785 |
} |
| 786 |
String[] attrs = { "subschemasubentry" }; |
| 787 |
LDAPEntry entry = ld.read( dn, attrs ); |
| 788 |
if ( entry == null ) { |
| 789 |
throw new LDAPException( "", LDAPException.NO_SUCH_OBJECT ); |
| 790 |
} |
| 791 |
LDAPAttribute attr = entry.getAttribute( attrs[0] ); |
| 792 |
String entryName = "cn=schema"; |
| 793 |
if ( attr != null ) { |
| 794 |
Enumeration<String> en = attr.getStringValues(); |
| 795 |
if ( en.hasMoreElements() ) { |
| 796 |
entryName = en.nextElement(); |
| 797 |
} |
| 798 |
} |
| 799 |
return entryName; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
protected void initialize( LDAPEntry entry ) { |
| 808 |
|
| 809 |
LDAPAttribute attr = entry.getAttribute( "objectclasses" ); |
| 810 |
Enumeration<String> en; |
| 811 |
if ( attr != null ) { |
| 812 |
en = attr.getStringValues(); |
| 813 |
while( en.hasMoreElements() ) { |
| 814 |
LDAPObjectClassSchema sch = |
| 815 |
new LDAPObjectClassSchema( en.nextElement() ); |
| 816 |
add( sch, false ); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
attr = entry.getAttribute( "attributetypes" ); |
| 822 |
if ( attr != null ) { |
| 823 |
en = attr.getStringValues(); |
| 824 |
while( en.hasMoreElements() ) { |
| 825 |
LDAPAttributeSchema sch = |
| 826 |
new LDAPAttributeSchema( en.nextElement() ); |
| 827 |
add( sch, false ); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
attr = entry.getAttribute( "ldapsyntaxes" ); |
| 833 |
if ( attr != null ) { |
| 834 |
en = attr.getStringValues(); |
| 835 |
while( en.hasMoreElements() ) { |
| 836 |
LDAPSyntaxSchema sch = |
| 837 |
new LDAPSyntaxSchema( en.nextElement() ); |
| 838 |
add( sch, false ); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
attr = entry.getAttribute( "ldapditstructurerules" ); |
| 844 |
if ( attr != null ) { |
| 845 |
en = attr.getStringValues(); |
| 846 |
while( en.hasMoreElements() ) { |
| 847 |
LDAPDITStructureRuleSchema sch = |
| 848 |
new LDAPDITStructureRuleSchema( |
| 849 |
en.nextElement() ); |
| 850 |
add( sch, false ); |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
attr = entry.getAttribute( "ldapditcontentrules" ); |
| 856 |
if ( attr != null ) { |
| 857 |
en = attr.getStringValues(); |
| 858 |
while( en.hasMoreElements() ) { |
| 859 |
LDAPDITContentRuleSchema sch = |
| 860 |
new LDAPDITContentRuleSchema( |
| 861 |
en.nextElement() ); |
| 862 |
add( sch, false ); |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
attr = entry.getAttribute( "nameforms" ); |
| 868 |
if ( attr != null ) { |
| 869 |
en = attr.getStringValues(); |
| 870 |
while( en.hasMoreElements() ) { |
| 871 |
LDAPNameFormSchema sch = |
| 872 |
new LDAPNameFormSchema( |
| 873 |
en.nextElement() ); |
| 874 |
add( sch, false ); |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
Hashtable<String, String> h = new Hashtable<>(); |
| 881 |
attr = entry.getAttribute( "matchingruleuse" ); |
| 882 |
if ( attr != null ) { |
| 883 |
en = attr.getStringValues(); |
| 884 |
while( en.hasMoreElements() ) { |
| 885 |
String use = en.nextElement(); |
| 886 |
LDAPMatchingRuleSchema sch = |
| 887 |
new LDAPMatchingRuleSchema( null, use ); |
| 888 |
h.put( sch.getID(), use ); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
attr = entry.getAttribute( "matchingrules" ); |
| 893 |
if ( attr != null ) { |
| 894 |
en = attr.getStringValues(); |
| 895 |
while( en.hasMoreElements() ) { |
| 896 |
String raw = en.nextElement(); |
| 897 |
LDAPMatchingRuleSchema sch = |
| 898 |
new LDAPMatchingRuleSchema( raw, null ); |
| 899 |
String use = h.get( sch.getID() ); |
| 900 |
if ( use != null ) |
| 901 |
sch = new LDAPMatchingRuleSchema( raw, use ); |
| 902 |
add( sch, false ); |
| 903 |
} |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
static boolean isAttributeSyntaxStandardsCompliant( LDAPConnection ld ) |
| 916 |
throws LDAPException { |
| 917 |
|
| 918 |
|
| 919 |
String schemaBug = (String)ld.getProperty( LDAPConnection.SCHEMA_BUG_PROPERTY ); |
| 920 |
if ( schemaBug != null ) { |
| 921 |
return schemaBug.equalsIgnoreCase( "standard" ); |
| 922 |
} |
| 923 |
|
| 924 |
boolean compliant = true; |
| 925 |
|
| 926 |
String entryName = getSchemaDN( ld, "" ); |
| 927 |
String[] attrs = { "attributetypes" }; |
| 928 |
LDAPEntry entry = ld.read( entryName, attrs ); |
| 929 |
|
| 930 |
LDAPAttribute attr = entry.getAttribute( "attributetypes" ); |
| 931 |
if ( attr != null ) { |
| 932 |
Enumeration<String> en = attr.getStringValues(); |
| 933 |
if( en.hasMoreElements() ) { |
| 934 |
compliant = !isSyntaxQuoted( en.nextElement() ); |
| 935 |
} |
| 936 |
} |
| 937 |
ld.setProperty( LDAPConnection.SCHEMA_BUG_PROPERTY, compliant ? "standard" : |
| 938 |
"NetscapeBug" ); |
| 939 |
return compliant; |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
static boolean isSyntaxQuoted( String raw ) { |
| 952 |
int ind = raw.indexOf( " SYNTAX " ); |
| 953 |
if ( ind >= 0 ) { |
| 954 |
ind += 8; |
| 955 |
int l = raw.length() - ind; |
| 956 |
|
| 957 |
char[] ch = new char[l]; |
| 958 |
raw.getChars( ind, raw.length(), ch, 0 ); |
| 959 |
ind = 0; |
| 960 |
|
| 961 |
while( (ind < ch.length) && (ch[ind] == ' ') ) { |
| 962 |
ind++; |
| 963 |
} |
| 964 |
if ( ind < ch.length ) { |
| 965 |
return ( ch[ind] == '\'' ); |
| 966 |
} |
| 967 |
} |
| 968 |
return false; |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
private static boolean namesTheSame( LDAPSchemaElement el1, |
| 980 |
LDAPSchemaElement el2 ) { |
| 981 |
|
| 982 |
if ( (el1 == null) || (el2 == null) ) { |
| 983 |
return false; |
| 984 |
} |
| 985 |
String[] names1 = el1.getNames(); |
| 986 |
String[] names2 = el2.getNames(); |
| 987 |
if ( (names1 == null) ^ (names2 == null) ) { |
| 988 |
return false; |
| 989 |
} |
| 990 |
|
| 991 |
if ( names1 == null ) { |
| 992 |
return true; |
| 993 |
} |
| 994 |
if ( names1.length != names2.length ) { |
| 995 |
return false; |
| 996 |
} |
| 997 |
if ( names1.length == 0 ) { |
| 998 |
return true; |
| 999 |
} |
| 1000 |
if ( names1.length == 1 ) { |
| 1001 |
return ( names1[0].equalsIgnoreCase( names2[0] ) ); |
| 1002 |
} |
| 1003 |
Comparator<String> comp = new Comparator<String>() { |
| 1004 |
public int compare( String o1, String o2 ) { |
| 1005 |
return o1.compareToIgnoreCase( o2 ); |
| 1006 |
} |
| 1007 |
public boolean equals( Object obj ) { |
| 1008 |
return false; |
| 1009 |
} |
| 1010 |
}; |
| 1011 |
Arrays.sort( names1, comp ); |
| 1012 |
Arrays.sort( names2, comp ); |
| 1013 |
for( int i = 0; i < names1.length; i++ ) { |
| 1014 |
if ( !names1[i].equalsIgnoreCase( names2[i] ) ) { |
| 1015 |
return false; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
return true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
private static void printEnum( Enumeration<? extends LDAPSchemaElement> en ) { |
| 1027 |
while( en.hasMoreElements() ) { |
| 1028 |
LDAPSchemaElement s = en.nextElement(); |
| 1029 |
System.out.println( " " + s ); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
private static LDAPEntry readSchema( LDAPConnection ld, |
| 1037 |
String dn, |
| 1038 |
String[] attrs ) |
| 1039 |
throws LDAPException { |
| 1040 |
LDAPSearchResults results = ld.search (dn, LDAPConnection.SCOPE_BASE, |
| 1041 |
"objectclass=subschema", |
| 1042 |
attrs, false); |
| 1043 |
if ( !results.hasMore() ) { |
| 1044 |
throw new LDAPException( "Cannot read schema", |
| 1045 |
LDAPException.INSUFFICIENT_ACCESS_RIGHTS ); |
| 1046 |
} |
| 1047 |
return results.next(); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
private static LDAPEntry readSchema( LDAPConnection ld, |
| 1054 |
String dn ) |
| 1055 |
throws LDAPException { |
| 1056 |
return readSchema( ld, dn, new String[] { "*", "ldapSyntaxes", |
| 1057 |
"matchingRules", "attributeTypes", "objectClasses" } ); |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
public static void main( String[] args ) { |
| 1076 |
if ( args.length < 2 ) { |
| 1077 |
System.err.println( "Usage: org.ietf.ldap.LDAPSchema HOST PORT" ); |
| 1078 |
System.exit(1 ); |
| 1079 |
} |
| 1080 |
int port = Integer.parseInt( args[1] ); |
| 1081 |
LDAPConnection ld = new LDAPConnection(); |
| 1082 |
try { |
| 1083 |
ld.connect( args[0], port ); |
| 1084 |
LDAPSchema schema = new LDAPSchema(); |
| 1085 |
schema.fetchSchema( ld ); |
| 1086 |
ld.disconnect(); |
| 1087 |
System.out.println( "Object classes: " ); |
| 1088 |
printEnum( schema.getObjectClassSchemas() ); |
| 1089 |
System.out.println( "\nAttributes: " ); |
| 1090 |
printEnum( schema.getAttributeSchemas() ); |
| 1091 |
System.out.println( "\nMatching rules: " ); |
| 1092 |
printEnum( schema.getMatchingRuleSchemas() ); |
| 1093 |
System.out.println( "\nSyntaxes: " ); |
| 1094 |
printEnum( schema.getSyntaxSchemas() ); |
| 1095 |
System.exit( 0 ); |
| 1096 |
} catch ( LDAPException e ) { |
| 1097 |
System.err.println( e ); |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
private Hashtable<String, LDAPObjectClassSchema> _objectClasses = new Hashtable<>(); |
| 1102 |
private Hashtable<String, LDAPAttributeSchema> _attributes = new Hashtable<>(); |
| 1103 |
private Hashtable<String, LDAPMatchingRuleSchema> _matchingRules = new Hashtable<>(); |
| 1104 |
private Hashtable<String, LDAPMatchingRuleUseSchema> _matchingRuleUses = new Hashtable<>(); |
| 1105 |
private Hashtable<String, LDAPSyntaxSchema> _syntaxes = new Hashtable<>(); |
| 1106 |
private Hashtable<String, LDAPDITStructureRuleSchema> _structureRulesByName = new Hashtable<>(); |
| 1107 |
private Hashtable<Integer, LDAPDITStructureRuleSchema> _structureRulesById = new Hashtable<>(); |
| 1108 |
private Hashtable<String, LDAPDITContentRuleSchema> _contentRules = new Hashtable<>(); |
| 1109 |
private Hashtable<String, LDAPNameFormSchema> _nameForms = new Hashtable<>(); |
| 1110 |
private Hashtable<String, LDAPSchemaElement> _IDToElement = new Hashtable<>(); |
| 1111 |
private ArrayList<LDAPModification> _changeList = new ArrayList<>(); |
| 1112 |
} |