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/