KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > protocol > http > CollectionHTTPStub


1 package com.ibm.webdav.protocol.http;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import com.ibm.webdav.*;
18 import com.ibm.webdav.impl.IRCollection;
19 import java.util.*;
20 import java.io.*;
21 import java.net.URL JavaDoc;
22 import org.w3c.dom.*;
23
24 /** A CollectionHTTPStub is a ResourceHTTPStub that contains other
25  * resources including other CollectionHTTPStubs. It provides a
26  * concrete, client side implementation of Collection for client/server
27  * communication over HTTP.
28  * @author Jim Amsden <jamsden@us.ibm.com>
29  */

30 public class CollectionHTTPStub extends ResourceHTTPStub implements IRCollection {
31
32    private Vector members = null; // lazy retrieve the members of the collection for the server
33
public CollectionHTTPStub() {
34     super();
35 }
36 /** Construct a CollectionHTTPStub with the given URL. The collection having
37 * the url may not exist as this constructor does not access the resource from
38 * the server. Use exists() or attmept to get the members of the collection to
39 * see if it exists. Other constructors are provided using parameters for the
40 * various parts of the URL. See java.net.URLConnection.
41 *
42 * @param url the URL of the resource.
43 * @exception com.ibm.webdav.WebDAVException
44 */

45 public CollectionHTTPStub(String JavaDoc url) throws WebDAVException {
46     if (!url.endsWith("/")) {
47         url = url + "/";
48     }
49     try {
50         this.url = new URL JavaDoc(url);
51     } catch (java.net.MalformedURLException JavaDoc exc) {
52         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Malformed URL");
53     }
54 }
55 /** Create a CollectionHTTPStub from the given URL components.
56  * @param protocol the protocol to use, http:, rmi:, or iiop:
57  * @param host the name or IP addres of the server host. Using the client host name,
58  * or 'localhost' without a port uses local access with no RPC or server required.
59  * @param port the TCP port to use. HTTP uses 80 by default.
60  * @param file the resource URL relative to the server including any query string, etc.
61  * @exception com.ibm.webdav.WebDAVException
62  * @see URLConnection
63  * @see com.ibm.webdav.ResourceFactory
64  */

65 public CollectionHTTPStub(String JavaDoc protocol, String JavaDoc host, int port, String JavaDoc file) throws WebDAVException {
66     if (!file.endsWith("/")) {
67         file = file + "/";
68     }
69     try {
70         this.url = new URL JavaDoc(protocol, host, port, file);
71     } catch (java.net.MalformedURLException JavaDoc exc) {
72         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Malformed URL");
73     }
74 }
75 /** Create a CollectionHTTPStub from the given URL components. This constructor uses the default
76  * HTTP port.
77  * @param protocol the protocol to use, http:, rmi:, or iiop:
78  * @param host the name or IP addres of the server host. Using the client host name,
79  * or 'localhost' without a port uses local access with no RPC or server required.
80  * @param file the resource URL relative to the server including any query string, etc.
81  * @exception com.ibm.webdav.WebDAVException
82  * @see com.ibm.webdav.ResourceFactory
83  */

84 public CollectionHTTPStub(String JavaDoc protocol, String JavaDoc host, String JavaDoc file) throws WebDAVException {
85     if (!file.endsWith("/")) {
86         file = file + "/";
87     }
88     try {
89         this.url = new URL JavaDoc(protocol, host, file);
90     } catch (java.net.MalformedURLException JavaDoc exc) {
91         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Malformed URL");
92     }
93 }
94 /** Construct a CollectionHTTPStub with the given URL. The resource having
95  * the url may not exist as this constructor does not access the resource from
96  * the server. Use exists() or attmept to get the contents of the resource to
97  * see if it exists. Other constructors are provided using parameters for the
98  * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
99  * may also be used to construct instances of a Resource.
100  *
101  * @param url the URL of the resource.
102  * @param targetSelector the revision target selector for this Collection
103  * @exception com.ibm.webdav.WebDAVException
104  * @see URLConnection
105  * @see com.ibm.webdav.ResourceFactory
106  */

107 public CollectionHTTPStub(URL JavaDoc url, TargetSelector targetSelector) throws WebDAVException {
108     super(url, targetSelector);
109 }
110 /** Construct a CollectionHTTPStub with the given URL specification in the given context.
111  * The resource having
112  * the url may not exist as this constructor does not access the resource from
113  * the server. Use exists() or attmept to get the contents of the resource to
114  * see if it exists. Other constructors are provided using parameters for the
115  * various parts of the URL. See java.net.URLConnection for details. A ResourceFactory
116  * may also be used to construct instances of a Collection.
117  *
118  * @param context a URL giving the context in which the spec is evaluated
119  * @param spec a URL whose missing parts are provided by the context
120  * @exception com.ibm.webdav.WebDAVException
121  * @see URLConnection
122  * @see com.ibm.webdav.ResourceFactory
123  */

124 public CollectionHTTPStub(URL JavaDoc context, String JavaDoc spec) throws WebDAVException {
125     if (!spec.endsWith("/")) {
126         spec = spec + "/";
127     }
128     try {
129         this.url = new URL JavaDoc(context, spec);
130     } catch (java.net.MalformedURLException JavaDoc exc) {
131         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Malformed URL");
132     }
133 }
134 /** Lock this resource collection and potentially all its members
135 * based on the given parameters. This allows control of the lock
136 * scope (exclusive or shared) the lock type (write), owner information, etc.
137 *
138 * @param scope the scope of the lock, exclusive or shared
139 * @param type the type of the lock, currently only write
140 * @param depth
141 * <ul>
142 * <li>shallow lock only this resource</li>/** Copy this resource to the destination URL.
143  * Partial results are possible, check the returned status for details.
144  *
145  * @param destinationURL the destination
146  * @param overwrite true implies overrite the destination if it exists
147  * @param propertiesToCopy a collection of properties that must be copied or
148  * the method will fail. propertiesToCopy may have one of the following values:
149  * <ul>
150  * <li>null - ignore properties that cannot be copied</li>
151  * <li>empty collection - all properties must be copied or the method will fail</li>
152  * <li>a collection of URIs - a list of the properties that must be copied
153  * or the method will fail</li>
154  * </ul>
155  *
156  * @return the status of the copy operation for each resource copied
157  * @exception com.ibm.webdav.WebDAVException
158  */

159 public MultiStatus copy(ResourceContext context, String JavaDoc destinationURL, boolean overwrite, Vector propertiesToCopy, String JavaDoc depth) throws WebDAVException {
160     context.getRequestContext().depth(depth);
161     return super.copy(context, destinationURL, overwrite, propertiesToCopy);
162 }
163 /** Actually create the collection in the repository. The resource indicated
164 * by the URL must not already exist. All ancestors of this URL must already
165 * exist.
166 *
167 * @param contents an XML Document describing the members of this collection, bodies
168 * of members, and properties on the collections or members. Not completely defined in
169 * version 10 of the WebDAV specification
170 *
171 * @return Multistatus describing the result
172 * of the operation
173 * @exception com.ibm.webdav.WebDAVException
174 */

175 public MultiStatus createCollection(ResourceContext context, Document contents) throws WebDAVException {
176     this.context = context;
177
178     try {
179         if (contents != null) {
180             connection.setDoOutput(true);
181             context.getRequestContext().contentType("text/xml");
182         }
183         setupRequest("MKCOL");
184         if (contents != null) {
185             OutputStream os = connection.getOutputStream();
186             PrintWriter pw = new PrintWriter(os, false);
187             pw.print(XMLUtility.printNode(contents.getDocumentElement()));
188                         //((Document) contents).printWithFormat(pw);
189
pw.flush();
190         }
191
192         getResults();
193
194     } catch (WebDAVException exc) {
195         throw exc;
196     } catch (java.io.IOException JavaDoc exc) {
197         throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR, "IO Exception");
198     }
199     return responseToMultiStatus();
200 }
201 /** Get the named properties for this resource and (potentially) its children.
202 *
203 * @param names an arrary of property names to retrieve.
204 * @param depth an indicator for immediate members or recursively all children.
205 * <ul>
206 * <li>immediateMembers: propeprties of this resource and its immediate children</li>
207 * <li>allMembers: properties of this resource and recursively all its children</li>
208 * </ul>
209 *
210 * @return a MultiStatus of PropertyResponses
211 * @exception com.ibm.webdav.WebDAVException
212 */

