Allow to set served path for NioWebDavServer by system property

This commit is contained in:
Markus Kreusch
2016-01-12 20:26:29 +01:00
parent b2a90ddcf6
commit b16ceb1ba8

View File

@@ -11,16 +11,20 @@ package org.cryptomator.webdav.jackrabbitservlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import org.cryptomator.filesystem.FileSystem;
import org.cryptomator.filesystem.nio.NioFileSystem;
public class NioWebDavServer {
private static final String PATH_TO_SERVE_PROPERTY = "pathToServe";
public static void main(String[] args) throws Exception {
FileSystem fileSystem = setupFilesystem();
FileSystemBasedWebDavServer server = new FileSystemBasedWebDavServer(fileSystem);
@@ -31,14 +35,36 @@ public class NioWebDavServer {
server.stop();
}
private static FileSystem setupFilesystem() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
System.out.print("Enter absolute path to serve (must be an existing directory): ");
Path path = Paths.get(in.readLine());
private static FileSystem setupFilesystem() {
return NioFileSystem.rootedAt(pathToServe());
}
private static Path pathToServe() {
Path path = pathFromSystemProperty().orElseGet(NioWebDavServer::pathEnteredByUser);
if (!Files.isDirectory(path)) {
throw new RuntimeException("Path is not a directory");
}
return NioFileSystem.rootedAt(path);
System.out.println("Serving " + path);
return path;
}
private static Optional<Path> pathFromSystemProperty() {
String value = System.getProperty(PATH_TO_SERVE_PROPERTY);
if (value == null) {
return Optional.empty();
} else {
return Optional.of(Paths.get(value));
}
}
private static Path pathEnteredByUser() {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
System.out.print("Enter absolute path to serve (must be an existing directory): ");
try {
return Paths.get(in.readLine());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}