KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > webdav > WebDAVUtil


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.webdav;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.cocoon.components.source.helpers.SourceProperty;
29 import org.apache.commons.httpclient.HttpException;
30 import org.apache.commons.httpclient.HttpURL;
31 import org.apache.webdav.lib.Property;
32 import org.apache.webdav.lib.PropertyName;
33 import org.apache.webdav.lib.ResponseEntity;
34 import org.apache.webdav.lib.WebdavResource;
35
36 /**
37  * A utility for WebDAV.
38  */

39 public class WebDAVUtil {
40     
41     static private String JavaDoc staticURI;
42     static private WebdavResource staticResource;
43     
44     /**
45      * instantiate a WebdavResource object from a given uri
46      *
47      * @param uri the uri of the resource.
48      * @throws HttpException
49      * @throws IOException
50      */

51     static synchronized public WebdavResource getWebdavResource(String JavaDoc uri)
52     throws HttpException, IOException JavaDoc {
53
54         if (uri == null) return null;
55         if (uri.equals(staticURI)) {
56             staticResource.discoverOwnLocks();
57             return staticResource;
58         }
59         HttpURL sourceURL = new HttpURL(uri);
60         staticURI = uri;
61         staticResource = new WebdavResource(sourceURL);
62         staticResource.discoverOwnLocks();
63         return staticResource;
64     }
65
66     /**
67      * create a new resource on the server
68      *
69      * @param uri the uri of the resource.
70      * @param content the content to initialize the resource with.
71      * @throws HttpException
72      * @throws IOException
73      */

74     static public void createResource(final String JavaDoc uri, final String JavaDoc content)
75     throws HttpException, IOException JavaDoc {
76
77         final String JavaDoc filename = uri.substring(uri.lastIndexOf("/"));
78         final String JavaDoc uriPrefix = uri.substring(0, uri.lastIndexOf("/") + 1);
79         final HttpURL sourceURL = new HttpURL(uri);
80         final WebdavResource resource = getWebdavResource(uriPrefix);
81                         
82         if(!resource.putMethod(uriPrefix + filename, content)) {
83             throw new HttpException("Error creating resource: " + uri
84                                     + " Status: " + resource.getStatusCode()
85                                     + " Message: " + resource.getStatusMessage());
86         }
87     }
88
89     /**
90      * copy a WebDAV resource
91      *
92      * @param from the URI of the resource to copy
93      * @param to the URI of the destination
94      * @param overwrite if true overwrites the destination
95      * @param recurse if true recursively creates parent collections if not existant
96      * @throws HttpException
97      * @throws IOException
98      */

99     static public void copyResource(String JavaDoc from, String JavaDoc to, boolean recurse, boolean overwrite)
100     throws HttpException, IOException JavaDoc {
101
102         String JavaDoc relativeDestination = (to.substring(to.indexOf("://") + 3));
103         relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/"));
104
105         // make parentCollection of target if not existant
106
if (recurse) WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));
107
108         // copy the resource
109
WebdavResource resource = WebDAVUtil.getWebdavResource(from);
110         resource.setOverwrite(overwrite);
111         if (!resource.copyMethod(relativeDestination)) {
112             throw new HttpException("Error copying resource: " + from
113                                     + " Status: " + resource.getStatusCode()
114                                     + " Message: " + resource.getStatusMessage());
115         }
116     }
117
118     /**
119      * move a WebDAV resource
120      *
121      * @param from the URI of the resource to move
122      * @param to the URI of the destination
123      * @param overwrite if true overwrites the destination
124      * @param recurse if true recursively creates parent collections if not existant
125      * @throws HttpException
126      * @throws IOException
127      */

128     static public void moveResource(String JavaDoc from, String JavaDoc to, boolean recurse, boolean overwrite)
129     throws HttpException, IOException JavaDoc {
130
131         String JavaDoc relativeDestination = (to.substring(to.indexOf("://") + 3));
132         relativeDestination = relativeDestination.substring(relativeDestination.indexOf("/"));
133
134         // make parentCollection if not existant
135
if (recurse) WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));
136
137         // move the resource
138
WebdavResource resource = WebDAVUtil.getWebdavResource(from);
139         resource.setOverwrite(overwrite);
140         if (!resource.moveMethod(relativeDestination)) {
141             throw new HttpException("Error moving resource: " + from
142                                     + " Status: " + resource.getStatusCode()
143                                     + " Message: " + resource.getStatusMessage());
144         }
145     }
146
147     /**
148      * make the complete path of a given collection URI (including all parent collections)
149      *
150      * @param path the URI of the collection to make
151      * @throws HttpException
152      * @throws IOException
153      */

154     static public void makePath(String JavaDoc path)
155     throws HttpException, IOException JavaDoc {
156         String JavaDoc parentPath = path;
157         while (true) {
158
159             try {
160                 HttpURL sourceURL = new HttpURL(parentPath+"/");
161                 new WebdavResource(sourceURL);
162
163                 // if code reaches here, pathUrl exists
164
break;
165             } catch (HttpException he) {
166                 parentPath = parentPath.substring(0, parentPath.lastIndexOf("/"));
167             }
168         }
169
170         // the complete path to make
171
if(parentPath.length() < path.length()) {
172             String JavaDoc pathToMake = path.substring(parentPath.length()+1)+"/";
173             String JavaDoc colToMake = null;
174             while (pathToMake.indexOf("/") != -1) {
175                 colToMake = pathToMake.substring(0, pathToMake.indexOf("/"));
176                 WebDAVUtil.makeCollection(path.substring(0, path.lastIndexOf(colToMake)), colToMake);
177                 pathToMake = pathToMake.substring(pathToMake.indexOf("/")+1);
178             }
179         }
180     }
181
182     /**
183      * create a collection
184      *
185      * @param parent the uri of the parent collection
186      * @param collection the name of the collection to make
187      * @throws HttpException
188      * @throws IOException
189      */

