jwi
jwi

Reputation: 1178

WebDAV FileSystemProvider - Java NIO

I have an Java application with lots of NIO methods like Files.copy, Files.move, Files.delete, FileChannel...

What I now trying to achieve: I want to access a remote WebDAV server and modify data on that server with the basic functions like upload, delete or update the remote WebDAV data - without changing every method on my application. So here comes my idea:

I think an WebDAV FileSystem implementation would do the trick. Adding a custom WebDAV FileSystemProvider which is managing the mentioned file operations on the remote data. I've googled a lot and the Apache VFS with Sardine implementation looks good - BUT it seems that the Apache VFS is not compatible with NIO?

Here's some example code, as I imagine it:

public class WebDAVManagerTest {

private static DefaultFileSystemManager fsManager;

private static WebdavFileObject testFile1;
private static WebdavFileObject testFile2;

private static FileSystem webDAVFileSystem1;
private  static FileSystem webDAVFileSystem2;

@Before
public static void initWebDAVFileSystem(String webDAVServerURL) throws FileSystemException, org.apache.commons.vfs2.FileSystemException {

    try {
           fsManager = new DefaultFileSystemManager();
           fsManager.addProvider("webdav", new WebdavFileProvider());
           fsManager.addProvider("file", new DefaultLocalFileProvider());
           fsManager.init();
        } catch (org.apache.commons.vfs2.FileSystemException e) {
        throw new FileSystemException("Exception initializing DefaultFileSystemManager: " + e.getMessage());
        }

    String exampleRemoteFile1 = "/foo/bar1.txt";
    String exampleRemoteFile2 = "/foo/bar2.txt";

    testFile1 = (WebdavFileObject) fsManager.resolveFile(webDAVServerURL + exampleRemoteFile1);
    webDAVFileSystem1 = (FileSystem) fsManager.createFileSystem(testFile1);
    Path localPath1 = webDAVFileSystem1.getPath(testFile1.toString());

    testFile2 = (WebdavFileObject) fsManager.resolveFile(webDAVServerURL + exampleRemoteFile2);
    webDAVFileSystem2 = (FileSystem) fsManager.createFileSystem(testFile2);
    Path localPath2 = webDAVFileSystem1.getPath(testFile1.toString());

    }
}

After that I want to work in my application with localPath1 + localPath2. So that e.g. a Files.copy(localPath1, newRemotePath) would copy a file on the WebDAV server to a new directory.

Is this the right course of action? Or are there other libraries to achieve that?

Upvotes: 0

Views: 1223

Answers (1)

Harry
Harry

Reputation: 11638

Apache VFS uses it's own FileSystem interface not the NIO one. You have three options with varying levels of effort.

  1. Change your code to use an existing webdav project that uses it's own FileSystem ie Apache VFS.
  2. Find an existing project that uses webdav and implements NIO FileSystem etc.
  3. Implement the NIO FileSystem interface yourself.

Option 3 has already been done so you may be able to customize what someone else has already written, have a look at nio-fs-provider or nio-fs-webdav. I'm sure there are others but these two were easy to find using Google.

Implementing a WebDav NIO FileSystem from scratch would be quite a lot of work so I wouldn't recommend starting there, I'd likely take what someone has done and make that work for me ie Option 2.

Upvotes: 1

Related Questions