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.

Java, WebDAV | permalink

Comments

  1. Colin Crist

    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!

  2. Pingback: Anyone using Milton webdav library? - Programmers Goodies

  3. Peter

    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.

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

  5. Rafael

    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.

  6. 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?

  7. lapen

    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

    • 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)

Leave a 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=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">