190     static public void makeCollection(String JavaDoc parent, String JavaDoc collection)
191     throws HttpException, IOException JavaDoc {
192
193         WebdavResource parentResource = WebDAVUtil.getWebdavResource(parent);
194         parentResource.mkcolMethod(parent + collection + "/");
195     }
196
197     /**
198      * get a property
199      *
200      * @param uri the URI to get the property from
201      * @param name the name of the property
202      * @param namespace the namespace of the property
203      * @throws HttpException
204      * @throws IOException
205      */

206     static public SourceProperty getProperty(String JavaDoc uri, String JavaDoc name, String JavaDoc namespace)
207     throws HttpException, IOException JavaDoc {
208
209         Vector JavaDoc propNames = new Vector JavaDoc(1);
210         propNames.add(new PropertyName(namespace,name));
211         Enumeration JavaDoc props= null;
212         Property prop = null;
213         WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
214         Enumeration JavaDoc responses = resource.propfindMethod(0, propNames);
215         while (responses.hasMoreElements()) {
216             ResponseEntity response = (ResponseEntity)responses.nextElement();
217             props = response.getProperties();
218             if (props.hasMoreElements()) {
219                 prop = (Property) props.nextElement();
220                 return new SourceProperty(prop.getElement());
221             }
222         }
223         return null;
224     }
225
226     /**
227      * get multiple properties
228      *
229      * @param uri the URI to get the properties from
230      * @param propNames the Set containing the properties to set
231      * @throws HttpException
232      * @throws IOException
233      */

234     static public Map JavaDoc getProperties(String JavaDoc uri, Set JavaDoc propNames)
235     throws HttpException, IOException JavaDoc {
236
237         List JavaDoc sourceproperties = new ArrayList JavaDoc();
238         Enumeration JavaDoc responses = null;
239         Enumeration JavaDoc props = null;
240         Property prop = null;
241         Map JavaDoc propertiesMap = new HashMap JavaDoc();
242         WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
243         responses = resource.propfindMethod(0, new Vector JavaDoc(propNames));
244         while (responses.hasMoreElements()) {
245             ResponseEntity response = (ResponseEntity)responses.nextElement();
246             props = response.getProperties();
247             while (props.hasMoreElements()) {
248                 prop = (Property) props.nextElement();
249                 SourceProperty srcProperty = new SourceProperty(prop.getElement());
250                 sourceproperties.add(srcProperty);
251             }
252         }
253
254         for (int i = 0; i<sourceproperties.size(); i++) {
255             propertiesMap.put(((SourceProperty) sourceproperties.get(i)).getNamespace()
256                               + ":" + ((SourceProperty) sourceproperties.get(i)).getName(),
257                               sourceproperties.get(i));
258         }
259         return propertiesMap;
260     }
261
262     /**
263      * get all properties for given uri
264      *
265      * @param uri the URI to get the properties from
266      * @throws HttpException
267      * @throws IOException
268      */

269     static public List JavaDoc getAllProperties(String JavaDoc uri)
270     throws HttpException, IOException JavaDoc {
271
272         List JavaDoc sourceproperties = new ArrayList JavaDoc();
273         WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
274         Enumeration JavaDoc responses = resource.propfindMethod(0);
275         Enumeration JavaDoc props = null;
276         Property prop = null;
277         while (responses.hasMoreElements()) {
278             ResponseEntity response = (ResponseEntity)responses.nextElement();
279             props = response.getProperties();
280             while (props.hasMoreElements()) {
281                 prop = (Property) props.nextElement();
282                 SourceProperty srcProperty = new SourceProperty(prop.getElement());
283                 sourceproperties.add(srcProperty);
284             }
285         }
286         return sourceproperties;
287     }
288
289     /**
290      * set a property
291      *
292      * @param uri the URI of the resource to set the property on
293      * @param name the name of the property
294      * @param namespace the namespace of the property
295      * @param value the new value of the property
296      * @throws HttpException
297      * @throws IOException
298      */

299     static public void setProperty(String JavaDoc uri, String JavaDoc name, String JavaDoc namespace, String JavaDoc value)
300     throws HttpException, IOException JavaDoc {
301
302         WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
303         if(!resource.proppatchMethod(new PropertyName(namespace, name), value, true)) {
304             throw new HttpException("Error setting property " + namespace + ":" + name + " on resource: " + uri
305                                     + " Status: " + resource.getStatusCode()
306                                     + " Message: " + resource.getStatusMessage());
307         }
308     }
309
310     /**
311      * set multiple property
312      *
313      * @param uri the URI of the resource to set the property on
314      * @param properties the Map containing the property values to set
315      * @throws HttpException
316      * @throws IOException
317      */

318     static public void setProperties(String JavaDoc uri, Map JavaDoc properties)
319     throws HttpException, IOException JavaDoc {
320
321         WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
322         if (!resource.proppatchMethod(new Hashtable JavaDoc(properties), true)) {
323             throw new HttpException("Error setting properties on resource: " + uri
324                                     + " Status: " + resource.getStatusCode()
325                                     + " Message: " + resource.getStatusMessage());
326         }
327     }
328
329 }
Popular Tags