Monitor a http resource using Java NIO watch service

2k views Asked by At

I need to monitor a directory on Amazon S3 to check if any new file is added to this directory. I tried using Java NIO Watch Service, however it is not working properly. If I used following syntax with the provided path of S3:

String pathToMonitor="file://https://abc/dir";  //Line1
Path path=Paths.get(new URL(pathToMonitor).toURI()); //Line2
Boolean isFolder=(Boolean) Files.getAttribute(file, "basic:isDirectory", LinkOption.NOFOLLOW_LINKS); //Line3

Then i get following error:

java.nio.file.FileSystemException: \\https\abc\dir: The network path was not found.

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:53)
at sun.nio.fs.WindowsFileAttributeViews$Basic.readAttributes(WindowsFileAttributeViews.java:38)
at sun.nio.fs.AbstractBasicFileAttributeView.readAttributes(AbstractBasicFileAttributeView.java:168)
at sun.nio.fs.AbstractFileSystemProvider.readAttributes(AbstractFileSystemProvider.java:92)
at java.nio.file.Files.readAttributes(Files.java:1961)
at java.nio.file.Files.getAttribute(Files.java:1866)

If i remove the file:// prefix from the path then following error is generated:

Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "https" not installed
at java.nio.file.Paths.get(Paths.java:147)

If i modify 'Line2' to Path path=Paths.get("https://abc/dir"); then following trace is generated:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 5: https://abc/dir
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)

Kindly let me know what am i doing wrong here and if it is possible to monitor web resources like these using the Java watch service or if there is any other framework/api.

Thanks

1

There are 1 answers

1
chiastic-security On BEST ANSWER

You've given it a mixture of a file protocol and an http protocol, which doesn't really make sense.

Basically you can't do what you're trying to do, if the only way you have access to the resource is via HTTP. There's no general mechanism for being notified when an HTTP resource changes, because there's no general mechanism for having push notifications from an HTTP resource, and it's outside the operating system's control so it can't intercept changes as they happen. With a local file, your operating system can detect changes as they happen because it's ultimately responsible for dealing with writes to local disk, but that doesn't apply in your situation.

You'd need something that polls for changes, unless S3 has something bespoke in place to push change notifications (but that's something you'd have to investigate separately). You can't do it with Java NIO.