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.
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( );
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.
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.