1 package com.ibm.webdav; 2 3 17 import java.io.*; 18 import java.net.*; 19 import java.util.*; 20 21 import javax.xml.parsers.*; 22 23 import org.w3c.dom.*; 24 25 import com.ibm.webdav.impl.*; 26 27 37 public class Resource implements Serializable { 38 39 41 public static String XMLVersion = "1.0"; 42 public static String DAV4JVersion = "2.0.10"; 43 44 47 public static String defaultXMLEncoding = "UTF-8"; 48 public static String defaultCharEncoding = "UTF-8"; 49 50 static { 51 try { 52 65 URL.setURLStreamHandlerFactory(new com.ibm.webdav.protocol.URLStreamHandlerFactory()); 66 } catch (Error exc) { 67 } 68 } 69 70 72 protected URL url = null; protected TargetSelector targetSelector; 75 protected ResourceContext context = new ResourceContext(); 77 protected IRResource impl = null; 79 protected byte[] cachedContents = null; 81 83 public Resource() { 84 this.url = null; 85 } 86 91 public Resource(Resource resource) throws WebDAVException { 92 this.url = resource.url; 93 this.context = resource.context; 94 this.targetSelector = resource.targetSelector; 95 this.impl = ResourceFactory.createResource(url, null); 96 } 97 110 public Resource(String url) throws WebDAVException { 111 try { 112 initialize(new URL(url), null); 113 } catch (java.io.IOException exc) { 114 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 115 } 116 } 117 130 public Resource(String url, TargetSelector targetSelector) throws WebDAVException { 131 try { 132 initialize(new URL(url), targetSelector); 133 } catch (java.io.IOException exc) { 134 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 135 } 136 } 137 147 public Resource(String protocol, String host, int port, String file) throws WebDAVException { 148 try { 149 initialize(new URL(protocol, host, port, file), null); 150 } catch (java.io.IOException exc) { 151 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 152 } 153 } 154 164 public Resource(String protocol, String host, String file) throws WebDAVException { 165 try { 166 initialize(new URL(protocol, host, file), null); 167 } catch (java.io.IOException exc) { 168 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 169 } 170 } 171 183 public Resource(URL url) throws WebDAVException { 184 try { 185 initialize(url, null); 186 } catch (java.io.IOException exc) { 187 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 188 } 189 } 190 203 public Resource(URL url, TargetSelector targetSelector) throws WebDAVException { 204 try { 205 initialize(url, targetSelector); 206 } catch (java.io.IOException exc) { 207 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 208 } 209 } 210 223 public Resource(URL context, String spec) throws WebDAVException { 224 try { 225 initialize(new URL(context, spec), null); 226 } catch (java.io.IOException exc) { 227 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Bad URL"); 228 } 229 } 230 250 public void addLabel(String label) throws WebDAVException { 251 } 252 258 public MultiStatus addProperties( PropertyName[] names, Element[] values) throws WebDAVException { 259 Document document = null; 261 try { 262 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 263 } catch(Exception e) { 264 throw new WebDAVException(WebDAVStatus.SC_PROCESSING,"Parsing problem"); 265 } 266 269 Element propertyUpdate = document.createElementNS("DAV:","D:propertyupdate"); 270 propertyUpdate.setAttribute("xmlns:D", "DAV:"); 271 document.appendChild(propertyUpdate); 272 Element set = document.createElementNS("DAV:","D:set"); 273 propertyUpdate.appendChild(set); 274 Element prop = document.createElementNS("DAV:","D:prop"); 275 set.appendChild(prop); 276 for (int i = 0; i < names.length; i++) { 277 prop.appendChild((Element) values[i]); 278 } 279 return setProperties(document); 280 } 281 287 public void addProperty( PropertyName name, Element value) throws WebDAVException { 288 PropertyName[] names = new PropertyName[1]; 289 names[0] = name; 290 Element[] values = new Element[1]; 291 values[0] = value; 292 MultiStatus result = addProperties(names, values); 293 294 if (result.getResponses().hasMoreElements()) { 296 Response response = (Response) result.getResponses().nextElement(); 297 if (response instanceof MethodResponse) { 298 int status = ((MethodResponse) response).getStatus(); 299 if (status != WebDAVStatus.SC_OK) { 300 throw new WebDAVException(status, WebDAVStatus.getStatusMessage(status)); 301 } 302 } else { 303 PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response).getPropertiesByPropName().elements().nextElement(); 304 if ((propertyValue.status != WebDAVStatus.SC_OK) && (propertyValue.status != WebDAVStatus.SC_FAILED_DEPENDENCY)) { 305 throw new WebDAVException(propertyValue.status, WebDAVStatus.getStatusMessage(propertyValue.status)); 306 } 307 } 308 } 309 } 310 318 public void cancelCheckOut() throws WebDAVException { 319 } 320 327 public void checkin() throws WebDAVException { 328 } 329 348 public void checkin(Activity activity, boolean makeCurrentTarget, boolean overwrite) throws WebDAVException { 349 } 350 368 public void checkin(boolean makeCurrentTarget, boolean overwrite) throws WebDAVException { 369 } 370 393 public TargetSelector checkOut() throws WebDAVException { 394 return checkOut(null); 395 } 396 420 public TargetSelector checkOut(Workspace workspace) throws WebDAVException { 421 return null; 422 } 423 441 public void checkPoint(boolean makeCurrentTarget, boolean overwrite) throws WebDAVException { 442 } 443 447 public void closeContentsOutputStream() throws WebDAVException { 448 this.closeContentsOutputStream(null); 449 } 450 458 public MultiStatus copy(String destinationURL) throws WebDAVException { 459 return copy(destinationURL, true, null); 460 } 461 478 public MultiStatus copy(String destinationURL, boolean overwrite, Vector propertiesToCopy) throws WebDAVException { 479 flushCaches(); 480 return impl.copy(context, destinationURL, overwrite, propertiesToCopy); 481 } 482 490 public MultiStatus delete() throws WebDAVException { 491 flushCaches(); 492 return impl.delete(context); 493 } 494 502 public Document differencesWith(Resource resource) throws WebDAVException { 503 return null; 504 } 505 511 public boolean equals(Resource resource) throws WebDAVException { 512 URL resourceURL = resource.getURL(); 513 514 int thisPort = url.getPort() == -1 ? 80 : url.getPort(); 515 int resourcePort = resourceURL.getPort() == -1 ? 80 : resourceURL.getPort(); 516 return url.getProtocol().equals(resourceURL.getProtocol()) && thisPort == resourcePort && url.getFile().equals(resourceURL.getFile()); 517 } 518 524 public boolean exists() throws WebDAVException { 525 boolean exists = true; 526 InputStream is = null; 527 try { 528 is = getContentsInputStream(); 529 } catch (WebDAVException exc) { 530 if (exc.getStatusCode() == WebDAVStatus.SC_NOT_FOUND) { 531 exists = false; 532 } 533 } 534 535 return exists; 536 } 537 540 public void flushCaches() throws WebDAVException { 541 cachedContents = null; 542 } 543 557 public ActiveLock getActiveLockFor(String principal) throws WebDAVException { 558 Enumeration locks = getLocks().elements(); 559 ActiveLock ownedLock = null; 560 while (ownedLock == null && locks.hasMoreElements()) { 561 ActiveLock lock = (ActiveLock) locks.nextElement(); 562 if (lock.getPrincipal().equals(principal)) { 563 ownedLock = lock; 564 } 565 } 566 return ownedLock; 567 } 568 576 public Activity getActivity() throws WebDAVException { 577 return null; 578 } 579 586 public byte[] getContents() throws WebDAVException { 587 try { 588 if (cachedContents == null) { 589 InputStream is = getContentsInputStream(); 590 int length = (int) getResponseContext().contentLength(); 591 if (length != -1) { 592 int rcvd = 0; 593 int size = 0; 594 cachedContents = new byte[length]; 595 do { 596 size += rcvd; 597 rcvd = is.read(cachedContents, size, length - size); 598 } while (size < length && rcvd != -1); 599 if (rcvd == -1) 600 601 cachedContents = resizeArray(cachedContents, size); 603 } else { 604 cachedContents = new byte[0]; 605 int inc = 8192; 606 int off = cachedContents.length; 607 int rcvd = 0; 608 do { 609 off += rcvd; 610 cachedContents = resizeArray(cachedContents, off + inc); 611 rcvd = is.read(cachedContents, off, inc); 612 } while (rcvd != -1); 613 cachedContents = resizeArray(cachedContents, off); 614 } 615 is.close(); 616 } 617 } catch (WebDAVException exc) { 618 throw exc; 619 } catch (java.io.IOException exc) { 620 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error"); 621 } 622 return cachedContents; 623 } 624 631 public InputStream getContentsInputStream() throws WebDAVException { 632 InputStream is = null; 633 if (cachedContents != null) { 634 is = new ByteArrayInputStream(cachedContents); 635 } else { 636 is = impl.getContentsInputStream(context); 637 } 638 return is; 639 } 640 647 public OutputStream getContentsOutputStream() throws WebDAVException { 648 flushCaches(); 649 return impl.getContentsOutputStream(context); 650 } 651 657 public Enumeration getLabels() throws WebDAVException { 658 return null; 659 } 660 665 public Vector getLocks() throws WebDAVException { 666 PropertyValue p = getProperty( PropertyName.createPropertyNameQuietly("DAV:lockdiscovery") ); 667 Element lockdiscovery = null; 668 if (p != null) { 669 lockdiscovery = p.value; 670 } 671 Vector allLocks = new Vector(); 672 if (lockdiscovery != null) { 673 NodeList activeLocks = ((Element) lockdiscovery).getElementsByTagNameNS("DAV:", "activelock"); 674 Element activeLock = null; 675 for (int i = 0; i < activeLocks.getLength(); i++) { 676 activeLock = (Element) activeLocks.item(i); 677 allLocks.addElement(new ActiveLock(activeLock)); 678 } 679 } 680 return allLocks; 681 } 682 691 public Enumeration getMergeCandidates() throws WebDAVException { 692 return null; 693 } 694 702 public Enumeration getMergePredecessors() throws WebDAVException { 703 return null; 704 } 705 706 718 public void getMetaInformation() throws WebDAVException { 719 if (cachedContents != null) { 720 return; } 722 impl.getMetaInformation(context); 723 } 724 732 public Enumeration getMutableProperties() throws WebDAVException { 733 return null; 734 } 735 736 749 public Element getOptions() throws WebDAVException { 750 return null; 751 } 752 753 758 public Collection getParentCollection() throws WebDAVException { 759 String parentURL = getURL().toString(); 760 761 int delimiterPosition = 0; 762 if (parentURL.endsWith("/")) { 763 delimiterPosition = parentURL.substring(0, parentURL.length() - 1).lastIndexOf("/"); 764 } else { 765 delimiterPosition = parentURL.lastIndexOf("/"); 766 } 767 parentURL = parentURL.substring(0, delimiterPosition + 1); 768 Collection parent = null; 769 try { 770 parent = new Collection(parentURL); 771 } catch (WebDAVException exc) { 772 throw exc; 773 } 774 return parent; 775 } 776 785 public Resource getPredecessor() throws WebDAVException { 786 return null; 787 } 788 789 797 public MultiStatus getProperties() throws WebDAVException { 798 return impl.getProperties(context); 799 } 800 808 public MultiStatus getProperties( PropertyName names[]) throws WebDAVException { 809 return impl.getProperties(context, names); 810 } 811 818 public PropertyValue getProperty( PropertyName name) throws WebDAVException { 819 PropertyName[] names = new PropertyName[1]; 820 names[0] = name; 821 Enumeration responses = getProperties(names).getResponses(); 822 PropertyResponse response = (PropertyResponse) responses.nextElement(); 823 Dictionary properties = response.getPropertiesByPropName(); 824 return (PropertyValue) properties.get(name); 825 } 826 834 public MultiStatus getPropertyNames() throws WebDAVException { 835 return impl.getPropertyNames(context); 836 } 837 844 public HTTPHeaders getRequestContext() throws WebDAVException { 845 return context.getRequestContext(); 846 } 847 854 public HTTPHeaders getResponseContext() throws WebDAVException { 855 return context.getResponseContext(); 856 } 857 public ResourceContext getContext() { 858 return context; 859 } 860 861 872 public Document getRevisionHistory() throws WebDAVException { 873 return null; 874 } 875 876 889 public String getRevisionId() throws WebDAVException { 890 return null; 891 } 892 893 898 public int getStatusCode() throws WebDAVException { 899 return context.getStatusCode().getStatusCode(); 900 } 901 906 public String getStatusMessage() throws WebDAVException { 907 return context.getStatusCode().getStatusMessage(); 908 } 909 918 public Enumeration getSuccessors() throws WebDAVException { 919 return null; 920 } 921 922 927 public TargetSelector getTargetSelector() throws WebDAVException { 928 return targetSelector; 929 } 930 935 public URL getURL() throws WebDAVException { 936 return url; 937 } 938 950 public String getWorkingResourceId() throws WebDAVException { 951 return null; 952 } 953 954 962 public Enumeration getWorkingResources() throws WebDAVException { 963 return null; 964 } 965 967 protected void initialize(URL url, TargetSelector targetSelector) throws WebDAVException { 968 try { 969 this.url = url; 970 this.targetSelector = targetSelector; 971 impl = ResourceFactory.createResource(url, targetSelector); 972 973 } catch (Exception exc) { 974 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Malformed URL"); 975 } 976 } 977 988 public boolean isAutomaticallyVersioned() throws WebDAVException { 989 return false; 990 } 991 992 1002public boolean isCheckedOut(Activity activity) throws WebDAVException { 1003 return false; 1004} 1005 1010public boolean isCollection() throws WebDAVException { 1011 boolean isCollection = false; 1012 PropertyValue pv = getProperty(PropertyName.pnResourcetype); 1013 if (pv != null) { 1014 Element resourcetype = (Element) pv.value; 1015 if (resourcetype.getElementsByTagNameNS("DAV:", "collection") != null) { 1016 isCollection = true; 1017 } 1018 } 1019 return isCollection; 1020} 1021 1029public boolean isLabeledWith(String label) throws WebDAVException { 1030 return false; 1031} 1032 1037public boolean isLocked() throws WebDAVException { 1038 return !getLocks().isEmpty(); 1040} 1041 1051public boolean isLockedByMe() throws WebDAVException { 1052 String principal = getRequestContext().getAuthorizationId(); 1053 Precondition precondition = getRequestContext().precondition(); 1054 if (precondition == null) { 1055 return false; } 1058 1059 Enumeration locks = getLocks().elements(); 1061 boolean isLockedByMe = false; 1062 while (locks.hasMoreElements()) { 1064 ActiveLock activeLock = (ActiveLock) locks.nextElement(); 1065 Condition condition = new Condition(getURL().getFile()); 1066 ConditionTerm term = new ConditionTerm(); 1067 StateToken stateToken = new StateToken(activeLock.getLockToken()); 1068 term.addConditionFactor(stateToken); 1069 condition.addConditionTerm(term); 1070 if (precondition.matches(condition) && activeLock.getPrincipal().equals(principal) && activeLock.getLockType().equals(ActiveLock.writeLock)) { 1071 isLockedByMe = true; 1072 break; 1073 } 1074 } 1075 return isLockedByMe; 1076} 1077 1092public boolean isMutable() throws WebDAVException { 1093 return false; 1094} 1095 1102boolean isVersioned() throws WebDAVException { 1103 return false; 1104} 1105 1116public MultiStatus lock(Document document) throws WebDAVException { 1117 String sPrefix = "D"; 1118 String userName = System.getProperties().getProperty("user.name"); 1119 Element owner = document.createElementNS("DAV:","D:owner"); 1120 owner.setAttribute("xmlns:D", "DAV:"); 1121 owner.appendChild(document.createTextNode(userName)); 1122 return lock(ActiveLock.exclusive, ActiveLock.writeLock, -1, owner); 1123} 1124 1140public MultiStatus lock(String scope, String type, int timeout, Element owner) throws WebDAVException { 1141 getRequestContext().precondition((String ) null); 1144 return impl.lock(context, scope, type, timeout, owner); 1145} 1146 1155public MultiStatus move(String destinationURL) throws WebDAVException { 1156 return move(destinationURL, true, null); 1157} 1158 1175public MultiStatus move(String destinationURL, boolean overwrite, Vector propertiesToMove) throws WebDAVException { 1176 flushCaches(); 1177 return impl.move(context, destinationURL, overwrite, propertiesToMove); 1178} 1179 1191public byte[] performWith(String args) throws WebDAVException { 1192 flushCaches(); return impl.performWith(context, args); 1194} 1195 1205public MultiStatus refreshLock(String lockToken, int timeout) throws WebDAVException { 1206 return impl.refreshLock(context, lockToken, timeout); 1207} 1208 1218public void removeLabel(String label) throws WebDAVException { 1219} 1220 1225public MultiStatus removeProperties( PropertyName[] names) throws WebDAVException { 1226 String sPrefix = "D"; 1227 Document document = null; 1228 1229 try { 1230 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 1231 } catch(Exception e) { 1232 throw new WebDAVException(WebDAVStatus.SC_PROCESSING,e.getMessage()); 1233 } 1234 1237 Element propertyUpdate = document.createElementNS("DAV:","D:propertyupdate"); 1238 1239 propertyUpdate.setAttribute("xmlns:D", "DAV:"); 1240 document.appendChild(propertyUpdate); 1241 Element remove = document.createElementNS("DAV:","D:remove"); 1242 1243 propertyUpdate.appendChild(remove); 1244 Element prop = document.createElementNS("DAV:","D:prop"); 1245 1246 remove.appendChild(prop); 1247 for (int i = 0; i < names.length; i++) { 1248 PropertyName name = names[i]; 1251 String prefix = "E"; 1252 if (name.ns.equals("DAV:")) { 1253 prefix = "D"; 1254 } 1255 Element newel = (Element) document.createElementNS(name.ns,prefix + ":" + name.local ); 1256 1257 if (prefix.equals("E")) { 1258 newel.setAttribute( "xmlns:E", name.ns ); 1259 } 1260 prop.appendChild( newel ); 1261 } 1262 return setProperties(document); 1263} 1264 1269public void removeProperty( PropertyName name) throws WebDAVException { 1270 PropertyName[] names = new PropertyName[1]; 1271 names[0] = name; 1272 MultiStatus result = removeProperties(names); 1273 setStatusCode(WebDAVStatus.SC_OK); 1274 if (result.getResponses().hasMoreElements()) { 1275 Response response = (Response) result.getResponses().nextElement(); 1276 if (response instanceof MethodResponse) { 1278 int status = ((MethodResponse) response).getStatus(); 1279 if (status != WebDAVStatus.SC_OK) { 1280 throw new WebDAVException(status, WebDAVStatus.getStatusMessage(status)); 1281 } 1282 } else { 1283 PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response).getPropertiesByPropName().elements().nextElement(); 1284 if ((propertyValue.status != WebDAVStatus.SC_OK) && (propertyValue.status != WebDAVStatus.SC_FAILED_DEPENDENCY)) { 1285 throw new WebDAVException(propertyValue.status, WebDAVStatus.getStatusMessage(propertyValue.status)); 1286 } 1287 } 1288 } 1289} 1290 1295private final static byte[] resizeArray(byte[] src, int new_size) { 1296 byte tmp[] = new byte[new_size]; 1297 System.arraycopy(src, 0, tmp, 0, (src.length < new_size ? src.length : new_size)); 1298 return tmp; 1299} 1300 1311public void setContents(byte[] value) throws WebDAVException { 1312 setContents( value, "text/plain" ); 1313} 1314 1325public void setContents(byte[] value, String mimetype) throws WebDAVException { 1326 context.getRequestContext().contentType(mimetype); 1327 OutputStream os = getContentsOutputStream(); 1328 try { 1329 os.write(value, 0, value.length); 1330 } catch (WebDAVException exc) { 1331 throw exc; 1332 } catch (java.io.IOException exc) { 1333 throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error"); 1334 } 1335 closeContentsOutputStream(); 1336} 1337 1344public MultiStatus setProperties( PropertyName[] names, Element[] values) throws WebDAVException { 1345 1346 Document document = null; 1347 1348 try { 1349 document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 1350 } catch(Exception e) { 1351 throw new WebDAVException(WebDAVStatus.SC_PROCESSING,e.getMessage()); 1352 } 1353 1356 Element propertyUpdate = document.createElementNS("DAV:","D:propertyupdate"); 1357 1358 propertyUpdate.setAttribute("xmlns:D", "DAV:"); 1359 document.appendChild(propertyUpdate); 1360 Element set = document.createElementNS("DAV:","D:set"); 1361 1362 propertyUpdate.appendChild(set); 1363 Element prop = document.createElementNS("DAV:","D:prop"); 1364 1365 set.appendChild(prop); 1366 for (int i = 0; i < names.length; i++) { 1367 prop.appendChild((Element) values[i]); 1368 } 1369 return setProperties(document); 1370} 1371 1379public MultiStatus setProperties(Document updates) throws WebDAVException { 1380 return impl.setProperties(context, updates); 1381} 1382 1388public void setProperty( PropertyName name, Element value) throws WebDAVException { 1389 PropertyName[] names = new PropertyName[1]; 1390 names[0] = name; 1391 Element[] values = new Element[1]; 1392 values[0] = value; 1393 int responseCode = 0; 1394 1395 MultiStatus result = setProperties(names, values); 1396 setStatusCode(WebDAVStatus.SC_OK); 1397 if (result.getResponses().hasMoreElements()) { 1398 Response response = (Response) result.getResponses().nextElement(); 1399 if (response instanceof MethodResponse) { 1401 responseCode = ((MethodResponse) response).getStatus(); 1402 if (responseCode != WebDAVStatus.SC_OK) { 1403 throw new WebDAVException(getStatusCode(), getStatusMessage()); 1404 } 1405 } else { 1406 PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response).getPropertiesByPropName().elements().nextElement(); 1407 responseCode = propertyValue.status; 1408 if ((responseCode != WebDAVStatus.SC_OK) && (responseCode != WebDAVStatus.SC_FAILED_DEPENDENCY)) { 1409 throw new WebDAVException(propertyValue.status, getStatusMessage()); 1410 } 1411 } 1412 } 1413} 1414 1423public void setRequestContext(HTTPHeaders value) throws WebDAVException { 1424 context.setRequestContext(value); 1425} 1426 1435public void setResponseContext(HTTPHeaders value) throws WebDAVException { 1436 context.setResponseContext(value); 1437} 1438 1443public void setStatusCode(int value) throws WebDAVException { 1444 context.getStatusCode().setStatusCode(value); 1445} 1446 1450public String toString() { 1451 String value = null; 1452 try { 1453 value = getURL().toString(); 1454 } catch (Exception exc) { 1455 } 1456 return value; 1457} 1458 1468public MultiStatus unlock(String lockToken) throws WebDAVException { 1469 return impl.unlock(context, lockToken); 1470} 1471 1474public void closeContentsOutputStream(String sContentType) throws WebDAVException { 1475 if(sContentType == null) { 1476 impl.closeContentsOutputStream(context); 1477 } else { 1478 impl.closeContentsOutputStream(context,sContentType); 1479 } 1480 1481} 1482} 1483 | Popular Tags |