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