213 public MultiStatus getProperties(ResourceContext context, PropertyName names[], String JavaDoc depth) throws WebDAVException {
214     context.getRequestContext().depth(depth);
215     return super.getProperties(context, names);
216 }
217 /** Get all the properties for this resource and (potentially) its children.
218 *
219 * @param depth an indicator for immediate members or recursively all children.
220 * <ul>
221 * <li>thisResource: propeprties of this resource</li>
222 * <li>immediateMembers: propeprties of this resource and its immediate children</li>
223 * <li>allMembers: properties of this resource and recursively all its children</li>
224 * </ul>
225 *
226 * @return a MultiStatus of PropertyResponses
227 * @exception com.ibm.webdav.WebDAVException
228 */

229 public MultiStatus getProperties(ResourceContext context, String JavaDoc depth) throws WebDAVException {
230     context.getRequestContext().depth(depth);
231     return super.getProperties(context);
232 }
233 /** Get the names of all properties for this resource and (potentially) its children.
234 *
235 * @param depth an indicator for immediate members or recursively all children.
236 * <ul>
237 * <li>thisResource: propeprties of this resource</li>
238 * <li>immediateMembers: propeprties of this resource and its immediate children</li>
239 * <li>allMembers: properties of this resource and recursively all its children</li>
240 * </ul>
241 *
242 * @return a MultiStatus of PropertyResponses
243 * (PropertyValue.value is always null, PropertyValue.status contains the status)
244 * @exception com.ibm.webdav.WebDAVException
245 */

246 public MultiStatus getPropertyNames(ResourceContext context, String JavaDoc depth) throws WebDAVException {
247     context.getRequestContext().depth(depth);
248     return super.getPropertyNames(context);
249 }
250 /** Lock this resource collection and potentially all its members
251 * based on the given parameters. This allows control of the lock
252 * scope (exclusive or shared) the lock type (write), owner information, etc.
253 *
254 * @param scope the scope of the lock, exclusive or shared
255 * @param type the type of the lock, currently only write
256 * @param depth
257 * <ul>
258 * <li>shallow lock only this resource</li>
259 * <li>deep lock this resource and all its children</li>
260 * </ul>
261 * @param timeout the number of seconds before the lock times out or
262 * 0 for infinite timeout.
263 * @param owner an XML element containing useful information that can be
264 * used to identify the owner of the lock. An href to a home page, an
265 * email address, phone number, etc. Can be null if no owner information
266 * is provided.
267 *
268 * @return a MultiStatus containing a lockdiscovery property indicating
269 * the results of the lock operation.
270 * @exception com.ibm.webdav.WebDAVException
271 */

272 public MultiStatus lock(ResourceContext context, String JavaDoc scope, String JavaDoc type, int timeout, Element owner, String JavaDoc depth) throws WebDAVException {
273     context.getRequestContext().depth(depth);
274     return super.lock(context, scope, type, timeout, owner);
275 }
276 }
277
Popular Tags