Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

11.18. Modifying a WebDAV Resource

Warning

Slide has died. While this section remains in the book, you should know that it will soon be replaced with a section devoted to Apache Jackrabbit: the successor to Jakarta Slide.

11.18.1. Problem

You need to modify a WebDAV resource.

11.18.2. Solution

Use the putMethod( ) on WebdavResource, and be sure to lock and unlock the resource before and after modification. The following example demonstrates the use of lockMethod( ) , putMethod( ), and unlockMethod( ) to modify a resource:

               import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.webdav.lib.WebdavResource;
String url = "http://www.discursive.com/jccook/dav/test.html";
Credentials credentials =
    new UsernamePasswordCredentials("davuser", "davpass");
// List resources in top directory
WebdavResource resource = new WebdavResource(url, credentials);
// Lock the Resource for 100 seconds
boolean locked = resource.lockMethod( "tobrien", 100 );
if( locked ) {
    try {
        // Read content as a String
        String resourceData = resource.getMethodDataAsString( );
        
        // Modify a resource
        System.out.println( "*** Modifying Resource");
        resourceData = resourceData.replaceAll( "test", "modified test" );
        resource.putMethod( resourceData );
    } finally {
        // Unlock the resource
        resource.unlockMethod( );
    }
}
        
// Close the resource    
resource.close( );

11.18.3. Discussion

lockMethod( ) accepts an owner and a timeout; the owner is the owner of the lock, and the timeout is the timeout of the lock in number of seconds. When locking a resource, the lockMethod( ) will return a boolean value: true if the lock was granted and false if the lock was not granted. The resource.putMethod( ) was called with a String object, but it can also be invoked when a byte[], InputStream, or File. putMethod( ) returns a boolean value: true if the put was successful; otherwise, false. The unlockMethod( ) unlocks the resource and makes it available for other clients to lock, modify, and unlock.

11.18.4. See Also

Section 7 of RFC 2518 (HTTP Extensions for Distributed Authoring—WEBDAV) discusses the various types of write locks available in WebDAV. This RFC is available at http://www.zvon.org/tmRFC/RFC2518/Output/chapter7.html.

The Slide recipes in this chapter are only meant as a brief introduction to WebDAV. If you would like to learn more about how to work with properties, create collections, and search a WebDAV repository, please see the Jakarta Slide project page at http://jakarta.apache.org/slide.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.