1 17 18 package org.apache.naming.resources; 19 20 import java.text.ParseException ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.Locale ; 24 import java.util.TimeZone ; 25 import java.util.Vector ; 26 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 29 import javax.naming.directory.Attribute ; 30 import javax.naming.directory.Attributes ; 31 import javax.naming.directory.BasicAttribute ; 32 33 39 public class ResourceAttributes implements Attributes { 40 41 42 44 45 47 50 public static final String CREATION_DATE = "creationdate"; 51 52 53 56 public static final String ALTERNATE_CREATION_DATE = "creation-date"; 57 58 59 62 public static final String LAST_MODIFIED = "getlastmodified"; 63 64 65 68 public static final String ALTERNATE_LAST_MODIFIED = "last-modified"; 69 70 71 74 public static final String NAME = "displayname"; 75 76 77 80 public static final String TYPE = "resourcetype"; 81 82 83 86 public static final String ALTERNATE_TYPE = "content-type"; 87 88 89 92 public static final String SOURCE = "source"; 93 94 95 98 public static final String CONTENT_TYPE = "getcontenttype"; 99 100 101 104 public static final String CONTENT_LANGUAGE = "getcontentlanguage"; 105 106 107 110 public static final String CONTENT_LENGTH = "getcontentlength"; 111 112 113 116 public static final String ALTERNATE_CONTENT_LENGTH = "content-length"; 117 118 119 122 public static final String ETAG = "getetag"; 123 124 125 128 public static final String COLLECTION_TYPE = "<collection/>"; 129 130 131 134 protected static final SimpleDateFormat format = 135 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 136 137 138 141 protected static final SimpleDateFormat formats[] = { 142 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 143 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 144 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 145 }; 146 147 148 protected final static TimeZone gmtZone = TimeZone.getTimeZone("GMT"); 149 150 151 154 static { 155 156 format.setTimeZone(gmtZone); 157 158 formats[0].setTimeZone(gmtZone); 159 formats[1].setTimeZone(gmtZone); 160 formats[2].setTimeZone(gmtZone); 161 162 } 163 164 165 167 168 171 public ResourceAttributes() { 172 } 173 174 175 178 public ResourceAttributes(Attributes attributes) { 179 this.attributes = attributes; 180 } 181 182 183 185 186 189 protected boolean collection = false; 190 191 192 195 protected long contentLength = -1; 196 197 198 201 protected long creation = -1; 202 203 204 207 protected Date creationDate = null; 208 209 210 213 protected long lastModified = -1; 214 215 216 219 protected Date lastModifiedDate = null; 220 221 222 225 protected String lastModifiedHttp = null; 226 227 228 231 protected String mimeType = null; 232 233 234 237 protected String name = null; 238 239 240 243 protected String weakETag = null; 244 245 246 249 protected String strongETag = null; 250 251 252 255 protected Attributes attributes = null; 256 257 258 260 261 264 public boolean isCollection() { 265 if (attributes != null) { 266 return (getResourceType().equals(COLLECTION_TYPE)); 267 } else { 268 return (collection); 269 } 270 } 271 272 273 278 public void setCollection(boolean collection) { 279 this.collection = collection; 280 if (attributes != null) { 281 String value = ""; 282 if (collection) 283 value = COLLECTION_TYPE; 284 attributes.put(TYPE, value); 285 } 286 } 287 288 289 294 public long getContentLength() { 295 if (contentLength != -1L) 296 return contentLength; 297 if (attributes != null) { 298 Attribute attribute = attributes.get(CONTENT_LENGTH); 299 if (attribute != null) { 300 try { 301 Object value = attribute.get(); 302 if (value instanceof Long ) { 303 contentLength = ((Long ) value).longValue(); 304 } else { 305 try { 306 contentLength = Long.parseLong(value.toString()); 307 } catch (NumberFormatException e) { 308 ; } 310 } 311 } catch (NamingException e) { 312 ; } 314 } 315 } 316 return contentLength; 317 } 318 319 320 325 public void setContentLength(long contentLength) { 326 this.contentLength = contentLength; 327 if (attributes != null) 328 attributes.put(CONTENT_LENGTH, new Long (contentLength)); 329 } 330 331 332 337 public long getCreation() { 338 if (creation != -1L) 339 return creation; 340 if (creationDate != null) 341 return creationDate.getTime(); 342 if (attributes != null) { 343 Attribute attribute = attributes.get(CREATION_DATE); 344 if (attribute != null) { 345 try { 346 Object value = attribute.get(); 347 if (value instanceof Long ) { 348 creation = ((Long ) value).longValue(); 349 } else if (value instanceof Date ) { 350 creation = ((Date ) value).getTime(); 351 creationDate = (Date ) value; 352 } else { 353 String creationDateValue = value.toString(); 354 Date result = null; 355 for (int i = 0; (result == null) && 357 (i < formats.length); i++) { 358 try { 359 result = formats[i].parse(creationDateValue); 360 } catch (ParseException e) { 361 ; 362 } 363 } 364 if (result != null) { 365 creation = result.getTime(); 366 creationDate = result; 367 } 368 } 369 } catch (NamingException e) { 370 ; } 372 } 373 } 374 return creation; 375 } 376 377 378 383 public void setCreation(long creation) { 384 this.creation = creation; 385 this.creationDate = null; 386 if (attributes != null) 387 attributes.put(CREATION_DATE, new Date (creation)); 388 } 389 390 391 396 public Date getCreationDate() { 397 if (creationDate != null) 398 return creationDate; 399 if (creation != -1L) { 400 creationDate = new Date (creation); 401 return creationDate; 402 } 403 if (attributes != null) { 404 Attribute attribute = attributes.get(CREATION_DATE); 405 if (attribute != null) { 406 try { 407 Object value = attribute.get(); 408 if (value instanceof Long ) { 409 creation = ((Long ) value).longValue(); 410 creationDate = new Date (creation); 411 } else if (value instanceof Date ) { 412 creation = ((Date ) value).getTime(); 413 creationDate = (Date ) value; 414 } else { 415 String creationDateValue = value.toString(); 416 Date result = null; 417 for (int i = 0; (result == null) && 419 (i < formats.length); i++) { 420 try { 421 result = formats[i].parse(creationDateValue); 422 } catch (ParseException e) { 423 ; 424 } 425 } 426 if (result != null) { 427 creation = result.getTime(); 428 creationDate = result; 429 } 430 } 431 } catch (NamingException e) { 432 ; } 434 } 435 } 436 return creationDate; 437 } 438 439 440 445 public void setCreationDate(Date creationDate) { 446 this.creation = creationDate.getTime(); 447 this.creationDate = creationDate; 448 if (attributes != null) 449 attributes.put(CREATION_DATE, creationDate); 450 } 451 452 453 458 public long getLastModified() { 459 if (lastModified != -1L) 460 return lastModified; 461 if (lastModifiedDate != null) 462 return lastModifiedDate.getTime(); 463 if (attributes != null) { 464 Attribute attribute = attributes.get(LAST_MODIFIED); 465 if (attribute != null) { 466 try { 467 Object value = attribute.get(); 468 if (value instanceof Long ) { 469 lastModified = ((Long ) value).longValue(); 470 } else if (value instanceof Date ) { 471 lastModified = ((Date ) value).getTime(); 472 lastModifiedDate = (Date ) value; 473 } else { 474 String lastModifiedDateValue = value.toString(); 475 Date result = null; 476 for (int i = 0; (result == null) && 478 (i < formats.length); i++) { 479 try { 480 result = 481 formats[i].parse(lastModifiedDateValue); 482 } catch (ParseException e) { 483 ; 484 } 485 } 486 if (result != null) { 487 lastModified = result.getTime(); 488 lastModifiedDate = result; 489 } 490 } 491 } catch (NamingException e) { 492 ; } 494 } 495 } 496 return lastModified; 497 } 498 499 500 505 public void setLastModified(long lastModified) { 506 this.lastModified = lastModified; 507 this.lastModifiedDate = null; 508 if (attributes != null) 509 attributes.put(LAST_MODIFIED, new Date (lastModified)); 510 } 511 512 513 519 public void setLastModified(Date lastModified) { 520 setLastModifiedDate(lastModified); 521 } 522 523 524 529 public Date getLastModifiedDate() { 530 if (lastModifiedDate != null) 531 return lastModifiedDate; 532 if (lastModified != -1L) { 533 lastModifiedDate = new Date (lastModified); 534 return lastModifiedDate; 535 } 536 if (attributes != null) { 537 Attribute attribute = attributes.get(LAST_MODIFIED); 538 if (attribute != null) { 539 try { 540 Object value = attribute.get(); 541 if (value instanceof Long ) { 542 lastModified = ((Long ) value).longValue(); 543 lastModifiedDate = new Date (lastModified); 544 } else if (value instanceof Date ) { 545 lastModified = ((Date ) value).getTime(); 546 lastModifiedDate = (Date ) value; 547 } else { 548 String lastModifiedDateValue = value.toString(); 549 Date result = null; 550 for (int i = 0; (result == null) && 552 (i < formats.length); i++) { 553 try { 554 result = 555 formats[i].parse(lastModifiedDateValue); 556 } catch (ParseException e) { 557 ; 558 } 559 } 560 if (result != null) { 561 lastModified = result.getTime(); 562 lastModifiedDate = result; 563 } 564 } 565 } catch (NamingException e) { 566 ; } 568 } 569 } 570 return lastModifiedDate; 571 } 572 573 574 579 public void setLastModifiedDate(Date lastModifiedDate) { 580 this.lastModified = lastModifiedDate.getTime(); 581 this.lastModifiedDate = lastModifiedDate; 582 if (attributes != null) 583 attributes.put(LAST_MODIFIED, lastModifiedDate); 584 } 585 586 587 590 public String getLastModifiedHttp() { 591 if (lastModifiedHttp != null) 592 return lastModifiedHttp; 593 Date modifiedDate = getLastModifiedDate(); 594 if (modifiedDate == null) { 595 modifiedDate = getCreationDate(); 596 } 597 if (modifiedDate == null) { 598 modifiedDate = new Date (); 599 } 600 synchronized (format) { 601 lastModifiedHttp = format.format(modifiedDate); 602 } 603 return lastModifiedHttp; 604 } 605 606 607 610 public void setLastModifiedHttp(String lastModifiedHttp) { 611 this.lastModifiedHttp = lastModifiedHttp; 612 } 613 614 615 618 public String getMimeType() { 619 return mimeType; 620 } 621 622 623 626 public void setMimeType(String mimeType) { 627 this.mimeType = mimeType; 628 } 629 630 631 636 public String getName() { 637 if (name != null) 638 return name; 639 if (attributes != null) { 640 Attribute attribute = attributes.get(NAME); 641 if (attribute != null) { 642 try { 643 name = attribute.get().toString(); 644 } catch (NamingException e) { 645 ; } 647 } 648 } 649 return name; 650 } 651 652 653 658 public void setName(String name) { 659 this.name = name; 660 if (attributes != null) 661 attributes.put(NAME, name); 662 } 663 664 665 670 public String getResourceType() { 671 String result = null; 672 if (attributes != null) { 673 Attribute attribute = attributes.get(TYPE); 674 if (attribute != null) { 675 try { 676 result = attribute.get().toString(); 677 } catch (NamingException e) { 678 ; } 680 } 681 } 682 if (result == null) { 683 if (collection) 684 result = COLLECTION_TYPE; 685 else 686 result = ""; 687 } 688 return result; 689 } 690 691 692 697 public void setResourceType(String resourceType) { 698 collection = resourceType.equals(COLLECTION_TYPE); 699 if (attributes != null) 700 attributes.put(TYPE, resourceType); 701 } 702 703 704 709 public String getETag() { 710 return getETag(false); 711 } 712 713 714 720 public String getETag(boolean strong) { 721 String result = null; 722 if (attributes != null) { 723 Attribute attribute = attributes.get(ETAG); 724 if (attribute != null) { 725 try { 726 result = attribute.get().toString(); 727 } catch (NamingException e) { 728 ; } 730 } 731 } 732 if (strong) { 733 result = strongETag; 735 } else { 736 if (weakETag == null) { 738 weakETag = "W/\"" + getContentLength() + "-" 739 + getLastModified() + "\""; 740 } 741 result = weakETag; 742 } 743 return result; 744 } 745 746 747 750 public void setETag(String eTag) { 751 this.strongETag = eTag; 752 if (attributes != null) 753 attributes.put(ETAG, eTag); 754 } 755 756 757 764 public String getCanonicalPath() { 765 return null; 766 } 767 768 769 771 772 775 public Attribute get(String attrID) { 776 if (attributes == null) { 777 if (attrID.equals(CREATION_DATE)) { 778 return new BasicAttribute (CREATION_DATE, getCreationDate()); 779 } else if (attrID.equals(ALTERNATE_CREATION_DATE)) { 780 return new BasicAttribute (ALTERNATE_CREATION_DATE, 781 getCreationDate()); 782 } else if (attrID.equals(LAST_MODIFIED)) { 783 return new BasicAttribute (LAST_MODIFIED, 784 getLastModifiedDate()); 785 } else if (attrID.equals(ALTERNATE_LAST_MODIFIED)) { 786 return new BasicAttribute (ALTERNATE_LAST_MODIFIED, 787 getLastModifiedDate()); 788 } else if (attrID.equals(NAME)) { 789 return new BasicAttribute (NAME, getName()); 790 } else if (attrID.equals(TYPE)) { 791 return new BasicAttribute (TYPE, getResourceType()); 792 } else if (attrID.equals(ALTERNATE_TYPE)) { 793 return new BasicAttribute (ALTERNATE_TYPE, getResourceType()); 794 } else if (attrID.equals(CONTENT_LENGTH)) { 795 return new BasicAttribute (CONTENT_LENGTH, 796 new Long (getContentLength())); 797 } else if (attrID.equals(ALTERNATE_CONTENT_LENGTH)) { 798 return new BasicAttribute (ALTERNATE_CONTENT_LENGTH, 799 new Long (getContentLength())); 800 } 801 } else { 802 return attributes.get(attrID); 803 } 804 return null; 805 } 806 807 808 811 public Attribute put(Attribute attribute) { 812 if (attributes == null) { 813 try { 814 return put(attribute.getID(), attribute.get()); 815 } catch (NamingException e) { 816 return null; 817 } 818 } else { 819 return attributes.put(attribute); 820 } 821 } 822 823 824 827 public Attribute put(String attrID, Object val) { 828 if (attributes == null) { 829 return null; } else { 831 return attributes.put(attrID, val); 832 } 833 } 834 835 836 839 public Attribute remove(String attrID) { 840 if (attributes == null) { 841 return null; } else { 843 return attributes.remove(attrID); 844 } 845 } 846 847 848 851 public NamingEnumeration getAll() { 852 if (attributes == null) { 853 Vector attributes = new Vector (); 854 attributes.addElement(new BasicAttribute 855 (CREATION_DATE, getCreationDate())); 856 attributes.addElement(new BasicAttribute 857 (LAST_MODIFIED, getLastModifiedDate())); 858 attributes.addElement(new BasicAttribute (NAME, getName())); 859 attributes.addElement(new BasicAttribute (TYPE, getResourceType())); 860 attributes.addElement 861 (new BasicAttribute (CONTENT_LENGTH, 862 new Long (getContentLength()))); 863 return new RecyclableNamingEnumeration(attributes); 864 } else { 865 return attributes.getAll(); 866 } 867 } 868 869 870 873 public NamingEnumeration getIDs() { 874 if (attributes == null) { 875 Vector attributeIDs = new Vector (); 876 attributeIDs.addElement(CREATION_DATE); 877 attributeIDs.addElement(LAST_MODIFIED); 878 attributeIDs.addElement(NAME); 879 attributeIDs.addElement(TYPE); 880 attributeIDs.addElement(CONTENT_LENGTH); 881 return new RecyclableNamingEnumeration(attributeIDs); 882 } else { 883 return attributes.getIDs(); 884 } 885 } 886 887 888 891 public int size() { 892 if (attributes == null) { 893 return 5; 894 } else { 895 return attributes.size(); 896 } 897 } 898 899 900 903 public Object clone() { 904 return this; 905 } 906 907 908 911 public boolean isCaseIgnored() { 912 return false; 913 } 914 915 916 } 917 | Popular Tags |