How To Write A Dead-Simple Online Network Drive using Java & WebDAV

There are a variety of ways you can enhance your application with WebDAV access which we will be going into in a latter part of the WebDAV series. For now we will show you how to create a basic online network drive using WebDAV via Milton, a Java WebDAV Server library.

Example

We will write the logic to create a network drive containing just one file, scratchpad.txt, which can be modified at will from any location where the drive is mounted.
You may want to load the project into your IDE so that you can follow along.

Step 1: Create The Domain Object

This is a simple POJO which will allow us to hold some text and attributes which will nicely represent a virtual scratchpad.txt file. Your applications could have something similar which would benefit from being exposed though WebDAV.

Step 2: Create The Equivalent Resource Class As A Composition

In this step, we have created a class with a “has-a” relationship with the original domain object. The key part here is to note which interfaces are implemented. Implementing PropFindableResource allows the file or folder to be searchable. Implementing GetableResource allows you to define how WebDAV will get the content of the virtual file or folder. You should pick the interfaces based on the behavior of the resource. In this example, we want the file to be able to be read, and modified, but never deleted. Subsequently we have implemented only the required interfaces. For more details on these interfaces, please refer to the Milton JavaDoc.
The methods implementations are done in this way so that the virtual scratchpad.txt file behaves like you would expect a text file to behave. Notably sendContent(…) will get the text stored in the POJO and supply it to the OutputStream.

To keep the file-structure analogy going, we should have a root folder to contain our one file. That’s the purpose of the RootResource class. Once again, key here is to determine which interfaces are implemented based on the behavior that we seek. Implementing PutableResource allows files to be created within this folder using the PUT request.
The methods implementations are done in this way so that the scratchpad.txt file can have it’s contents modified. Any other file that is attempted to be created within the root folder will return an error.

Step 3: Implement The ResourceFactory To Create Your Tree Structure

Here is where you define how your tree structure will work. What you want to do is think about which resource is being referred to for a given URL. In our simple case, we have just two possible valid URLS: http:///scratchpad/ and http:///scratchpad/scratchpad.txt .
This makes defining our tree structure pretty simple to code up as you can see below. If your tree structure is more complex with several child folders, you may want to consider a recursive strategy.

Step 4: Implement The ResourceFactoryFactory To Allow Milton To Access Your Resource Classes

This class exists most importantly to associate the MiltonServlet with the ResourceFactory implementation in the previous step. You can use this class to load some initialization parameters but for our simple example, it was not necessary.

Step 5: Update Your web.xml To Allow Access To Your Milton Servlet And Classes

Finally, to direct requests appropriately, you will need to update the web.xml so that the MiltonServlet can serve up the content for the desired URLs.

That’s it! You can now point your web browser, WebDAV client, or network drive mount to the server URL that is running this project and you will see the root folder containing the scratchpad.txt file. You can open and edit it as you wish and the content is persisted and automatically shared across any computer that has shared that drive.

Concluding Thoughts

Our example made a single POJO accessible from a cloud. What’s remarkable about WebDAV shows itself when you start to think about the business objects in your application that can be represented in a file format. This would allow direct, real-time access to read and update this data in a way that is both natural and simple to code.

Future WebDAV posts will explore this thought as well as touch on some of the more advanced capabilities WebDAV offers that you find in full-fledged Content Management Systems.

Download

The WebDAV download contains the entire source code (including Eclipse project). The source code is licensed under the terms of the Apache License, Version 2.0.

Java Exception Tracking

If you enjoyed this, We’ve got an exception tracking tool for Java coming out. Sign-up at StackHunter.com to be notified when it does.

About Kuhan Paramsothy

We make Data Pipeline — an embedded, real-time data processing engine. Use it in your applications and services to process, filter, and transform large volumes of data on-the-fly. Learn More about it at http://DataPipeline.io/

12 thoughts on “How To Write A Dead-Simple Online Network Drive using Java & WebDAV

  • By Colin Crist - Reply

    The example code does not work out of the box, I get the following error:

    2011-08-02 14:45:49,491 [http-8080-2] DEBUG com.bradmcevoy.http.http11.GetHandler – process
    2011-08-02 14:45:49,496 [http-8080-2] INFO com.bradmcevoy.http.ResourceHandlerHelper – resource not compatible. Resource class: class com.northconcepts.webdav.RootResource handler: class com.bradmcevoy.http.http11.GetHandler

    And the web page shows “Method Not Implemented”

    Any ideas? Thanks for the article!

    • By Kuhan Paramsothy - Reply

      Hi Colin,

      Did you try the URL http://localhost/webdav-example/WebDAV/ itself? I ask because the GetableResource interface isn’t implemented for the RootResource class, so that’s why you would get the “Method Not Implemented” for a HTTP GET on that URL.

      Instead try the URL http://localhost/webdav-example/WebDAV/scratchpad.txt on your browser (correcting for your port number if not port 80) to see the response.

      Note that I updated the example code as well to correct an unrelated encoding issue, so you may want to download the latest ZIP.

      • By Colin Crist - Reply

        Yeah my own silly mistake, thanks for pointing me to the right URL to use 🙂

  • Pingback: Anyone using Milton webdav library? - Programmers Goodies

  • By Peter - Reply

    Hi,

    im getting an exception altough i added all necesary jar files (or at least all i was aware):

    java.lang.ClassNotFoundException: com.bradmcevoy.http.Response

    What can cause this? What did i forget to add? Thx.

  • Pingback: How To Manage Your Application Properties using WebDAV | North Concepts

  • By Rafael - Reply

    Hello.

    Where are compiled files deployed? I checked tomcat/webapps folder, but there was no folder related to this project. Where shall I find the compiled project files after deployment and where to place files I want to share via DAV protocol?

    Thank you.

  • By Tung Le - Reply

    Hi,
    I deploy this project onto my host, go to a browser and input: http://my_host_name.com:8180/webdav-example/WebDAV/scratchpad.txt and all I got is a blank page. No complaint at all. According to your text:

    ” You can now point your web browser… to the server URL that is running this project and you will see the root folder containing the scratchpad.txt file”

    I guess it’s NOT successful, is it? Am I missing something here?

  • By lapen - Reply

    Hi,

    It works fine. But I have one more requirement. How can I ask the users for username and password when the user access it from Desktop client.

    Thank you

    • By lapen - Reply

      Found the solution, used http://securityfilter.sourceforge.net/

    • By Brad - Reply

      Or just implement the authenticate and authorise methods. They’re stubbed out in this example:

      @Override
      public Object authenticate(String user, String password) {
      return “anonymous”;
      }

      @Override
      public boolean authorise(Request request, Method method, Auth auth) {
      return true;
      }

      But implement logic to check credentials (authenticate) and the to check the user has access to the request (authorise)

  • By maol - Reply

    Thanks for the great article! Really useful 🙂

Leave a Reply to Rafael Cancel reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">