WebDAV(1)Introduction of WebDAV

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:
<servlet>
<servlet-name>webdav</servlet-name>
<servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>webdav</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
configuration in WebContent/META-INF/context.xml:
<Context path="/webapp-name" allowLinking="true">
</Context>

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:
<dependency>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>jackrabbit-jcr2dav</artifactId>
    <version>2.4.1</version>
</dependency>

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:
<dependency>
      <groupId>com.googlecode.sardine</groupId>
      <artifactId>sardine</artifactId>
      <version>314</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.4</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.4</version>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
    </dependency>
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
    </dependency>
    <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.6</version>
    </dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2-beta1</version>
    </dependency>
...snip...
<repositories>
     <repository>
        <id>sardine-google-svn-repo</id>
        <snapshots> <enabled>true</enabled> </snapshots>
        <name>Sardine maven repo at Google Code</name>
        <url>http://sardine.googlecode.com/svn/maven/</url>
     </repository>
  </repositories>
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<DavResource> 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


猜你喜欢

转载自sillycat.iteye.com/blog/1520952