| 1 23 24 package org.apache.webdav.lib; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.net.URL ; 33 import java.text.DateFormat ; 34 import java.text.ParseException ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Collection ; 37 import java.util.Date ; 38 import java.util.Enumeration ; 39 import java.util.Hashtable ; 40 import java.util.Locale ; 41 import java.util.TimeZone ; 42 import java.util.Vector ; 43 44 import org.apache.commons.httpclient.Credentials; 45 import org.apache.commons.httpclient.HostConfiguration; 46 import org.apache.commons.httpclient.HttpClient; 47 import org.apache.commons.httpclient.HttpException; 48 import org.apache.commons.httpclient.HttpMethod; 49 import org.apache.commons.httpclient.HttpStatus; 50 import org.apache.commons.httpclient.HttpURL; 51 import org.apache.commons.httpclient.HttpsURL; 52 import org.apache.commons.httpclient.URIException; 53 import org.apache.commons.httpclient.UsernamePasswordCredentials; 54 import org.apache.commons.httpclient.methods.GetMethod; 55 import org.apache.commons.httpclient.methods.HeadMethod; 56 import org.apache.commons.httpclient.methods.PutMethod; 57 import org.apache.commons.httpclient.util.URIUtil; 58 59 import org.apache.webdav.lib.methods.*; 60 import org.apache.webdav.lib.properties.AclProperty; 61 import org.apache.webdav.lib.properties.LockDiscoveryProperty; 62 import org.apache.webdav.lib.properties.PrincipalCollectionSetProperty; 63 import org.apache.webdav.lib.properties.ResourceTypeProperty; 64 import org.apache.webdav.lib.util.DOMUtils; 65 import org.apache.webdav.lib.util.WebdavStatus; 66 67 170 public class WebdavResource extends WebdavSession { 171 172 173 175 178 protected WebdavResource() { 179 } 180 181 182 185 protected WebdavResource(HttpClient client) { 186 super(); 187 this.client = client; 188 } 189 190 201 public WebdavResource(HttpURL httpURL, Credentials credentials, int action, 202 int depth) 203 throws HttpException, IOException { 204 205 setCredentials(credentials); 206 setHttpURL(httpURL, action, depth); 207 } 208 209 210 220 public WebdavResource(HttpURL httpURL, int action, int depth) 221 throws HttpException, IOException { 222 223 setHttpURL(httpURL, action, depth); 224 } 225 226 237 public WebdavResource(HttpURL httpURL, int action, int depth, 238 boolean followRedirects) 239 throws HttpException, IOException { 240 241 setFollowRedirects(this.followRedirects); 242 setHttpURL(httpURL, action, depth); 243 } 244 245 246 255 public WebdavResource(HttpURL httpURL, int depth) 256 throws HttpException, IOException { 257 258 setHttpURL(httpURL, defaultAction, depth); 259 260 } 261 271 public WebdavResource(HttpURL httpURL, int depth, boolean followRedirects) 272 throws HttpException, IOException { 273 274 setFollowRedirects(followRedirects); 275 setHttpURL(httpURL, defaultAction, depth); 276 } 277 278 279 287 public WebdavResource(HttpURL httpURL) 288 throws HttpException, IOException { 289 290 setHttpURL(httpURL); 291 } 292 298 public WebdavResource(HttpURL httpURL, boolean followRedirects) 299 throws HttpException, IOException { 300 301 setFollowRedirects(followRedirects); 302 setHttpURL(httpURL); 303 } 304 305 306 316 public WebdavResource(HttpURL httpURL, String proxyHost, int proxyPort) 317 throws HttpException, IOException { 318 319 setProxy(proxyHost, proxyPort); 320 setHttpURL(httpURL); 321 } 322 public WebdavResource(HttpURL httpURL, String proxyHost, int proxyPort, boolean followRedirects) 323 throws HttpException, IOException { 324 325 setFollowRedirects(followRedirects); 326 setProxy(proxyHost, proxyPort); 327 setHttpURL(httpURL); 328 } 329 330 331 342 public WebdavResource(HttpURL httpURL, String proxyHost, int proxyPort, 343 Credentials proxyCredentials) 344 throws HttpException, IOException { 345 346 setProxy(proxyHost, proxyPort); 347 setProxyCredentials(proxyCredentials); 348 setHttpURL(httpURL); 349 } 350 351 public WebdavResource(HttpURL httpURL, String proxyHost, int proxyPort, 352 Credentials proxyCredentials, boolean followRedirects) 353 throws HttpException, IOException { 354 355 setFollowRedirects(followRedirects); 356 setProxy(proxyHost, proxyPort); 357 setProxyCredentials(proxyCredentials); 358 setHttpURL(httpURL); 359 } 360 361 370 public WebdavResource(String escapedHttpURL) 371 throws HttpException, IOException { 372 373 setHttpURL(escapedHttpURL); 374 } 375 public WebdavResource(String escapedHttpURL, boolean followRedirects) 376 throws HttpException, IOException { 377 378 setFollowRedirects(followRedirects); 379 setHttpURL(escapedHttpURL); 380 } 381 382 383 393 public WebdavResource(String escapedHttpURL, Credentials credentials) 394 throws HttpException, IOException { 395 396 setCredentials(credentials); 397 setHttpURL(escapedHttpURL); 398 } 399 400 public WebdavResource(String escapedHttpURL, Credentials credentials, 401 boolean followRedirects) 402 throws HttpException, IOException { 403 404 setFollowRedirects(followRedirects); 405 setCredentials(credentials); 406 setHttpURL(escapedHttpURL); 407 } 408 409 410 421 public WebdavResource(String escapedHttpURL, String proxyHost, 422 int proxyPort) throws HttpException, IOException { 423 424 setProxy(proxyHost, proxyPort); 425 setHttpURL(escapedHttpURL); 426 } 427 428 440 public WebdavResource(String escapedHttpURL, String proxyHost, 441 int proxyPort, Credentials proxyCredentials) 442 throws HttpException, IOException { 443 444 setProxy(proxyHost, proxyPort); 445 setProxyCredentials(proxyCredentials); 446 setHttpURL(escapedHttpURL); 447 } 448 449 450 459 public WebdavResource(HttpURL httpURL, String additionalPath) 460 throws HttpException, IOException { 461 462 setHttpURL(httpURL, additionalPath); 463 } 464 465 472 public WebdavResource(HttpURL httpURL, String additionalPath, boolean followRedirects) 473 throws HttpException, IOException { 474 475 setFollowRedirects(followRedirects); 476 setHttpURL(httpURL, additionalPath); 477 } 478 479 480 482 483 486 public static final String DISPLAYNAME = "displayname"; 487 488 489 492 public static final String GETCONTENTLANGUAGE = "getcontentlanguage"; 493 494 495 498 public static final String GETCONTENTLENGTH = "getcontentlength"; 499 500 501 504 public static final String GETLASTMODIFIED = "getlastmodified"; 505 506 507 510 public static final String CREATIONDATE = "creationdate"; 511 512 513 516 public static final String RESOURCETYPE = "resourcetype"; 517 518 519 522 public static final String SOURCE = "source"; 523 524 525 528 public static final String GETCONTENTTYPE = "getcontenttype"; 529 530 531 534 public static final String GETETAG = "getetag"; 535 536 537 540 public static final String ISHIDDEN = "ishidden"; 541 542 543 546 public static final String ISCOLLECTION = "iscollection"; 547 548 549 552 public static final String SUPPORTEDLOCK = "supportedlock"; 553 554 555 558 public static final String LOCKDISCOVERY = "lockdiscovery"; 559 560 561 563 564 567 public static final int NOACTION = 1; 568 569 570 573 public static final int NAME = 2; 574 575 576 579 public static final int BASIC = 3; 580 581 582 585 public static final int DEFAULT = 4; 586 587 588 591 public static final int ALL = 5; 592 593 594 597 public static final int OPTIONS_WORKSPACE = 8; 598 599 602 public static final int OPTIONS_VERSION_HISTORY = 9; 603 604 public static final int LABEL_SET = 10; 605 public static final int LABEL_REMOVE = 11; 606 public static final int LABEL_ADD = 12; 607 608 609 612 public static final String defaultOwner = "Slide"; 613 614 615 618 public static final String TRUE = "1"; 619 620 621 624 public static final String FALSE = "0"; 625 626 627 630 public static final SimpleDateFormat formats[] = { 631 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 632 new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US), 633 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 634 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US), 635 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US), 636 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.sss'Z'", Locale.US) 637 }; 638 639 640 643 protected final static TimeZone gmtZone = TimeZone.getTimeZone("GMT"); 644 645 646 static { 647 for (int i = 0; i < formats.length; i++) { 648 formats[i].setTimeZone(gmtZone); 649 } 650 } 651 652 653 655 656 659 protected HttpURL httpURL; 660 661 662 665 protected WebdavResources childResources = new WebdavResources(); 666 667 668 671 protected static int defaultAction = BASIC; 672 673 674 677 protected static int defaultDepth = DepthSupport.DEPTH_0; 678 679 680 684 protected static String tempDirForGet; 685 686 687 691 protected static boolean useDiskForGet = true; 692 693 694 697 protected boolean thisResource; 698 699 700 703 protected Enumeration allowedMethods; 704 705 706 709 protected Enumeration davCapabilities; 710 711 712 715 protected boolean exists; 716 717 718 721 protected boolean overwrite; 722 723 724 727 protected int latestStatusCode; 728 729 730 733 protected String latestStatusMessage = ""; 734 735 736 739 protected String displayName = ""; 740 741 742 745 protected long getContentLength; 746 747 748 751 protected String getContentType = ""; 752 753 754 757 protected ResourceTypeProperty resourceType; 758 759 760 763 protected long getLastModified; 764 765 766 769 protected long creationDate; 770 771 772 775 protected String getEtag = ""; 776 777 780 protected String owner = null; 781 782 783 786 protected boolean isHidden; 787 788 789 792 protected boolean isCollection; 793 794 795 798 protected String supportedLock = ""; 799 800 801 804 protected LockDiscoveryProperty lockDiscovery; 805 806 protected boolean followRedirects = false; 807 808 810 814 protected void generateTransactionHeader(HttpMethod method) { 815 if (client == null || method == null) return; 816 817 WebdavState state = (WebdavState) client.getState(); 818 String txHandle = state.getTransactionHandle(); 819 if (txHandle != null) { 820 method.setRequestHeader("Transaction", "<" + txHandle + ">"); 821 } 822 } 823 824 827 protected void generateIfHeader(HttpMethod method) { 828 829 if (client == null) return; 830 if (method == null) return; 831 832 WebdavState state = (WebdavState) client.getState(); 833 String [] lockTokens = state.getAllLocks(method.getPath()); 834 835 if (lockTokens.length == 0) return; 836 837 StringBuffer ifHeaderValue = new StringBuffer (); 838 839 for (int i = 0; i < lockTokens.length; i++) { 840 ifHeaderValue.append("(<").append(lockTokens[i]).append(">) "); 841 } 842 843 method.setRequestHeader("If", ifHeaderValue.toString()); 844 845 } 846 847 848 853 protected Date parseDate(String dateValue) { 854 Date date = null; 856 for (int i = 0; (date == null) && (i < formats.length); i++) { 857 try { 858 synchronized (formats[i]) { 859 date = formats[i].parse(dateValue); 860 } 861 } catch (ParseException e) { 862 } 863 } 864 865 return date; 866 } 867 868 869 874 protected void setNameProperties(int depth) 875 throws HttpException, IOException { 876 877 Vector properties = new Vector (); 878 properties.addElement(DISPLAYNAME); 879 880 setNamedProp(depth, properties); 881 } 882 883 884 901 protected void setBasicProperties(int depth) 902 throws HttpException, IOException { 903 904 Vector properties = new Vector (); 905 properties.addElement(DISPLAYNAME); 906 properties.addElement(GETCONTENTLENGTH); 907 properties.addElement(GETCONTENTTYPE); 908 properties.addElement(RESOURCETYPE); 909 properties.addElement(GETLASTMODIFIED); 910 properties.addElement(LOCKDISCOVERY); 911 912 setNamedProp(depth, properties); 913 } 914 915 916 938 protected void setDefaultProperties(int depth) 939 throws HttpException, IOException { 940 941 Vector properties = new Vector (); 942 properties.addElement(CREATIONDATE); 943 properties.addElement(DISPLAYNAME); 944 properties.addElement(GETCONTENTLANGUAGE); 945 properties.addElement(GETCONTENTLENGTH); 946 properties.addElement(GETCONTENTTYPE); 947 properties.addElement(GETETAG); 948 properties.addElement(GETLASTMODIFIED); 949 properties.addElement(LOCKDISCOVERY); 950 properties.addElement(RESOURCETYPE); 951 properties.addElement(SOURCE); 952 properties.addElement(SUPPORTEDLOCK); 953 954 setNamedProp(depth, properties); 955 } 956 957 958 964 protected void setNamedProp(int depth, Vector propertyNames) 965 throws HttpException, IOException { 966 967 Enumeration responses = propfindMethod(depth, propertyNames); 968 setWebdavProperties(responses); 969 } 970 971 972 977 protected void setAllProp(int depth) 978 throws HttpException, IOException { 979 980 Enumeration responses = propfindMethod(depth); 981 setWebdavProperties(responses); 982 } 983 984 985 |