Archive for the ‘webdav’ tag
Mounting WebDav in Ubuntu
$ sudo apt-get install davfs2 $ sudo mount -t davfs http://localhost:8080/alfresco/webdav <filesystem_folder>
WebDav Client in Java
The history of Java WebDav client is discussed well in this blog: http://pragmaticchris.blogspot.com/2007/11/java-webdav-clients.html. There is one update to it though: the JackRabbit project has a well-defined WebDav client (extending Commons HTTP Client) available.
To add JackRabbit WebDav client to your Maven project, use this:
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-webdav</artifactId>
<version>1.6.0</version>
</dependency>
I will show one example of using the WebDav client which I had developed to upload content to an WebDav server:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
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.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
...
// WebDAV URL:
final String baseUrl = ...;
// Source file to upload:
File f = ...;
try{
HttpClient client = new HttpClient();
Credentials creds = new UsernamePasswordCredentials("username", "password");
client.getState().setCredentials(AuthScope.ANY, creds);
PutMethod method = new PutMethod(baseUrl + "/" + f.getName());
RequestEntity requestEntity = new InputStreamRequestEntity(
new FileInputStream(f));
method.setRequestEntity(requestEntity);
client.executeMethod(method);
System.out.println(method.getStatusCode() + " " + method.getStatusText());
}
catch(HttpException ex){
// Handle Exception
}
catch(IOException ex){
// Handle Exception
}
Accessing WebDAV From Commandline
Linux has a wonderful commandline tool for accessing WebDAV resources. It is cadaver. To access any resource:
$ cadaver http://portale:8080/alfresco/webdav/Web%20Projects
If the resource requires authentication, cadaver will prompt for it. Then you may use the standard UNIX and FTP commands for navigating and manipulating content. Some of the common commands are: cd, pwd, ls, put, get, mput, mget, less, cat and delete. Similar to FTP commands, it has corresponding local commands too: lcd, lpwd and lls.
Edit In-line
To edit a file in-line in cadaver, just issue the edit <file-name> command. The default editor will open the file. To change the editor (to, say, emacs), issue the command: set editor emacs from cadaver prompt.