0001: package com.ibm.webdav;
0002:
0003: /*
0004: * (C) Copyright IBM Corp. 2000 All rights reserved.
0005: *
0006: * The program is provided "AS IS" without any warranty express or
0007: * implied, including the warranty of non-infringement and the implied
0008: * warranties of merchantibility and fitness for a particular purpose.
0009: * IBM will not be liable for any damages suffered by you as a result
0010: * of using the Program. In no event will IBM be liable for any
0011: * special, indirect or consequential damages or lost profits even if
0012: * IBM has been advised of the possibility of their occurrence. IBM
0013: * will not be liable for any third party claims against you.
0014: *
0015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
0016: */
0017: import java.io.*;
0018: import java.net.*;
0019: import java.util.*;
0020:
0021: import javax.xml.parsers.*;
0022:
0023: import org.w3c.dom.*;
0024:
0025: import com.ibm.webdav.impl.*;
0026:
0027: /** A Resource implements a client proxy of the Resource interface locally where
0028: * possible, and by delegating the methods that must be implemented by the server
0029: * to another proxy stub as specified by the protocol given
0030: * in the resource URL. This allows WebDAV client applications to communicate
0031: * with a server through multiple RPC protocols, including no protocol at all for
0032: * local access.
0033: * @see com.ibm.webdav.CollectionP
0034: * @see com.ibm.webdav.Precondition#addStateTokenCondition
0035: * @author Jim Amsden <jamsden@us.ibm.com>
0036: */
0037: public class Resource implements Serializable {
0038:
0039: /** The version of XML used by WebDAV
0040: */
0041: public static String XMLVersion = "1.0";
0042: public static String DAV4JVersion = "2.0.10";
0043:
0044: /** The default charset encoding for XML documents and for storing
0045: * text/* MIME types on the server
0046: */
0047: public static String defaultXMLEncoding = "UTF-8";
0048: public static String defaultCharEncoding = "UTF-8";
0049:
0050: static {
0051: try {
0052: // (for now, always use UTF-8, explore using the platform default later)
0053: /*OutputStreamWriter temp = new OutputStreamWriter(System.out);
0054: String charEncoding = temp.getEncoding();
0055: if (charEncoding.startsWith("ISO")) {
0056: charEncoding = charEncoding.substring(3);
0057: }
0058: String XMLEncoding = MIME2Java.reverse(charEncoding);
0059: if (XMLEncoding != null) {
0060: defaultCharEncoding = charEncoding;
0061: defaultXMLEncoding = XMLEncoding;
0062: } else {
0063: System.err.println("Java encoding "+charEncoding+" is not supported");
0064: }*/
0065: URL
0066: .setURLStreamHandlerFactory(new com.ibm.webdav.protocol.URLStreamHandlerFactory());
0067: } catch (Error exc) {
0068: }
0069: }
0070:
0071: //------------------------------------------------------------------------------------
0072:
0073: protected URL url = null; // the resource URL, key, or identifier
0074: protected TargetSelector targetSelector; // identifier for revision selector
0075:
0076: // contexts for communicating HTTP and WebDAV headers (method contol couples)
0077: protected ResourceContext context = new ResourceContext();
0078: protected IRResource impl = null; // the implementation to delegate to
0079:
0080: // cache the contents to avoid unnecessary trips to the server
0081: protected byte[] cachedContents = null;
0082:
0083: /** The default constructor. Should be rarely used.
0084: */
0085: public Resource() {
0086: this .url = null;
0087: }
0088:
0089: /** A copy constructor. This copies by reference so the resources share the
0090: * same URL and contexts. TODO: probably should clone.
0091: * @param resource the resource to copy
0092: * @exception com.ibm.webdav.WebDAVException
0093: */
0094: public Resource(Resource resource) throws WebDAVException {
0095: this .url = resource.url;
0096: this .context = resource.context;
0097: this .targetSelector = resource.targetSelector;
0098: this .impl = ResourceFactory.createResource(url, null);
0099: }
0100:
0101: /** Construct a Resource with the given URL. This is the constructor most clients
0102: * will use to construct and access Resources using WebDAV. The resource having
0103: * the url may not exist as this constructor does not access the resource from
0104: * the server. Use exists() or attmept to get the contents of the resource to
0105: * see if it exists. Other constructors are provided using parameters for the
0106: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
0107: * may also be used to construct instances of a Resource.
0108: *
0109: * @param url the URL of the resource.
0110: * @exception com.ibm.webdav.WebDAVException
0111: * @see URLConnection
0112: * @see com.ibm.webdav.ResourceFactory
0113: */
0114: public Resource(String url) throws WebDAVException {
0115: try {
0116: initialize(new URL(url), null);
0117: } catch (java.io.IOException exc) {
0118: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0119: "Bad URL");
0120: }
0121: }
0122:
0123: /** Construct a Resource with the given URL. The resource having
0124: * the url may not exist as this constructor does not access the resource from
0125: * the server. Use exists() or attmept to get the contents of the resource to
0126: * see if it exists. Other constructors are provided using parameters for the
0127: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
0128: * may also be used to construct instances of a Resource.
0129: *
0130: * @param url the URL of the resource.
0131: * @param targetSelector the revision target selector for this Collection
0132: * @exception java.io.IOException
0133: * @see URLConnection
0134: * @see com.ibm.webdav.ResourceFactory
0135: */
0136: public Resource(String url, TargetSelector targetSelector)
0137: throws WebDAVException {
0138: try {
0139: initialize(new URL(url), targetSelector);
0140: } catch (java.io.IOException exc) {
0141: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0142: "Bad URL");
0143: }
0144: }
0145:
0146: /** Create a Resource from the given URL components.
0147: * @param protocol the protocol to use, http:, rmi:, or iiop:
0148: * @param host the name or IP addres of the server host. Using the client host name,
0149: * or 'localhost' without a port uses local access with no RPC or server required.
0150: * @param port the TCP port to use. HTTP uses 80 by default.
0151: * @param file the resource URL relative to the server including any query string, etc.
0152: * @exception WebDAVException
0153: * @see URLConnection
0154: * @see com.ibm.webdav.ResourceFactory
0155: */
0156: public Resource(String protocol, String host, int port, String file)
0157: throws WebDAVException {
0158: try {
0159: initialize(new URL(protocol, host, port, file), null);
0160: } catch (java.io.IOException exc) {
0161: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0162: "Bad URL");
0163: }
0164: }
0165:
0166: /** Create a Resource from the given URL components. This constructor uses the default
0167: * HTTP port.
0168: * @param protocol the protocol to use, http:, rmi:, or iiop:
0169: * @param host the name or IP addres of the server host. Using the client host name,
0170: * or 'localhost' without a port uses local access with no RPC or server required.
0171: * @param file the resource URL relative to the server including any query string, etc.
0172: * @exception com.ibm.webdav.WebDAVException
0173: * @see URLConnection
0174: * @see com.ibm.webdav.ResourceFactory
0175: */
0176: public Resource(String protocol, String host, String file)
0177: throws WebDAVException {
0178: try {
0179: initialize(new URL(protocol, host, file), null);
0180: } catch (java.io.IOException exc) {
0181: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0182: "Bad URL");
0183: }
0184: }
0185:
0186: /** Construct a Resource with the given URL. The resource having
0187: * the url may not exist as this constructor does not access the resource from
0188: * the server. Use exists() or attmept to get the contents of the resource to
0189: * see if it exists. Other constructors are provided using parameters for the
0190: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
0191: * may also be used to construct instances of a Resource.
0192: *
0193: * @param url the URL of the resource.
0194: * @exception java.io.IOException
0195: * @see URLConnection
0196: * @see com.ibm.webdav.ResourceFactory
0197: */
0198: public Resource(URL url) throws WebDAVException {
0199: try {
0200: initialize(url, null);
0201: } catch (java.io.IOException exc) {
0202: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0203: "Bad URL");
0204: }
0205: }
0206:
0207: /** Construct a Resource with the given URL. The resource having
0208: * the url may not exist as this constructor does not access the resource from
0209: * the server. Use exists() or attmept to get the contents of the resource to
0210: * see if it exists. Other constructors are provided using parameters for the
0211: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
0212: * may also be used to construct instances of a Resource.
0213: *
0214: * @param url the URL of the resource.
0215: * @param targetSelector the revision target selector for this Collection
0216: * @exception java.io.IOException
0217: * @see URLConnection
0218: * @see com.ibm.webdav.ResourceFactory
0219: */
0220: public Resource(URL url, TargetSelector targetSelector)
0221: throws WebDAVException {
0222: try {
0223: initialize(url, targetSelector);
0224: } catch (java.io.IOException exc) {
0225: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0226: "Bad URL");
0227: }
0228: }
0229:
0230: /** Construct a Resource with the given URL specification in the given context. The resource having
0231: * the url may not exist as this constructor does not access the resource from
0232: * the server. Use exists() or attmept to get the contents of the resource to
0233: * see if it exists. Other constructors are provided using parameters for the
0234: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
0235: * may also be used to construct instances of a Resource.
0236: *
0237: * @param context a URL giving the context in which the spec is evaluated
0238: * @param spec a URL whose missing parts are provided by the context
0239: * @exception com.ibm.webdav.WebDAVException
0240: * @see URLConnection
0241: * @see com.ibm.webdav.ResourceFactory
0242: */
0243: public Resource(URL context, String spec) throws WebDAVException {
0244: try {
0245: initialize(new URL(context, spec), null);
0246: } catch (java.io.IOException exc) {
0247: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
0248: "Bad URL");
0249: }
0250: }
0251:
0252: /**
0253: * Add a label to this revision of a versioned resource. The
0254: * versioned resource must not already have the label on any
0255: * revision, and the label cannot be the same as any revision
0256: * id. The label must be removed from one revision before it
0257: * can be added to a different revision. The operation will
0258: * fail if the resource is not a versioned resource.
0259: * <p>
0260: * Labels are used to provide meaningful names that distinguish
0261: * revisions of versioned resources. They can be used in the revision
0262: * selection rule of the workspace to specify what revision should
0263: * be used in that workspace. A specific label may be used to override
0264: * the workspace to access revisions.
0265: * <p>
0266: * A revision does not need to be checked out to add a label.
0267: *
0268: * @param label the label to add to the labels used to identify
0269: * this revision
0270: * @exception com.ibm.webdav.WebDAVException
0271: */
0272: public void addLabel(String label) throws WebDAVException {
0273: }
0274:
0275: /** Add properties to a resource.
0276: *
0277: * @param names an array of property names
0278: * @param value an array of property values
0279: * @exception com.ibm.webdav.WebDAVException
0280: */
0281: public MultiStatus addProperties(PropertyName[] names,
0282: Element[] values) throws WebDAVException {
0283: // create a propertyupdate document to set the values
0284: Document document = null;
0285: try {
0286: document = DocumentBuilderFactory.newInstance()
0287: .newDocumentBuilder().newDocument();
0288: } catch (Exception e) {
0289: throw new WebDAVException(WebDAVStatus.SC_PROCESSING,
0290: "Parsing problem");
0291: }
0292: //document.setVersion(Resource.XMLVersion);
0293: //document.setEncoding(Resource.defaultXMLEncoding);
0294:
0295: Element propertyUpdate = document.createElementNS("DAV:",
0296: "D:propertyupdate");
0297: propertyUpdate.setAttribute("xmlns:D", "DAV:");
0298: document.appendChild(propertyUpdate);
0299: Element set = document.createElementNS("DAV:", "D:set");
0300: propertyUpdate.appendChild(set);
0301: Element prop = document.createElementNS("DAV:", "D:prop");
0302: set.appendChild(prop);
0303: for (int i = 0; i < names.length; i++) {
0304: prop.appendChild((Element) values[i]);
0305: }
0306: return setProperties(document);
0307: }
0308:
0309: /** Add a property to a resource
0310: *
0311: * @param name the property name
0312: * @param value the property value
0313: * @exception com.ibm.webdav.WebDAVException
0314: */
0315: public void addProperty(PropertyName name, Element value)
0316: throws WebDAVException {
0317: PropertyName[] names = new PropertyName[1];
0318: names[0] = name;
0319: Element[] values = new Element[1];
0320: values[0] = value;
0321: MultiStatus result = addProperties(names, values);
0322:
0323: // raise any necessary exceptions
0324: if (result.getResponses().hasMoreElements()) {
0325: Response response = (Response) result.getResponses()
0326: .nextElement();
0327: if (response instanceof MethodResponse) {
0328: int status = ((MethodResponse) response).getStatus();
0329: if (status != WebDAVStatus.SC_OK) {
0330: throw new WebDAVException(status, WebDAVStatus
0331: .getStatusMessage(status));
0332: }
0333: } else {
0334: PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response)
0335: .getPropertiesByPropName().elements()
0336: .nextElement();
0337: if ((propertyValue.status != WebDAVStatus.SC_OK)
0338: && (propertyValue.status != WebDAVStatus.SC_FAILED_DEPENDENCY)) {
0339: throw new WebDAVException(
0340: propertyValue.status,
0341: WebDAVStatus
0342: .getStatusMessage(propertyValue.status));
0343: }
0344: }
0345: }
0346: }
0347:
0348: /**
0349: * Cancel the checkout of this working resource, delete the
0350: * working resource, and remove any predecessor/successor
0351: * relationships created by checkout or merge. An exception
0352: * is raised if the resource is not currently checked out.
0353: *
0354: * @exception com.ibm.webdav.WebDAVException
0355: */
0356: public void cancelCheckOut() throws WebDAVException {
0357: }
0358:
0359: /**
0360: * Checkin a resource creating a new immutable revision and
0361: * releasing the revision so other user agents may subsequently
0362: * check it out.
0363: *
0364: * @exception com.ibm.webdav.WebDAVException
0365: */
0366: public void checkin() throws WebDAVException {
0367: }
0368:
0369: /**
0370: * Checkin a resource releasing it so other user agents may check
0371: * it out. If overwrite is false, create a new revision and set
0372: * the predecessor and successor relationships. If overwrite is
0373: * true, the revision is updated in place and the previous contents
0374: * are lost. Effectively, no new revision is created, and the revision
0375: * id now refers to the updated revision. Overwrite will fail if the
0376: * revision being overwritten is not mutable.
0377: * <p>
0378: * If makeCurrentTarget is true, this revision becomes the default target
0379: * for the versioned resource. Otherwise the current target is unchanged.
0380: *
0381: * @param activity the activity associted with the changes made in this revision
0382: * @param makeCurrentTarget true means the new revision becomes the
0383: * target for the versioned resource. Otherwise the target is unchanged.
0384: * @param overwrite ture means overwrite the existing revision,
0385: * false means create a new revision.
0386: * @exception com.ibm.webdav.WebDAVException
0387: */
0388: public void checkin(Activity activity, boolean makeCurrentTarget,
0389: boolean overwrite) throws WebDAVException {
0390: }
0391:
0392: /**
0393: * Checkin a resource releasing it so other user agents may check
0394: * it out. If overwrite is false, create a new revision and set
0395: * the predecessor and successor relationships. If overwrite is
0396: * true, the revision is updated in place and the previous contents
0397: * are lost. Effectively, no new revision is created, and the revision
0398: * id now refers to the updated revision. Overwrite will fail if the
0399: * revision being overwritten is not mutable.
0400: * <p>
0401: * If makeCurrentTarget is true, this revision becomes the default target
0402: * for the versioned resource. Otherwise the current target is unchanged.
0403: *
0404: * @param makeCurrentTarget true means the new revision becomes the
0405: * target for the versioned resource. Otherwise the target is unchanged.
0406: * @param overwrite ture means overwrite the existing revision,
0407: * false means create a new revision.
0408: * @exception com.ibm.webdav.WebDAVException
0409: */
0410: public void checkin(boolean makeCurrentTarget, boolean overwrite)
0411: throws WebDAVException {
0412: }
0413:
0414: /**
0415: * Check out a resource in order to create a new working resource.
0416: * A resource is checked out in the context of the workspace, and
0417: * can only be checked out once in a given activity. The workspace
0418: * to use can be set in the request context. Checkout control
0419: * on versioned may be managed by locking the versioned resource or a
0420: * revision before checking out a revision.
0421: * <p>
0422: * CheckOut fails is the resource is not a versioned resource, is
0423: * currently checked out in the current activity, or the versioned
0424: * <p>
0425: * resource or revision is locked by another user. CheckOut also
0426: * fails if the current activity or workspace is locked. If the
0427: * versioned resource or revision is locked, the request context
0428: * must include a precondition containing the lock token.
0429: * <p>
0430: * If workspace is null, the server will return a workspace that
0431: * can be subsequently used to access the checked out working
0432: * resource.
0433: *
0434: * @return the TargetSelector for this working resource
0435: * @exception com.ibm.webdav.WebDAVException
0436: */
0437: public TargetSelector checkOut() throws WebDAVException {
0438: return checkOut(null);
0439: }
0440:
0441: /**
0442: * Check out a resource in order to create a new working resource.
0443: * A resource is checked out in the context of the workspace, and
0444: * can only be checked out once in a given activity. The workspace
0445: * to use can be set in the request context. Checkout control
0446: * on versioned may be managed by locking the versioned resource or a
0447: * revision before checking out a revision.
0448: * <p>
0449: * CheckOut fails is the resource is not a versioned resource, is
0450: * currently checked out in the current activity, or the versioned
0451: * <p>
0452: * resource or revision is locked by another user. CheckOut also
0453: * fails if the current activity or workspace is locked. If the
0454: * versioned resource or revision is locked, the request context
0455: * must include a precondition containing the lock token.
0456: * <p>
0457: * If workspace is null, the server will return a workspace that
0458: * can be subsequently used to access the checked out working
0459: * resource.
0460: *
0461: * @param workspace the Workspace in which the revision is checked out.
0462: * @return the TargetSelector for this working resource
0463: * @exception com.ibm.webdav.WebDAVException
0464: */
0465: public TargetSelector checkOut(Workspace workspace)
0466: throws WebDAVException {
0467: return null;
0468: }
0469:
0470: /**
0471: * Create a new revision of this resource, but keep it checked
0472: * out. If overwrite is false, create a new revision and set
0473: * the predecessor and successor relationships. If overwrite is
0474: * true, the revision is updated in place and the previous contents
0475: * are lost. Effectively, no new revision is created, and the revision
0476: * id now refers to the updated revision. Overwrite will fail if the
0477: * revision being overwritten is not mutable.
0478: * <p>
0479: * If makeCurrentTarget is true, this revision becomes the default target
0480: * for the versioned resource. Otherwise the current target is unchanged.
0481: *
0482: * @param makeCurrentTarget true means the new revision becomes the
0483: * target for the versioned resource. Otherwise the target is unchanged.
0484: * @param overwrite ture means overwrite the existing revision,
0485: * false means create a new revision.
0486: * @exception com.ibm.webdav.WebDAVException
0487: */
0488: public void checkPoint(boolean makeCurrentTarget, boolean overwrite)
0489: throws WebDAVException {
0490: }
0491:
0492: /** This method must be called after the client has completed writing to the contents
0493: * output stream that was obtained from <code>getContentsOutputStream()</code>.
0494: * @exception com.ibm.webdav.WebDAVException
0495: */
0496: public void closeContentsOutputStream() throws WebDAVException {
0497: this .closeContentsOutputStream(null);
0498: }
0499:
0500: /** Copy this resource to the destination URL. The destination resource must not already exist.
0501: * Partial results are possible, check the returned status for details.
0502: *
0503: * @param destinationURL the destination
0504: *
0505: * @return the status of the copy operation for each resource copied
0506: * @exception com.ibm.webdav.WebDAVException
0507: */
0508: public MultiStatus copy(String destinationURL)
0509: throws WebDAVException {
0510: return copy(destinationURL, true, null);
0511: }
0512:
0513: /** Copy this resource to the destination URL.
0514: * Partial results are possible, check the returned status for details.
0515: *
0516: * @param destinationURL the destination
0517: * @param overwrite true implies overrite the destination if it exists
0518: * @param propertiesToCopy a collection of properties that must be copied or
0519: * the method will fail. propertiesToCopy may have one of the following values:
0520: * <ul>
0521: * <li>null - ignore properties that cannot be copied</li>
0522: * <li>empty collection - all properties must be copied or the method will fail</li>
0523: * <li>a collection of URIs - a list of the properties that must be copied
0524: * or the method will fail</li>
0525: * </ul>
0526: *
0527: * @return the status of the copy operation for each resource copied
0528: * @exception com.ibm.webdav.WebDAVException
0529: */
0530: public MultiStatus copy(String destinationURL, boolean overwrite,
0531: Vector propertiesToCopy) throws WebDAVException {
0532: flushCaches();
0533: return impl.copy(context, destinationURL, overwrite,
0534: propertiesToCopy);
0535: }
0536:
0537: /** Delete this resouce from the server. The actual effect of the delete operation is
0538: * determined by the underlying repository manager. The visible effect to WebDAV
0539: * is that the resource is no longer available.
0540: *
0541: * @return a MultiStatus containing the status of the delete method on each
0542: * effected resource.
0543: * @exception com.ibm.webdav.WebDAVException
0544: */
0545: public MultiStatus delete() throws WebDAVException {
0546: flushCaches();
0547: return impl.delete(context);
0548: }
0549:
0550: /**
0551: * Return an XML document describing the differences between two
0552: * revisions, both contents and properties.
0553: *
0554: * @return an XML document describing the differences between
0555: * the given resource and this resource
0556: * @exception com.ibm.webdav.WebDAVException
0557: */
0558: public Document differencesWith(Resource resource)
0559: throws WebDAVException {
0560: return null;
0561: }
0562:
0563: /** Two Resources are equal if they have the same URL. In this case, port
0564: * number -1 and port number 80 are considered the same port for equality
0565: * purposes.
0566: * @return true if the resources have URLs indicating the same server resource.
0567: * @exception com.ibm.webdav.WebDAVException
0568: */
0569: public boolean equals(Resource resource) throws WebDAVException {
0570: URL resourceURL = resource.getURL();
0571:
0572: int this Port = url.getPort() == -1 ? 80 : url.getPort();
0573: int resourcePort = resourceURL.getPort() == -1 ? 80
0574: : resourceURL.getPort();
0575: return url.getProtocol().equals(resourceURL.getProtocol())
0576: && this Port == resourcePort
0577: && url.getFile().equals(resourceURL.getFile());
0578: }
0579:
0580: /** See if the contents of this resource exists. A resource exists
0581: * if it has contents or state maintained by a server.
0582: *
0583: * @return true if the contents exists, false otherwise
0584: * @exception com.ibm.webdav.WebDAVException
0585: */
0586: public boolean exists() throws WebDAVException {
0587: boolean exists = true;
0588: InputStream is = null;
0589: try {
0590: is = getContentsInputStream();
0591: } catch (WebDAVException exc) {
0592: if (exc.getStatusCode() == WebDAVStatus.SC_NOT_FOUND) {
0593: exists = false;
0594: }
0595: }
0596:
0597: return exists;
0598: }
0599:
0600: /** Flush any caches so that subsequent methods obtain fresh data from the server.
0601: * @exception com.ibm.webdav.WebDAVException
0602: */
0603: public void flushCaches() throws WebDAVException {
0604: cachedContents = null;
0605: }
0606:
0607: /** Get the active lock on this resource owned by the given principal if any.
0608: * NOTE: this method cannot be reliably implemented based on version 10 of
0609: * the WebDAV spec as an activelock element in a lockdiscovery does not contain
0610: * the authorization credentials of the owner of the lock. For now, this method
0611: * relies on an additional principal element in the activelock that contains
0612: * the required id. This is an IBM EXTENSTION. When WebDAV ACLs are introduced,
0613: * the principal will likely be added to the activelock element.
0614: *
0615: * @param principal the authorization id of the requesting principal
0616: *
0617: * @return the active lock owned by that principal or null if the resource is
0618: * not locked by that principal.
0619: * @exception com.ibm.webdav.WebDAVException
0620: */
0621: public ActiveLock getActiveLockFor(String principal)
0622: throws WebDAVException {
0623: Enumeration locks = getLocks().elements();
0624: ActiveLock ownedLock = null;
0625: while (ownedLock == null && locks.hasMoreElements()) {
0626: ActiveLock lock = (ActiveLock) locks.nextElement();
0627: if (lock.getPrincipal().equals(principal)) {
0628: ownedLock = lock;
0629: }
0630: }
0631: return ownedLock;
0632: }
0633:
0634: /**
0635: * Get the activity this revision was created in. Returns null
0636: * for un-versioned resources or revisions that weren't
0637: * updated in an activity.
0638: *
0639: * @return the Activity used to create this revision if any
0640: * @exception com.ibm.webdav.WebDAVException
0641: */
0642: public Activity getActivity() throws WebDAVException {
0643: return null;
0644: }
0645:
0646: /** Get the contents of this resource. This method does not decode text contents. The
0647: * caller should convert the result to a String using a character set based on the
0648: * contentType.
0649: *
0650: * @return the contents as a byte array
0651: * @exception com.ibm.webdav.WebDAVException
0652: */
0653: public byte[] getContents() throws WebDAVException {
0654: try {
0655: if (cachedContents == null) {
0656: InputStream is = getContentsInputStream();
0657: int length = (int) getResponseContext().contentLength();
0658: if (length != -1) {
0659: int rcvd = 0;
0660: int size = 0;
0661: cachedContents = new byte[length];
0662: do {
0663: size += rcvd;
0664: rcvd = is.read(cachedContents, size, length
0665: - size);
0666: } while (size < length && rcvd != -1);
0667: if (rcvd == -1)
0668:
0669: // premature EOF
0670: cachedContents = resizeArray(cachedContents,
0671: size);
0672: } else {
0673: cachedContents = new byte[0];
0674: int inc = 8192;
0675: int off = cachedContents.length;
0676: int rcvd = 0;
0677: do {
0678: off += rcvd;
0679: cachedContents = resizeArray(cachedContents,
0680: off + inc);
0681: rcvd = is.read(cachedContents, off, inc);
0682: } while (rcvd != -1);
0683: cachedContents = resizeArray(cachedContents, off);
0684: }
0685: is.close();
0686: }
0687: } catch (WebDAVException exc) {
0688: throw exc;
0689: } catch (java.io.IOException exc) {
0690: throw new WebDAVException(
0691: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error");
0692: }
0693: return cachedContents;
0694: }
0695:
0696: /** Get an InputStream for accessing the contents of this resource. This method may provide
0697: * more efficient access for resources that have large contents. Clients may want to create
0698: * a Reader to perform appropriate character conversions on this stream.
0699: *
0700: * @return an InputStream on the contents
0701: * @exception com.ibm.webdav.WebDAVException
0702: */
0703: public InputStream getContentsInputStream() throws WebDAVException {
0704: InputStream is = null;
0705: if (cachedContents != null) {
0706: is = new ByteArrayInputStream(cachedContents);
0707: } else {
0708: is = impl.getContentsInputStream(context);
0709: }
0710: return is;
0711: }
0712:
0713: /** Get an OutputStream for setting the contents of this resource. This method may provide
0714: * more efficient access for resources that have large contents. Remember to call
0715: * closeContentsOutputStream() when all the data has been written.
0716: *
0717: * @return an OutputStream to set the contents
0718: * @exception com.ibm.webdav.WebDAVException
0719: */
0720: public OutputStream getContentsOutputStream()
0721: throws WebDAVException {
0722: flushCaches();
0723: return impl.getContentsOutputStream(context);
0724: }
0725:
0726: /**
0727: * Return all the labels on this revision, not including its revision id.
0728: *
0729: * @return an Enumeration of revision labels that identify this revision
0730: * @exception com.ibm.webdav.WebDAVException
0731: */
0732: public Enumeration getLabels() throws WebDAVException {
0733: return null;
0734: }
0735:
0736: /** Get the locks that exist on this resource.
0737: *
0738: * @return a Vector of ActiveLock objects
0739: * @exception com.ibm.webdav.WebDAVException
0740: */
0741: public Vector getLocks() throws WebDAVException {
0742: PropertyValue p = getProperty(PropertyName
0743: .createPropertyNameQuietly("DAV:lockdiscovery"));
0744: Element lockdiscovery = null;
0745: if (p != null) {
0746: lockdiscovery = p.value;
0747: }
0748: Vector allLocks = new Vector();
0749: if (lockdiscovery != null) {
0750: NodeList activeLocks = ((Element) lockdiscovery)
0751: .getElementsByTagNameNS("DAV:", "activelock");
0752: Element activeLock = null;
0753: for (int i = 0; i < activeLocks.getLength(); i++) {
0754: activeLock = (Element) activeLocks.item(i);
0755: allLocks.addElement(new ActiveLock(activeLock));
0756: }
0757: }
0758: return allLocks;
0759: }
0760:
0761: /**
0762: * Return a list of activities on different lines of descent
0763: * for this revision that are candidates for merging. Returns
0764: * null if the resource is not a versioned resource.
0765: *
0766: * @return an Enumeration of Activities that specify revisions
0767: * on different lines of descent.
0768: * @exception com.ibm.webdav.WebDAVException
0769: */
0770: public Enumeration getMergeCandidates() throws WebDAVException {
0771: return null;
0772: }
0773:
0774: /**
0775: * Get the predecessors of this revision that were established
0776: * by merging changes from another activity. The list may be empty.
0777: *
0778: * @return an Enumeration of Resources that are the merge
0779: * predecessors of this revision.
0780: * @exception com.ibm.webdav.WebDAVException
0781: */
0782: public Enumeration getMergePredecessors() throws WebDAVException {
0783: return null;
0784: }
0785:
0786: /** This method can be used for obtaining meta-information about this resource without
0787: * actually reading the resource contents. This meta-information is maintained by the server
0788: * in addition to the resource properties.</p>
0789: * <p>
0790: * After this call, the resource context has been updated and
0791: * <code>getStatusCode()</code>, <code>getStatusMessage()</code>, and <code>getResponseContext()</code>
0792: * as well as all the ResourceContext methods return updated values based on the current
0793: * state of the resource.</p>
0794: * <p>This methods corresponds to the HTTP HEAD method.</p>
0795: *
0796: * @exception com.ibm.webdav.WebDAVException
0797: */
0798: public void getMetaInformation() throws WebDAVException {
0799: if (cachedContents != null) {
0800: return; // already have them
0801: }
0802: impl.getMetaInformation(context);
0803: }
0804:
0805: /**
0806: * A resource may have a number of mutable properties. These are
0807: * properties that may change even when the resource is checked in.
0808: * Changes to these properties does not require a new revision.
0809: *
0810: * @return an Enumeration of the mutable properties of this resource
0811: * @exception com.ibm.webdav.WebDAVException
0812: */
0813: public Enumeration getMutableProperties() throws WebDAVException {
0814: return null;
0815: }
0816:
0817: /**
0818: * Get the options for this resource. Versioning options
0819: * are established by the server and include:
0820: * <ul>
0821: * <li>Mutable/immutable revisions</li>
0822: * <li>Supports multiple activities </li>
0823: * <li>Is automatically versioned</li>
0824: * </ul>
0825: *
0826: * @return an XML Element containing the options for
0827: * this resource
0828: * @exception com.ibm.webdav.WebDAVException
0829: */
0830: public Element getOptions() throws WebDAVException {
0831: return null;
0832: }
0833:
0834: /** Get the collection containing this resource.
0835: *
0836: * @return the parent collection
0837: * @exception com.ibm.webdav.WebDAVException
0838: */
0839: public Collection getParentCollection() throws WebDAVException {
0840: String parentURL = getURL().toString();
0841:
0842: int delimiterPosition = 0;
0843: if (parentURL.endsWith("/")) {
0844: delimiterPosition = parentURL.substring(0,
0845: parentURL.length() - 1).lastIndexOf("/");
0846: } else {
0847: delimiterPosition = parentURL.lastIndexOf("/");
0848: }
0849: parentURL = parentURL.substring(0, delimiterPosition + 1);
0850: Collection parent = null;
0851: try {
0852: parent = new Collection(parentURL);
0853: } catch (WebDAVException exc) {
0854: throw exc;
0855: }
0856: return parent;
0857: }
0858:
0859: /**
0860: * Get the predecessor of this revision. That is, get the
0861: * revision from which this revision was checked out.
0862: * Returns null if the Resource has no predecessor.
0863: *
0864: * @return the predecessor of this revision or null if the revision
0865: * has no successor.
0866: * @exception com.ibm.webdav.WebDAVException
0867: */
0868: public Resource getPredecessor() throws WebDAVException {
0869: return null;
0870: }
0871:
0872: /** Get all the properties of this resource.
0873: *
0874: * @return a MultiStatus of PropertyResponses. It should contain only one
0875: * response element.
0876: * @see com.ibm.webdav.MultiStatus
0877: * @see com.ibm.webdav.PropertyResponse
0878: * @exception com.ibm.webdav.WebDAVException
0879: */
0880: public MultiStatus getProperties() throws WebDAVException {
0881: return impl.getProperties(context);
0882: }
0883:
0884: /** Get the named properties of this resource.
0885: *
0886: * @param names an arrary of property names to retrieve
0887: *
0888: * @return a MultiStatus of PropertyResponses
0889: * @exception com.ibm.webdav.WebDAVException
0890: * @see com.ibm.webdav.PropertyResponse
0891: */
0892: public MultiStatus getProperties(PropertyName names[])
0893: throws WebDAVException {
0894: return impl.getProperties(context, names);
0895: }
0896:
0897: /** Get the value of the given property for this resource.
0898: *
0899: * @param name the name of the property to retrieve
0900: *
0901: * @return PropertyValue or null if the resource does not have the requested property
0902: * @exception com.ibm.webdav.WebDAVException
0903: */
0904: public PropertyValue getProperty(PropertyName name)
0905: throws WebDAVException {
0906: PropertyName[] names = new PropertyName[1];
0907: names[0] = name;
0908: Enumeration responses = getProperties(names).getResponses();
0909: PropertyResponse response = (PropertyResponse) responses
0910: .nextElement();
0911: Dictionary properties = response.getPropertiesByPropName();
0912: return (PropertyValue) properties.get(name);
0913: }
0914:
0915: /** Get the names of all properties for this resource. The result is similar to
0916: * getProperties(), but the properties have no values.
0917: *
0918: * @return a MultiStatus of PropertyResponses
0919: * (PropertyValue.value is always null, PropertyValue.status contains the status)
0920: * @exception com.ibm.webdav.WebDAVException
0921: * @see com.ibm.webdav.PropertyResponse
0922: */
0923: public MultiStatus getPropertyNames() throws WebDAVException {
0924: return impl.getPropertyNames(context);
0925: }
0926:
0927: /** Get the request context for this resource. The context contains information
0928: * used by methods on a resource when the method is called.
0929: *
0930: * @return the ResourceContext providing information that controls
0931: * method execution.
0932: * @exception com.ibm.webdav.WebDAVException
0933: */
0934: public HTTPHeaders getRequestContext() throws WebDAVException {
0935: return context.getRequestContext();
0936: }
0937:
0938: /** Get the response context for this resource. The context contains information
0939: * returned from invocations of methods on a resource.
0940: *
0941: * @return the ResourceContext providing information that
0942: * is returned by method execution.
0943: * @exception com.ibm.webdav.WebDAVException
0944: */
0945: public HTTPHeaders getResponseContext() throws WebDAVException {
0946: return context.getResponseContext();
0947: }
0948:
0949: public ResourceContext getContext() {
0950: return context;
0951: }
0952:
0953: /**
0954: * Get the revision history for a versioned resource. The revision
0955: * history lists the revisions of a resource and their predecessors
0956: * and successors. The format of the document is given in section
0957: * Revision History. The document will not contain any revisions
0958: * if the resource is not versioned.
0959: *
0960: * @return an XML document containing the revision history of the
0961: * associated versioned resource.
0962: * @exception com.ibm.webdav.WebDAVException
0963: */
0964: public Document getRevisionHistory() throws WebDAVException {
0965: return null;
0966: }
0967:
0968: /**
0969: * Get the system-assigned revision id for this revision. This
0970: * revision name cannot be changed, and cannot be reused if
0971: * this revision is deleted. Returns NULL if the resource is
0972: * not versioned.
0973: * <p>
0974: * The revision id must be unique for the revision across all
0975: * time. Servers may choose to use an opaque identifier consisting
0976: * of a time stamp similar to UUIDs for lock tokens.
0977: *
0978: * @return the revision id of this revision of a versioned resource
0979: * @exception com.ibm.webdav.WebDAVException
0980: */
0981: public String getRevisionId() throws WebDAVException {
0982: return null;
0983: }
0984:
0985: /** Get the status code corresponding to the last method execution.
0986: *
0987: * @return the status code as defined by HTTP/1.1 and the WebDAV extensions.
0988: * @exception com.ibm.webdav.WebDAVException
0989: */
0990: public int getStatusCode() throws WebDAVException {
0991: return context.getStatusCode().getStatusCode();
0992: }
0993:
0994: /** Get the status message corresponding to the last method execution.
0995: *
0996: * @return the status message as defined by HTTP/1.1 and the WebDAV extensions.
0997: * @exception com.ibm.webdav.WebDAVException
0998: */
0999: public String getStatusMessage() throws WebDAVException {
1000: return context.getStatusCode().getStatusMessage();
1001: }
1002:
1003: /**
1004: * Get the immediate successors of this revision. That is, get the revisions
1005: * that were created by checking out this revision.
1006: * The list may be empty.
1007: *
1008: * @return an Enumeration of Resources that are
1009: * successors of this revision.
1010: * @exception com.ibm.webdav.WebDAVException
1011: */
1012: public Enumeration getSuccessors() throws WebDAVException {
1013: return null;
1014: }
1015:
1016: /** Get the TargetSelector that identifies this resource revision.
1017: *
1018: * @return the TargetSelector for this revision
1019: * @exception com.ibm.webdav.WebDAVException
1020: */
1021: public TargetSelector getTargetSelector() throws WebDAVException {
1022: return targetSelector;
1023: }
1024:
1025: /** Get the name that identifies this resource.
1026: *
1027: * @return the URL for this resource
1028: * @exception com.ibm.webdav.WebDAVException
1029: */
1030: public URL getURL() throws WebDAVException {
1031: return url;
1032: }
1033:
1034: /**
1035: * Get the system-assigned working resource id for this revision.
1036: * Returns NULL if the resource is not versioned or is not checked out.
1037: * <p>
1038: * The working resource id must be unique for all working resources
1039: * of this revision. Servers may choose to use an opaque identifier consisting
1040: * of a time stamp similar to UUIDs for lock tokens.
1041: *
1042: * @return the working resource id of this working resource of a revision of
1043: * a versioned resource
1044: * @exception com.ibm.webdav.WebDAVException
1045: */
1046: public String getWorkingResourceId() throws WebDAVException {
1047: return null;
1048: }
1049:
1050: /**
1051: * Get the current working resources of this revision. Returns an
1052: * empty Enumeration if this revision has no current working resources.
1053: * Returns null if this resource is not a revision.
1054: *
1055: * @return An Enumeration of current working resources of this VersionedResource
1056: * @exception com.ibm.webdav.WebDAVException
1057: */
1058: public Enumeration getWorkingResources() throws WebDAVException {
1059: return null;
1060: }
1061:
1062: /** Initialize this collection instance. Make sure the URL ends in a '/'.
1063: */
1064: protected void initialize(URL url, TargetSelector targetSelector)
1065: throws WebDAVException {
1066: try {
1067: this .url = url;
1068: this .targetSelector = targetSelector;
1069: impl = ResourceFactory.createResource(url, targetSelector);
1070:
1071: } catch (Exception exc) {
1072: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
1073: "Malformed URL");
1074: }
1075: }
1076:
1077: /**
1078: * A resource can be automatically versioned on each method
1079: * that updates its state (content or properties). Non-versioning
1080: * aware clients use automatic versioning to support updates. If
1081: * a resource is not automatically versioned, attempts to update
1082: * the revision without explicitly checking it out first will fail.
1083: *
1084: * @return true if this resource is automatically versioned,
1085: * false if not.
1086: * @exception com.ibm.webdav.WebDAVException
1087: */
1088: public boolean isAutomaticallyVersioned() throws WebDAVException {
1089: return false;
1090: }
1091:
1092: /**
1093: * Return true if this revision is checked out in the given activity.
1094: * The activity may be null to see if the revision was checked out
1095: * without using an activity.
1096: *
1097: * @param activity the Activity to check for
1098: * @return boolean return true if this revision is checked out in the
1099: * given activity
1100: * @exception com.ibm.webdav.WebDAVException
1101: */
1102: public boolean isCheckedOut(Activity activity)
1103: throws WebDAVException {
1104: return false;
1105: }
1106:
1107: /** Returns true if this Resource is a collection. Returns false otherwise.
1108: *
1109: * @return true if this Resource is a collection.
1110: * @exception com.ibm.webdav.WebDAVException
1111: */
1112: public boolean isCollection() throws WebDAVException {
1113: boolean isCollection = false;
1114: PropertyValue pv = getProperty(PropertyName.pnResourcetype);
1115: if (pv != null) {
1116: Element resourcetype = (Element) pv.value;
1117: if (resourcetype.getElementsByTagNameNS("DAV:",
1118: "collection") != null) {
1119: isCollection = true;
1120: }
1121: }
1122: return isCollection;
1123: }
1124:
1125: /**
1126: * Return true if any revision of this versioned resource is
1127: * labeled with the given label
1128: *
1129: * @param label the label to check
1130: * @return true if this revision is labeled with the given label
1131: * @exception com.ibm.webdav.WebDAVException
1132: */
1133: public boolean isLabeledWith(String label) throws WebDAVException {
1134: return false;
1135: }
1136:
1137: /** See if this resource is locked.
1138: *
1139: * @return true if this resource is locked, false otherwise.
1140: * @exception com.ibm.webdav.WebDAVException
1141: */
1142: public boolean isLocked() throws WebDAVException {
1143: // see if there are any active locks
1144: return !getLocks().isEmpty();
1145: }
1146:
1147: /** Is this resource locked by the current authorized user? That is, does the
1148: * current user have sufficient locking access to modify this resource. The
1149: * method, like all methods that do modify the resource, must have a precondition
1150: * set in the context containing the lock token of the resource owned by this
1151: * user. The user is set in the request context using the authorization method.
1152: * @return true if this resource is locked by the principal in the context
1153: * sufficient to modify the resource.
1154: * @exception com.ibm.webdav.WebDAVException
1155: * @see com.ibm.webdav.ResourceContext#authorization
1156: */
1157: public boolean isLockedByMe() throws WebDAVException {
1158: String principal = getRequestContext().getAuthorizationId();
1159: Precondition precondition = getRequestContext().precondition();
1160: if (precondition == null) {
1161: return false; // it is not locked by me.
1162: //raise(new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Missing If header containing lock token"));
1163: }
1164:
1165: // get the locks on this resource
1166: Enumeration locks = getLocks().elements();
1167: boolean isLockedByMe = false;
1168: // look for a matching lock
1169: while (locks.hasMoreElements()) {
1170: ActiveLock activeLock = (ActiveLock) locks.nextElement();
1171: Condition condition = new Condition(getURL().getFile());
1172: ConditionTerm term = new ConditionTerm();
1173: StateToken stateToken = new StateToken(activeLock
1174: .getLockToken());
1175: term.addConditionFactor(stateToken);
1176: condition.addConditionTerm(term);
1177: if (precondition.matches(condition)
1178: && activeLock.getPrincipal().equals(principal)
1179: && activeLock.getLockType().equals(
1180: ActiveLock.writeLock)) {
1181: isLockedByMe = true;
1182: break;
1183: }
1184: }
1185: return isLockedByMe;
1186: }
1187:
1188: /**
1189: * Return true if this revision is mutable. That is, it was checked
1190: * in as a mutable revision. Mutable revisions may be checked in
1191: * overwriting the contents of the revision with the contents of
1192: * the checked out working resource. This allows users to make
1193: * pdates that do not require a new revision.
1194: * <p>
1195: * An immutable revision can never be made mutable, but a new revision
1196: * can be. A mutable revision can be made immutable by checking it out
1197: * in place and checking it back is as immutable.
1198: *
1199: * @return an XML document describing the differences between
1200: * the given resource and this resource
1201: * @exception com.ibm.webdav.WebDAVException
1202: */
1203: public boolean isMutable() throws WebDAVException {
1204: return false;
1205: }
1206:
1207: /**
1208: * Return true if this resource is a versioned resource. A versioned
1209: * resource has multiple revisions and a revision history.
1210: *
1211: * @return true if this resource is a versioned resource, false otherwise
1212: * @exception com.ibm.webdav.WebDAVException
1213: */
1214: boolean isVersioned() throws WebDAVException {
1215: return false;
1216: }
1217:
1218: /** Exclusively write Lock this resource for all time.
1219: *
1220: * @return detailed information about the lock status of this resource. A MultiStatus
1221: * containing lockdiscovery properties.
1222: * An ActiveLock may be constructed by accessing the lockdiscovery element(s) of the
1223: * returned MultiStatus in order to obtain information about the lock.
1224: *
1225: * @return a MultiStatus containing a lockdiscovery property indicating
1226: * the results of the lock operation.
1227: * @exception com.ibm.webdav.WebDAVException
1228: */
1229: public MultiStatus lock(Document document) throws WebDAVException {
1230: String sPrefix = "D";
1231: String userName = System.getProperties().getProperty(
1232: "user.name");
1233: Element owner = document.createElementNS("DAV:", "D:owner");
1234: owner.setAttribute("xmlns:D", "DAV:");
1235: owner.appendChild(document.createTextNode(userName));
1236: return lock(ActiveLock.exclusive, ActiveLock.writeLock, -1,
1237: owner);
1238: }
1239:
1240: /** Lock this resource based on the given parameters. This allows control of
1241: * the lock scope (exclusive or shared) the lock type (write), owner information, etc.
1242: *
1243: * @param scope the scope of the lock, exclusive or shared
1244: * @param type the type of the lock, currently only write
1245: * @param timeout the number of seconds before the lock times out or
1246: * -1 for infinite timeout.
1247: * @param owner an XML element containing useful information that can be
1248: * used to identify the owner of the lock. An href to a home page, an
1249: * email address, phone number, etc. Can be null if no owner information
1250: * is provided.
1251: *
1252: * @return a MultiStatus containing a lockdiscovery property indicating
1253: * the results of the lock operation.
1254: * @exception com.ibm.webdav.WebDAVException
1255: */
1256: public MultiStatus lock(String scope, String type, int timeout,
1257: Element owner) throws WebDAVException {
1258: // remove any lock tokens in the context so we don't try to
1259: // do a refresh by mistake.
1260: getRequestContext().precondition((String) null);
1261: return impl.lock(context, scope, type, timeout, owner);
1262: }
1263:
1264: /** Move this resource to the destination URL.
1265: * The destination resource must not already exist.
1266: * Partial results are possible, check the returned status for details
1267: *
1268: * @param destinationURL the destination
1269: *
1270: * @return the status of the move operation for each resource moved
1271: * @exception com.ibm.webdav.WebDAVException
1272: */
1273: public MultiStatus move(String destinationURL)
1274: throws WebDAVException {
1275: return move(destinationURL, true, null);
1276: }
1277:
1278: /** Move this resource to the destination URL.
1279: * Partial results are possible, check the returned status for details
1280: *
1281: * @param destinationURL the destination
1282: * @param overwrite true implies overrite the destination if it exists
1283: * @param propertiesToMove a collection of properties that must be moved or
1284: * the method will fail. propertiesToMove may have one of the following values:
1285: * <ul>
1286: * <li>null - ignore properties that cannot be moved</li>
1287: * <li>empty collection - all properties must be moved or the method will fail</li>
1288: * <li>a collection of URIs - a list of the properties that must be moved
1289: * or the method will fail</li>
1290: * </ul>
1291: *
1292: * @return the status of the move operation for each resource moved
1293: * @exception com.ibm.webdav.WebDAVException
1294: */
1295: public MultiStatus move(String destinationURL, boolean overwrite,
1296: Vector propertiesToMove) throws WebDAVException {
1297: flushCaches();
1298: return impl.move(context, destinationURL, overwrite,
1299: propertiesToMove);
1300: }
1301:
1302: /** This method treats this resource as a method or service, and sends its parameter to
1303: * this resource where it is handled in a resource-specific way. For example,
1304: * sending data from an HTML form to a URL representing a Servlet or CGI script that processes
1305: * the form data to produce some result.
1306: *
1307: * @param args a string representing the arguments to the method represented by this URL. The
1308: * arguments are in the form ?parameterName1=value1&parameterName2=value2... as specified
1309: * for URL queries.
1310: *
1311: * @return the results of sending the arguments to the URL
1312: * @exception com.ibm.webdav.WebDAVException
1313: */
1314: public byte[] performWith(String args) throws WebDAVException {
1315: flushCaches(); // can't cache the results of a POST
1316: return impl.performWith(context, args);
1317: }
1318:
1319: /** Refresh the lock on this resource by resetting the lock timeout.
1320: * The context must contain the proper authorization for the requesting
1321: * principal.
1322: *
1323: * @param lockToken the lock token identifying the lock.
1324: * @param timeout the new timeout in seconds. -1 means infinite timeout.
1325: *
1326: * @return updated information about the lock status of this resource
1327: * @exception com.ibm.webdav.WebDAVException
1328: */
1329: public MultiStatus refreshLock(String lockToken, int timeout)
1330: throws WebDAVException {
1331: return impl.refreshLock(context, lockToken, timeout);
1332: }
1333:
1334: /**
1335: * Remove a label from a revision. An exception is raised
1336: * if the revision does not have this label.
1337: * <p>
1338: * A revision does not need to be checked out to add a label.
1339: *
1340: * @param label the label to add to the labels used to identify
1341: * this revision
1342: * @exception com.ibm.webdav.WebDAVException
1343: */
1344: public void removeLabel(String label) throws WebDAVException {
1345: }
1346:
1347: /** Remove properties from a resource.
1348: *
1349: * @param names an array of property names
1350: * @exception com.ibm.webdav.WebDAVException
1351: */
1352: public MultiStatus removeProperties(PropertyName[] names)
1353: throws WebDAVException {
1354: String sPrefix = "D";
1355: Document document = null;
1356:
1357: try {
1358: document = DocumentBuilderFactory.newInstance()
1359: .newDocumentBuilder().newDocument();
1360: } catch (Exception e) {
1361: throw new WebDAVException(WebDAVStatus.SC_PROCESSING, e
1362: .getMessage());
1363: }
1364: //document.setVersion(Resource.XMLVersion);
1365: //document.setEncoding(Resource.defaultXMLEncoding);
1366:
1367: Element propertyUpdate = document.createElementNS("DAV:",
1368: "D:propertyupdate");
1369:
1370: propertyUpdate.setAttribute("xmlns:D", "DAV:");
1371: document.appendChild(propertyUpdate);
1372: Element remove = document.createElementNS("DAV:", "D:remove");
1373:
1374: propertyUpdate.appendChild(remove);
1375: Element prop = document.createElementNS("DAV:", "D:prop");
1376:
1377: remove.appendChild(prop);
1378: for (int i = 0; i < names.length; i++) {
1379: // we don't care about the property value, only its name. But sending the whole
1380: // value element would work too because it still has the property name.
1381: PropertyName name = names[i];
1382: String prefix = "E";
1383: if (name.ns.equals("DAV:")) {
1384: prefix = "D";
1385: }
1386: Element newel = (Element) document.createElementNS(name.ns,
1387: prefix + ":" + name.local);
1388:
1389: if (prefix.equals("E")) {
1390: newel.setAttribute("xmlns:E", name.ns);
1391: }
1392: prop.appendChild(newel);
1393: }
1394: return setProperties(document);
1395: }
1396:
1397: /** Remove a property from a resource.
1398: *
1399: * @param name the property name
1400: * @exception com.ibm.webdav.WebDAVException
1401: */
1402: public void removeProperty(PropertyName name)
1403: throws WebDAVException {
1404: PropertyName[] names = new PropertyName[1];
1405: names[0] = name;
1406: MultiStatus result = removeProperties(names);
1407: setStatusCode(WebDAVStatus.SC_OK);
1408: if (result.getResponses().hasMoreElements()) {
1409: Response response = (Response) result.getResponses()
1410: .nextElement();
1411: // raise any necessary exceptions
1412: if (response instanceof MethodResponse) {
1413: int status = ((MethodResponse) response).getStatus();
1414: if (status != WebDAVStatus.SC_OK) {
1415: throw new WebDAVException(status, WebDAVStatus
1416: .getStatusMessage(status));
1417: }
1418: } else {
1419: PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response)
1420: .getPropertiesByPropName().elements()
1421: .nextElement();
1422: if ((propertyValue.status != WebDAVStatus.SC_OK)
1423: && (propertyValue.status != WebDAVStatus.SC_FAILED_DEPENDENCY)) {
1424: throw new WebDAVException(
1425: propertyValue.status,
1426: WebDAVStatus
1427: .getStatusMessage(propertyValue.status));
1428: }
1429: }
1430: }
1431: }
1432:
1433: /** A utility to resize a byte array and copy its current contents.
1434: * @param src the source array
1435: * @param new_size the new size to make the array
1436: * @param the newly sized array (may be smaller than src)
1437: */
1438: private final static byte[] resizeArray(byte[] src, int new_size) {
1439: byte tmp[] = new byte[new_size];
1440: System.arraycopy(src, 0, tmp, 0,
1441: (src.length < new_size ? src.length : new_size));
1442: return tmp;
1443: }
1444:
1445: /** Set the contents of this resource. This may create a new resource on the server,
1446: * or update the contents of an existing resource. Sufficient authorization is required
1447: * and administered by the target web server. For text/* MIME types, the caller should
1448: * be sure to convert Strings to byte codes using an acceptable charset, and to set
1449: * that charset in the request context so the server knows how to decode the byte
1450: * stream.
1451: * <p><B>deprecated</B>: Use the setContents method that takes content type as a parameter.
1452: *
1453: * @param value the new contents for the resource
1454: * @exception com.ibm.webdav.WebDAVException
1455: */
1456: public void setContents(byte[] value) throws WebDAVException {
1457: setContents(value, "text/plain");
1458: }
1459:
1460: /** Set the contents of this resource. This may create a new resource on the server,
1461: * or update the contents of an existing resource. Sufficient authorization is required
1462: * and administered by the target web server. For text/* MIME types, the caller should
1463: * be sure to convert Strings to byte codes using an acceptable charset, and to set
1464: * that charset in the request context so the server knows how to decode the byte
1465: * stream.
1466: *
1467: * @param value the new contents for the resource
1468: * @param mimetype the mimetype of the new contents
1469: * @exception com.ibm.webdav.WebDAVException
1470: */
1471: public void setContents(byte[] value, String mimetype)
1472: throws WebDAVException {
1473: context.getRequestContext().contentType(mimetype);
1474: OutputStream os = getContentsOutputStream();
1475: try {
1476: os.write(value, 0, value.length);
1477: } catch (WebDAVException exc) {
1478: throw exc;
1479: } catch (java.io.IOException exc) {
1480: throw new WebDAVException(
1481: WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Error");
1482: }
1483: closeContentsOutputStream();
1484: }
1485:
1486: /** Set properties of a resource.
1487: *
1488: * @param names an array of property names
1489: * @param values an array of property value
1490: * @return a MultiStatus indicating the result of the update
1491: * @exception com.ibm.webdav.WebDAVException
1492: */
1493: public MultiStatus setProperties(PropertyName[] names,
1494: Element[] values) throws WebDAVException {
1495:
1496: Document document = null;
1497:
1498: try {
1499: document = DocumentBuilderFactory.newInstance()
1500: .newDocumentBuilder().newDocument();
1501: } catch (Exception e) {
1502: throw new WebDAVException(WebDAVStatus.SC_PROCESSING, e
1503: .getMessage());
1504: }
1505: //document.setVersion(Resource.XMLVersion);
1506: //document.setEncoding(Resource.defaultXMLEncoding);
1507:
1508: Element propertyUpdate = document.createElementNS("DAV:",
1509: "D:propertyupdate");
1510:
1511: propertyUpdate.setAttribute("xmlns:D", "DAV:");
1512: document.appendChild(propertyUpdate);
1513: Element set = document.createElementNS("DAV:", "D:set");
1514:
1515: propertyUpdate.appendChild(set);
1516: Element prop = document.createElementNS("DAV:", "D:prop");
1517:
1518: set.appendChild(prop);
1519: for (int i = 0; i < names.length; i++) {
1520: prop.appendChild((Element) values[i]);
1521: }
1522: return setProperties(document);
1523: }
1524:
1525: /** Edit the properties of a resource. The updates must refer to a Document containing a WebDAV
1526: * DAV:propertyupdates element as the document root.
1527: *
1528: * @param updates an XML Document containing DAV:propertyupdate elements
1529: * describing the edits to be made
1530: * @return a MultiStatus indicating the status of the updates
1531: * @exception com.ibm.webdav.WebDAVException
1532: */
1533: public MultiStatus setProperties(Document updates)
1534: throws WebDAVException {
1535: return impl.setProperties(context, updates);
1536: }
1537:
1538: /** Set a property of a resource to a value.
1539: *
1540: * @param name the property name
1541: * @param value the property value
1542: * @exception com.ibm.webdav.WebDAVException
1543: */
1544: public void setProperty(PropertyName name, Element value)
1545: throws WebDAVException {
1546: PropertyName[] names = new PropertyName[1];
1547: names[0] = name;
1548: Element[] values = new Element[1];
1549: values[0] = value;
1550: int responseCode = 0;
1551:
1552: MultiStatus result = setProperties(names, values);
1553: setStatusCode(WebDAVStatus.SC_OK);
1554: if (result.getResponses().hasMoreElements()) {
1555: Response response = (Response) result.getResponses()
1556: .nextElement();
1557: // raise any necessary exceptions
1558: if (response instanceof MethodResponse) {
1559: responseCode = ((MethodResponse) response).getStatus();
1560: if (responseCode != WebDAVStatus.SC_OK) {
1561: throw new WebDAVException(getStatusCode(),
1562: getStatusMessage());
1563: }
1564: } else {
1565: PropertyValue propertyValue = (PropertyValue) ((PropertyResponse) response)
1566: .getPropertiesByPropName().elements()
1567: .nextElement();
1568: responseCode = propertyValue.status;
1569: if ((responseCode != WebDAVStatus.SC_OK)
1570: && (responseCode != WebDAVStatus.SC_FAILED_DEPENDENCY)) {
1571: throw new WebDAVException(propertyValue.status,
1572: getStatusMessage());
1573: }
1574: }
1575: }
1576: }
1577:
1578: /** Set the request context for this resource. The context contains information
1579: * used by methods on a resource. This method is provided
1580: * for implementation reasons and would generally not be used by client
1581: * applications.
1582: *
1583: * @value the ResourceContext providing information that controls
1584: * method execution.
1585: * @exception com.ibm.webdav.WebDAVException
1586: */
1587: public void setRequestContext(HTTPHeaders value)
1588: throws WebDAVException {
1589: context.setRequestContext(value);
1590: }
1591:
1592: /** Set the response context for this resource. The context contains information
1593: * returned from methods on a resource. This method is provided
1594: * for implementation reasons and would generally not be used by client
1595: * applications.
1596: *
1597: * @value the ResourceContext providing information resulting from
1598: * method execution.
1599: * @exception com.ibm.webdav.WebDAVException
1600: */
1601: public void setResponseContext(HTTPHeaders value)
1602: throws WebDAVException {
1603: context.setResponseContext(value);
1604: }
1605:
1606: /** Set the status code corresponding to the last method execution.
1607: *
1608: * @value the status code as defined by HTTP/1.1 and the WebDAV extensions.
1609: * @exception com.ibm.webdav.WebDAVException
1610: */
1611: public void setStatusCode(int value) throws WebDAVException {
1612: context.getStatusCode().setStatusCode(value);
1613: }
1614:
1615: /** Get a String representation of this resource.
1616: *
1617: * @return the URL of this Resource
1618: */
1619: public String toString() {
1620: String value = null;
1621: try {
1622: value = getURL().toString();
1623: } catch (Exception exc) {
1624: }
1625: return value;
1626: }
1627:
1628: /** Unlock the lock identified by the lockToken on this resource. The request context
1629: * must contain the proper authorization.
1630: *
1631: * @param lockToken the lock token obtained from the ActiveLock of a previous <code>lock() </code>
1632: * or <code>getLocks()</code>.
1633: *
1634: * @return a MultiStatus containing any responses on resources that could not
1635: * be unlocked.
1636: * @exception com.ibm.webdav.WebDAVException
1637: */
1638: public MultiStatus unlock(String lockToken) throws WebDAVException {
1639: return impl.unlock(context, lockToken);
1640: }
1641:
1642: /**
1643: * @param sContentType
1644: */
1645: public void closeContentsOutputStream(String sContentType)
1646: throws WebDAVException {
1647: if (sContentType == null) {
1648: impl.closeContentsOutputStream(context);
1649: } else {
1650: impl.closeContentsOutputStream(context, sContentType);
1651: }
1652:
1653: }
1654: }
|