001: package com.ibm.webdav.protocol.http;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import com.ibm.webdav.*;
018: import com.ibm.webdav.impl.IRCollection;
019: import java.util.*;
020: import java.io.*;
021: import java.net.URL;
022: import org.w3c.dom.*;
023:
024: /** A CollectionHTTPStub is a ResourceHTTPStub that contains other
025: * resources including other CollectionHTTPStubs. It provides a
026: * concrete, client side implementation of Collection for client/server
027: * communication over HTTP.
028: * @author Jim Amsden <jamsden@us.ibm.com>
029: */
030: public class CollectionHTTPStub extends ResourceHTTPStub implements
031: IRCollection {
032:
033: private Vector members = null; // lazy retrieve the members of the collection for the server
034:
035: public CollectionHTTPStub() {
036: super ();
037: }
038:
039: /** Construct a CollectionHTTPStub with the given URL. The collection having
040: * the url may not exist as this constructor does not access the resource from
041: * the server. Use exists() or attmept to get the members of the collection to
042: * see if it exists. Other constructors are provided using parameters for the
043: * various parts of the URL. See java.net.URLConnection.
044: *
045: * @param url the URL of the resource.
046: * @exception com.ibm.webdav.WebDAVException
047: */
048: public CollectionHTTPStub(String url) throws WebDAVException {
049: if (!url.endsWith("/")) {
050: url = url + "/";
051: }
052: try {
053: this .url = new URL(url);
054: } catch (java.net.MalformedURLException exc) {
055: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
056: "Malformed URL");
057: }
058: }
059:
060: /** Create a CollectionHTTPStub from the given URL components.
061: * @param protocol the protocol to use, http:, rmi:, or iiop:
062: * @param host the name or IP addres of the server host. Using the client host name,
063: * or 'localhost' without a port uses local access with no RPC or server required.
064: * @param port the TCP port to use. HTTP uses 80 by default.
065: * @param file the resource URL relative to the server including any query string, etc.
066: * @exception com.ibm.webdav.WebDAVException
067: * @see URLConnection
068: * @see com.ibm.webdav.ResourceFactory
069: */
070: public CollectionHTTPStub(String protocol, String host, int port,
071: String file) throws WebDAVException {
072: if (!file.endsWith("/")) {
073: file = file + "/";
074: }
075: try {
076: this .url = new URL(protocol, host, port, file);
077: } catch (java.net.MalformedURLException exc) {
078: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
079: "Malformed URL");
080: }
081: }
082:
083: /** Create a CollectionHTTPStub from the given URL components. This constructor uses the default
084: * HTTP port.
085: * @param protocol the protocol to use, http:, rmi:, or iiop:
086: * @param host the name or IP addres of the server host. Using the client host name,
087: * or 'localhost' without a port uses local access with no RPC or server required.
088: * @param file the resource URL relative to the server including any query string, etc.
089: * @exception com.ibm.webdav.WebDAVException
090: * @see com.ibm.webdav.ResourceFactory
091: */
092: public CollectionHTTPStub(String protocol, String host, String file)
093: throws WebDAVException {
094: if (!file.endsWith("/")) {
095: file = file + "/";
096: }
097: try {
098: this .url = new URL(protocol, host, file);
099: } catch (java.net.MalformedURLException exc) {
100: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
101: "Malformed URL");
102: }
103: }
104:
105: /** Construct a CollectionHTTPStub with the given URL. The resource having
106: * the url may not exist as this constructor does not access the resource from
107: * the server. Use exists() or attmept to get the contents of the resource to
108: * see if it exists. Other constructors are provided using parameters for the
109: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
110: * may also be used to construct instances of a Resource.
111: *
112: * @param url the URL of the resource.
113: * @param targetSelector the revision target selector for this Collection
114: * @exception com.ibm.webdav.WebDAVException
115: * @see URLConnection
116: * @see com.ibm.webdav.ResourceFactory
117: */
118: public CollectionHTTPStub(URL url, TargetSelector targetSelector)
119: throws WebDAVException {
120: super (url, targetSelector);
121: }
122:
123: /** Construct a CollectionHTTPStub with the given URL specification in the given context.
124: * The resource having
125: * the url may not exist as this constructor does not access the resource from
126: * the server. Use exists() or attmept to get the contents of the resource to
127: * see if it exists. Other constructors are provided using parameters for the
128: * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
129: * may also be used to construct instances of a Collection.
130: *
131: * @param context a URL giving the context in which the spec is evaluated
132: * @param spec a URL whose missing parts are provided by the context
133: * @exception com.ibm.webdav.WebDAVException
134: * @see URLConnection
135: * @see com.ibm.webdav.ResourceFactory
136: */
137: public CollectionHTTPStub(URL context, String spec)
138: throws WebDAVException {
139: if (!spec.endsWith("/")) {
140: spec = spec + "/";
141: }
142: try {
143: this .url = new URL(context, spec);
144: } catch (java.net.MalformedURLException exc) {
145: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
146: "Malformed URL");
147: }
148: }
149:
150: /** Lock this resource collection and potentially all its members
151: * based on the given parameters. This allows control of the lock
152: * scope (exclusive or shared) the lock type (write), owner information, etc.
153: *
154: * @param scope the scope of the lock, exclusive or shared
155: * @param type the type of the lock, currently only write
156: * @param depth
157: * <ul>
158: * <li>shallow lock only this resource</li>/** Copy this resource to the destination URL.
159: * Partial results are possible, check the returned status for details.
160: *
161: * @param destinationURL the destination
162: * @param overwrite true implies overrite the destination if it exists
163: * @param propertiesToCopy a collection of properties that must be copied or
164: * the method will fail. propertiesToCopy may have one of the following values:
165: * <ul>
166: * <li>null - ignore properties that cannot be copied</li>
167: * <li>empty collection - all properties must be copied or the method will fail</li>
168: * <li>a collection of URIs - a list of the properties that must be copied
169: * or the method will fail</li>
170: * </ul>
171: *
172: * @return the status of the copy operation for each resource copied
173: * @exception com.ibm.webdav.WebDAVException
174: */
175: public MultiStatus copy(ResourceContext context,
176: String destinationURL, boolean overwrite,
177: Vector propertiesToCopy, String depth)
178: throws WebDAVException {
179: context.getRequestContext().depth(depth);
180: return super .copy(context, destinationURL, overwrite,
181: propertiesToCopy);
182: }
183:
184: /** Actually create the collection in the repository. The resource indicated
185: * by the URL must not already exist. All ancestors of this URL must already
186: * exist.
187: *
188: * @param contents an XML Document describing the members of this collection, bodies
189: * of members, and properties on the collections or members. Not completely defined in
190: * version 10 of the WebDAV specification
191: *
192: * @return Multistatus describing the result
193: * of the operation
194: * @exception com.ibm.webdav.WebDAVException
195: */
196: public MultiStatus createCollection(ResourceContext context,
197: Document contents) throws WebDAVException {
198: this .context = context;
199:
200: try {
201: if (contents != null) {
202: connection.setDoOutput(true);
203: context.getRequestContext().contentType("text/xml");
204: }
205: setupRequest("MKCOL");
206: if (contents != null) {
207: OutputStream os = connection.getOutputStream();
208: PrintWriter pw = new PrintWriter(os, false);
209: pw.print(XMLUtility.printNode(contents
210: .getDocumentElement()));
211: //((Document) contents).printWithFormat(pw);
212: pw.flush();
213: }
214:
215: getResults();
216:
217: } catch (WebDAVException exc) {
218: throw exc;
219: } catch (java.io.IOException exc) {
220: throw new WebDAVException(
221: WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
222: "IO Exception");
223: }
224: return responseToMultiStatus();
225: }
226:
227: /** Get the named properties for this resource and (potentially) its children.
228: *
229: * @param names an arrary of property names to retrieve.
230: * @param depth an indicator for immediate members or recursively all children.
231: * <ul>
232: * <li>immediateMembers: propeprties of this resource and its immediate children</li>
233: * <li>allMembers: properties of this resource and recursively all its children</li>
234: * </ul>
235: *
236: * @return a MultiStatus of PropertyResponses
237: * @exception com.ibm.webdav.WebDAVException
238: */
239: public MultiStatus getProperties(ResourceContext context,
240: PropertyName names[], String depth) throws WebDAVException {
241: context.getRequestContext().depth(depth);
242: return super .getProperties(context, names);
243: }
244:
245: /** Get all the properties for this resource and (potentially) its children.
246: *
247: * @param depth an indicator for immediate members or recursively all children.
248: * <ul>
249: * <li>thisResource: propeprties of this resource</li>
250: * <li>immediateMembers: propeprties of this resource and its immediate children</li>
251: * <li>allMembers: properties of this resource and recursively all its children</li>
252: * </ul>
253: *
254: * @return a MultiStatus of PropertyResponses
255: * @exception com.ibm.webdav.WebDAVException
256: */
257: public MultiStatus getProperties(ResourceContext context,
258: String depth) throws WebDAVException {
259: context.getRequestContext().depth(depth);
260: return super .getProperties(context);
261: }
262:
263: /** Get the names of all properties for this resource and (potentially) its children.
264: *
265: * @param depth an indicator for immediate members or recursively all children.
266: * <ul>
267: * <li>thisResource: propeprties of this resource</li>
268: * <li>immediateMembers: propeprties of this resource and its immediate children</li>
269: * <li>allMembers: properties of this resource and recursively all its children</li>
270: * </ul>
271: *
272: * @return a MultiStatus of PropertyResponses
273: * (PropertyValue.value is always null, PropertyValue.status contains the status)
274: * @exception com.ibm.webdav.WebDAVException
275: */
276: public MultiStatus getPropertyNames(ResourceContext context,
277: String depth) throws WebDAVException {
278: context.getRequestContext().depth(depth);
279: return super .getPropertyNames(context);
280: }
281:
282: /** Lock this resource collection and potentially all its members
283: * based on the given parameters. This allows control of the lock
284: * scope (exclusive or shared) the lock type (write), owner information, etc.
285: *
286: * @param scope the scope of the lock, exclusive or shared
287: * @param type the type of the lock, currently only write
288: * @param depth
289: * <ul>
290: * <li>shallow lock only this resource</li>
291: * <li>deep lock this resource and all its children</li>
292: * </ul>
293: * @param timeout the number of seconds before the lock times out or
294: * 0 for infinite timeout.
295: * @param owner an XML element containing useful information that can be
296: * used to identify the owner of the lock. An href to a home page, an
297: * email address, phone number, etc. Can be null if no owner information
298: * is provided.
299: *
300: * @return a MultiStatus containing a lockdiscovery property indicating
301: * the results of the lock operation.
302: * @exception com.ibm.webdav.WebDAVException
303: */
304: public MultiStatus lock(ResourceContext context, String scope,
305: String type, int timeout, Element owner, String depth)
306: throws WebDAVException {
307: context.getRequestContext().depth(depth);
308: return super.lock(context, scope, type, timeout, owner);
309: }
310: }
|