Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

11.17. Connecting to WebDAV Resources

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.17.1. Problem

You need to connect to a WebDAV resource and list the contents of a collection. As with most WebDAV resources, you need to supply authentication information.

11.17.2. Solution

Create an instance of WebdavResource, passing in a URL and a UsernamePasswordCredential object. List the contents of a WebDAV collection by calling the listWebdavResources() method. The following example demonstrates the use of WebdavResource to list the contents of a WebDAV collection:

               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.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.apache.webdav.lib.WebdavResource;
HttpClient client = new HttpClient( );
String url = "http://www.discursive.com/jccook/dav/";
Credentials credentials =
    new UsernamePasswordCredentials("davuser", "davpass");
// List resources in top directory
WebdavResource resource = new WebdavResource(url, credentials);
WebdavResource[] resources = resource.listWebdavResources( );
System.out.println( "type  name           size    type" +
                    "                   modified");
System.out.println( "-----------------------------------------" +
                    "---------------------------");
for( int i = 0; i < resources.length; i++ )    {
    WebdavResource item = resources[i];
    String type;
    if( item.isCollection( ) ) {
        type = "dir";
    } else {
        type = "file";
    }
    System.out.print( StringUtils.rightPad( type, 6 ) );
    System.out.print( StringUtils.rightPad( item.getName( ), 15 ) );
    System.out.print( StringUtils.rightPad( item.getGetContentLength( ) 
    + "", 8 ) );
    System.out.print( StringUtils.rightPad( item.getGetContentType( ), 
    23 ) );
    Date lastMod = new Date( item.getGetLastModified( ) );
    System.out.print( 
        StringUtils.rightPad( 
            FastDateFormat.getInstance( ).format( lastMod ), 25 ));
    System.out.print( "\n" );
}

The program connects to a WebDAV resource using the credentials supplied to the constructor of WebdavResource. It lists the contents of a collection and produces the following output:

type  name           size    type                   modified
---------------------------------------------------------------------
file  test.html      14      text/html              5/6/04 12:16 AM          
file  index.html     14      text/html              5/5/04 11:59 PM          
dir   test           0       httpd/unix-directory   5/6/04 12:01 AM

11.17.3. Discussion

Jakarta Slide is built on top of Jakarta HttpClient, but you will notice that this recipe did not involve the execution of an HttpMethod. This is because WebdavResource manages the complexity of working with an instance of HttpClient behind the scenes. The only trace of HttpClient is the UsernamePasswordCredentials object that is passed to the constructor of WebdavResource. Because Jakarta Slide is built on top of Jakarta HttpClient, almost any feature of Jakarta HttpClient translates directly to Jakarta Slide. If you need to access a WebDAV repository over SSL with a self-signed certificate, or if you need to alter the cookie policy, you can use the same facilities that are available to you when you are using HttpClient directly.


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.