1 package com.ca.commons.naming; 2 3 5 7 import javax.naming.InvalidNameException ; 8 9 44 45 public class RDN 46 { 47 64 65 private int[] elements = null; 66 67 70 71 private String ldapEscapedRDN; 72 73 74 78 79 private int UNTESTED = 0, SINGLEVALUED = 1, MULTIVALUED = 2; 80 81 84 85 private int status = UNTESTED; 86 87 91 92 private static int MAXELEMENTS = 16; 94 97 98 public RDN() { ldapEscapedRDN = "";} 99 100 101 private boolean debug = false; 102 103 108 109 110 111 public RDN(String rdn) 112 { 113 if (rdn == null) 114 { 115 rdn = ""; 116 } 117 else 118 { 119 int len = rdn.length(); 121 122 if ((rdn.indexOf('\\') > -1) && (len >=2 && rdn.charAt(len-2) == '\\' && rdn.charAt(len-1) == ' ')) 123 { 124 rdn = specialSpaceHandling(rdn); } 126 else 127 rdn = rdn.trim(); 128 } 129 130 ldapEscapedRDN = rdn; 131 132 if (debug) System.out.println(" % NEW RDN: " + rdn); 133 } 134 135 139 140 public RDN(RDN copyMe) 141 { 142 this(copyMe.ldapEscapedRDN); 143 } 144 145 150 151 153 private String specialSpaceHandling(String rdn) 154 { 155 157 int finalPos = rdn.length() - 2; 158 int pos = finalPos; 159 160 162 while (rdn.charAt(pos) == '\\') { 164 pos--; 165 } 166 167 int numSlashesDeleted = finalPos - pos; 168 169 int valuePos = rdn.indexOf('=')+1; 170 String att = rdn.substring(0, valuePos); 171 String val = rdn.substring(valuePos); 172 173 if (numSlashesDeleted%2 == 0) { 175 val = val.trim(); 176 } else { val = val.trim() + " "; 180 } 181 182 rdn = att + val; 183 184 return rdn; 185 } 186 187 190 191 public boolean isEmpty() 192 { 193 return ("".equals(ldapEscapedRDN)); 194 } 195 196 200 201 public void addEscaped(String rdnfragment) 202 throws InvalidNameException 203 { 204 validate(); 206 int equalpos = NameUtility.next(rdnfragment, 0, '='); 207 209 if (equalpos <= 0 || equalpos == rdnfragment.length()-1) 210 throw new InvalidNameException ("RDN.add(): invalid rdn fragment '" + ((rdnfragment==null)?"<null>":rdnfragment) + "' (can't find equal sign)"); 211 212 if (ldapEscapedRDN.length()>0) 213 ldapEscapedRDN += "+" + rdnfragment; 214 else 215 ldapEscapedRDN = rdnfragment; 216 217 } 218 219 225 226 public void addRaw(String rdnfragment) 227 throws InvalidNameException 228 { 229 int equalpos = NameUtility.next(rdnfragment, 0, '='); 230 232 if (equalpos <= 0 || equalpos == rdnfragment.length()-1) 233 throw new InvalidNameException ("RDN.addRaw(): invalid rdn fragment '" + ((rdnfragment==null)?"<null>":rdnfragment) + "' (can't find equal sign)"); 234 235 String attribute = rdnfragment.substring(0, equalpos); 236 String value = rdnfragment.substring(equalpos+1); 237 238 addEscaped(attribute + "=" + NameUtility.escape(value)); 239 } 240 241 242 248 249 public String toString() 250 { 251 return ldapEscapedRDN; 252 } 253 254 257 258 public void dump() 259 { 260 if (status == UNTESTED) 261 checkForMultiValued(); 262 263 System.out.println("DEBUG DUMP - RDN: " + ldapEscapedRDN + ((status==MULTIVALUED)?" MULTI VALUED":" SINGLE VALUED")); 264 265 if (status == MULTIVALUED) 266 { 267 for (int i=0; i<(elements.length - 1); i++) 268 { 269 System.out.println("element-m (" + (elements[i]+1) + ") -> (" + elements[i+1] + ") " + i + ": " + getElement(i)); 270 } 271 } 272 else 273 { 274 System.out.println("element-s 0: " + ldapEscapedRDN); 275 } 276 277 Thread.currentThread().dumpStack(); 278 279 } 280 281 286 287 public String getElement(int i) 288 { 289 if (status == UNTESTED) 290 checkForMultiValued(); 291 292 if (status == SINGLEVALUED && i==0) 293 return ldapEscapedRDN; 294 295 if (i<0 || elements == null || elements.length <= i+1) 296 return "error VII"; 297 298 return ldapEscapedRDN.substring(elements[i]+1, elements[i+1]); 299 } 300 301 304 305 public String [] getElements() 306 { 307 if (status == UNTESTED) 308 checkForMultiValued(); 309 310 if (status == SINGLEVALUED) 311 return new String [] {ldapEscapedRDN}; 312 313 if (elements == null) 314 return new String [] {"error VIIB"}; 315 316 String [] elementArray = new String [elements.length-1]; 317 318 for (int i=0; i<(elements.length-1); i++) 319 elementArray[i] = ldapEscapedRDN.substring(elements[i]+1, elements[i+1]); 320 321 return elementArray; 322 } 323 324 330 331 public void setElement(int i, String ldapEscapedElement) 332 throws InvalidNameException 333 { 334 validate(); 335 336 if (status == SINGLEVALUED) 337 { 338 if (i==0) 339 ldapEscapedRDN = ldapEscapedElement; 340 else 341 throw new InvalidNameException ("cannot set non zero element of single valued rdn."); 342 } 343 else 344 { 345 if (i < 0 || i >= size()) 346 throw new InvalidNameException ("attempt to set element " + i + " of rdn: '" + ldapEscapedRDN + "' (size = " + size() + ")"); 347 348 ldapEscapedRDN = ldapEscapedRDN.substring(0, elements[i]+1) + 349 ldapEscapedElement + 350 ldapEscapedRDN.substring(elements[i+1]); 351 352 parseMultiValued(); 353 354 } 355 } 356 357 358 359 362 363 public String getAtt() 364 { 365 return getAtt(0); 366 } 367 368 371 372 public String getAtt(int i) 373 { 374 if (status == UNTESTED) 375 checkForMultiValued(); 376 377 if (status == SINGLEVALUED && i!=0) 378 return "rdn error VIII"; 379 380 String element = getElement(i); 381 382 int pos = element.indexOf('='); 384 if (pos == -1) return "rdn error IX"; 385 386 if (debug) 387 { 388 System.out.println("Debug = " + debug); 389 Thread.currentThread().dumpStack(); 390 System.out.println(" % RDN -> found attribute as '" + element.substring(0,pos) + "'"); 391 } 392 393 return element.substring(0, pos); 394 } 395 396 400 401 public String [] getAtts() 402 { 403 if (status == UNTESTED) 404 checkForMultiValued(); 405 406 String [] atts = getElements(); 407 408 for (int i=0; i<atts.length; i++) 409 { 410 int pos = atts[i].indexOf('='); 412 if (pos == -1) return new String [] {"rdn error IXB"}; 413 414 atts[i] = atts[i].substring(0, pos); 415 } 416 417 return atts; 418 } 419 420 421 422 423 430 431 public boolean contains(String attributeType) 432 { 433 if (status == UNTESTED) 434 checkForMultiValued(); 435 436 if (attributeType == null || attributeType.length()==0) 437 return false; 438 439 for (int i=0; i<size(); i++) 440 if (attributeType.equalsIgnoreCase(getAtt(i))) 441 return true; 442 443 return false; 444 } 445 446 456 457 public String getRawVal(String attributeType) 458 { 459 if (status == UNTESTED) 460 checkForMultiValued(); 461 462 if (attributeType == null || attributeType.length()==0) 463 return null; 464 465 for (int i=0; i<size(); i++) 466 if (attributeType.equalsIgnoreCase(getAtt(i))) 467 return getRawVal(i); 468 469 return null; 470 471 } 472 473 476 477 public String getRawVal() 478 { 479 return getRawVal(0); 480 } 481 482 485 486 public String getRawVal(int i) 487 { 488 if (status == UNTESTED) 489 checkForMultiValued(); 490 491 if (status == SINGLEVALUED && i!=0) 492 return "rdn error X"; 493 494 String element = getElement(i); 495 496 int pos = element.indexOf('='); 498 if (pos == -1) 499 { 500 return "rdn error XI"; 501 } 502 503 String raw = element.substring(pos+1); 504 505 try 507 { 508 return NameUtility.unescape(raw); 509 } 510 catch (Exception e) 511 { 512 return "rdn error XII"; 513 } 514 } 515 516 520 521 public String [] getRawVals() 522 { 523 if (status == UNTESTED) 524 checkForMultiValued(); 525 526 String [] vals = getElements(); 527 528 for (int i=0; i<vals.length; i++) 529 { 530 vals[i] = getRawVal(i); 531 } 532 533 return vals; 534 } 535 536 537 538 541 542 public void setRawVal(String v) 543 throws InvalidNameException 544 { 545 setRawVal(v, 0); 546 } 547 548 551 552 public void setRawVal(String v, int i) 553 throws InvalidNameException 554 { 555 validate(); 556 557 String attval = getElement(i); 558 String att = attval.substring(0, attval.indexOf('=')); 559 if (att == null || att.length()==0) 560 throw new InvalidNameException ("can't parse old RDN '" + ldapEscapedRDN); 561 562 String newElement = att + "=" + NameUtility.escape(v); 563 setElement(i, newElement); 564 } 565 566 569 570 public int size() 571 { 572 if (status == UNTESTED) 573 checkForMultiValued(); 574 575 return (status==SINGLEVALUED)?1:elements.length-1; 576 } 577 578 579 580 583 584 public boolean isMultiValued() 585 { 586 if (status == UNTESTED) 587 checkForMultiValued(); 588 589 return (status == MULTIVALUED); 590 } 591 592 593 594 606 607 public boolean equals(RDN test) 608 { 609 if (test == null) 610 return false; 611 else if (test.size() != size()) return false; 612 613 if (isMultiValued()) 614 { 615 623 625 String [] atts = getAtts(); 626 String [] vals = getRawVals(); String [] testAtts = test.getAtts(); 628 String [] testVals = test.getRawVals(); 630 for (int i=0; i<size(); i++) 631 if (!elementsEqual(atts[i], testAtts[i], vals[i], testVals[i]) ) 632 return false; 633 634 return true; 635 } 636 else 637 { 638 return elementsEqual(getAtt(), test.getAtt(), getRawVal(), test.getRawVal()); } 640 } 641 642 643 644 655 656 private boolean elementsEqual(String att1, String att2, String val1, String val2) 657 { 658 671 672 if (att1.equalsIgnoreCase(att2) == false) 673 return false; 674 675 if (val1.equalsIgnoreCase(val2) == false) 677 return false; 678 679 return true; 680 } 681 682 683 684 688 689 public boolean equals(Object o) 690 { 691 if (o == null) 692 return false; 693 if (o instanceof RDN) 694 return equals((RDN)o); 695 else 696 return (ldapEscapedRDN.equalsIgnoreCase(o.toString())); 697 } 698 699 700 701 706 707 private void checkForMultiValued() 708 { 709 if (status != UNTESTED) return; 711 if (NameUtility.next(ldapEscapedRDN, 0, '+') == -1) { 713 status = SINGLEVALUED; 714 } 715 else { 717 status = MULTIVALUED; 718 parseMultiValued(MAXELEMENTS); 719 } 720 } 721 722 725 726 private void parseMultiValued() 727 { 728 parseMultiValued(MAXELEMENTS); 729 } 730 731 732 private void parseMultiValued(int max) 733 { 734 if (max > 512) 735 { 736 System.err.println("wierd error in RDN - attempt to parse RDN with more than 512 sub units???"); 737 return; 738 } 739 740 try 741 { 742 int[] temp = new int[max]; 743 744 temp[0] = -1; 746 int numElements = 0; 747 int pos = 0; 748 749 if (debug) System.out.println("\n*** parsing multi valued rdn"); 750 if (debug) System.out.println("parsing " + ldapEscapedRDN); 751 while ((pos = NameUtility.next(ldapEscapedRDN, pos, '+'))>-1) 752 { 753 numElements++; 754 temp[numElements] = pos; 755 756 if (debug) System.out.println("found " + numElements + " -th element at " + pos); 757 758 int pos1, pos2; 759 pos1 = temp[numElements-1] + 1; 760 pos2 = temp[numElements]; 761 if (debug) System.out.println(" = string " + pos1 + " -> " + pos2 + " = "); 762 if (debug) System.out.println(ldapEscapedRDN.substring(pos1, pos2)); 763 764 pos++; 765 } 766 767 numElements++; 768 temp[numElements] = ldapEscapedRDN.length(); 769 770 int pos1, pos2; 771 pos1 = temp[numElements-1] + 1; 772 pos2 = temp[numElements]; 773 774 if (debug) System.out.println("found " + numElements + " -th element at " + pos + " = string " + 775 pos1 + " -> " + pos2 + " final len: " + ldapEscapedRDN.length()); 776 if (debug) System.out.println(" = '" + ldapEscapedRDN.substring(pos1, pos2) + "'"); 777 778 779 780 if (debug) System.out.println("found total of " + numElements + " elements...\n*****\n"); 781 782 elements = new int[numElements+1]; 783 System.arraycopy(temp, 0, elements, 0, numElements+1); 784 } 785 catch (IndexOutOfBoundsException e) 786 { 787 if (debug) e.printStackTrace(); 788 System.err.println("huge number of multi-valued RDN units - increasing to: " + max*2); 789 parseMultiValued(max*2); 790 791 } 792 } 793 794 798 799 public boolean validate() 800 { 801 try 802 { 803 if (status == UNTESTED) 804 checkForMultiValued(); 805 806 if (isEmpty()) return false; 808 809 int noElements = size(); 810 for (int i=0; i<noElements; i++) 811 { 812 String att = getAtt(i); 813 String val = getRawVal(i); 814 815 if (att == null || att.length()==0) 816 { 817 return false; 818 } 819 if (val == null || val.length()==0 || val.startsWith("error ")) 820 { 821 return false; 822 } 823 } 824 } 825 catch (Exception e) 826 { 827 return false; 828 } 829 830 return true; } 832 833 } | Popular Tags |