BullyWiiPlaza
BullyWiiPlaza

Reputation: 19195

Jackrabbit WebDAV Synchronization Examples?

I'm using the Jackrabbit library for communicating with a cloud storage using the webdav protocol. I need a way to list all files from a specific directory and get the last modified property but I can't seem to find any working examples on this.

I basically need code to synchronize files from the local directory with the webdav url.

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;

public class WebDavClient
{
    private String resourceUrl;
    private HttpClient client;
    private Credentials credentials;
    private DavMethod method;

    public WebDavClient(String resourceUrl, String username, String password)
            throws Exception
    {
        this.resourceUrl = resourceUrl;
        client = new HttpClient();
        credentials = new UsernamePasswordCredentials(username, password);
        client.getState().setCredentials(AuthScope.ANY, credentials);
    }

    public int upload(String fileToUpload) throws Exception
    {
        method = new PutMethod(getUpdatedWebDavPath(fileToUpload));

        RequestEntity requestEntity = new InputStreamRequestEntity(
                new FileInputStream(fileToUpload));
        ((PutMethod) method).setRequestEntity(requestEntity);

        client.executeMethod(method);

        return method.getStatusCode();
    }

    public int createFolder(String folder) throws Exception
    {
        method = new MkColMethod(getUpdatedWebDavPath(folder));

        client.executeMethod(method);

        return method.getStatusCode();
    }

    private String getUpdatedWebDavPath(String file)
    {
        // Make sure file names do not contain spaces
        return resourceUrl + "/" + new File(file).getName().replace(" ", "");
    }
}

Usage example for uploading the file Test.txt to the Backup folder:

String myAccountName = "...";
String myPassword = "...";
WebDavClient webdavUploader = new WebDavClient("https:\\\\webdav.hidrive.strato.com\\users\\" + myAccountName + "\\Backup", myAccountName, myPassword);

webdavUploader.upload("C:\\Users\\Username\\Desktop\\Test.txt");

Here's a list of different DavMethods that could be helpful: http://jackrabbit.apache.org/api/1.6/org/apache/jackrabbit/webdav/client/methods/package-summary.html

Please help, I'm been struggling on this for so long!

Upvotes: 1

Views: 3979

Answers (1)

Stefan
Stefan

Reputation: 31

Take a look at the AMES WebDAV Client code from Krusche and Partner on the EU portal. It is licensed under GPL, so should it may fit your purpose.

https://joinup.ec.europa.eu/svn/ames-web-service/trunk/AMES-WebDAV/ames-webdav/src/de/kp/ames/webdav/WebDAVClient.java

It works for me, though to access e.g. Win32LastModifiedTime I need to get the custom namespace, e.g.

private static final Namespace WIN32_NAMESPACE = Namespace.getNamespace("Z2", "urn:schemas-microsoft-com:");

and retrieve the custom Property Win32LastModifiedTime from the properties.

                /* 
                 * Win32LastModifiedTime
                 */
                String win32lastmodifiedtime = null;

                DavProperty<?> Win32LastModifiedTime = properties.get("Win32LastModifiedTime", WIN32_NAMESPACE);            
                if ((Win32LastModifiedTime != null) && (Win32LastModifiedTime.getValue() != null)) win32lastmodifiedtime = Win32LastModifiedTime.getValue().toString();

Upvotes: 2

Related Questions