001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.webdav;
018:
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Hashtable;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027: import java.util.Vector;
028:
029: import org.apache.cocoon.components.source.helpers.SourceProperty;
030: import org.apache.commons.httpclient.HttpException;
031: import org.apache.commons.httpclient.HttpURL;
032: import org.apache.webdav.lib.Property;
033: import org.apache.webdav.lib.PropertyName;
034: import org.apache.webdav.lib.ResponseEntity;
035: import org.apache.webdav.lib.WebdavResource;
036:
037: /**
038: * A utility for WebDAV.
039: */
040: public class WebDAVUtil {
041:
042: static private String staticURI;
043: static private WebdavResource staticResource;
044:
045: /**
046: * instantiate a WebdavResource object from a given uri
047: *
048: * @param uri the uri of the resource.
049: * @throws HttpException
050: * @throws IOException
051: */
052: static synchronized public WebdavResource getWebdavResource(
053: String uri) throws HttpException, IOException {
054:
055: if (uri == null)
056: return null;
057: if (uri.equals(staticURI)) {
058: staticResource.discoverOwnLocks();
059: return staticResource;
060: }
061: HttpURL sourceURL = new HttpURL(uri);
062: staticURI = uri;
063: staticResource = new WebdavResource(sourceURL);
064: staticResource.discoverOwnLocks();
065: return staticResource;
066: }
067:
068: /**
069: * create a new resource on the server
070: *
071: * @param uri the uri of the resource.
072: * @param content the content to initialize the resource with.
073: * @throws HttpException
074: * @throws IOException
075: */
076: static public void createResource(final String uri,
077: final String content) throws HttpException, IOException {
078:
079: final String filename = uri.substring(uri.lastIndexOf("/"));
080: final String uriPrefix = uri.substring(0,
081: uri.lastIndexOf("/") + 1);
082: final HttpURL sourceURL = new HttpURL(uri);
083: final WebdavResource resource = getWebdavResource(uriPrefix);
084:
085: if (!resource.putMethod(uriPrefix + filename, content)) {
086: throw new HttpException("Error creating resource: " + uri
087: + " Status: " + resource.getStatusCode()
088: + " Message: " + resource.getStatusMessage());
089: }
090: }
091:
092: /**
093: * copy a WebDAV resource
094: *
095: * @param from the URI of the resource to copy
096: * @param to the URI of the destination
097: * @param overwrite if true overwrites the destination
098: * @param recurse if true recursively creates parent collections if not existant
099: * @throws HttpException
100: * @throws IOException
101: */
102: static public void copyResource(String from, String to,
103: boolean recurse, boolean overwrite) throws HttpException,
104: IOException {
105:
106: String relativeDestination = (to
107: .substring(to.indexOf("://") + 3));
108: relativeDestination = relativeDestination
109: .substring(relativeDestination.indexOf("/"));
110:
111: // make parentCollection of target if not existant
112: if (recurse)
113: WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));
114:
115: // copy the resource
116: WebdavResource resource = WebDAVUtil.getWebdavResource(from);
117: resource.setOverwrite(overwrite);
118: if (!resource.copyMethod(relativeDestination)) {
119: throw new HttpException("Error copying resource: " + from
120: + " Status: " + resource.getStatusCode()
121: + " Message: " + resource.getStatusMessage());
122: }
123: }
124:
125: /**
126: * move a WebDAV resource
127: *
128: * @param from the URI of the resource to move
129: * @param to the URI of the destination
130: * @param overwrite if true overwrites the destination
131: * @param recurse if true recursively creates parent collections if not existant
132: * @throws HttpException
133: * @throws IOException
134: */
135: static public void moveResource(String from, String to,
136: boolean recurse, boolean overwrite) throws HttpException,
137: IOException {
138:
139: String relativeDestination = (to
140: .substring(to.indexOf("://") + 3));
141: relativeDestination = relativeDestination
142: .substring(relativeDestination.indexOf("/"));
143:
144: // make parentCollection if not existant
145: if (recurse)
146: WebDAVUtil.makePath(to.substring(0, to.lastIndexOf("/")));
147:
148: // move the resource
149: WebdavResource resource = WebDAVUtil.getWebdavResource(from);
150: resource.setOverwrite(overwrite);
151: if (!resource.moveMethod(relativeDestination)) {
152: throw new HttpException("Error moving resource: " + from
153: + " Status: " + resource.getStatusCode()
154: + " Message: " + resource.getStatusMessage());
155: }
156: }
157:
158: /**
159: * make the complete path of a given collection URI (including all parent collections)
160: *
161: * @param path the URI of the collection to make
162: * @throws HttpException
163: * @throws IOException
164: */
165: static public void makePath(String path) throws HttpException,
166: IOException {
167: String parentPath = path;
168: while (true) {
169:
170: try {
171: HttpURL sourceURL = new HttpURL(parentPath + "/");
172: new WebdavResource(sourceURL);
173:
174: // if code reaches here, pathUrl exists
175: break;
176: } catch (HttpException he) {
177: parentPath = parentPath.substring(0, parentPath
178: .lastIndexOf("/"));
179: }
180: }
181:
182: // the complete path to make
183: if (parentPath.length() < path.length()) {
184: String pathToMake = path.substring(parentPath.length() + 1)
185: + "/";
186: String colToMake = null;
187: while (pathToMake.indexOf("/") != -1) {
188: colToMake = pathToMake.substring(0, pathToMake
189: .indexOf("/"));
190: WebDAVUtil.makeCollection(path.substring(0, path
191: .lastIndexOf(colToMake)), colToMake);
192: pathToMake = pathToMake.substring(pathToMake
193: .indexOf("/") + 1);
194: }
195: }
196: }
197:
198: /**
199: * create a collection
200: *
201: * @param parent the uri of the parent collection
202: * @param collection the name of the collection to make
203: * @throws HttpException
204: * @throws IOException
205: */
206: static public void makeCollection(String parent, String collection)
207: throws HttpException, IOException {
208:
209: WebdavResource parentResource = WebDAVUtil
210: .getWebdavResource(parent);
211: parentResource.mkcolMethod(parent + collection + "/");
212: }
213:
214: /**
215: * get a property
216: *
217: * @param uri the URI to get the property from
218: * @param name the name of the property
219: * @param namespace the namespace of the property
220: * @throws HttpException
221: * @throws IOException
222: */
223: static public SourceProperty getProperty(String uri, String name,
224: String namespace) throws HttpException, IOException {
225:
226: Vector propNames = new Vector(1);
227: propNames.add(new PropertyName(namespace, name));
228: Enumeration props = null;
229: Property prop = null;
230: WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
231: Enumeration responses = resource.propfindMethod(0, propNames);
232: while (responses.hasMoreElements()) {
233: ResponseEntity response = (ResponseEntity) responses
234: .nextElement();
235: props = response.getProperties();
236: if (props.hasMoreElements()) {
237: prop = (Property) props.nextElement();
238: return new SourceProperty(prop.getElement());
239: }
240: }
241: return null;
242: }
243:
244: /**
245: * get multiple properties
246: *
247: * @param uri the URI to get the properties from
248: * @param propNames the Set containing the properties to set
249: * @throws HttpException
250: * @throws IOException
251: */
252: static public Map getProperties(String uri, Set propNames)
253: throws HttpException, IOException {
254:
255: List sourceproperties = new ArrayList();
256: Enumeration responses = null;
257: Enumeration props = null;
258: Property prop = null;
259: Map propertiesMap = new HashMap();
260: WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
261: responses = resource.propfindMethod(0, new Vector(propNames));
262: while (responses.hasMoreElements()) {
263: ResponseEntity response = (ResponseEntity) responses
264: .nextElement();
265: props = response.getProperties();
266: while (props.hasMoreElements()) {
267: prop = (Property) props.nextElement();
268: SourceProperty srcProperty = new SourceProperty(prop
269: .getElement());
270: sourceproperties.add(srcProperty);
271: }
272: }
273:
274: for (int i = 0; i < sourceproperties.size(); i++) {
275: propertiesMap
276: .put(
277: ((SourceProperty) sourceproperties.get(i))
278: .getNamespace()
279: + ":"
280: + ((SourceProperty) sourceproperties
281: .get(i)).getName(),
282: sourceproperties.get(i));
283: }
284: return propertiesMap;
285: }
286:
287: /**
288: * get all properties for given uri
289: *
290: * @param uri the URI to get the properties from
291: * @throws HttpException
292: * @throws IOException
293: */
294: static public List getAllProperties(String uri)
295: throws HttpException, IOException {
296:
297: List sourceproperties = new ArrayList();
298: WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
299: Enumeration responses = resource.propfindMethod(0);
300: Enumeration props = null;
301: Property prop = null;
302: while (responses.hasMoreElements()) {
303: ResponseEntity response = (ResponseEntity) responses
304: .nextElement();
305: props = response.getProperties();
306: while (props.hasMoreElements()) {
307: prop = (Property) props.nextElement();
308: SourceProperty srcProperty = new SourceProperty(prop
309: .getElement());
310: sourceproperties.add(srcProperty);
311: }
312: }
313: return sourceproperties;
314: }
315:
316: /**
317: * set a property
318: *
319: * @param uri the URI of the resource to set the property on
320: * @param name the name of the property
321: * @param namespace the namespace of the property
322: * @param value the new value of the property
323: * @throws HttpException
324: * @throws IOException
325: */
326: static public void setProperty(String uri, String name,
327: String namespace, String value) throws HttpException,
328: IOException {
329:
330: WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
331: if (!resource.proppatchMethod(
332: new PropertyName(namespace, name), value, true)) {
333: throw new HttpException("Error setting property "
334: + namespace + ":" + name + " on resource: " + uri
335: + " Status: " + resource.getStatusCode()
336: + " Message: " + resource.getStatusMessage());
337: }
338: }
339:
340: /**
341: * set multiple property
342: *
343: * @param uri the URI of the resource to set the property on
344: * @param properties the Map containing the property values to set
345: * @throws HttpException
346: * @throws IOException
347: */
348: static public void setProperties(String uri, Map properties)
349: throws HttpException, IOException {
350:
351: WebdavResource resource = WebDAVUtil.getWebdavResource(uri);
352: if (!resource.proppatchMethod(new Hashtable(properties), true)) {
353: throw new HttpException(
354: "Error setting properties on resource: " + uri
355: + " Status: " + resource.getStatusCode()
356: + " Message: "
357: + resource.getStatusMessage());
358: }
359: }
360:
361: }
|