1 15 16 package com.ibm.webdav.impl; 17 18 import java.io.*; 20 import java.net.*; 21 import java.rmi.*; 22 import java.rmi.server.*; 23 import java.util.*; 24 25 import javax.xml.parsers.*; 26 27 import org.w3c.dom.*; 28 29 import com.ibm.webdav.*; 30 import com.ibm.webdav.Collection; 31 import com.ibm.webdav.protocol.http.*; 32 33 49 public class ResourceImpl implements IRResource { 50 54 public static boolean debug = false; 55 public static java.util.Properties webdavProperties = 56 new java.util.Properties (); 57 private static Vector liveProperties = new Vector(); 59 61 static { 62 String classpath = System.getProperty("java.class.path"); 64 StringTokenizer paths = new StringTokenizer(classpath, ";"); 65 File propertiesFile = null; 66 boolean found = false; 67 68 while (!found && paths.hasMoreTokens()) { 69 String path = paths.nextToken(); 70 propertiesFile = new File(path, "dav4j.properties"); 71 found = propertiesFile.exists(); 72 } 73 74 if (found) { 75 try { 76 webdavProperties.load(new FileInputStream(propertiesFile)); 77 } catch (Exception exc) { 78 exc.printStackTrace(); 79 } 80 } 81 82 String debugString = webdavProperties.getProperty("debug"); 83 debug = (debugString != null) && debugString.equals("true"); 84 85 liveProperties.addElement(new LockDiscovery()); 87 } 88 89 protected ResourceContext context = new ResourceContext(); 91 92 protected String fileName = null; 94 protected URL url = null; 95 protected NamespaceManager namespaceManager = null; 96 protected PropertiesManager propertiesManager = null; 97 protected LockManager lockManager = null; 98 static protected SearchManager searchManager = null; 99 static protected UserAuthenticator authenticator = null; 100 101 public ResourceImpl() { 102 this.url = null; 103 this.fileName = null; 104 } 105 106 113 public ResourceImpl(URL url, String localName) throws WebDAVException { 114 this.url = url; 115 this.fileName = localName; 116 117 if (url.getProtocol().equals("rmi")) { 118 try { 119 UnicastRemoteObject.exportObject(this); 120 } catch (java.rmi.RemoteException exc) { 121 throw new WebDAVException( 122 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 123 "Unable to export rmi object"); 124 } 125 } 126 127 namespaceManager = ResourceImplFactory.createNamespaceManager(this); 130 propertiesManager = 131 ResourceImplFactory.createPropertiesManager(this, namespaceManager); 132 lockManager = 133 ResourceImplFactory.createLockManager( 134 this, 135 namespaceManager, 136 propertiesManager); 137 138 if(searchManager == null) { 139 searchManager = ResourceImplFactory.createSearchManager(this); 140 } 141 142 if(authenticator == null) { 143 authenticator = ResourceImplFactory.getAuthenticator(this); 144 } 145 146 147 getResponseContext().put("Cache-Control", "No-Cache"); 150 getResponseContext().put("Pragma", "No-Cache"); 151 152 getResponseContext().put("Server", "IBM DAV4J Server/1.0"); 154 } 155 156 164 public ResourceImpl( 165 URL url, 166 String localName, 167 TargetSelector targetSelector) 168 throws WebDAVException { 169 this.url = url; 170 this.fileName = localName; 171 172 if (url.getProtocol().equals("rmi")) { 173 try { 174 UnicastRemoteObject.exportObject(this); 175 } catch (java.rmi.RemoteException exc) { 176 throw new WebDAVException( 177 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 178 "Unable to export rmi object"); 179 } 180 } 181 182 namespaceManager = ResourceImplFactory.createNamespaceManager(this); 185 propertiesManager = 186 ResourceImplFactory.createPropertiesManager(this, namespaceManager); 187 lockManager = 188 ResourceImplFactory.createLockManager( 189 this, 190 namespaceManager, 191 propertiesManager); 192 if(searchManager == null) { 193 searchManager = ResourceImplFactory.createSearchManager(this); 194 } 195 196 if(authenticator == null) { 197 authenticator = ResourceImplFactory.getAuthenticator(this); 198 } 199 200 getResponseContext().put("Cache-Control", "No-Cache"); 203 getResponseContext().put("Pragma", "No-Cache"); 204 205 getResponseContext().put("Server", "IBM DAV4J Server/1.0"); 207 } 208 209 213 public void closeContentsOutputStream(ResourceContext context) 214 throws WebDAVException { 215 closeContentsOutputStream(context,null); 216 } 217 218 235 public MultiStatus atomicMove( 236 ResourceContext context, 237 String destinationURL, 238 boolean overwrite) 239 throws WebDAVException { 240 this.context = context; 241 242 setStatusCode(WebDAVStatus.SC_CREATED); 243 244 MultiStatus multiStatus = new MultiStatus(); 246 247 try { 248 if (!hasValidURI()) { 250 throw new WebDAVException( 251 WebDAVStatus.SC_BAD_REQUEST, 252 "Invalid URI"); 253 } 254 255 if (!exists()) { 257 throw new WebDAVException( 258 WebDAVStatus.SC_NOT_FOUND, 259 "Cannot copy a lock-null resource"); 260 } 261 262 URL destURL = new URL(this.url, destinationURL); 264 Resource destination = new Resource(destURL.toString()); 265 266 if (this.equals(destination)) { 268 throw new WebDAVException( 269 WebDAVStatus.SC_FORBIDDEN, 270 "Can't copy source on top of itself"); 271 } 272 273 destination.getRequestContext().precondition( 275 getRequestContext().precondition()); 276 destination.getRequestContext().authorization( 277 getRequestContext().authorization()); 278 279 if (destination.exists()) { 280 if (destination.isLocked()) { 281 if (!destination.isLockedByMe()) { 282 throw new WebDAVException( 283 WebDAVStatus.SC_LOCKED, 284 "Destination resource is locked"); 285 } 286 } 287 } 288 289 if (destination.exists()) { 291 if (!overwrite) { 292 throw new WebDAVException( 293 WebDAVStatus.SC_PRECONDITION_FAILED, 294 "Destination exists and overwrite not specified"); 295 } else { 296 setStatusCode(WebDAVStatus.SC_NO_CONTENT); 297 } 298 } 299 300 this.namespaceManager.move( 301 URLDecoder.decode(ResourceFactory.getRealPath(destURL))); 302 303 getResponseContext().contentType("text/xml"); 306 } catch (WebDAVException exc) { 307 throw exc; 308 } catch (java.net.MalformedURLException exc) { 309 throw new WebDAVException( 310 WebDAVStatus.SC_BAD_REQUEST, 311 "Malformed URL"); 312 } catch (java.io.IOException exc) { 313 throw new WebDAVException( 314 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 315 "IO Error"); 316 } 317 318 return multiStatus; 319 } 320 321 338 public MultiStatus copy( 339 ResourceContext context, 340 String destinationURL, 341 boolean overwrite, 342 Vector propertiesToCopy) 343 throws WebDAVException { 344 this.context = context; 345 346 setStatusCode(WebDAVStatus.SC_CREATED); 347 348 MultiStatus multiStatus = new MultiStatus(); 350 351 try { 352 if (!hasValidURI()) { 354 throw new WebDAVException( 355 WebDAVStatus.SC_BAD_REQUEST, 356 "Invalid URI"); 357 } 358 359 if (!exists()) { 361 throw new WebDAVException( 362 WebDAVStatus.SC_NOT_FOUND, 363 "Cannot copy a lock-null resource"); 364 } 365 366 URL destURL = new URL(this.url, destinationURL); 368 Resource destination = new Resource(destURL.toString()); 369 370 String sContentType = namespaceManager.getContentType(); 371 372 if (this.equals(destination)) { 374 throw new WebDAVException( 375 WebDAVStatus.SC_FORBIDDEN, 376 "Can't copy source on top of itself"); 377 } 378 379 destination.getRequestContext().precondition( 381 getRequestContext().precondition()); 382 destination.getRequestContext().authorization( 383 getRequestContext().authorization()); 384 385 if (destination.exists()) { 386 if (destination.isLocked()) { 387 if (!destination.isLockedByMe()) { 388 throw new WebDAVException( 389 WebDAVStatus.SC_LOCKED, 390 "Destination resource is locked"); 391 } 392 } 393 } 394 395 if (destination.exists()) { 397 if (!overwrite) { 398 throw new WebDAVException( 399 WebDAVStatus.SC_PRECONDITION_FAILED, 400 "Destination exists and overwrite not specified"); 401 } else { 402 setStatusCode(WebDAVStatus.SC_NO_CONTENT); 403 } 404 } 405 406 InputStream is = getContentsInputStream(context); 407 408 409 OutputStream os = destination.getContentsOutputStream(); 410 if(is != null) { 411 byte[] buf = new byte[8192]; 412 int numRead = 0; 413 414 while ((numRead = is.read(buf, 0, buf.length)) != -1) { 415 os.write(buf, 0, numRead); 416 } 417 is.close(); 418 } 419 420 destination.closeContentsOutputStream(sContentType); 421 422 WebDAVStatus savedStatusCode = getStatusCode(); 424 MultiStatus ms2 = copyProperties(destination, propertiesToCopy); 425 426 if (!ms2.isOK()) { 427 throw new WebDAVException( 431 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 432 "problem copying properties"); 433 } 434 435 setStatusCode(savedStatusCode); 436 437 String authorization = getRequestContext().authorization(); 440 destination.getRequestContext().setBasicAuthorization("root", ""); 441 442 Enumeration locks = destination.getLocks().elements(); 443 444 while (locks.hasMoreElements()) { 445 ActiveLock lock = (ActiveLock) locks.nextElement(); 446 447 try { 449 destination.unlock(lock.getLockToken()); 450 } catch (Exception exc) { 451 } 452 } 453 454 destination.getRequestContext().authorization(authorization); 455 456 getResponseContext().contentType("text/xml"); 459 } catch (WebDAVException exc) { 460 throw exc; 461 } catch (java.net.MalformedURLException exc) { 462 throw new WebDAVException( 463 WebDAVStatus.SC_BAD_REQUEST, 464 "Malformed URL"); 465 } catch (java.io.IOException exc) { 466 throw new WebDAVException( 467 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 468 "IO Error"); 469 } 470 471 return multiStatus; 472 } 473 474 487 protected MultiStatus copyProperties( 488 Resource destination, 489 Vector propertiesToCopy) 490 throws WebDAVException { 491 MultiStatus result = getProperties(context); 492 boolean bOnlySomeProperties = 493 ((propertiesToCopy != null) && (propertiesToCopy.size() > 0)); 494 495 Document document = null; 497 498 try { 499 DocumentBuilderFactory factory = 500 DocumentBuilderFactory.newInstance(); 501 factory.setNamespaceAware(true); 502 503 DocumentBuilder docbuilder = factory.newDocumentBuilder(); 504 document = docbuilder.newDocument(); 505 } catch (Exception e) { 506 throw new WebDAVException( 507 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 508 e.getMessage()); 509 } 510 511 Element propertyUpdate = 514 document.createElementNS("DAV:", "D:propertyupdate"); 515 516 propertyUpdate.setAttribute("xmlns:D", "DAV:"); 517 document.appendChild(propertyUpdate); 518 519 Element set = document.createElementNS("DAV:", "D:set"); 520 521 propertyUpdate.appendChild(set); 522 523 Element prop = document.createElementNS("DAV:", "D:prop"); 524 525 set.appendChild(prop); 526 527 Hashtable PropsWillCopy = new java.util.Hashtable (); 528 529 PropertyResponse response = 531 (PropertyResponse) result.getResponses().nextElement(); 532 Enumeration propertyNames = response.getPropertyNamesPN(); 533 534 while (propertyNames.hasMoreElements()) { 535 PropertyName name = (PropertyName) propertyNames.nextElement(); 536 PropertyValue value = response.getProperty(name); 537 Node node = document.importNode(value.value, true); 538 PropsWillCopy.put(name, node); 539 prop.appendChild((Element) node); 540 } 541 542 MultiStatus msRc = destination.setProperties(document); 544 545 Enumeration resources = msRc.getResponses(); 547 548 while (resources.hasMoreElements()) { 549 Response resmember = (Response) resources.nextElement(); 550 PropertyResponse propresponse = resmember.toPropertyResponse(); 551 Dictionary htProperties = 552 (Hashtable) propresponse.getPropertiesByPropName(); 553 Enumeration propertynames = htProperties.keys(); 554 555 while (propertynames.hasMoreElements()) { 556 PropertyName propname = 557 (PropertyName) propertynames.nextElement(); 558 PropertyValue pv = (PropertyValue) htProperties.get(propname); 559 int stat = pv.getStatus(); 560 561 if ((stat != WebDAVStatus.SC_OK) 562 && (stat != WebDAVStatus.SC_FAILED_DEPENDENCY)) { 563 Node node = (Node) PropsWillCopy.get(propname); 564 565 if ((propertiesToCopy == null) 566 || (propertiesToCopy.size() > 0 567 && !propertiesToCopy.contains(propname))) { 568 prop.removeChild(node); } 570 } 571 } 572 } 573 574 return destination.setProperties(document); 581 } 582 583 590 public static ResourceImpl create(URL url, String localName) 591 throws WebDAVException { 592 ResourceImpl resource = new ResourceImpl(url, localName); 593 594 if (resource.isCollection()) { 595 resource = new CollectionImpl(url, localName, null); 596 } 597 598 return resource; 599 } 600 601 613 public MultiStatus createPropPatchMultiStatus( 614 WebDAVException exc, 615 Document updates) 616 throws WebDAVException { 617 MultiStatus ms2 = new MultiStatus(); 618 Element el0 = updates.getDocumentElement(); 619 Element txel0 = (Element) el0; 620 NodeList nl = txel0.getElementsByTagNameNS("DAV:", "prop"); 621 622 624 int nllen = nl.getLength(); 625 int idx = 0; 626 String lxx = "xx"; 627 628 if (nllen <= 0) { 629 throw exc; 630 } 631 632 while (idx < nllen) { 633 Element txelProp = (Element) nl.item(idx); 634 Node node2 = txelProp.getFirstChild(); 635 Element txel2 = null; 636 637 try { 638 txel2 = (Element) node2; 639 } catch (Exception exc2) { 640 throw exc; 641 } 642 643 { 644 PropertyName pn = new PropertyName(txel2); 647 PropertyResponse response = 648 new PropertyResponse(getURL().toString()); 649 response.addProperty( 650 pn, 651 (Element) txel2.cloneNode(false), 652 exc.getStatusCode()); 653 ms2.addResponse(response); 654 } 655 656 idx++; 657 } 658 659 return ms2; 660 } 661 662 670 public MultiStatus delete(ResourceContext context) throws WebDAVException { 671 this.context = context; 672 673 setStatusCode(WebDAVStatus.SC_NO_CONTENT); 674 675 MultiStatus result = new MultiStatus(); 676 677 if (!hasValidURI()) { 679 throw new WebDAVException( 680 WebDAVStatus.SC_BAD_REQUEST, 681 "Invalid URI"); 682 } 683 684 CollectionImpl parent = (CollectionImpl) getParentCollection(); 686 687 if ((parent != null) && !parent.exists()) { 688 throw new WebDAVException( 689 WebDAVStatus.SC_CONFLICT, 690 "Parent collection does not exist"); 691 } 692 693 718 719 if (!exists()) { 720 throw new WebDAVException( 721 WebDAVStatus.SC_NOT_FOUND, 722 "Resource does not exist"); 723 } 724 725 if (isLocked() && !isLockedByMe()) { 727 throw new WebDAVException( 728 WebDAVStatus.SC_LOCKED, 729 "Resource is locked by another user"); 730 } 731 732 setStatusCode(WebDAVStatus.SC_NO_CONTENT); 735 736 namespaceManager.delete(); 738 propertiesManager.deleteProperties(); 739 getResponseContext().contentType("text/xml"); 740 741 return result; 742 } 743 744 754 protected MultiStatus doUnlock(String lockToken) throws WebDAVException { 755 String principal = getRequestContext().getAuthorizationId(); 756 757 Enumeration locks = getLocks().elements(); 759 760 ActiveLock lockToRemove = null; 762 763 while (locks.hasMoreElements()) { 764 ActiveLock activeLock = (ActiveLock) locks.nextElement(); 765 766 if (activeLock.getLockToken().equals(lockToken) 767 && (activeLock.getPrincipal().equals(principal) 768 || principal.equals("root") 769 || authenticator.isSuperUser(this) == true)) { 770 lockToRemove = activeLock; 771 772 break; 773 } 774 } 775 776 if (lockToRemove == null) { 777 throw new WebDAVException( 778 WebDAVStatus.SC_PRECONDITION_FAILED, 779 "resource is not locked by this principal"); 780 } 781 782 MultiStatus result = lockManager.unlock(lockToRemove); 783 784 locks = getLocks().elements(); 786 787 if (!locks.hasMoreElements() && namespaceManager.isLockNull()) { 788 propertiesManager.deleteProperties(); 789 } 790 791 getResponseContext().contentType("text/xml"); 792 793 return result; 794 } 795 796 802 public boolean exists() throws WebDAVException { 803 return namespaceManager.exists(); 804 } 805 806 public boolean authenticateUser(String user, String pwd) 807 throws WebDAVException { 808 boolean bIsAuthenticated = true; 809 810 if (authenticator != null) { 811 bIsAuthenticated = authenticator.authenticate(user, pwd); 812 } 813 814 return bIsAuthenticated; 815 } 816 817 831 protected ActiveLock getActiveLockFor( 832 String scope, 833 String type, 834 int timeout, 835 Element owner) 836 throws WebDAVException { 837 String principal = getRequestContext().getAuthorizationId(); 838 839 if (principal == null) { 840 throw new WebDAVException( 841 WebDAVStatus.SC_UNAUTHORIZED, 842 "missing authorization identification"); 843 } 844 845 if ((scope == null) 847 || (!scope.equals(ActiveLock.exclusive) 848 && !scope.equals(ActiveLock.shared))) { 849 throw new WebDAVException( 850 WebDAVStatus.SC_BAD_REQUEST, 851 "unsupported or missing lock scope: " + scope); 852 } 853 854 if ((type == null) || !type.equals(ActiveLock.writeLock)) { 855 throw new WebDAVException( 856 WebDAVStatus.SC_BAD_REQUEST, 857 "unsupported or missing lock type: " + type); 858 } 859 860 if (!exists()) { 862 namespaceManager.createLockNullResource(); 863 } 864 865 ActiveLock activeLock = new ActiveLock(); 867 activeLock.setScope(scope); 868 activeLock.setLockType(type); 869 activeLock.setDepth(Collection.shallow); 870 871 if (owner != null) { 872 activeLock.setOwner(owner); 873 } 874 875 if (timeout < 0) { 876 activeLock.setTimeout("Infinite"); 877 } else { 878 activeLock.setTimeout("Second-" + timeout); 879 } 880 881 String lockToken = "opaquelocktoken:" + new UUID(); 882 activeLock.setLockToken(lockToken); 883 activeLock.setPrincipal(principal); 884 885 return activeLock; 886 } 887 888 895 public InputStream getContentsInputStream(ResourceContext context) 896 throws WebDAVException { 897 this.context = context; 898 899 if (!hasValidURI()) { 901 throw new WebDAVException( 902 WebDAVStatus.SC_BAD_REQUEST, 903 "Invalid URI"); 904 } 905 906 InputStream is = namespaceManager.getContentsInputStream(); 907 908 return is; 909 } 910 911 918 public OutputStream getContentsOutputStream(ResourceContext context) 919 throws WebDAVException { 920 this.context = context; 921 922 if (!hasValidURI()) { 924 throw new WebDAVException( 925 WebDAVStatus.SC_BAD_REQUEST, 926 "Invalid URI"); 927 } 928 929 CollectionImpl parent = (CollectionImpl) getParentCollection(); 931 932 if ((parent != null) && !parent.exists()) { 933 throw new WebDAVException( 934 WebDAVStatus.SC_CONFLICT, 935 "Parent collection does not exist"); 936 } 937 938 964 965 if (exists() && isLocked() && !isLockedByMe()) { 967 throw new WebDAVException( 968 WebDAVStatus.SC_LOCKED, 969 "Resource is locked by another user"); 970 } 971 972 return namespaceManager.getContentsOutputStream(); 974 } 975 976 981 public com.ibm.webdav.ResourceContext getContext() { 982 return context; 983 } 984 985 987 public LockManager getLockManager() { 988 return lockManager; 989 } 990 991 994 public UserAuthenticator getUserAuthenticator() { 995 return ResourceImpl.authenticator; 996 } 997 998 1003 public Vector getLocks() throws WebDAVException { 1004 return lockManager.getLocks(); 1005 } 1006 1007 1021 public void getMetaInformation(ResourceContext context) 1022 throws WebDAVException { 1023 this.context = context; 1024 1025 InputStream is = getContentsInputStream(context); 1026 1027 try { 1028 is.close(); 1029 } catch (WebDAVException exc) { 1030 throw exc; 1031 } catch (java.io.IOException exc) { 1032 } 1033 } 1034 1035 1044 public String getName() throws WebDAVException { 1045 if ((fileName == null) && (url != null)) { 1046 fileName = ResourceFactory.getRealPath(url); 1047 } 1048 1049 String sDecoded = null; 1050 try { 1051 sDecoded = URLDecoder.decode(fileName, "UTF-8"); 1052 } catch (UnsupportedEncodingException e) { 1053 throw new WebDAVException( 1054 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1055 e.getLocalizedMessage()); 1056 } 1057 1058 return sDecoded; 1059 } 1060 1061 1066 public IRCollection getParentCollection() throws WebDAVException { 1067 String parentURL = getURL().toString(); 1068 String parentLocalName = getName(); 1069 int delimiterPosition = 0; 1070 1071 if(namespaceManager instanceof VersionedNamespaceManager) { 1072 if(((VersionedNamespaceManager)namespaceManager).isVersionURL(parentURL) == true) { 1073 parentURL = ((VersionedNamespaceManager)namespaceManager).getResourceURL(); 1074 try { 1075 parentLocalName = ResourceFactory.getRealPath(new URL(url,parentURL)); 1076 } catch (MalformedURLException e) { 1077 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage()); 1078 } 1079 } 1080 } 1081 1082 1083 if (parentURL.endsWith("/")) { 1084 delimiterPosition = 1085 parentURL.substring(0, parentURL.length() - 1).lastIndexOf("/"); 1086 } else { 1087 delimiterPosition = parentURL.lastIndexOf("/"); 1088 } 1089 1090 parentURL = parentURL.substring(0, delimiterPosition + 1); 1091 1092 if (parentLocalName.endsWith(File.separator)) { 1093 delimiterPosition = 1094 parentLocalName.substring( 1095 0, 1096 parentLocalName.length() - 1).lastIndexOf( 1097 File.separator); 1098 } else { 1099 delimiterPosition = parentLocalName.lastIndexOf(File.separator); 1100 } 1101 1102 parentLocalName = parentLocalName.substring(0, delimiterPosition + 1); 1103 1104 CollectionImpl parent = null; 1105 1106 try { 1107 URL url = new URL(getURL(),parentURL); 1108 parent = new CollectionImpl(url, parentLocalName, null); 1109 } catch (java.net.MalformedURLException exc) { 1110 exc.printStackTrace(); 1111 } 1112 1113 return parent; 1114 } 1115 1116 1121 public URL getParentURL() throws WebDAVException { 1122 String uri = getURL().getFile(); 1123 int delimiterPosition = 0; 1124 1125 if (uri.endsWith("/")) { 1126 delimiterPosition = 1127 uri.substring(0, uri.length() - 1).lastIndexOf("/"); 1128 } else { 1129 delimiterPosition = uri.lastIndexOf("/"); 1130 } 1131 1132 URL parentURL = null; 1133 1134 try { 1135 parentURL = 1136 new URL(getURL(), uri.substring(0, delimiterPosition + 1)); 1137 } catch (java.net.MalformedURLException exc) { 1138 throw new WebDAVException( 1139 WebDAVStatus.SC_BAD_REQUEST, 1140 "Malformed URL"); 1141 } 1142 1143 return parentURL; 1144 } 1145 1146 public MultiStatus getSearchSchema( 1147 ResourceContext context, 1148 SearchRequest searchReq) 1149 throws WebDAVException { 1150 this.context = context; 1151 1152 MultiStatus results = new MultiStatus(); 1154 1155 Document document = null; 1157 1158 try { 1159 DocumentBuilderFactory factory = 1160 DocumentBuilderFactory.newInstance(); 1161 factory.setNamespaceAware(true); 1162 1163 DocumentBuilder docbuilder = factory.newDocumentBuilder(); 1164 document = docbuilder.newDocument(); 1165 } catch (Exception e) { 1166 e.printStackTrace(System.err); 1167 throw new WebDAVException( 1168 WebDAVStatus.SC_PROCESSING, 1169 e.getMessage()); 1170 } 1171 1172 SearchSchema schema = searchManager.getSearchSchema(searchReq); 1173 1174 SchemaResponse response = 1175 new SchemaResponse(document, schema, searchReq.getScopeURI()); 1176 1177 results.addResponse(response); 1178 1179 return results; 1180 } 1181 1182 public MultiStatus executeSearch( 1183 ResourceContext context, 1184 SearchRequest searchReq) 1185 throws WebDAVException { 1186 this.context = context; 1187 1188 MultiStatus result = new MultiStatus(); 1190 1191 if (searchManager.validate(searchReq)) { 1192 Vector resources = searchManager.executeSearch(searchReq, this); 1193 1194 Enumeration members = resources.elements(); 1196 1197 while (members.hasMoreElements()) { 1198 ResourceImpl member = (ResourceImpl) members.nextElement(); 1199 1200 try { 1201 MultiStatus memberResult = null; 1202 1203 if (member.isCollection()) { 1204 if (searchReq.isAllSelectProperties()) { 1205 memberResult = 1206 ((CollectionImpl) member).getProperties( 1207 context, 1208 Collection.thisResource); 1209 } else { 1210 memberResult = 1211 ((CollectionImpl) member).getProperties( 1212 context, 1213 (PropertyName[]) searchReq 1214 .getSelectProperties() 1215 .toArray( 1216 new PropertyName[0]), 1217 Collection.thisResource); 1218 } 1219 } else { 1220 if (searchReq.isAllSelectProperties()) { 1221 memberResult = member.getProperties(context); 1222 } else { 1223 memberResult = 1224 member.getProperties( 1225 context, 1226 (PropertyName[]) searchReq 1227 .getSelectProperties() 1228 .toArray( 1229 new PropertyName[0])); 1230 } 1231 } 1232 1233 result.mergeWith(memberResult); 1234 } catch (WebDAVException exc) { 1235 MethodResponse response = 1236 new MethodResponse( 1237 member.getURL().toString(), 1238 exc.getStatusCode()); 1239 result.addResponse(response); 1240 } catch (Exception e) { 1241 e.printStackTrace(); 1242 throw new WebDAVException( 1243 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1244 "unable to get properties"); 1245 } 1246 } 1247 } else { 1248 throw new WebDAVException( 1249 WebDAVStatus.SC_BAD_REQUEST, 1250 "Invalid query"); 1251 } 1252 1253 getResponseContext().contentType("text/xml"); 1254 1255 return result; 1256 } 1257 1258 1266 public MultiStatus getProperties(ResourceContext context) 1267 throws WebDAVException { 1268 this.context = context; 1269 1270 if (!(exists() || namespaceManager.isLockNull())) { 1272 1273 throw new WebDAVException( 1274 WebDAVStatus.SC_NOT_FOUND, 1275 "Resource does not exist"); 1276 } 1277 1278 setStatusCode(WebDAVStatus.SC_MULTI_STATUS); 1279 getResponseContext().contentType("text/xml"); 1280 1281 return propertiesManager.getProperties(); 1282 } 1283 1284 1292 public MultiStatus getProperties( 1293 ResourceContext context, 1294 PropertyName[] names) 1295 throws WebDAVException { 1296 this.context = context; 1297 1298 if (!(exists() || namespaceManager.isLockNull())) { 1300 throw new WebDAVException( 1301 WebDAVStatus.SC_NOT_FOUND, 1302 "Resource does not exist"); 1303 } 1304 1305 getResponseContext().contentType("text/xml"); 1306 1307 return propertiesManager.getProperties(names); 1308 } 1309 1310 1317 public PropertyValue getProperty(PropertyName name) 1318 throws WebDAVException { 1319 PropertyName[] names = new PropertyName[1]; 1320 names[0] = name; 1321 1322 Enumeration responses = getProperties(context, names).getResponses(); 1323 PropertyResponse response = (PropertyResponse) responses.nextElement(); 1324 Dictionary properties = response.getPropertiesByPropName(); 1325 1326 return (PropertyValue) properties.get(name); 1327 } 1328 1329 1337 public MultiStatus getPropertyNames(ResourceContext context) 1338 throws WebDAVException { 1339 this.context = context; 1340 1341 if (!(exists() || namespaceManager.isLockNull())) { 1343 throw new WebDAVException( 1344 WebDAVStatus.SC_NOT_FOUND, 1345 "Resource does not exist"); 1346 } 1347 1348 getResponseContext().contentType("text/xml"); 1349 1350 return propertiesManager.getPropertyNames(); 1351 } 1352 1353 1360 public HTTPHeaders getRequestContext() throws WebDAVException { 1361 return context.getRequestContext(); 1362 } 1363 1364 1371 public HTTPHeaders getResponseContext() throws WebDAVException { 1372 return context.getResponseContext(); 1373 } 1374 1375 1380 public com.ibm.webdav.WebDAVStatus getStatusCode() { 1381 return context.getStatusCode(); 1382 } 1383 1384 1389 public URL getURL() throws WebDAVException { 1390 return url; 1391 } 1392 1393 1397 protected boolean hasLock(String lockToken) throws WebDAVException { 1398 boolean hasLock = false; 1399 Enumeration locks = getLocks().elements(); 1400 1401 while (!hasLock && locks.hasMoreElements()) { 1402 ActiveLock lock = (ActiveLock) locks.nextElement(); 1403 hasLock = lock.getLockToken().equals(lockToken); 1404 } 1405 1406 return hasLock; 1407 } 1408 1409 1413 public boolean hasValidURI() throws WebDAVException { 1414 return getName() != null; 1415 } 1416 1417 1420 protected void inheritParentDeepLocks() throws WebDAVException { 1421 CollectionImpl parent = (CollectionImpl) getParentCollection(); 1422 1423 Enumeration parentLocks = parent.getLocks().elements(); 1424 1425 while (parentLocks.hasMoreElements()) { 1426 ActiveLock parentLock = (ActiveLock) parentLocks.nextElement(); 1427 1428 if (parentLock.getDepth().equals(Collection.deep)) { 1429 if (!hasLock(parentLock.getLockToken())) { 1430 lock(parentLock); 1431 } 1432 } 1433 } 1434 } 1435 1436 1441 public boolean isCollection() throws WebDAVException { 1442 return namespaceManager.isCollection(); 1443 } 1444 1445 1450 public boolean isLocked() throws WebDAVException { 1451 return !getLocks().isEmpty(); 1453 } 1454 1455 1465 public boolean isLockedByMe() throws WebDAVException { 1466 String principal = getRequestContext().getAuthorizationId(); 1467 Precondition precondition = getRequestContext().precondition(); 1468 1469 if (precondition == null) { 1470 return false; 1472 } 1474 1475 Enumeration locks = getLocks().elements(); 1477 boolean isLockedByMe = false; 1478 1479 while (locks.hasMoreElements()) { 1481 ActiveLock activeLock = (ActiveLock) locks.nextElement(); 1482 Condition condition = new Condition(getURL().getFile()); 1483 ConditionTerm term = new ConditionTerm(); 1484 StateToken stateToken = new StateToken(activeLock.getLockToken()); 1485 term.addConditionFactor(stateToken); 1486 condition.addConditionTerm(term); 1487 1488 1489 if (precondition.matches(condition) 1490 && activeLock.getPrincipal().equals(principal) 1491 && activeLock.getLockType().equals(ActiveLock.writeLock)) { 1492 isLockedByMe = true; 1493 1494 break; 1495 } 1496 } 1497 1498 return isLockedByMe; 1499 } 1500 1501 1508 public boolean isSameServerAs(URL target) { 1509 return target.getHost().equals(url.getHost()) 1510 && (target.getPort() == url.getPort()); 1511 } 1512 1513 1518 public Document loadProperties() throws WebDAVException { 1519 Document propertiesDocument = propertiesManager.loadProperties(); 1520 updateLiveProperties(propertiesDocument); 1521 if (propertiesManager instanceof VersionedPropertiesManager) { 1522 if (((VersionedNamespaceManager) namespaceManager).isVersioned() 1523 == true) { 1524 ((VersionedPropertiesManager) propertiesManager).updateVersionProperties(propertiesDocument); 1525 } 1526 } 1527 1528 return propertiesDocument; 1529 } 1530 1531 1536 protected MultiStatus lock(ActiveLock activeLock) throws WebDAVException { 1537 Enumeration locks = getLocks().elements(); 1539 1540 while (locks.hasMoreElements()) { 1541 ActiveLock lock = (ActiveLock) locks.nextElement(); 1542 1543 if (lock.getScope().equals(ActiveLock.exclusive)) { 1544 throw new WebDAVException( 1545 WebDAVStatus.SC_LOCKED, 1546 "Resource has an exclusive lock"); 1547 } 1548 1549 if (lock.getScope().equals(ActiveLock.shared) 1550 && activeLock.getScope().equals(ActiveLock.exclusive)) { 1551 throw new WebDAVException( 1552 WebDAVStatus.SC_LOCKED, 1553 "Resource already has a shared lock"); 1554 } 1555 1556 if (lock.getScope().equals(ActiveLock.shared) 1557 && lock.getPrincipal().equals(activeLock.getPrincipal())) { 1558 throw new WebDAVException( 1559 WebDAVStatus.SC_LOCKED, 1560 "The principal already has a lock on this resource"); 1561 } 1562 } 1563 1564 return lockManager.lock(activeLock); 1565 } 1566 1567 1583 public MultiStatus lock( 1584 ResourceContext context, 1585 String scope, 1586 String type, 1587 int timeout, 1588 Element owner) 1589 throws WebDAVException { 1590 this.context = context; 1591 1592 ActiveLock activeLock = getActiveLockFor(scope, type, timeout, owner); 1593 MultiStatus result = lock(activeLock); 1594 1595 getResponseContext().lockToken(activeLock.getLockToken()); 1597 getResponseContext().contentType("text/xml"); 1598 1599 return result; 1600 } 1601 1602 1607 public static void main(String [] args) { 1608 if (args.length != 2) { 1610 System.err.println("Usage: java ResourceImpl url localName"); 1611 System.exit(-1); 1612 } 1613 1614 try { 1616 URL url = new URL(args[0]); 1617 String localName = args[1]; 1618 1619 System.setSecurityManager(new RMISecurityManager()); 1621 1622 ResourceImpl resource = ResourceImpl.create(url, localName); 1623 1624 String name = url.toString(); 1626 name = name.substring(name.indexOf(":") + 1); 1627 Naming.rebind(name, resource); 1628 1629 } catch (Exception exc) { 1630 System.err.println("ResourceImpl error: " + exc.getMessage()); 1631 exc.printStackTrace(); 1632 System.exit(-1); 1633 } 1634 } 1636 public MultiStatus createBinding( 1637 ResourceContext context, 1638 String bindName,String resourceURI) 1639 throws WebDAVException { 1640 this.context = context; 1641 1642 setStatusCode(WebDAVStatus.SC_CREATED); 1643 1644 MultiStatus result = new MultiStatus(); 1645 1646 try { 1647 if (!hasValidURI()) { 1649 throw new WebDAVException( 1650 WebDAVStatus.SC_BAD_REQUEST, 1651 "Invalid URI"); 1652 } 1653 1654 if (!exists()) { 1656 throw new WebDAVException( 1657 WebDAVStatus.SC_NOT_FOUND, 1658 "Cannot copy a lock-null resource"); 1659 } 1660 1661 if(isCollection() == false) { 1663 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,"URI indentified in BIND request must be an existing collection"); 1664 } 1665 1666 StringBuffer strbuf = new StringBuffer (); 1668 strbuf.append(url.toExternalForm()); 1669 1670 if(strbuf.toString().endsWith("/") == false) { 1671 strbuf.append("/"); 1672 } 1673 strbuf.append(bindName); 1674 1675 Resource destination = new Resource(strbuf.toString()); 1676 1677 Resource bindSource = new Resource(resourceURI); 1678 1679 if (bindSource.equals(destination)) { 1681 throw new WebDAVException( 1682 WebDAVStatus.SC_FORBIDDEN, 1683 "Can't copy source on top of itself"); 1684 } 1685 1686 destination.getRequestContext().precondition( 1687 getRequestContext().precondition()); 1688 destination.getRequestContext().authorization( 1689 getRequestContext().authorization()); 1690 1691 if (destination.exists()) { 1692 throw new WebDAVException( 1693 WebDAVStatus.SC_PRECONDITION_FAILED, 1694 "Destination exists and overwrite not specified"); 1695 } 1696 1697 this.namespaceManager.createBinding(bindName,new URL(resourceURI)); 1698 1699 getResponseContext().contentType("text/xml"); 1702 } catch (WebDAVException exc) { 1703 throw exc; 1704 } catch (java.net.MalformedURLException exc) { 1705 throw new WebDAVException( 1706 WebDAVStatus.SC_BAD_REQUEST, 1707 "Malformed URL"); 1708 } catch (java.io.IOException exc) { 1709 throw new WebDAVException( 1710 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1711 "IO Error"); 1712 } 1713 1714 return result; 1715 } 1716 1717 1734 public MultiStatus move( 1735 ResourceContext context, 1736 String destinationURL, 1737 boolean overwrite, 1738 Vector propertiesToMove) 1739 throws WebDAVException { 1740 this.context = context; 1741 1742 MultiStatus result = new MultiStatus(); 1743 1744 if (webdavProperties 1745 .getProperty("atomicmove") 1746 .equalsIgnoreCase("true")) { 1747 1748 if (isLocked() && !isLockedByMe()) { 1750 throw new WebDAVException( 1751 WebDAVStatus.SC_LOCKED, 1752 "Resource is locked by another user"); 1753 } 1754 1755 result = atomicMove(context, destinationURL, overwrite); 1756 1757 Enumeration propertyNames = context.getResponseContext().keys(); 1758 } else { 1759 result = copy(context, destinationURL, overwrite, propertiesToMove); 1763 1764 ResourceContext copyContext = context; 1765 WebDAVStatus oldStatus = context.getStatusCode(); 1766 result.mergeWith(delete(context)); 1767 1768 if (getStatusCode().getStatusCode() == 204) { 1769 context.setStatusCode(oldStatus); 1772 } 1773 1774 Enumeration propertyNames = copyContext.getResponseContext().keys(); 1775 1776 while (propertyNames.hasMoreElements()) { 1777 String name = (String ) propertyNames.nextElement(); 1778 String value = 1779 (String ) copyContext.getResponseContext().get(name); 1780 1781 if (!context.getResponseContext().containsKey(name)) { 1782 getResponseContext().put(name, value); 1783 } 1784 } 1785 } 1786 1787 getResponseContext().contentType("text/xml"); 1788 1789 return result; 1790 } 1791 1792 1797 public boolean parentIsLockedWith(String lockToken) 1798 throws WebDAVException { 1799 boolean isLocked = false; 1801 CollectionImpl parent = null; 1802 1803 try { 1804 parent = (CollectionImpl) getParentCollection(); 1805 } catch (Exception exc) { 1806 } 1807 1808 if (parent != null) { 1809 Enumeration locks = parent.getLocks().elements(); 1810 1811 while (locks.hasMoreElements()) { 1813 ActiveLock activeLock = (ActiveLock) locks.nextElement(); 1814 1815 if (activeLock.getLockToken().equals(lockToken)) { 1816 isLocked = true; 1817 1818 break; 1819 } 1820 } 1821 } 1822 1823 return isLocked; 1824 } 1825 1826 1838 public byte[] performWith(ResourceContext context, String args) 1839 throws WebDAVException { 1840 this.context = context; 1841 1842 return namespaceManager.performWith(args); 1843 } 1844 1845 1855 public MultiStatus refreshLock( 1856 ResourceContext context, 1857 String lockToken, 1858 int timeout) 1859 throws WebDAVException { 1860 this.context = context; 1861 1862 String principal = getRequestContext().getAuthorizationId(); 1863 1864 ActiveLock lockToRefresh = null; 1866 Enumeration activeLocks = getLocks().elements(); 1867 1868 while (activeLocks.hasMoreElements()) { 1869 ActiveLock activeLock = (ActiveLock) activeLocks.nextElement(); 1870 1871 if (activeLock.getLockToken().equals(lockToken) 1872 && (activeLock.getPrincipal().equals(principal) 1873 || principal.equals("root") 1874 || ResourceImpl.authenticator.isSuperUser(this) == true)) { 1875 lockToRefresh = activeLock; 1876 1877 break; 1878 } 1879 } 1880 1881 if (lockToRefresh == null) { 1882 throw new WebDAVException( 1883 WebDAVStatus.SC_PRECONDITION_FAILED, 1884 "principal does not own a lock"); 1885 } 1886 1887 lockToRefresh.setTimeout(timeout); 1889 getResponseContext().contentType("text/xml"); 1890 1891 return lockManager.refreshLock(lockToRefresh); 1892 } 1893 1894 1899 public void removeLiveProperties(Document propertiesDocument) { 1900 Element properties = (Element) propertiesDocument.getDocumentElement(); 1901 Element p = null; 1902 p = 1903 (Element) ((Element) properties) 1904 .getElementsByTagNameNS("DAV:", "supportedlock") 1905 .item(0); 1906 1907 if (p != null) { 1908 properties.removeChild(p); 1909 } 1910 1911 propertiesManager.removeLiveProperties(propertiesDocument); 1913 } 1914 1915 1920 public void saveProperties(Document propertiesDocument) 1921 throws WebDAVException { 1922 removeLiveProperties(propertiesDocument); 1924 if (propertiesManager instanceof VersionedPropertiesManager) { 1925 ( 1927 ( 1928 VersionedPropertiesManager) propertiesManager) 1929 .removeVersionProperties( 1930 propertiesDocument); 1931 } 1932 1933 propertiesManager.saveProperties(propertiesDocument); 1934 } 1935 1936 1946 public void setContents(byte[] value) throws WebDAVException { 1947 BufferedOutputStream os = 1948 (BufferedOutputStream) getContentsOutputStream(context); 1949 1950 for (int i = 0; i < value.length; i++) { 1951 try { 1952 os.write(value[i]); 1953 } catch (WebDAVException exc) { 1954 throw exc; 1955 } catch (java.io.IOException exc) { 1956 throw new WebDAVException( 1957 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 1958 "IO Error"); 1959 } 1960 } 1961 1962 closeContentsOutputStream(context); 1963 } 1964 1965 1970 void setContext(com.ibm.webdav.ResourceContext newContext) { 1971 context = newContext; 1972 } 1973 1974 1982 public MultiStatus setProperties(ResourceContext context, Document updates) 1983 throws WebDAVException { 1984 this.context = context; 1985 1986 if (!exists()) { 1988 throw new WebDAVException( 1989 WebDAVStatus.SC_NOT_FOUND, 1990 "Resource does not exist"); 1991 } 1992 1993 if (isLocked() && !isLockedByMe()) { 1995 return createPropPatchMultiStatus( 1996 new WebDAVException( 1997 WebDAVStatus.SC_LOCKED, 1998 "Resource is locked by another user"), 1999 updates); 2000 } 2001 2002 getResponseContext().contentType("text/xml"); 2003 2004 return propertiesManager.setProperties(updates); 2005 } 2006 2007 2012 public void setRequestContext(ResourceContext newRequestContext) { 2013 context.setRequestContext(newRequestContext.getRequestContext()); 2014 } 2015 2016 2021 public void setResponseContext( 2022 com.ibm.webdav.ResourceContext newResponseContext) { 2023 context.setResponseContext(newResponseContext.getResponseContext()); 2024 } 2025 2026 2031 void setStatusCode(int newStatusCode) { 2032 context.getStatusCode().setStatusCode(newStatusCode); 2033 } 2034 2035 2040 void setStatusCode(com.ibm.webdav.WebDAVStatus newStatusCode) { 2041 context.getStatusCode().setStatusCode(newStatusCode.getStatusCode()); 2042 } 2043 2044 2053 public String translatePathRelativeToMe(String target) 2054 throws WebDAVException { 2055 String result = target; 2056 URL targetURL = null; 2057 2058 try { 2059 targetURL = new URL(url, target); 2060 } catch (java.net.MalformedURLException exc) { 2061 throw new WebDAVException( 2062 WebDAVStatus.SC_BAD_REQUEST, 2063 "malformed target URL"); 2064 } 2065 2066 if (targetURL.getHost().equals(url.getHost())) { 2067 result = targetURL.getFile().replace('/', File.separatorChar); 2069 2070 String uri = url.getFile(); 2072 String docRoot = 2073 fileName.substring(0, fileName.length() - uri.length()); 2074 result = docRoot + result; 2075 } 2076 2077 return result; 2078 } 2079 2080 2089 public MultiStatus unlock(ResourceContext context, String lockToken) 2090 throws WebDAVException { 2091 this.context = context; 2092 2093 if (parentIsLockedWith(lockToken)) { 2095 throw new WebDAVException( 2096 WebDAVStatus.SC_METHOD_NOT_ALLOWED, 2097 "Must unlock depth lock from root collection having the lock."); 2098 } 2099 2100 MultiStatus result = doUnlock(lockToken); 2101 setStatusCode(WebDAVStatus.SC_NO_CONTENT); 2103 return result; 2104 } 2105 2106 2112 public void updateLiveProperties(Document document) 2113 throws WebDAVException { 2114 Element properties = document.getDocumentElement(); 2115 2116 removeLiveProperties(document); 2118 2119 Enumeration liveprops = liveProperties.elements(); 2121 2122 while (liveprops.hasMoreElements()) { 2123 LiveProperty liveProperty = (LiveProperty) liveprops.nextElement(); 2124 PropertyValue value = liveProperty.getValueFor(this); 2125 Element e = 2126 (Element) ((Element) properties) 2127 .getElementsByTagNameNS( 2128 liveProperty.getNSName(), 2129 liveProperty.getNSLocalName()) 2130 .item(0); 2131 2132 if ((e != null) && (value != null) && (value.getValue() != null)) { 2133 properties.removeChild(e); 2134 } 2135 2136 if ((value != null) && (value.getValue() != null)) { 2137 properties.appendChild(value.getValue()); 2138 } else { 2139 e = 2141 document.createElementNS( 2142 liveProperty.getNSName(), 2143 liveProperty.getPreferredPrefix() 2144 + ":" 2145 + liveProperty.getNSLocalName()); 2146 2147 e.setAttribute( 2148 "xmlns:" + liveProperty.getPreferredPrefix(), 2149 liveProperty.getNSName()); 2150 properties.appendChild(e); 2151 } 2152 2153 } 2155 2156 2158 2166 2167 Element creationDate = 2169 (Element) ((Element) properties) 2170 .getElementsByTagNameNS("DAV:", "creationdate") 2171 .item(0); 2172 2173 if (creationDate == null) { 2174 creationDate = document.createElementNS("DAV:", "D:creationdate"); 2175 2176 String cdstring = 2177 new SimpleISO8601DateFormat().format(new java.util.Date ()); 2178 creationDate.appendChild(document.createTextNode(cdstring)); 2179 properties.appendChild(creationDate); 2180 } 2181 2182 Element displayName = 2184 (Element) ((Element) properties) 2185 .getElementsByTagNameNS("DAV:", "displayname") 2186 .item(0); 2187 2188 if (displayName == null) { 2189 displayName = document.createElementNS("DAV:", "D:displayname"); 2190 2191 displayName.appendChild( 2192 document.createTextNode(URLEncoder.encode(getName()))); 2193 properties.appendChild(displayName); 2194 } 2195 2196 properties.appendChild( 2198 document.importNode(lockManager.getSupportedLock(), true)); 2199 2200 propertiesManager.updateLiveProperties(document); 2202 } 2203 2204 2209 public void checkin() throws WebDAVException { 2210 if (namespaceManager instanceof VersionedNamespaceManager) { 2211 if (hasValidURI() == false) { 2213 throw new WebDAVException( 2214 WebDAVStatus.SC_BAD_REQUEST, 2215 "Invalid URI"); 2216 } 2217 2218 if (exists() == false) { 2220 throw new WebDAVException( 2221 WebDAVStatus.SC_NOT_FOUND, 2222 "Cannot copy a lock-null resource"); 2223 } 2224 2225 if (((VersionedNamespaceManager) namespaceManager) 2226 .isCheckedOutVersion() 2227 == false) { 2228 throw new WebDAVException( 2229 WebDAVStatus.SC_BAD_REQUEST, 2230 "Not checked out"); 2231 } 2232 2233 if (isLocked() && !isLockedByMe()) { 2235 throw new WebDAVException( 2236 WebDAVStatus.SC_LOCKED, 2237 "Resource is locked by another user"); 2238 } 2239 2240 String sLoc = 2241 ((VersionedNamespaceManager) namespaceManager).checkin(); 2242 context.getResponseContext().location(sLoc); 2243 setStatusCode(WebDAVStatus.SC_CREATED); 2244 } else { 2245 throw new WebDAVException( 2246 WebDAVStatus.SC_METHOD_NOT_ALLOWED, 2247 "Repository does not support this method"); 2248 } 2249 } 2250 2251 2256 public void checkout() throws WebDAVException { 2257 if (namespaceManager instanceof VersionedNamespaceManager) { 2258 if (hasValidURI() == false) { 2260 throw new WebDAVException( 2261 WebDAVStatus.SC_BAD_REQUEST, 2262 "Invalid URI"); 2263 } 2264 2265 if (exists() == false) { 2267 throw new WebDAVException( 2268 WebDAVStatus.SC_NOT_FOUND, 2269 "Cannot copy a lock-null resource"); 2270 } 2271 2272 if (isLocked() && !isLockedByMe()) { 2274 throw new WebDAVException( 2275 WebDAVStatus.SC_LOCKED, 2276 "Resource is locked by another user"); 2277 } 2278 2279 ((VersionedNamespaceManager) namespaceManager).checkout(); 2280 setStatusCode(WebDAVStatus.SC_OK); 2281 } else { 2282 throw new WebDAVException( 2283 WebDAVStatus.SC_METHOD_NOT_ALLOWED, 2284 "Repository does not support this method"); 2285 } 2286 } 2287 2288 2297 public MultiStatus getVersionTreeReport( 2298 ResourceContext context, 2299 PropertyName[] names) 2300 throws WebDAVException { 2301 this.context = context; 2302 2303 if (namespaceManager.isVersionable() == false) { 2305 throw new WebDAVException( 2306 WebDAVStatus.SC_BAD_REQUEST, 2307 "Invalid depth on copy"); 2308 } 2309 MultiStatus result = new MultiStatus(); 2310 2312 Iterator memberIter = 2313 ((VersionedNamespaceManager) namespaceManager) 2314 .getVersions() 2315 .iterator(); 2316 while (memberIter.hasNext()) { 2317 ResourceImpl member = (ResourceImpl) memberIter.next(); 2318 try { 2319 MultiStatus memberResult = member.getProperties(context, names); 2320 2321 result.mergeWith(memberResult); 2322 } catch (WebDAVException exc) { 2323 MethodResponse response = 2324 new MethodResponse( 2325 member.getURL().toString(), 2326 exc.getStatusCode()); 2327 result.addResponse(response); 2328 } catch (Exception e) { 2329 e.printStackTrace(); 2330 throw new WebDAVException( 2331 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 2332 "unable to delete resource"); 2333 } 2334 } 2335 2336 getResponseContext().contentType("text/xml"); 2337 return result; 2338 2339 } 2340 2341 2348 public MultiStatus getVersionTreeReport(ResourceContext context) 2349 throws WebDAVException { 2350 this.context = context; 2351 2352 if (namespaceManager.isVersionable() == false) { 2354 throw new WebDAVException( 2355 WebDAVStatus.SC_BAD_REQUEST, 2356 "Invalid depth on copy"); 2357 } 2358 MultiStatus result = new MultiStatus(); 2359 2361 Iterator memberIter = 2362 ((VersionedNamespaceManager) namespaceManager) 2363 .getVersions() 2364 .iterator(); 2365 while (memberIter.hasNext()) { 2366 ResourceImpl member = (ResourceImpl) memberIter.next(); 2367 try { 2368 MultiStatus memberResult = member.getProperties(context); 2369 2370 result.mergeWith(memberResult); 2371 } catch (WebDAVException exc) { 2372 MethodResponse response = 2373 new MethodResponse( 2374 member.getURL().toString(), 2375 exc.getStatusCode()); 2376 result.addResponse(response); 2377 } catch (Exception e) { 2378 e.printStackTrace(); 2379 throw new WebDAVException( 2380 WebDAVStatus.SC_INTERNAL_SERVER_ERROR, 2381 "unable to delete resource"); 2382 } 2383 } 2384 2385 getResponseContext().contentType("text/xml"); 2386 return result; 2387 2388 } 2389 2390 2393 public void uncheckout() throws WebDAVException { 2394 if (namespaceManager instanceof VersionedNamespaceManager) { 2395 if (hasValidURI() == false) { 2397 throw new WebDAVException( 2398 WebDAVStatus.SC_BAD_REQUEST, 2399 "Invalid URI"); 2400 } 2401 2402 if (exists() == false) { 2404 throw new WebDAVException( 2405 WebDAVStatus.SC_NOT_FOUND, 2406 "Cannot copy a lock-null resource"); 2407 } 2408 2409 if (((VersionedNamespaceManager) namespaceManager) 2410 .isCheckedOutVersion() 2411 == false) { 2412 throw new WebDAVException( 2413 WebDAVStatus.SC_BAD_REQUEST, 2414 "Not checked out"); 2415 } 2416 2417 ((VersionedNamespaceManager) namespaceManager).uncheckout(); 2418 setStatusCode(WebDAVStatus.SC_OK); 2419 } else { 2420 throw new WebDAVException( 2421 WebDAVStatus.SC_METHOD_NOT_ALLOWED, 2422 "Repository does not support this method"); 2423 } 2424 2425 } 2426 2427 2430 public void versionControl() throws WebDAVException { 2431 if (namespaceManager instanceof VersionedNamespaceManager) { 2432 if (hasValidURI() == false) { 2434 throw new WebDAVException( 2435 WebDAVStatus.SC_BAD_REQUEST, 2436 "Invalid URI"); 2437 } 2438 2439 if (exists() == false) { 2441 throw new WebDAVException( 2442 WebDAVStatus.SC_NOT_FOUND, 2443 "Cannot copy a lock-null resource"); 2444 } 2445 2446 if (((VersionedNamespaceManager) namespaceManager).isVersioned() 2447 == true) { 2448 throw new WebDAVException( 2449 WebDAVStatus.SC_BAD_REQUEST, 2450 "Already versioned"); 2451 } 2452 2453 ((VersionedNamespaceManager) namespaceManager).versionControl(); 2454 setStatusCode(WebDAVStatus.SC_OK); 2455 } else { 2456 throw new WebDAVException( 2457 WebDAVStatus.SC_METHOD_NOT_ALLOWED, 2458 "Repository does not support this method"); 2459 } 2460 2461 } 2462 2463 2466 public List getAllowedMethods() throws WebDAVException { 2467 2468 return namespaceManager.getAllowedMethods(); 2469 } 2470 2471 2474 public void closeContentsOutputStream(ResourceContext context, String sContentType) throws WebDAVException { 2475 this.context = context; 2476 2477 if(sContentType == null) { 2478 namespaceManager.closeContentsOutputStream(); 2479 } else { 2480 namespaceManager.closeContentsOutputStream(sContentType); 2481 } 2482 2483 2484 Document propertiesDocument = loadProperties(); 2486 saveProperties(propertiesDocument); 2487 2488 inheritParentDeepLocks(); 2490 2491 } 2492} | Popular Tags |