Sardine powered webdav client?

Extra Sardines on my pizza please

A few days ago I came across the need for an easy to use webdav client. Currently we’re using jakarta slide, which as it turns out is a project that was discontinued (as of fall 2007!), and whose code base as of this writing is practically 10 years old. Who wants those jars collecting dust in their lib directories? Sure it works, but hey, I’m trying to keep up with the Jones’ here, I’d like an up-to-date library that hasn’t been discontinued.

Dismayed, I took a look a the replacement suggested by the jakarta site – the Jackrabbit project which is a java based content repository API implementation (JCR, as outlined in JSR 170 and 283). Uh.. I’m not really looking to integrate a full fledged content repository into my project just so I can access some files on a webdav server. If I was building a CMS though, I’d be way more interested. All I was looking for was an easy way to access files on a webdav server.

Next I found Apache’s commons-vfs project but I was disappointed to find this note regarding webdav: “.. We can’t release WebDAV as we depend on an snapshot, thus it is in our sandbox.” (full page here, skip to “Things from the sandbox” ). Dammit! Guess I’ll have to keep looking..

Finally, I stumbled across Google’s Sardine project, an oasis in a desert of mismatched suitors. I practically feel guilty about rehashing whats already well documented, but I am compelled if only to underscore the ease of use.

Classpath Dependacies

At the minimum you’ll need commons-logging.jar, commons-codec.jar, httpcore-4.0.1.jar and httpclient-4.0.1.jar if you’re on Java6+. If you’re on Java5 you’ll need JAXB 2.1 and any dependancies. Luckily for you, the authors have included links to the JAXB jars and have included the other jars in the Sardine distribution so you can easily add them to your classpath.

Code Examples

Using Sardine is really simple, and pretty self explanatory. You must first call SardineFactory.begin() to initiate the webdav session. If you don’t have authentication enabled, you don’t need to provide the username/password parameters.

public List<DavResource> listFiles() throws SardineException {

	log.debug("fetching webdav directory");

	Sardine sardine = SardineFactory.begin("username", "password");
	List<DavResource> resources = sardine.getResources("http://webdav/dir/");

	return resources;

This List of DavResource objects is essentially meta data about the webdav files, which you can then use to perform whatever tasks you need.

Grabbing the contents of a file is just as easy:

	public InputStream getFile(String fullURL) throws SardineException {"fetching webdav file");

		Sardine sardine = SardineFactory.begin("username", "password");
		return sardine.getInputStream("http://webdav/dir/file.txt");

as is saving a file:

	public void storeFile(String filePath) throws IOException {
		Sardine sardine = SardineFactory.begin("username", "password");
		byte[] data = FileUtils.readFileToByteArray(new File(filePath));
		sardine.put("http://webdav/dir/filename.jpg", data);

checking if a file exists:

	public boolean fileExists(String filePath) throws IOException {
		Sardine sardine = SardineFactory.begin();
		if (sardine.exists("http://webdav/dir/filename.jpg")) {
			return true;

		return false;

Other code examples can be found for deleting, moving files from one place to another, copying files so you end up with two, and creating directories in the user guide in the Sardine project page.

Overall, Sardine is simple, elegant, easy to use and pretty darned sexy, so check it out. I guess it’s time to update all that jakarta API related code…

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.