WebDAV(1)Introduction of WebDAV
1. configure in tomcat project to start the server part
org.apache.catalina.servlets.WebdavServlet
The configuration in web.xml:
webdav
org.apache.catalina.servlets.WebdavServlet
debug
0
listings
true
readonly
false
webdav
/*
configuration in WebContent/META-INF/context.xml:
2. Learn the open-source project jackrabbit
http://jackrabbit.apache.org
There are both server side and client side of this open-source project.
I made a very simple example of client side as follow:
org.apache.jackrabbit
jackrabbit-jcr2dav
2.4.1
package com.sillycat.easywebdavclient.temprunner;
import java.io.FileInputStream;
import java.io.IOException;
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.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
public class TestJackrabbit {
public static void main(String[] args) throws IOException, DavException {
HttpClient client = new HttpClient();
//Credentials creds = new UsernamePasswordCredentials("admin", "admin");
//client.getState().setCredentials(AuthScope.ANY, creds);
DavMethod mkCol = new MkColMethod(
"http://localhost:8080/netstore-webdav/shared/testfolder/");
client.executeMethod(mkCol);
System.out.println("mkcol testfolder:" + mkCol.getStatusCode() + " "
+ mkCol.getStatusText());
PutMethod put = new PutMethod(
"http://localhost:8080/netstore-webdav/shared/testfolder/1.txt");
RequestEntity requestEntity = new InputStreamRequestEntity(
new FileInputStream("d://1.txt"));
put.setRequestEntity(requestEntity);
client.executeMethod(put);
System.out.println("put image file:" + put.getStatusCode() + " "
+ put.getStatusText());
DavMethod find = new PropFindMethod(
"http://localhost:8080/netstore-webdav/shared/testfolder/",
DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
client.executeMethod(find);
MultiStatus multiStatus = find.getResponseBodyAsMultiStatus();
MultiStatusResponse[] responses = multiStatus.getResponses();
System.out.println("Folders and files:");
for (int i = 0; i < responses.length; i++) {
System.out.println(responses[i].getHref());
}
}
}
3. Sardine
http://code.google.com/p/sardine/
This is a very simple client API.
I made a very simple client side sample as follow:
com.googlecode.sardine
sardine
314
org.slf4j
slf4j-api
1.6.4
org.slf4j
slf4j-log4j12
1.6.4
log4j
log4j
1.2.16
commons-logging
commons-logging
1.1.1
commons-codec
commons-codec
1.6
org.apache.httpcomponents
httpclient
4.2-beta1
...snip...
sardine-google-svn-repo
true
Sardine maven repo at Google Code
http://sardine.googlecode.com/svn/maven/
package com.sillycat.easywebdavclient.temprunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.googlecode.sardine.DavResource;
import com.googlecode.sardine.Sardine;
import com.googlecode.sardine.SardineFactory;
public class TestSardine {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//Sardine sardine = SardineFactory.begin("admin", "admin");
Sardine sardine = SardineFactory.begin();
sardine.disableCompression();
sardine.disablePreemptiveAuthentication();
if (sardine.exists("http://localhost:8080/netstore-webdav/shared/")) {
System.out.println("shared folder exists");
}
if(!sardine.exists("http://localhost:8080/netstore-webdav/shared/testfolder/")){
sardine.createDirectory("http://localhost:8080/netstore-webdav/shared/testfolder/");
System.out.println("testfolder not folder exists");
}
InputStream fis = new FileInputStream(new File("d://1.txt"));
sardine.put("http://localhost:8080/netstore-webdav/shared/testfolder/2.txt", fis);
@SuppressWarnings("deprecation")
List
resources = sardine.getResources("http://localhost:8080/netstore-webdav/shared/testfolder/");
for (DavResource res : resources)
{
System.out.println(res); // calls the .toString() method.
}
}
}
4. apache module mod_webdav
I heard that there is a module named mod_webdav, but I need to build and configure the server side.
references:
http://www.iteye.com/topic/6568
http://www.iteye.com/topic/896302
http://www.iteye.com/topic/892408
http://code.google.com/p/sardine/
http://jackrabbit.apache.org/getting-started-with-apache-jackrabbit.html
http://www.artran.co.uk/articles/article/webdav-tomcat-apache/
http://www.tomcatexpert.com/knowledge-base/how-configure-tomcat-webdav
http://wiki.apache.org/jackrabbit/WebDAV
http://jackrabbit.apache.org/jackrabbit-configuration.html
http://jackrabbit.apache.org/first-hops.html
http://jackrabbit.apache.org/building-jackrabbit.html
http://jackrabbit.apache.org/source-repository.html