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 995 protected void setWebdavProperties(Enumeration responses) 996 throws HttpException, IOException { 997 998 childResources.removeAll(); 1000 while (responses.hasMoreElements()) { 1001 1002 ResponseEntity response = 1003 (ResponseEntity) responses.nextElement(); 1004 1005 boolean itself = false; 1006 String href = response.getHref(); 1007 if (!href.startsWith("/")) 1008 href = URIUtil.getPath(href); 1009 href = decodeMarks(href); 1010 1011 1015 String httpURLPath = httpURL.getPath(); 1016 String escapedHref = URIUtil.decode(href); 1017 1018 int lenDiff = escapedHref.length() - httpURLPath.length(); 1020 int compareLen = 0; 1021 1022 if ( lenDiff == -1 && !escapedHref.endsWith("/")) { 1023 compareLen = escapedHref.length(); 1024 lenDiff = 0; 1025 } 1026 else 1027 if ( lenDiff == 1 && !httpURLPath.endsWith("/")) { 1028 compareLen = httpURLPath.length(); 1029 lenDiff = 0; 1030 } 1031 1032 if (lenDiff == 0) { 1034 if ((compareLen == 0 && httpURLPath.equals(escapedHref)) 1035 || httpURLPath.regionMatches(0, escapedHref, 0, compareLen)) 1036 { 1037 if (response.getStatusCode() > 0) 1040 setStatusCode(response.getStatusCode()); 1041 setExistence(true); 1042 itself = true; 1043 } 1044 } 1045 1046 WebdavResource workingResource = null; 1048 if (itself) { 1049 workingResource = this; 1050 } 1051 else { 1052 workingResource = createWebdavResource(client); 1053 workingResource.setDebug(debug); 1054 } 1055 1056 workingResource.setLockDiscovery(null); 1058 1059 Enumeration properties = response.getProperties(); 1061 while (properties.hasMoreElements()) { 1062 1063 Property property = (Property) properties.nextElement(); 1064 1065 workingResource.processProperty(property); 1067 } 1068 1069 String displayName = workingResource.getDisplayName(); 1070 1071 if (displayName == null || displayName.trim().equals("")) { 1072 displayName = getName(href); 1073 } 1074 if (!itself) { 1075 String myURI = httpURL.getEscapedURI(); 1076 char[] childURI = (myURI + (myURI.endsWith("/") ? "" : "/") 1077 + URIUtil.getName(href)).toCharArray(); 1078 HttpURL childURL = httpURL instanceof HttpsURL 1079 ? new HttpsURL(childURI) 1080 : new HttpURL(childURI); 1081 childURL.setRawAuthority(httpURL.getRawAuthority()); 1082 workingResource.setHttpURL(childURL, NOACTION, defaultDepth); 1083 workingResource.setExistence(true); 1084 workingResource.setOverwrite(getOverwrite()); 1085 } 1086 workingResource.setDisplayName(displayName); 1087 1088 if (!itself) 1089 childResources.addResource(workingResource); 1090 } 1091 } 1092 1093 1094 1096 1097 1115 public static void setDefaultAction(int action) { 1116 defaultAction = action; 1117 } 1118 1119 1120 1125 public static int getDefaultAction() { 1126 return defaultAction; 1127 } 1128 1129 1130 1140 public static void setDefaultDepth(int depth) { 1141 defaultDepth = depth; 1142 } 1143 1144 1145 1150 public static int getDefaultDepth() { 1151 return defaultDepth; 1152 } 1153 1154 1155 1161 public static void setGetTempDir(String tempDir) { 1162 tempDirForGet = tempDir; 1163 } 1164 1165 1166 1174 public static String getGetTempDir() { 1175 return tempDirForGet; 1176 } 1177 1178 1179 1180 1186 public static void setGetUseDisk(boolean useDisk) { 1187 } 1189 1190 1191 1198 public static boolean getGetUseDisk() { 1199 return false; 1200 } 1201 1202 1206 public void setFollowRedirects(boolean value) { 1207 this.followRedirects = value; 1208 } 1209 1213 public boolean getFollowRedirects() { 1214 return this.followRedirects; 1215 } 1216 1217 1218 1223 protected synchronized boolean isTheClient() throws URIException { 1224 HostConfiguration hostConfig = client.getHostConfiguration(); 1225 Credentials creds = 1226 client.getState().getCredentials(null, hostConfig.getHost()); 1227 String userName = null; 1228 String password = null; 1229 1230 if (creds instanceof UsernamePasswordCredentials) { 1231 UsernamePasswordCredentials upc = (UsernamePasswordCredentials) creds; 1232 userName = upc.getUserName(); 1233 password = upc.getPassword(); 1234 } 1235 String ref = httpURL.getUser(); 1236 boolean userMatches = userName != null ? userName.equals(ref) 1237 : ref == null; 1238 if (userMatches) { 1239 ref = httpURL.getPassword(); 1240 userMatches = password != null ? password.equals(ref) 1241 : ref == null; 1242 } else { 1243 return false; 1244 } 1245 if (userMatches) { 1246 return httpURL.getHost().equalsIgnoreCase(hostConfig.getHost()) 1247 && httpURL.getPort() 1248 == hostConfig.getProtocol().resolvePort(hostConfig.getPort()); 1249 } 1250 return false; 1251 } 1252 1253 1254 1259 protected void setClient() throws IOException { 1260 setClient(httpURL); 1261 } 1262 1263 1264 1270 protected synchronized void setClient(HttpURL httpURL) throws IOException { 1271 1272 if (client == null) { 1273 client = getSessionInstance(httpURL); 1274 } else if (!isTheClient()) { 1275 closeSession(); 1276 client = getSessionInstance(httpURL); 1277 } 1278 } 1279 1280 1281 1294 public void setHttpURL(HttpURL httpURL, int action, int depth) 1295 throws HttpException, IOException { 1296 1297 this.httpURL = httpURL; 1298 setClient(httpURL); 1299 setExistence(false); 1301 setProperties(action, depth); 1302 } 1303 1304 1305 1316 public void setHttpURL(HttpURL httpURL, int depth) 1317 throws HttpException, IOException { 1318 1319 setHttpURL(httpURL, defaultAction, depth); 1321 } 1322 1323 1324 1339 public void setHttpURL 1340 (HttpURL httpURL, String additionalPath, int action, int depth) 1341 throws HttpException, IOException { 1342 1343 setHttpURL(httpURL instanceof HttpsURL 1344 ? new HttpsURL((HttpsURL) httpURL, additionalPath) 1345 : new HttpURL(httpURL, additionalPath), action, depth); 1346 } 1347 1348 1349 1363 public void setHttpURL 1364 (HttpURL httpURL, String additionalPath, int action) 1365 throws HttpException, IOException { 1366 1367 setHttpURL(httpURL instanceof HttpsURL 1368 ? new HttpsURL((HttpsURL) httpURL, additionalPath) 1369 : new HttpURL(httpURL, additionalPath), 1370 action, defaultDepth); 1371 } 1372 1373 1374 1385 public void setHttpURL(HttpURL httpURL, String additionalPath) 1386 throws HttpException, IOException { 1387 1388 setHttpURL(httpURL instanceof HttpsURL 1389 ? new HttpsURL((HttpsURL) httpURL, additionalPath) 1390 : new HttpURL(httpURL, additionalPath), 1391 defaultAction, defaultDepth); 1392 } 1393 1394 1395 1405 public void setHttpURL(HttpURL httpURL) 1406 throws HttpException, IOException { 1407 1408 setHttpURL(httpURL, defaultDepth); 1409 } 1410 1411 1412 1423 public void setHttpURL(String escapedHttpURL) 1424 throws HttpException, IOException { 1425 1426 setHttpURL(escapedHttpURL.startsWith("https") 1427 ? new HttpsURL(escapedHttpURL) 1428 : new HttpURL(escapedHttpURL)); 1429 } 1430 1431 1432 1437 public HttpURL getHttpURL() { 1438 return httpURL; 1439 } 1440 1441 1442 1447 public HttpURL getHttpURLExceptForUserInfo() 1448 throws URIException { 1449 1450 return httpURL instanceof HttpsURL ? new HttpsURL(httpURL.getRawURI()) 1451 : new HttpURL(httpURL.getRawURI()); 1452 } 1453 1454 1455 1465 public void setPath(String path) 1466 throws HttpException, IOException { 1467 1468 httpURL.setPath(path); 1469 setHttpURL(httpURL); 1470 } 1471 1472 1473 1482 public String getPath() { 1483 try { 1484 return httpURL.getPath(); 1485 } catch (URIException e) { 1486 return httpURL.getEscapedPath(); 1487 } 1488 } 1489 1490 1491 1499 public String getName() { 1500 return getName(httpURL.getEscapedPath()); 1501 } 1502 1503 1504 1510 public String getHost() throws URIException { 1511 return httpURL.getHost(); 1512 } 1513 1514 1515 1524 public void setUserInfo(String userName, String password) 1525 throws HttpException, IOException { 1526 1527 httpURL.setUserinfo(userName, password); 1528 setHttpURL(httpURL); 1529 } 1530 1531 1532 1534 1535 1540 public String getDisplayName() { 1541 return displayName; 1542 } 1543 1544 1545 1550 protected void setDisplayName(String displayName) { 1551 this.displayName = displayName; 1552 } 1553 1554 1555 1560 public long getGetContentLength() { 1561 return getContentLength; 1562 } 1563 1564 1565 1570 protected void setGetContentLength(long getContentLength) { 1571 this.getContentLength = getContentLength; 1572 } 1573 1574 1575 1580 protected void setGetContentLength(String getContentLength) { 1581 try { 1582 this.getContentLength = Long.parseLong(getContentLength); 1583 } catch (NumberFormatException nfe) { 1584 } 1586 } 1587 1588 1589 1595 public ResourceTypeProperty getResourceType() { 1596 return resourceType; 1597 } 1598 1599 1600 1605 protected void setResourceType(ResourceTypeProperty resourceType) { 1606 this.resourceType = resourceType; 1607 } 1608 1609 1610 1617 public boolean isCollection() { 1618 if (getResourceType() == null) return false; 1619 return getResourceType().isCollection(); 1620 } 1621 1622 1623 1628 public String getGetContentType() { 1629 return getContentType; 1630 } 1631 1632 1633 1638 protected void setGetContentType(String getContentType) { 1639 this.getContentType = getContentType; 1640 } 1641 1642 1646 public void setContentType(String contentType) { 1647 this.getContentType = contentType; 1648 } 1649 1650 1655 public long getGetLastModified() { 1656 return getLastModified; 1657 } 1658 1659 1660 1666 protected void setGetLastModified(long getLastModified) { 1667 this.getLastModified = getLastModified; 1668 } 1669 1670 1671 1677 protected void setGetLastModified(String getLastModified) { 1678 Date date = parseDate(getLastModified); 1679 if (date != null) 1680 this.getLastModified = date.getTime(); 1681 } 1682 1683 1684 1689 public long getCreationDate() { 1690 return creationDate; 1691 } 1692 1693 1694 1699 protected void setCreationDate(long creationDate) { 1700 this.creationDate = creationDate; 1701 } 1702 1703 1704 1709 protected void setCreationDate(String creationDate) { 1710 Date date = parseDate(creationDate); 1711 if (date != null) 1712 this.creationDate = date.getTime(); 1713 } 1714 1715 1716 1721 public String getGetEtag() { 1722 return getEtag; 1723 } 1724 1725 1726 1731 protected void setGetEtag(String getEtag) { 1732 this.getEtag = getEtag; 1733 } 1734 1735 1738 public String getOwner() { 1739 return owner; 1740 } 1741 1742 1747 public String getSupportedLock() { 1748 return supportedLock; 1749 } 1750 1751 1752 1757 protected void setSupportedLock(String supportedLock) { 1758 this.supportedLock = supportedLock; 1759 } 1760 1761 1762 1767 public LockDiscoveryProperty getLockDiscovery() { 1768 return lockDiscovery; 1769 } 1770 1771 1776 protected void setLockDiscovery(LockDiscoveryProperty lockDiscovery) { 1777 this.lockDiscovery = lockDiscovery; 1778 } 1779 1780 1781 1786 public Enumeration getActiveLockOwners() { 1787 if (lockDiscovery == null) return null; 1788 Lock[] activeLocks = lockDiscovery.getActiveLocks(); 1789 if (activeLocks == null) return null; 1790 Vector buff = new Vector (); 1791 int count = activeLocks.length; 1792 for (int i = 0; i < count; i++) { 1793 buff.addElement(activeLocks[i].getOwner()); 1794 } 1795 return buff.elements(); 1796 } 1797 1798 1799 1804 public boolean isLocked() { 1805 if (lockDiscovery == null) return false; 1806 Lock[] activeLocks = lockDiscovery.getActiveLocks(); 1807 if (activeLocks == null) return false; 1808 for (int i = 0; i < activeLocks.length; i++) { 1809 if (activeLocks[i].getLockType() == Lock.TYPE_WRITE) return true; 1810 } 1811 return false; 1812 } 1813 1814 1815 1820 public boolean getIsHidden() { 1821 return isHidden; 1822 } 1823 1824 1825 1830 protected void setIsHidden(boolean isHidden) { 1831 this.isHidden = isHidden; 1832 } 1833 1834 1835 1840 protected void setIsHidden(String isHidden) { 1841 this.isHidden = isHidden.equals(TRUE) ? true : false; 1842 } 1843 1844 1845 1851 public boolean getIsCollection() { 1852 return isCollection; 1853 } 1854 1855 1856 1861 protected void setIsCollection(boolean isCollection) { 1862 this.isCollection = isCollection; 1863 } 1864 1865 1866 1871 protected void setIsCollection(String isCollection) { 1872 this.isCollection = isCollection.equals(TRUE) ? true : false; 1873 } 1874 1875 1876 1878 1879 1886 public void setProperties(int action, int depth) 1887 throws HttpException, IOException { 1888 1889 switch (action) { 1890 case NAME: 1891 setNameProperties(depth); 1892 break; 1893 case BASIC: 1894 setBasicProperties(depth); 1895 break; 1896 case DEFAULT: 1897 setDefaultProperties(depth); 1898 break; 1899 case ALL: 1900 setAllProp(depth); 1901 break; 1902 case NOACTION: 1903 default: 1904 break; 1905 } 1906 } 1907 1908 1909 1914 public void setProperties(int depth) 1915 throws HttpException, IOException { 1916 1917 setProperties(defaultAction, depth); 1918 } 1919 1920 1924 protected void refresh() throws HttpException, IOException { 1925 int latestStatusCode = this.latestStatusCode; 1926 String latestStatusMessage = this.latestStatusMessage; 1927 setProperties(DepthSupport.DEPTH_0); 1928 this.latestStatusCode = latestStatusCode; 1929 this.latestStatusMessage = latestStatusMessage; 1930 } 1931 1932 1944 public boolean exists() { 1945 return getExistence(); 1946 } 1947 1948 1949 1954 protected void setExistence(boolean exists) { 1955 this.exists = exists; 1956 } 1957 1958 1959 1969 public boolean getExistence() { 1970 return exists; 1971 } 1972 1973 1974 1981 public void setOverwrite(boolean overwrite) { 1982 this.overwrite = overwrite; 1983 } 1984 1985 1986 1993 public boolean getOverwrite() { 1994 return overwrite; 1995 } 1996 1997 1998 2001 public void close() throws IOException { 2002 closeSession(); 2003 } 2004 2005 2006 2011 public String getStatusMessage() { 2012 return latestStatusMessage; 2013 } 2014 2015 2016 2021 public int getStatusCode() { 2022 return latestStatusCode; 2023 } 2024 2025 2026 2031 protected void setStatusCode(int statusCode) { 2032 setStatusCode(statusCode, null); 2033 } 2034 2035 2036 2042 protected void setStatusCode(int statusCode, String message) { 2043 2044 latestStatusCode = statusCode; 2045 latestStatusMessage = WebdavStatus.getStatusText(statusCode) + 2046 " (" + statusCode + ")" + ((message == null) ? "" : message); 2047 } 2048 2049 2050 2056 public Enumeration getAllowedMethods() { 2057 return allowedMethods; 2058 } 2059 2060 2061 2067 public Enumeration getDavCapabilities() { 2068 return davCapabilities; 2069 } 2070 2071 2072 2079 public WebdavResources getChildResources() 2080 throws HttpException, IOException { 2081 2082 setProperties(DepthSupport.DEPTH_1); 2083 2084 return childResources; 2085 } 2086 2087 2088 2096 public WebdavResource[] listWebdavResources() 2097 throws HttpException, IOException { 2098 2099 return getChildResources().listResources(); 2100 } 2101 2102 2103 2110 public String [] list() { 2111 2112 try { 2113 setNameProperties(DepthSupport.DEPTH_1); 2114 } catch (IOException e) { 2115 return null; 2116 } 2117 Enumeration hrefs = childResources.getResourceNames(); 2118 2119 Vector hrefList = new Vector (); 2121 while (hrefs.hasMoreElements()) { 2122 hrefList.addElement((String ) hrefs.nextElement()); 2123 } 2124 int num = hrefList.size(); 2126 String [] pathnames = new String [num]; 2127 for (int i = 0; i < num; i++) { 2128 pathnames[i] = (String ) hrefList.elementAt(i); 2129 } 2130 2131 return pathnames; 2132 } 2133 2134 2135 2149 public Vector listBasic() 2150 throws HttpException, IOException { 2151 2152 setBasicProperties(DepthSupport.DEPTH_1); 2153 Enumeration hrefs = childResources.getResourceNames(); 2154 2155 Vector hrefList = new Vector (); 2156 while (hrefs.hasMoreElements()) { 2157 try { 2158 String resourceName = (String ) hrefs.nextElement(); 2159 WebdavResource currentResource = 2160 childResources.getResource(resourceName); 2161 2162 String [] longFormat = new String [5]; 2163 longFormat[0] = currentResource.getDisplayName(); 2165 2166 2167 long length = currentResource.getGetContentLength(); 2168 longFormat[1] = new Long (length).toString(); 2170 ResourceTypeProperty resourceTypeProperty = 2172 currentResource.getResourceType(); 2173 String getContentType = 2175 currentResource.getGetContentType(); 2176 longFormat[2] = resourceTypeProperty.isCollection() ? 2177 "COLLECTION" : getContentType ; 2178 Date date = new Date (currentResource.getGetLastModified()); 2179 longFormat[3] = (date == null) ? "-- -- ----" : 2182 DateFormat.getDateTimeInstance().format(date); 2184 hrefList.addElement(longFormat); 2185 2186 longFormat[4] = currentResource.getName(); 2188 2189 2190 } catch (Exception e) { 2191 if (debug > 0) 2193 e.printStackTrace(); 2194 } 2196 } 2197 2198 return hrefList; 2199 } 2200 2201 2202 2211 public void setEncodeURLs(boolean encodeURLs) { 2212 2213 } 2214 2215 2216 2218 2219 2225 public HttpClient retrieveSessionInstance() 2226 throws IOException { 2227 2228 setClient(); 2229 return client; 2230 } 2231 2232 2233 2241 public int executeHttpRequestMethod(HttpClient client, HttpMethod method) 2242 throws IOException , HttpException { 2243 2244 client.executeMethod(method); 2245 return method.getStatusCode(); 2246 } 2247 2248 2249 2251 2259 public boolean aclMethod(String path, Ace[] aces) 2260 throws HttpException, IOException { 2261 2262 setClient(); 2263 2264 AclMethod method = new AclMethod(URIUtil.encodePath(path)); 2265 method.setDebug(debug); 2266 method.setFollowRedirects(this.followRedirects); 2267 2268 generateIfHeader(method); 2269 for (int i=0; i<aces.length ; i++) { 2270 Ace ace = aces[i]; 2271 method.addAce(ace); 2272 } 2273 2274 generateTransactionHeader(method); 2275 int statusCode = client.executeMethod(method); 2276 2277 setStatusCode(statusCode); 2278 2279 return (statusCode >= 200 && statusCode < 300) ? true : false; 2280 } 2281 2282 2283 2289 public AclProperty aclfindMethod() throws HttpException, IOException { 2290 thisResource = true; 2291 return aclfindMethod(httpURL.getPath()); 2292 } 2293 2294 2295 2302 public AclProperty aclfindMethod(String path) 2303 throws HttpException, IOException { 2304 2305 setClient(); 2306 2307 AclProperty acl = null; 2308 2309 Vector properties = new Vector (); 2310 properties.addElement(AclProperty.TAG_NAME); 2311 2312 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 2314 DepthSupport.DEPTH_0, 2315 properties.elements()); 2316 method.setDebug(debug); 2317 method.setFollowRedirects(this.followRedirects); 2318 2319 generateTransactionHeader(method); 2320 client.executeMethod(method); 2321 2322 Enumeration responses = method.getResponses(); 2323 if (responses.hasMoreElements()) { 2324 ResponseEntity response = 2325 (ResponseEntity) responses.nextElement(); 2326 String href = response.getHref(); 2327 2328 if ((thisResource == true) && (response.getStatusCode() > 0)) 2330 setStatusCode(response.getStatusCode()); 2331 thisResource = false; 2332 2333 Enumeration responseProperties = 2334 method.getResponseProperties(href); 2335 while (responseProperties.hasMoreElements()) { 2336 Property property = 2337 (Property) responseProperties.nextElement(); 2338 if (property instanceof AclProperty) { 2339 acl = (AclProperty)property; 2340 } 2341 2342 } 2343 } 2344 2345 return acl; 2346 } 2347 2348 2349 2356 public PrincipalCollectionSetProperty principalCollectionSetFindMethod() 2357 throws HttpException, IOException { 2358 thisResource = true; 2359 return principalCollectionSetFindMethod(httpURL.getPath()); 2360 } 2361 2362 2369 public PrincipalCollectionSetProperty principalCollectionSetFindMethod( 2370 String path) throws HttpException, IOException { 2371 2372 setClient(); 2373 2374 PrincipalCollectionSetProperty set = null; 2375 2376 Vector properties = new Vector (); 2377 properties.addElement(PrincipalCollectionSetProperty.TAG_NAME); 2378 2379 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 2381 DepthSupport.DEPTH_0, 2382 properties.elements()); 2383 method.setDebug(debug); 2384 method.setFollowRedirects(this.followRedirects); 2385 generateTransactionHeader(method); 2386 client.executeMethod(method); 2387 2388 Enumeration responses = method.getResponses(); 2389 if (responses.hasMoreElements()) { 2390 ResponseEntity response = 2391 (ResponseEntity) responses.nextElement(); 2392 String href = response.getHref(); 2393 2394 if ((thisResource == true) && (response.getStatusCode() > 0)) 2396 setStatusCode(response.getStatusCode()); 2397 thisResource = false; 2398 2399 Enumeration responseProperties = 2400 method.getResponseProperties(href); 2401 while (responseProperties.hasMoreElements()) { 2402 Property property = 2403 (Property) responseProperties.nextElement(); 2404 if (property instanceof PrincipalCollectionSetProperty) { 2405 set = (PrincipalCollectionSetProperty)property; 2406 } 2407 2408 } 2409 } 2410 2411 return set; 2412 } 2413 2414 2415 2420 public LockDiscoveryProperty lockDiscoveryPropertyFindMethod() 2421 throws HttpException, IOException { 2422 thisResource = true; 2423 return lockDiscoveryPropertyFindMethod(httpURL.getPath()); 2424 } 2425 2426 2427 2433 public LockDiscoveryProperty lockDiscoveryPropertyFindMethod(String path) 2434 throws HttpException, IOException { 2435 2436 setClient(); 2437 2438 LockDiscoveryProperty set = null; 2439 2440 Vector properties = new Vector (); 2441 properties.addElement(LockDiscoveryProperty.TAG_NAME); 2442 2443 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 2445 DepthSupport.DEPTH_0, 2446 properties.elements()); 2447 method.setDebug(debug); 2448 method.setFollowRedirects(this.followRedirects); 2449 generateTransactionHeader(method); 2450 client.executeMethod(method); 2451 2452 Enumeration responses = method.getResponses(); 2453 if (responses.hasMoreElements()) { 2454 ResponseEntity response = 2455 (ResponseEntity) responses.nextElement(); 2456 String href = response.getHref(); 2457 2458 if ((thisResource == true) && (response.getStatusCode() > 0)) 2460 setStatusCode(response.getStatusCode()); 2461 thisResource = false; 2462 2463 Enumeration responseProperties = 2464 method.getResponseProperties(href); 2465 while (responseProperties.hasMoreElements()) { 2466 Property property = 2467 (Property) responseProperties.nextElement(); 2468 if (property instanceof LockDiscoveryProperty) { 2469 set = (LockDiscoveryProperty)property; 2470 } 2471 2472 } 2473 } 2474 2475 return set; 2476 } 2477 2478 2479 2486 public InputStream getMethodData() 2487 throws HttpException, IOException { 2488 2489 return getMethodData(httpURL.getPathQuery()); 2490 } 2491 2492 2493 2501 public InputStream getMethodData(String path) 2502 throws HttpException, IOException { 2503 2504 setClient(); 2505 2506 GetMethod method = new GetMethod(URIUtil.encodePathQuery(path)); 2507 2508 generateTransactionHeader(method); 2509 client.executeMethod(method); 2510 2511 int statusCode = method.getStatusLine().getStatusCode(); 2512 setStatusCode(statusCode); 2513 2514 if(statusCode >= 200 && statusCode < 300) 2515 return method.getResponseBodyAsStream(); 2516 else 2517 throw new IOException ("Couldn't get file"); 2518 } 2519 2520 2521 2528 public String getMethodDataAsString() 2529 throws HttpException, IOException { 2530 2531 return getMethodDataAsString(httpURL.getPathQuery()); 2532 } 2533 2534 2535 2543 public String getMethodDataAsString(String path) 2544 throws HttpException, IOException { 2545 2546 setClient(); 2547 GetMethod method = new GetMethod(URIUtil.encodePathQuery(path)); 2548 generateTransactionHeader(method); 2549 int statusCode = client.executeMethod(method); 2550 2551 setStatusCode(statusCode); 2552 2553 return method.getResponseBodyAsString(); 2554 } 2555 2556 2557 2565 public boolean getMethod(File file) 2566 throws HttpException, IOException { 2567 2568 return getMethod(httpURL.getPathQuery(), file); 2569 } 2570 2571 2572 2581 public boolean getMethod(String path, File file) 2582 throws HttpException, IOException { 2583 2584 setClient(); 2585 GetMethod method = new GetMethod(URIUtil.encodePathQuery(path)); 2586 2587 generateTransactionHeader(method); 2588 int statusCode = client.executeMethod(method); 2589 2590 setStatusCode(statusCode); 2591 2592 if (statusCode >= 200 && statusCode < 300) { 2594 2595 InputStream inStream = method.getResponseBodyAsStream(); 2598 2599 FileOutputStream fos = new FileOutputStream (file); 2600 byte buffer[] = new byte[65535]; 2601 int bytesRead; 2602 while ((bytesRead = inStream.read(buffer)) >= 0) { 2603 fos.write(buffer, 0, bytesRead); 2604 } 2605 inStream.close(); 2606 fos.close(); 2607 2608 return true; 2609 2610 } else { 2611 return false; 2612 2613 } 2614 2615 2616 } 2617 2618 2619 2627 public boolean putMethod(byte[] data) 2628 throws HttpException, IOException { 2629 2630 boolean result = putMethod(httpURL.getPathQuery(), data); 2631 if (result) refresh(); 2632 return result; 2633 } 2634 2635 2636 2645 public boolean putMethod(String path, byte[] data) 2646 throws HttpException, IOException { 2647 2648 setClient(); 2649 PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); 2650 generateIfHeader(method); 2651 if (getGetContentType() != null && !getGetContentType().equals("")) 2652 method.setRequestHeader("Content-Type", getGetContentType()); 2653 2654 method.setRequestHeader("Content-Length", String.valueOf(data.length)); 2655 method.setRequestBody(new ByteArrayInputStream (data)); 2656 2657 generateTransactionHeader(method); 2658 int statusCode = client.executeMethod(method); 2659 2660 setStatusCode(statusCode); 2661 return (statusCode >= 200 && statusCode < 300) ? true : false; 2662 } 2663 2664 2665 2673 public boolean putMethod(InputStream is) 2674 throws HttpException, IOException { 2675 2676 return putMethod(httpURL.getPathQuery(), is); 2677 } 2678 2679 2680 2689 public boolean putMethod(String path, InputStream is) 2690 throws HttpException, IOException { 2691 2692 setClient(); 2693 PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); 2694 generateIfHeader(method); 2695 if (getGetContentType() != null && !getGetContentType().equals("")) 2696 method.setRequestHeader("Content-Type", getGetContentType()); 2697 method.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED); 2698 method.setRequestBody(is); 2699 generateTransactionHeader(method); 2700 int statusCode = client.executeMethod(method); 2701 2702 setStatusCode(statusCode); 2703 return (statusCode >= 200 && statusCode < 300) ? true : false; 2704 } 2705 2706 2707 2715 public boolean putMethod(String data) 2716 throws HttpException, IOException { 2717 2718 boolean result = putMethod(httpURL.getPathQuery(), data); 2719 if (result) refresh(); 2720 2721 return result; 2722 } 2723 2724 2725 2734 public boolean putMethod(String path, String data) 2735 throws HttpException, IOException { 2736 2737 setClient(); 2738 PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); 2739 generateIfHeader(method); 2740 if (getGetContentType() != null && !getGetContentType().equals("")) 2741 method.setRequestHeader("Content-Type", getGetContentType()); 2742 method.setRequestBody(data); 2743 generateTransactionHeader(method); 2744 int statusCode = client.executeMethod(method); 2745 2746 setStatusCode(statusCode); 2747 return (statusCode >= 200 && statusCode < 300) ? true : false; 2748 } 2749 2750 2751 2759 public boolean putMethod(File file) 2760 throws HttpException, IOException { 2761 2762 boolean result = putMethod(httpURL.getPathQuery(), file); 2763 if (result) refresh(); 2764 2765 return result; 2766 } 2767 2768 2769 2778 public boolean putMethod(String path, File file) 2779 throws HttpException, IOException { 2780 2781 setClient(); 2782 PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); 2783 generateIfHeader(method); 2784 if (getGetContentType() != null && !getGetContentType().equals("")) 2785 method.setRequestHeader("Content-Type", getGetContentType()); 2786 long fileLength = file.length(); 2787 method.setRequestContentLength(fileLength <= Integer.MAX_VALUE 2788 ? (int) fileLength 2789 : PutMethod.CONTENT_LENGTH_CHUNKED); 2790 method.setRequestBody(new FileInputStream (file)); 2791 generateTransactionHeader(method); 2792 int statusCode = client.executeMethod(method); 2793 2794 setStatusCode(statusCode); 2795 return (statusCode >= 200 && statusCode < 300) ? true : false; 2796 } 2797 2798 2799 2800 2810 public boolean putMethod(URL url) 2811 throws HttpException, IOException { 2812 2813 boolean result = putMethod(httpURL.getPathQuery(), url); 2814 if (result) refresh(); 2815 2816 return result; 2817 } 2818 2819 2820 2829 public boolean putMethod(String path, URL url) 2830 throws HttpException, IOException { 2831 2832 setClient(); 2833 PutMethod method = new PutMethod(URIUtil.encodePathQuery(path)); 2834 generateIfHeader(method); 2835 if (getGetContentType() != null && !getGetContentType().equals("")) 2836 method.setRequestHeader("Content-Type", getGetContentType()); 2837 method.setRequestBody(url.openStream()); 2838 generateTransactionHeader(method); 2839 int statusCode = client.executeMethod(method); 2840 2841 setStatusCode(statusCode); 2842 return (statusCode >= 200 && statusCode < 300) ? true : false; 2843 } 2844 2845 2846 2853 public boolean optionsMethod() 2854 throws HttpException, IOException { 2855 2856 return optionsMethod(httpURL.getPath()); 2857 } 2858 2859 2860 2869 public boolean optionsMethod(String path) 2870 throws HttpException, IOException { 2871 2872 setClient(); 2873 OptionsMethod method; 2874 if (path.trim().equals("*")) 2875 method = new OptionsMethod("*"); 2876 else 2877 method = new OptionsMethod(URIUtil.encodePath(path)); 2878 2879 method.setDebug(debug); 2880 method.setFollowRedirects(this.followRedirects); 2881 generateTransactionHeader(method); 2882 int statusCode = client.executeMethod(method); 2883 2884 setStatusCode(statusCode); 2885 2886 if (statusCode >= 200 && statusCode < 300) { 2887 allowedMethods = method.getAllowedMethods(); 2889 davCapabilities = method.getDavCapabilities(); 2891 return true; 2892 } 2893 2894 return false; 2895 } 2896 2897 2898 2907 public boolean optionsMethod(String path, String aMethod) 2908 throws HttpException, IOException { 2909 2910 if (aMethod != null && optionsMethod(path)) { 2911 while (allowedMethods.hasMoreElements()) { 2912 if (aMethod.equalsIgnoreCase((String ) 2913 allowedMethods.nextElement())) 2914 return true; 2915 } 2916 } 2917 2918 return false; 2919 } 2920 2921 2922 2930 public Enumeration optionsMethod(HttpURL httpURL) 2931 throws HttpException, IOException { 2932 2933 HttpClient client = getSessionInstance(httpURL, true); 2934 2935 OptionsMethod method = new OptionsMethod(httpURL.getEscapedPath()); 2936 method.setDebug(debug); 2937 method.setFollowRedirects(this.followRedirects); 2938 2939 generateTransactionHeader(method); 2940 client.executeMethod(method); 2941 2942 Vector options = new Vector (); 2943 int statusCode = method.getStatusLine().getStatusCode(); 2944 if (statusCode >= 200 && statusCode < 300) { 2945 Enumeration allowedMethods = method.getAllowedMethods(); 2947 while (allowedMethods.hasMoreElements()) { 2948 options.addElement(allowedMethods.nextElement()); 2949 } 2950 Enumeration davCapabilities = method.getDavCapabilities(); 2952 while (davCapabilities.hasMoreElements()) { 2953 options.addElement(davCapabilities.nextElement()); 2954 } 2955 Enumeration responses = method.getResponses(); 2956 if (responses.hasMoreElements()) { 2957 ResponseEntity response = 2958 (ResponseEntity) responses.nextElement(); 2959 Enumeration workspaces = response.getWorkspaces(); 2960 String sResult=""; 2961 while (workspaces.hasMoreElements()){ 2962 sResult += workspaces.nextElement().toString(); 2963 } 2964 Enumeration histories = response.getHistories(); 2965 while (histories.hasMoreElements()){ 2966 sResult += histories.nextElement().toString(); 2967 } 2968 if ((thisResource == true) && (response.getStatusCode() > 0)) 2970 setStatusCode(response.getStatusCode()); 2971 thisResource = false; 2972 options.addElement(sResult); 2973 } 2974 } 2975 2976 return options.elements(); 2977 } 2978 2979 2980 2988 public Enumeration optionsMethod(HttpURL httpURL, int type) 2989 throws HttpException, IOException { 2990 2991 HttpClient client = getSessionInstance(httpURL, true); 2992 2993 OptionsMethod method = new OptionsMethod(httpURL.getEscapedPath(), 2994 type); 2995 method.setDebug(debug); 2996 method.setFollowRedirects(this.followRedirects); 2997 2998 generateTransactionHeader(method); 2999 client.executeMethod(method); 3000 3001 Vector options = new Vector (); 3002 int statusCode = method.getStatusLine().getStatusCode(); 3003 if (statusCode >= 200 && statusCode < 300) { 3004 Enumeration responses = method.getResponses(); 3005 if (responses.hasMoreElements()) { 3006 ResponseEntity response = 3007 (ResponseEntity) responses.nextElement(); 3008 if (type == OPTIONS_WORKSPACE){ 3010 Enumeration workspaces = response.getWorkspaces(); 3011 while (workspaces.hasMoreElements()){ 3012 options.add(workspaces.nextElement().toString()); 3013 } 3014 } else if (type == OPTIONS_VERSION_HISTORY){ 3015 Enumeration histories = response.getHistories(); 3016 while (histories.hasMoreElements()){ 3017 options.add(histories.nextElement().toString()); 3018 } 3019 } 3020 3021 if ((thisResource == true) && (response.getStatusCode() > 0)) 3023 setStatusCode(response.getStatusCode()); 3024 thisResource = false; 3025 } 3027 } 3028 3029 return options.elements(); 3030 } 3031 3032 3033 3042 public Enumeration optionsMethod(String path, int type) 3043 throws HttpException, IOException { 3044 3045 setClient(); 3046 3047 OptionsMethod method = new OptionsMethod(URIUtil.encodePath(path), 3048 type); 3049 method.setDebug(debug); 3050 method.setFollowRedirects(this.followRedirects); 3051 3052 generateTransactionHeader(method); 3053 client.executeMethod(method); 3054 3055 Vector options = new Vector (); 3056 int statusCode = method.getStatusLine().getStatusCode(); 3057 if (statusCode >= 200 && statusCode < 300) { 3058 Enumeration responses = method.getResponses(); 3059 if (responses.hasMoreElements()) { 3060 ResponseEntity response = 3061 (ResponseEntity) responses.nextElement(); 3062 if (type == OPTIONS_WORKSPACE){ 3064 Enumeration workspaces = response.getWorkspaces(); 3065 while (workspaces.hasMoreElements()){ 3066 options.add(workspaces.nextElement().toString()); 3067 } 3068 } else if (type == OPTIONS_VERSION_HISTORY){ 3069 Enumeration histories = response.getHistories(); 3070 while (histories.hasMoreElements()){ 3071 options.add(histories.nextElement().toString()); 3072 } 3073 } 3074 3075 if ((thisResource == true) && (response.getStatusCode() > 0)) 3077 setStatusCode(response.getStatusCode()); 3078 thisResource = false; 3079 } 3081 } 3082 3083 return options.elements(); 3084 } 3085 3086 3098 public boolean labelMethod(String labelname, int type) 3099 throws HttpException, IOException { 3100 return labelMethod(httpURL.getPath(), labelname, type); 3101 } 3102 3103 3116 public boolean labelMethod(String path, String labelname, int type) 3117 throws HttpException, IOException { 3118 int labeltype=0; 3119 3120 switch(type) { 3121 case LABEL_SET: 3122 labeltype = LabelMethod.LABEL_SET; 3123 break; 3124 case LABEL_REMOVE: 3125 labeltype = LabelMethod.LABEL_REMOVE; 3126 break; 3127 case LABEL_ADD: 3128 labeltype = LabelMethod.LABEL_ADD; 3129 break; 3130 } 3131 3132 setClient(); 3133 LabelMethod method = new LabelMethod(URIUtil.encodePath(path), 3134 labeltype, labelname); 3135 method.setDebug(debug); 3136 method.setFollowRedirects(this.followRedirects); 3137 3138 generateTransactionHeader(method); 3139 int statusCode = client.executeMethod(method); 3140 3141 setStatusCode(statusCode); 3142 return (statusCode >= 200 && statusCode < 300) ? true : false; 3143 } 3144 3145 3148 public Enumeration reportMethod(HttpURL httpURL, int depth) 3149 3150 throws HttpException, IOException { 3151 setClient(); 3152 ReportMethod method = new ReportMethod(httpURL.getEscapedPath(), 3154 depth); 3155 method.setDebug(debug); 3156 method.setFollowRedirects(this.followRedirects); 3157 generateTransactionHeader(method); 3158 client.executeMethod(method); 3159 3160 Vector results = new Vector (); 3161 3162 Enumeration responses = method.getResponses(); 3163 while (responses.hasMoreElements()) { 3164 ResponseEntity response = (ResponseEntity) responses.nextElement(); 3165 String href = response.getHref(); 3166 String sResult = href; 3167 3168 if ((thisResource == true) && (response.getStatusCode() > 0)) 3170 setStatusCode(response.getStatusCode()); 3171 thisResource = false; 3172 3173 Enumeration responseProperties = method.getResponseProperties(href); 3174 while (responseProperties.hasMoreElements()) { 3175 Property property = (Property) responseProperties.nextElement(); 3176 sResult += "\n" + property.getName() + ":\t" + 3177 DOMUtils.getTextValue(property.getElement()); 3178 3179 } 3180 results.addElement(sResult); 3181 } 3182 3183 return results.elements(); 3184 } 3185 3186 public Enumeration reportMethod(HttpURL httpURL, Vector properties) 3187 3188 throws HttpException, IOException { 3189 setClient(); 3190 ReportMethod method = 3192 new ReportMethod(httpURL.getEscapedPath(), DepthSupport.DEPTH_0, 3193 properties.elements()); 3194 method.setDebug(debug); 3195 method.setFollowRedirects(this.followRedirects); 3196 generateTransactionHeader(method); 3197 client.executeMethod(method); 3198 3199 return method.getResponses(); 3200 } 3201 3202 public Enumeration reportMethod(HttpURL httpURL, Vector properties, 3203 int depth) 3204 3205 throws HttpException, IOException { 3206 setClient(); 3207 ReportMethod method = new ReportMethod(httpURL.getEscapedPath(), depth, 3209 properties.elements()); 3210 method.setDebug(debug); 3211 method.setFollowRedirects(this.followRedirects); 3212 generateTransactionHeader(method); 3213 client.executeMethod(method); 3214 3215 3219 3225 3226 Vector results = new Vector (); 3227 3228 Enumeration responses = method.getResponses(); 3229 while (responses.hasMoreElements()) { 3230 ResponseEntity response = (ResponseEntity) responses.nextElement(); 3231 String href = response.getHref(); 3232 String sResult = href; 3233 3234 if ((thisResource == true) && (response.getStatusCode() > 0)) 3236 setStatusCode(response.getStatusCode()); 3237 thisResource = false; 3238 3239 Enumeration responseProperties = method.getResponseProperties(href); 3240 while (responseProperties.hasMoreElements()) { 3241 Property property = (Property) responseProperties.nextElement(); 3242 sResult += "\n" + property.getName() + ":\t" + 3243 DOMUtils.getTextValue(property.getElement()); 3244 } 3246 results.addElement(sResult); 3247 } 3248 3249 return results.elements(); 3250 } 3251 3252 3253 public Enumeration reportMethod(HttpURL httpURL, Vector properties, 3255 Vector histUri, int depth) 3256 3257 throws HttpException, IOException { 3258 setClient(); 3259 ReportMethod method = new ReportMethod(httpURL.getEscapedPath(), depth, 3261 properties.elements(), 3262 histUri.elements()); 3263 method.setDebug(debug); 3264 method.setFollowRedirects(this.followRedirects); 3265 generateTransactionHeader(method); 3266 client.executeMethod(method); 3267 3268 Vector results = new Vector (); 3269 3270 Enumeration responses = method.getResponses(); 3271 while (responses.hasMoreElements()) { 3272 ResponseEntity response = (ResponseEntity) responses.nextElement(); 3273 String href = response.getHref(); 3274 String sResult = href; 3275 3276 if ((thisResource == true) && (response.getStatusCode() > 0)) 3278 setStatusCode(response.getStatusCode()); 3279 thisResource = false; 3280 3281 Enumeration responseProperties = method.getResponseProperties(href); 3282 while (responseProperties.hasMoreElements()) { 3283 Property property = (Property) responseProperties.nextElement(); 3284 sResult += "\n" + property.getName() + ":\t" + 3285 DOMUtils.getTextValue(property.getElement()); 3286 } 3287 results.addElement(sResult); 3288 } 3289 3290 return results.elements(); 3291 } 3292 public Enumeration reportMethod(HttpURL httpURL, String sQuery, int depth) 3294 3295 throws HttpException, IOException { 3296 setClient(); 3297 ReportMethod method = new ReportMethod(httpURL.getEscapedPath(), depth, 3299 sQuery); 3300 3301 method.setDebug(debug); 3302 method.setFollowRedirects(this.followRedirects); 3303 generateTransactionHeader(method); 3304 client.executeMethod(method); 3305 3306 Vector results = new Vector (); 3307 3308 Enumeration responses = method.getResponses(); 3309 while (responses.hasMoreElements()) { 3310 ResponseEntity response = (ResponseEntity) responses.nextElement(); 3311 String sResult; 3314 if ((thisResource == true) && (response.getStatusCode() > 0)) 3316 setStatusCode(response.getStatusCode()); 3317 thisResource = false; 3318 3319 sResult = response.toString(); 3320 3325 results.addElement(sResult); 3326 } 3327 3328 return results.elements(); 3329 } 3330 3331 3332 3347 public Enumeration propfindMethod(int depth) 3348 throws HttpException, IOException { 3349 3350 thisResource = true; 3351 return propfindMethod(httpURL.getPath(), depth); 3352 } 3353 3354 3355 3371 public Enumeration propfindMethod(String path, int depth) 3372 throws HttpException, IOException { 3373 3374 setClient(); 3375 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 3377 depth); 3378 3379 method.setDebug(debug); 3380 3381 generateTransactionHeader(method); 3383 int status = client.executeMethod(method); 3384 3385 if (thisResource == true) { 3387 setStatusCode(status); 3388 } 3389 if (status != HttpStatus.SC_MULTI_STATUS 3391 && status != HttpStatus.SC_OK) { 3392 HttpException ex = new HttpException(); 3393 ex.setReasonCode(status); 3394 throw ex; 3395 } 3396 thisResource = false; 3397 3398 return method.getResponses(); 3399 } 3400 3401 3402 3403 3419 public Enumeration propfindMethod(int depth, Vector properties) 3420 throws HttpException, IOException { 3421 3422 thisResource = true; 3423 return propfindMethod(httpURL.getPath(), depth, properties); 3424 } 3425 3426 3427 3444 public Enumeration propfindMethod(String path, int depth, 3445 Vector properties) 3446 throws HttpException, IOException { 3447 3448 setClient(); 3449 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 3451 depth, 3452 properties.elements()); 3453 3454 method.setDebug(debug); 3455 method.setFollowRedirects(this.followRedirects); 3456 generateTransactionHeader(method); 3457 int status = client.executeMethod(method); 3458 3459 if (thisResource == true) { 3461 setStatusCode(method.getStatusLine().getStatusCode()); 3463 } 3464 if (status != HttpStatus.SC_MULTI_STATUS 3466 && status != HttpStatus.SC_OK) { 3467 HttpException ex = new HttpException(); 3468 ex.setReasonCode(status); 3469 throw ex; 3470 } 3471 thisResource = false; 3472 3473 return method.getResponses(); 3474 } 3475 3476 3477 3486 public Enumeration propfindMethod(String propertyName) 3487 throws HttpException, IOException { 3488 3489 Vector property = new Vector (); 3490 property.addElement(propertyName); 3491 3492 thisResource = true; 3493 return propfindMethod(httpURL.getPath(), property); 3494 } 3495 3496 3497 3507 public Enumeration propfindMethod(String path, String propertyName) 3508 throws HttpException, IOException { 3509 3510 Vector property = new Vector (); 3511 property.addElement(propertyName); 3512 3513 thisResource = false; 3514 return propfindMethod(path, property); 3515 } 3516 3517 3518 3527 public Enumeration propfindMethod(Vector properties) 3528 throws HttpException, IOException { 3529 3530 thisResource = true; 3531 return propfindMethod(httpURL.getPath(), properties); 3532 } 3533 3534 3535 3545 public Enumeration propfindMethod(String path, Vector properties) 3546 throws HttpException, IOException { 3547 3548 setClient(); 3549 PropFindMethod method = new PropFindMethod(URIUtil.encodePath(path), 3551 DepthSupport.DEPTH_0, 3552 properties.elements()); 3553 method.setDebug(debug); 3554 method.setFollowRedirects(this.followRedirects); 3555 generateTransactionHeader(method); 3556 int status = client.executeMethod(method); 3557 3558 if (status != HttpStatus.SC_MULTI_STATUS 3560 && status != HttpStatus.SC_OK) { 3561 HttpException ex = new HttpException(); 3562 ex.setReasonCode(status); 3563 throw ex; 3564 } 3565 3566 Vector results = new Vector (); 3568 3569 Enumeration responses = method.getResponses(); 3570 if (responses.hasMoreElements()) { 3571 ResponseEntity response = 3572 (ResponseEntity) responses.nextElement(); 3573 String href = response.getHref(); 3574 3575 if ((thisResource == true) && (response.getStatusCode() > 0)) 3577 setStatusCode(response.getStatusCode()); 3578 thisResource = false; 3579 3580 Enumeration responseProperties = 3581 method.getResponseProperties(href); 3582 while (responseProperties.hasMoreElements()) { 3583 Property property = 3584 (Property) responseProperties.nextElement(); 3585 results.addElement(property.getPropertyAsString()); 3586 } 3587 } 3588 3589 return results.elements(); 3590 } 3591 3592 3593 3603 public boolean proppatchMethod(String propertyName, String propertyValue) 3604 throws HttpException, IOException { 3605 3606 boolean result = proppatchMethod(httpURL.getPath(), 3607 propertyName, propertyValue, true); 3608 if (result) refresh(); 3609 3610 return result; 3611 } 3612 3613 3614 3625 public boolean proppatchMethod(String propertyName, String propertyValue, 3626 boolean action) throws HttpException, IOException { 3627 3628 boolean result = proppatchMethod(httpURL.getPath(), 3629 propertyName, propertyValue, action); 3630 if (result) refresh(); 3631 3632 return result; 3633 } 3634 3635 3636 3646 public boolean proppatchMethod(PropertyName propertyName, 3647 String propertyValue) 3648 throws HttpException, IOException { 3649 3650 boolean result = proppatchMethod(httpURL.getPath(), 3651 propertyName, propertyValue, true); 3652 if (result) refresh(); 3653 3654 return result; 3655 } 3656 3657 3658 3669 public boolean proppatchMethod(PropertyName propertyName, 3670 String propertyValue, boolean action) 3671 throws HttpException, IOException { 3672 3673 boolean result = proppatchMethod(httpURL.getPath(), 3674 propertyName, propertyValue, action); 3675 if (result) refresh(); 3676 3677 return result; 3678 } 3679 3680 3681 3692 public boolean proppatchMethod(String path, String propertyName, 3693 String propertyValue) throws HttpException, IOException { 3694 3695 Hashtable property = new Hashtable (); 3696 property.put(propertyName, propertyValue); 3697 return proppatchMethod(path, property, true); 3698 } 3699 3700 3701 3714 public boolean proppatchMethod(String path, String propertyName, 3715 String propertyValue, boolean action) 3716 throws HttpException, IOException { 3717 3718 Hashtable property = new Hashtable (); 3719 property.put(propertyName, propertyValue); 3720 return proppatchMethod(path, property, action); 3721 } 3722 3723 3724 3735 public boolean proppatchMethod(String path, PropertyName propertyName, 3736 String propertyValue) throws HttpException, IOException { 3737 3738 Hashtable property = new Hashtable (); 3739 property.put(propertyName, propertyValue); 3740 3741 return proppatchMethod(path, property, true); 3742 } 3743 3744 3745 3758 public boolean proppatchMethod(String path, PropertyName propertyName, 3759 String propertyValue, boolean action) 3760 throws HttpException, IOException { 3761 3762 Hashtable property = new Hashtable (); 3763 property.put(propertyName, propertyValue); 3764 return proppatchMethod(path, property, action); 3765 } 3766 3767 3768 3778 public boolean proppatchMethod(Hashtable properties) 3779 throws HttpException, IOException { 3780 3781 boolean result = proppatchMethod(httpURL.getPath(), properties, true); 3782 if (result) refresh(); 3783 3784 return result; 3785 } 3786 3787 3788 3799 public boolean proppatchMethod(Hashtable properties, boolean action) 3800 throws HttpException, IOException { 3801 3802 boolean result = proppatchMethod(httpURL.getPath(), properties, action); 3803 if (result) refresh(); 3804 3805 return result; 3806 } 3807 3808 3809 3820 public boolean proppatchMethod(String path, Hashtable properties) 3821 throws HttpException, IOException { 3822 3823 return proppatchMethod(path, properties, true); 3824 } 3825 3826 3827 3840 public boolean proppatchMethod(String path, Hashtable properties, 3841 boolean action) throws HttpException, IOException { 3842 3843 setClient(); 3844 PropPatchMethod method = new PropPatchMethod(URIUtil.encodePath(path)); 3845 method.setDebug(debug); 3846 method.setFollowRedirects(this.followRedirects); 3847 3848 generateIfHeader(method); 3849 Enumeration names = properties.keys(); 3850 boolean hasSomething = false; 3851 if (names.hasMoreElements()) { 3852 hasSomething = true; 3853 } 3854 while (names.hasMoreElements()) { 3855 Object item = names.nextElement(); 3856 if (item instanceof String ) { 3857 String name = (String ) item; 3858 String value = (String ) properties.get(item); 3859 if (action) { 3860 method.addPropertyToSet(name, value); 3861 } else { 3862 method.addPropertyToRemove(name); 3863 } 3864 } else if (item instanceof PropertyName) { 3865 String name = ((PropertyName) item).getLocalName(); 3866 String namespaceURI = ((PropertyName) item).getNamespaceURI(); 3867 String value = (String ) properties.get(item); 3868 if (action) { 3869 method.addPropertyToSet(name, value, null, namespaceURI); 3870 } else { 3871 method.addPropertyToRemove(name, null, namespaceURI); 3872 } 3873 } else { 3874 } 3876 } 3877 if (hasSomething) { 3878 generateTransactionHeader(method); 3879 int statusCode = client.executeMethod(method); 3880 setStatusCode(statusCode); 3883 if (statusCode >= 200 && statusCode < 300) { 3884 return true; 3885 } 3886 } 3887 return false; 3888 } 3889 3890 3891 3898 public boolean headMethod() 3899 throws HttpException, IOException { 3900 3901 return headMethod(httpURL.getPathQuery()); 3902 } 3903 3904 3905 3913 public boolean headMethod(String path) 3914 throws HttpException, IOException { 3915 3916 setClient(); 3917 HeadMethod method = new HeadMethod(URIUtil.encodePathQuery(path)); 3918 3919 generateTransactionHeader(method); 3920 int statusCode = client.executeMethod(method); 3921 3922 setStatusCode(statusCode); 3923 return (statusCode >= 200 && statusCode < 300) ? true : false; 3924 } 3925 3926 3927 3934 public boolean deleteMethod() 3935 throws HttpException, IOException { 3936 3937 boolean result = deleteMethod(httpURL.getPath()); 3938 if (result) { 3939 setExistence(false); 3940 } 3941 3942 return result; 3943 } 3944 3945 3946 3954 public boolean deleteMethod(String path) 3955 throws HttpException, IOException { 3956 3957 setClient(); 3958 DeleteMethod method = new DeleteMethod(URIUtil.encodePath(path)); 3959 method.setDebug(debug); 3960 method.setFollowRedirects(this.followRedirects); 3961 3962 generateIfHeader(method); 3963 generateTransactionHeader(method); 3964 int statusCode = client.executeMethod(method); 3965 3966 setStatusCode(statusCode); 3967 return (statusCode >= 200 && statusCode < 300) ? true : false; 3968 } 3969 3970 3971 3979 public boolean moveMethod(String destination) 3980 throws HttpException, IOException { 3981 3982 boolean result = moveMethod(httpURL.getPath(), destination); 3983 if (result) { 3984 httpURL.setPath(destination); 3985 refresh(); 3986 } 3987 3988 return result; 3989 } 3990 3991 3992 4001 public boolean moveMethod(String source, String destination) 4002 throws HttpException, IOException { 4003 4004 setClient(); 4005 MoveMethod method = new MoveMethod(URIUtil.encodePath(source), 4006 URIUtil.encodePath(destination)); 4007 method.setDebug(debug); 4008 method.setFollowRedirects(this.followRedirects); 4009 generateIfHeader(method); 4010 method.setOverwrite(overwrite); 4011 generateTransactionHeader(method); 4012 int statusCode = client.executeMethod(method); 4013 4014 setStatusCode(statusCode); 4018 return (statusCode >= 200 && statusCode < 300) ? true : false; 4019 } 4020 4021 4022 4030 public boolean copyMethod(String destination) 4031 throws HttpException, IOException { 4032 4033 boolean result = copyMethod(httpURL.getPath(), destination); 4034 if (result) refresh(); 4035 4036 return result; 4037 } 4038 4039 4040 4049 public boolean copyMethod(String source, String destination) 4050 throws HttpException, IOException { 4051 4052 setClient(); 4053 CopyMethod method = new CopyMethod(URIUtil.encodePath(source), 4054 URIUtil.encodePath(destination)); 4055 method.setDebug(debug); 4056 method.setFollowRedirects(this.followRedirects); 4057 4058 generateIfHeader(method); 4059 method.setOverwrite(overwrite); 4060 generateTransactionHeader(method); 4061 int statusCode = client.executeMethod(method); 4062 4063 setStatusCode(statusCode); 4067 return (statusCode >= 200 && statusCode < 300) ? true : false; 4068 } 4069 4070 4071 4072 4079 public boolean mkcolMethod() 4080 throws HttpException, IOException { 4081 4082 boolean result = mkcolMethod(httpURL.getPath()); 4083 if (result) refresh(); 4084 4085 return result; 4086 } 4087 4088 4089 4098 public boolean mkcolMethod(String path) 4099 throws HttpException, IOException { 4100 4101 setClient(); 4102 MkcolMethod method = new MkcolMethod(URIUtil.encodePath(path)); 4103 4104 generateIfHeader(method); 4105 generateTransactionHeader(method); 4106 int statusCode = client.executeMethod(method); 4107 4108 setStatusCode(statusCode); 4112 return (statusCode >= 200 && statusCode < 300) ? true : false; 4113 } 4114 4115 4116 4124 public boolean lockMethod() 4125 throws HttpException, IOException { 4126 4127 String owner = (httpURL.getUser() != null) ? 4128 httpURL.getUser() : defaultOwner; 4129 4130 boolean result = lockMethod(httpURL.getPath(), owner, 120); 4131 if (result) refresh(); 4132 4133 return result; 4134 } 4135 4136 4137 4138 4148 public boolean lockMethod(String owner, int timeout) 4149 throws HttpException, IOException { 4150 4151 boolean result = lockMethod(httpURL.getPath(), owner, timeout); 4152 if (result) refresh(); 4153 4154 return result; 4155 } 4156 4157 4158 4167 public boolean lockMethod(String path) 4168 throws HttpException, IOException { 4169 4170 String owner = (httpURL.getUser() != null) ? 4171 httpURL.getUser() : defaultOwner; 4172 4173 return lockMethod(path, owner, 120); 4174 } 4175 4176 4177 4188 public boolean lockMethod(String path, String owner, int timeout) 4189 throws HttpException, IOException { 4190 4191 return lockMethod(path, owner, timeout, LockMethod.SCOPE_EXCLUSIVE); 4192 } 4193 4194 4206 public boolean lockMethod(String path, String owner, int timeout, short lockType) 4207 throws HttpException, IOException { 4208 4209 return lockMethod(path, owner, timeout, lockType, DepthSupport.DEPTH_INFINITY); 4210 } 4211 4212 4213 4225 public boolean lockMethod(String path, String owner, int timeout, short lockType, int depth) 4226 throws HttpException, IOException { 4227 4228 setClient(); 4229 4230 if (owner == null) { 4231 owner = (httpURL.getUser() != null) ? httpURL.getUser() : defaultOwner; 4232 } 4233 4234 LockMethod method = new LockMethod(URIUtil.encodePath(path), owner, 4236 lockType, timeout); 4237 method.setDebug(debug); 4238 method.setFollowRedirects(this.followRedirects); 4239 method.setDepth(depth); 4240 4241 generateIfHeader(method); 4242 generateTransactionHeader(method); 4243 int statusCode = client.executeMethod(method); 4244 String lock = method.getLockToken(); 4245 WebdavState state = (WebdavState) client.getState(); 4246 if (state != null) { 4247 state.addLock(path, lock); 4248 } 4249 this.owner = method.getOwner(); 4250 4251 setStatusCode(statusCode, lock); 4254 4255 return (statusCode >= 200 && statusCode < 300) ? true : false; 4256 } 4257 4258 4259 4265 public boolean lockMethod(String owner, short timeout) 4266 throws HttpException, IOException { 4267 4268 return lockMethod(owner, (int) timeout); 4269 } 4270 4271 4272 4278 public boolean lockMethod(String path, String owner, short timeout) 4279 throws HttpException, IOException { 4280 4281 return lockMethod(path, owner, (int) timeout); 4282 } 4283 4284 4298 public boolean startTransaction(String owner, int timeout) throws IOException { 4299 String path = httpURL.getPath(); 4300 4301 setClient(); 4302 4303 if (owner == null) { 4304 owner = (httpURL.getUser() != null) ? httpURL.getUser() : defaultOwner; 4305 } 4306 4307 LockMethod method = new LockMethod(path, owner, timeout, true); 4309 method.setDebug(debug); 4310 method.setFollowRedirects(this.followRedirects); 4311 4312 generateIfHeader(method); 4313 int statusCode = client.executeMethod(method); 4314 String txHandle = method.getLockToken(); 4315 WebdavState state = (WebdavState) client.getState(); 4316 if (state != null) { 4317 state.setTransactionHandle(txHandle); 4318 } 4319 this.owner = method.getOwner(); 4320 4321 setStatusCode(statusCode, txHandle); 4324 4325 return (statusCode >= 200 && statusCode < 300) ? true : false; 4326 } 4327 4328 4335 public String getTransactionHandle() throws IOException { 4336 setClient(); 4337 4338 WebdavState state = (WebdavState) client.getState(); 4340 if (state == null) return null; 4341 String txHandle = state.getTransactionHandle(); 4342 return txHandle; 4343 } 4344 4345 4354 public boolean commitTransaction() throws IOException { 4355 String path = httpURL.getPath(); 4356 return endTransaction(path, UnlockMethod.COMMIT_TRANSACTION); 4357 } 4358 4359 4368 public boolean abortTransaction() throws IOException { 4369 String path = httpURL.getPath(); 4370 return endTransaction(path, UnlockMethod.ABORT_TRANSACTION); 4371 } 4372 4373 protected boolean endTransaction(String path, int transactionStatus) throws IOException { 4374 setClient(); 4375 4376 WebdavState state = (WebdavState) client.getState(); 4378 if (state == null) return false; 4379 String txHandle = state.getTransactionHandle(); 4380 if (txHandle == null) return false; 4381 UnlockMethod method = new UnlockMethod(path, txHandle, transactionStatus); 4382 method.setDebug(debug); 4383 method.setFollowRedirects(this.followRedirects); 4384 4385 generateIfHeader(method); 4386 int statusCode = client.executeMethod(method); 4387 4388 setStatusCode(statusCode); 4389 if (statusCode >= 200 && statusCode < 300) { 4390 state.setTransactionHandle(null); 4391 return true; 4392 } 4393 return false; 4394 } 4395 4396 4403 public boolean unlockMethod() throws HttpException, IOException { 4404 4405 String owner = (httpURL.getUser() != null) ? 4406 httpURL.getUser() : defaultOwner; 4407 4408 boolean result = unlockMethod(httpURL.getPath(), owner); 4409 if (result) refresh(); 4410 4411 return result; 4412 } 4413 4414 4415 4423 public boolean unlockMethod(String path) 4424 throws HttpException, IOException { 4425 4426 String owner = (httpURL.getUser() != null) ? 4427 httpURL.getUser() : defaultOwner; 4428 4429 return unlockMethod(path, owner); 4430 } 4431 4432 4433 4441 public boolean unlockMethod(String path, String owner) 4442 throws HttpException, IOException { 4443 4444 setClient(); 4445 4446 if (owner == null) { 4447 owner = (httpURL.getUser() != null) ? httpURL.getUser() : defaultOwner; 4448 } 4449 4450 WebdavState state = (WebdavState) client.getState(); 4452 4454 state = discoverLock(owner, path, state); 4455 String lock = state.getLock(path); 4456 if (lock == null) return false; 4457 UnlockMethod method = new UnlockMethod(URIUtil.encodePath(path)); 4459 method.setDebug(debug); 4460 method.setFollowRedirects(this.followRedirects); 4461 4462 generateIfHeader(method); 4463 generateTransactionHeader(method); 4464 method.setLockToken(lock); 4465 int statusCode = client.executeMethod(method); 4466 4467 setStatusCode(statusCode); 4468 if (statusCode >= 200 && statusCode < 300) { 4469 state.removeLocks(path); 4470 return true; 4471 } 4472 4473 return false; 4474 } 4475 4476 4477 4483 public void discoverOwnLocks() 4484 throws HttpException, IOException { 4485 4486 setClient(); 4487 String owner = (httpURL.getUser() != null) ? 4488 httpURL.getUser() : defaultOwner; 4489 4490 WebdavState state = (WebdavState) client.getState(); 4491 state = discoverLock(owner, httpURL.getPath(), state); 4492 client.setState(state); 4493 } 4494 4495 4496 4503 public void discoverOwnLocks(String owner) 4504 throws HttpException, IOException { 4505 4506 setClient(); 4507 4508 WebdavState state = (WebdavState) client.getState(); 4509 state = discoverLock(owner, httpURL.getPath(), state); 4510 client.setState(state); 4511 } 4512 4513 4514 4522 protected WebdavState discoverLock(String owner, String path, 4523 WebdavState state) { 4524 try { 4525 lockDiscovery=lockDiscoveryPropertyFindMethod(path); 4526 } catch (Exception e) { 4527 return state; 4528 } 4529 4530 4531 if (lockDiscovery == null) return state; 4532 Lock[] activeLocks = lockDiscovery.getActiveLocks(); 4533 4534 if (activeLocks == null) return state; 4535 for (int i = 0; i < activeLocks.length; i++) { 4536 String activeLockOwner = activeLocks[i].getOwner(); 4537 if (activeLockOwner.equals(owner)) { 4538 String locktoken = activeLocks[i].getLockToken(); 4539 state.addLock(path, locktoken); 4540 } 4541 } 4542 return state; 4543 } 4544 4545 4546 4555 public boolean updateMethod(String target) 4556 throws HttpException, IOException { 4557 4558 return updateMethod(httpURL.getPath(), target); 4559 } 4560 4561 4562 4571 public boolean updateMethod(String path, String target) 4572 throws HttpException, IOException { 4573 4574 setClient(); 4575 UpdateMethod method = new UpdateMethod(URIUtil.encodePath(path), 4576 URIUtil.encodePath(target)); 4577 method.setDebug(debug); 4578 method.setFollowRedirects(this.followRedirects); 4579 4580 generateIfHeader(method); 4581 generateTransactionHeader(method); 4582 int statusCode = client.executeMethod(method); 4583 4584 setStatusCode(statusCode); 4585 4586 return (statusCode >= 200 && statusCode < 300) ? true : false; 4587 } 4588 4589 4590 public boolean versionControlMethod(String path) 4591 throws HttpException, IOException { 4592 4593 setClient(); 4594 4595 VersionControlMethod method = new VersionControlMethod( 4596 URIUtil.encodePath(path)); 4597 method.setDebug(debug); 4598 method.setFollowRedirects(this.followRedirects); 4599 generateIfHeader(method); 4600 generateTransactionHeader(method); 4601 int statusCode = client.executeMethod(method); 4602 4603 setStatusCode(statusCode); 4604 4605 return (statusCode >= 200 && statusCode < 300) ? true : false; 4606 } 4607 4608 4609 public boolean versionControlMethod(String path, String target) 4610 throws HttpException, IOException { 4611 4612 setClient(); 4613 4614 VersionControlMethod method = new VersionControlMethod( 4615 URIUtil.encodePath(path), 4616 URIUtil.encodePath(target)); 4617 method.setDebug(debug); 4618 method.setFollowRedirects(this.followRedirects); 4619 4620 generateIfHeader(method); 4621 generateTransactionHeader(method); 4622 int statusCode = client.executeMethod(method); 4623 4624 setStatusCode(statusCode); 4625 4626 return (statusCode >= 200 && statusCode < 300) ? true : false; 4627 } 4628 4629 4630 4637 public boolean mkWorkspaceMethod() 4638 throws HttpException, IOException { 4639 4640 boolean result = mkWorkspaceMethod(httpURL.getPath()); 4641 if (result) refresh(); 4642 4643 return result; 4644 } 4645 4646 4647 4656 public boolean mkWorkspaceMethod(String path) 4657 throws HttpException, IOException { 4658 4659 setClient(); 4660 MkWorkspaceMethod method = 4661 new MkWorkspaceMethod(URIUtil.encodePath(path)); 4662 method.setDebug(debug); 4663 method.setFollowRedirects(this.followRedirects); 4664 generateIfHeader(method); 4665 generateTransactionHeader(method); 4666 int statusCode = client.executeMethod(method); 4667 4668 4672 setStatusCode(statusCode); 4673 4674 return (statusCode >= 200 && statusCode < 300) ? true : false; 4675 } 4676 4677 4678 4680 4681 4687 public int compareToWebdavResource(WebdavResource another) { 4688 4689 try { 4690 HttpURL anotherUrl = another.getHttpURL(); 4691 4692 String thisHost = httpURL.getHost(); 4693 String anotherHost= anotherUrl.getHost(); 4694 if (!thisHost.equalsIgnoreCase(anotherHost)) 4695 return thisHost.compareTo(anotherHost); 4696 4697 int thisPort = httpURL.getPort(); 4698 int anotherPort= anotherUrl.getPort(); 4699 if (thisPort != anotherPort) 4700 return (thisPort < anotherPort) ? -1 : 1; 4701 4702 boolean thisCollection = isCollection(); 4703 boolean anotherCollection = another.isCollection(); 4704 if (thisCollection && !anotherCollection) 4705 return -1; 4706 if (anotherCollection && !thisCollection) 4707 return 1; 4708 4709 String thisPath = httpURL.getPathQuery(); 4710 String anotherPath= anotherUrl.getPathQuery(); 4711 return thisPath.compareTo(anotherPath); 4712 } catch (Exception e) { 4713 } 4715 4716 return 0; 4717 } 4718 4719 4720 4726 public int compareTo(Object another) { 4727 4728 if ((another != null) && (another instanceof WebdavResource)) { 4729 return compareToWebdavResource((WebdavResource) another); 4730 } 4731 4732 String thisUrl = toString(); 4733 String anotherUrl = another.toString(); 4734 4735 return thisUrl.compareTo(anotherUrl); 4736 } 4737 4738 4739 4745 public boolean equals(Object obj) { 4746 4747 if ((obj != null) && (obj instanceof WebdavResource)) { 4748 return compareTo(obj) == 0; 4749 } 4750 return false; 4751 } 4752 4753 4754 4759 public String toString() { 4760 return httpURL.toString(); 4761 } 4762 4763 4764 4771 public boolean checkinMethod() 4772 throws HttpException, IOException { 4773 4774 return checkinMethod(httpURL.getPath()); 4775 } 4776 4777 4778 4786 public boolean checkinMethod(String path) 4787 throws HttpException, IOException { 4788 4789 setClient(); 4790 CheckinMethod method = new CheckinMethod(URIUtil.encodePath(path)); 4791 method.setDebug(debug); 4792 method.setFollowRedirects(this.followRedirects); 4793 4794 generateIfHeader(method); 4795 generateTransactionHeader(method); 4796 int statusCode = client.executeMethod(method); 4797 4798 setStatusCode(statusCode); 4799 4800 return (statusCode >= 200 && statusCode < 300) ? true : false; 4801 } 4802 4803 4804 4811 public boolean checkoutMethod() 4812 throws HttpException, IOException { 4813 4814 return checkoutMethod(httpURL.getPath()); 4815 } 4816 4817 4818 4826 public boolean checkoutMethod(String path) 4827 throws HttpException, IOException { 4828 4829 setClient(); 4830 CheckoutMethod method = new CheckoutMethod(URIUtil.encodePath(path)); 4831 method.setDebug(debug); 4832 method.setFollowRedirects(this.followRedirects); 4833 4834 generateIfHeader(method); 4835 generateTransactionHeader(method); 4836 int statusCode = client.executeMethod(method); 4837 4838 setStatusCode(statusCode); 4839 4840 return (statusCode >= 200 && statusCode < 300) ? true : false; 4841 } 4842 4843 4844 4851 public boolean uncheckoutMethod() 4852 throws HttpException, IOException { 4853 4854 return uncheckoutMethod(httpURL.getPath()); 4855 } 4856 4857 4858 4859 4860 4868 public boolean uncheckoutMethod(String path) 4869 throws HttpException, IOException { 4870 4871 setClient(); 4872 UncheckoutMethod method = 4873 new UncheckoutMethod(URIUtil.encodePath(path)); 4874 method.setDebug(debug); 4875 method.setFollowRedirects(this.followRedirects); 4876 4877 generateIfHeader(method); 4878 generateTransactionHeader(method); 4879 int statusCode = client.executeMethod(method); 4880 4881 setStatusCode(statusCode); 4882 4883 return (statusCode >= 200 && statusCode < 300) ? true : false; 4884 } 4885 4886 4893 protected WebdavResource createWebdavResource(HttpClient client) { 4894 WebdavResource resource = new WebdavResource(client); 4895 resource.setProxy(proxyHost, proxyPort); 4896 resource.setProxyCredentials(proxyCredentials); 4897 return resource; 4898 } 4899 4900 4906 protected void processProperty(Property property) { 4907 if (property.getLocalName().equals(DISPLAYNAME)) { 4908 displayName = property.getPropertyAsString(); 4909 } 4910 else if (property.getLocalName().equals(GETCONTENTLENGTH)) { 4911 String getContentLength = property.getPropertyAsString(); 4912 setGetContentLength(getContentLength); 4913 } 4914 else if (property.getLocalName().equals(RESOURCETYPE)) { 4915 ResourceTypeProperty resourceType = 4916 (ResourceTypeProperty) property; 4917 setResourceType(resourceType); 4918 } 4919 else if (property.getLocalName().equals(GETCONTENTTYPE)) { 4920 String getContentType = property.getPropertyAsString(); 4921 setGetContentType(getContentType); 4922 } 4923 else if (property.getLocalName().equals(GETLASTMODIFIED)) { 4924 String getLastModified = property.getPropertyAsString(); 4925 setGetLastModified(getLastModified); 4926 } 4927 else if (property.getLocalName().equals(CREATIONDATE)) { 4928 String creationDate = property.getPropertyAsString(); 4929 setCreationDate(creationDate); 4930 } 4931 else if (property.getLocalName().equals(GETETAG)) { 4932 String getEtag = property.getPropertyAsString(); 4933 setGetEtag(getEtag); 4934 } 4935 else if (property.getLocalName().equals(ISHIDDEN)) { 4936 String isHidden = property.getPropertyAsString(); 4937 setIsHidden(isHidden); 4938 } 4939 else if (property.getLocalName().equals(ISCOLLECTION)) { 4940 String isCollection = property.getPropertyAsString(); 4941 setIsCollection(isCollection); 4942 } 4943 else if (property.getLocalName().equals(SUPPORTEDLOCK)) { 4944 String supportedLock = property.getPropertyAsString(); 4945 setSupportedLock(supportedLock); 4946 } 4947 else if (property.getLocalName().equals(LOCKDISCOVERY)) { 4948 LockDiscoveryProperty lockDiscovery = 4949 (LockDiscoveryProperty) property; 4950 setLockDiscovery(lockDiscovery); 4951 } 4952 } 4953 4954 4955 4969 public Enumeration aclReportMethod( 4970 String path, 4971 Collection properties, 4972 int reportType) 4973 throws HttpException, IOException { 4974 4975 setClient(); 4976 AclReportMethod method = 4977 new AclReportMethod( 4978 URIUtil.encodePath(path), 4979 properties, 4980 DepthSupport.DEPTH_INFINITY, 4981 reportType); 4982 4983 method.setDebug(debug); 4984 method.setFollowRedirects(this.followRedirects); 4985 generateTransactionHeader(method); 4986 int status = client.executeMethod(method); 4987 4988 if (thisResource == true) { 4990 setStatusCode(method.getStatusLine().getStatusCode()); 4992 } 4993 4994 if (status != HttpStatus.SC_MULTI_STATUS && status != HttpStatus.SC_OK) { 4997 HttpException ex = new HttpException(); 4998 ex.setReasonCode(status); 4999 throw ex; 5000 } 5001 thisResource = false; 5002 5003 return method.getResponses(); 5004 } 5005 5006 5007 5017 public boolean bindMethod(String newBinding) 5018 throws HttpException, IOException { 5019 return bindMethod(httpURL.getPath(), newBinding); 5020 } 5021 5022 5032 public boolean bindMethod(String existingBinding, String newBinding) 5033 throws HttpException, IOException { 5034 5035 setClient(); 5036 BindMethod method = 5037 new BindMethod(URIUtil.encodePath(existingBinding), 5038 URIUtil.encodePath(newBinding)); 5039 method.setDebug(debug); 5040 method.setOverwrite(overwrite); 5041 generateTransactionHeader(method); 5042 int statusCode = client.executeMethod(method); 5043 5044 setStatusCode(statusCode); 5049 return statusCode >= 200 && statusCode < 300; 5050 } 5051 5052 5059 public boolean unbindMethod() throws HttpException, IOException { 5060 boolean result = unbindMethod(httpURL.getPath()); 5061 if (result) { 5062 setExistence(false); 5063 } 5064 5065 return result; 5066 } 5067 5068 5076 public boolean unbindMethod(String binding) 5077 throws HttpException, IOException { 5078 5079 setClient(); 5080 UnbindMethod method = 5081 new UnbindMethod(URIUtil.encodePath(binding)); 5082 method.setDebug(debug); 5083 generateTransactionHeader(method); 5084 int statusCode = client.executeMethod(method); 5085 5086 setStatusCode(statusCode); 5090 return statusCode >= 200 && statusCode < 300; 5091 } 5092 5093 5106 public boolean rebindMethod(String newBinding) 5107 throws HttpException, IOException { 5108 boolean result = rebindMethod(httpURL.getPath(), newBinding); 5109 if (result) { 5110 httpURL.setPath(newBinding); 5111 refresh(); 5112 } 5113 5114 return result; 5115 } 5116 5117 5131 public boolean rebindMethod(String existingBinding, String newBinding) 5132 throws HttpException, IOException { 5133 5134 setClient(); 5135 RebindMethod method = 5136 new RebindMethod(URIUtil.encodePath(existingBinding), 5137 URIUtil.encodePath(newBinding)); 5138 method.setDebug(debug); 5139 method.setOverwrite(overwrite); 5140 generateTransactionHeader(method); 5141 int statusCode = client.executeMethod(method); 5142 5143 setStatusCode(statusCode); 5148 return statusCode >= 200 && statusCode < 300; 5149 } 5150 5151 5169 public Subscription subscribeMethod(String path, 5170 String notificationType, 5171 String callback, 5172 long notificationDelay, 5173 int depth, 5174 long lifetime) 5175 throws HttpException, IOException 5176 { 5177 setClient(); 5178 5179 SubscribeMethod method = new SubscribeMethod(path); 5180 method.setDebug(debug); 5181 method.setFollowRedirects(this.followRedirects); 5182 5183 method.setCallback(callback); 5184 method.setDepth(depth); 5185 method.setSubsciptionLifetime(lifetime); 5186 method.setNotificationType(notificationType); 5187 method.setNotificationDelay(notificationDelay); 5188 generateTransactionHeader(method); 5189 5190 int statusCode = client.executeMethod(method); 5191 5192 if (statusCode == HttpStatus.SC_OK) { 5193 return new Subscription( 5194 path, 5195 method.getResponsedSubscriptionId(), 5196 method.getCallback(), 5197 method.getResponsedSubscriptionLifetime(), 5198 method.getResponsedContentLocation(), 5199 method.getNotificationType() 5200 ); 5201 } else { 5202 return null; 5203 } 5204 } 5205 5206 5211 public boolean subscribeMethod(String path, int subscriptionId) 5212 throws HttpException, IOException 5213 { 5214 setClient(); 5215 5216 SubscribeMethod method = new SubscribeMethod(path); 5217 method.setDebug(debug); 5218 method.setFollowRedirects(this.followRedirects); 5219 5220 method.setSubscriptionId(subscriptionId); 5221 generateTransactionHeader(method); 5222 5223 int statusCode = client.executeMethod(method); 5224 5225 if (statusCode == HttpStatus.SC_OK) { 5226 return true; 5227 } else { 5228 return false; 5229 } 5230 } 5231 5232 5238 public boolean subscribeMethod(Subscription subscription) 5239 throws HttpException, IOException 5240 { 5241 return subscribeMethod(subscription.getPath(), subscription.getId()); 5242 } 5243 5244 5249 public boolean unsubscribeMethod(String path, int subscriptionId) 5250 throws HttpException, IOException 5251 { 5252 setClient(); 5253 5254 UnsubscribeMethod method = new UnsubscribeMethod(path); 5255 method.setDebug(debug); 5256 method.setFollowRedirects(this.followRedirects); 5257 5258 method.addSubscriptionId(subscriptionId); 5259 generateTransactionHeader(method); 5260 5261 int statusCode = client.executeMethod(method); 5262 5263 if (statusCode == HttpStatus.SC_OK) { 5264 return true; 5265 } else { 5266 return false; 5267 } 5268 } 5269 5274 public boolean unsubscribeMethod(Subscription subscription) 5275 throws HttpException, IOException 5276 { 5277 return unsubscribeMethod(subscription.getPath(),subscription.getId()); 5278 } 5279 5280 5287 public boolean pollMethod(String contentLocation, int subscriptionId) 5288 throws HttpException, IOException 5289 { 5290 setClient(); 5291 5292 PollMethod method = new PollMethod(contentLocation); 5293 method.setDebug(debug); 5294 method.setFollowRedirects(this.followRedirects); 5295 5296 method.addSubscriptionId(subscriptionId); 5297 generateTransactionHeader(method); 5298 5299 int statusCode = client.executeMethod(method); 5300 5301 if (statusCode == HttpStatus.SC_MULTI_STATUS) { 5302 return method.getSubscriptionsWithEvents().size() > 0; 5303 } else { 5304 return false; 5305 } 5306 } 5307 5312 public boolean pollMethod(Subscription subscription) 5313 throws HttpException, IOException 5314 { 5315 return pollMethod(subscription.getContentLocation(), subscription.getId()); 5316 } 5317 5318 5319 5320 private static String getName(String uri) { 5321 String escapedName = URIUtil.getName( 5322 uri.endsWith("/") ? uri.substring(0, uri.length() - 1): uri); 5323 try { 5324 return URIUtil.decode(escapedName); 5325 } catch (URIException e) { 5326 return escapedName; 5327 } 5328 } 5329 5330 5335 private static String decodeMarks(String input) { 5336 char[] sequence = input.toCharArray(); 5337 StringBuffer decoded = new StringBuffer (sequence.length); 5338 for (int i = 0; i < sequence.length; i++) { 5339 if (sequence[i] == '%' && i < sequence.length - 2) { 5340 switch (sequence[i + 1]) { 5341 case '2': 5342 switch (sequence[i + 2]) { 5343 case 'd': 5344 case 'D': 5345 decoded.append('-'); 5346 i += 2; 5347 continue; 5348 case 'e': 5349 case 'E': 5350 decoded.append('.'); 5351 i += 2; 5352 continue; 5353 case '1': 5354 decoded.append('!'); 5355 i += 2; 5356 continue; 5357 case 'a': 5358 case 'A': 5359 decoded.append('*'); 5360 i += 2; 5361 continue; 5362 case '7': 5363 decoded.append('\''); 5364 i += 2; 5365 continue; 5366 case '8': 5367 decoded.append('('); 5368 i += 2; 5369 continue; 5370 case '9': 5371 decoded.append(')'); 5372 i += 2; 5373 continue; 5374 } 5375 break; 5376 case '5': 5377 switch (sequence[i + 2]) { 5378 case 'f': 5379 case 'F': 5380 decoded.append('_'); 5381 i += 2; 5382 continue; 5383 } 5384 break; 5385 case '7': 5386 switch (sequence[i + 2]) { 5387 case 'e': 5388 case 'E': 5389 decoded.append('~'); 5390 i += 2; 5391 continue; 5392 } 5393 break; 5394 } 5395 } 5396 decoded.append(sequence[i]); 5397 } 5398 return decoded.toString(); 5399 } 5400} 5401 | Popular Tags |