Browse Source

Created NioWebDavServer

* Extracted Server-logic from InMemoryWebDavServer to
FileSystemBasedWebdavServer
* Made InMemoryWebdavServer and NioWebdavvServer use
FileSystemBasedWebdavServer
Markus Kreusch 9 years ago
parent
commit
64dec64e95

+ 5 - 0
main/jackrabbit-filesystem-adapter/pom.xml

@@ -70,6 +70,11 @@
 			<artifactId>filesystem-inmemory</artifactId>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.cryptomator</groupId>
+			<artifactId>filesystem-nio</artifactId>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 
 	<build>

+ 71 - 0
main/jackrabbit-filesystem-adapter/src/test/java/org/cryptomator/webdav/jackrabbitservlet/FileSystemBasedWebDavServer.java

@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Sebastian Stenzel and others.
+ * This file is licensed under the terms of the MIT license.
+ * See the LICENSE.txt file for more info.
+ *
+ * Contributors:
+ *     Sebastian Stenzel - initial API and implementation
+ *******************************************************************************/
+package org.cryptomator.webdav.jackrabbitservlet;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.EnumSet;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import javax.servlet.DispatcherType;
+
+import org.cryptomator.filesystem.FileSystem;
+import org.cryptomator.webdav.filters.AcceptRangeFilter;
+import org.cryptomator.webdav.filters.UriNormalizationFilter;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.server.handler.ContextHandlerCollection;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jetty.util.thread.QueuedThreadPool;
+import org.eclipse.jetty.util.thread.ThreadPool;
+
+class FileSystemBasedWebDavServer {
+
+	private final Server server;
+	private final ServerConnector localConnector;
+	private final ContextHandlerCollection servletCollection;
+
+	public FileSystemBasedWebDavServer(FileSystem fileSystem) {
+		final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(10);
+		final ThreadPool tp = new QueuedThreadPool(4, 1, 1, queue);
+		server = new Server(tp);
+		localConnector = new ServerConnector(server);
+		localConnector.setHost("localhost");
+		localConnector.setPort(8080);
+		servletCollection = new ContextHandlerCollection();
+
+		URI servletContextRootUri;
+		try {
+			servletContextRootUri = new URI("http", null, "localhost", 8080, "/", null, null);
+		} catch (URISyntaxException e) {
+			throw new IllegalStateException(e);
+		}
+		final ServletContextHandler servletContext = new ServletContextHandler(servletCollection, "/", ServletContextHandler.SESSIONS);
+		final ServletHolder servletHolder = new ServletHolder("FileSystem-WebDAV-Servlet", new WebDavServlet(servletContextRootUri, fileSystem));
+		servletContext.addServlet(servletHolder, "/*");
+		servletContext.addFilter(AcceptRangeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
+		servletContext.addFilter(UriNormalizationFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
+		servletCollection.mapContexts();
+
+		server.setConnectors(new Connector[] { localConnector });
+		server.setHandler(servletCollection);
+	}
+
+	public void start() throws Exception {
+		server.start();
+	}
+
+	public void stop() throws Exception {
+		server.stop();
+	}
+
+}

+ 11 - 62
main/jackrabbit-filesystem-adapter/src/test/java/org/cryptomator/webdav/jackrabbitservlet/InMemoryWebDavServer.java

@@ -8,83 +8,32 @@
  *******************************************************************************/
 package org.cryptomator.webdav.jackrabbitservlet;
 
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.nio.ByteBuffer;
-import java.util.EnumSet;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.LinkedBlockingQueue;
-
-import javax.servlet.DispatcherType;
 
 import org.cryptomator.filesystem.FileSystem;
 import org.cryptomator.filesystem.WritableFile;
 import org.cryptomator.filesystem.inmem.InMemoryFileSystem;
-import org.cryptomator.webdav.filters.AcceptRangeFilter;
-import org.cryptomator.webdav.filters.UriNormalizationFilter;
-import org.eclipse.jetty.server.Connector;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.ServerConnector;
-import org.eclipse.jetty.server.handler.ContextHandlerCollection;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.servlet.ServletHolder;
-import org.eclipse.jetty.util.thread.QueuedThreadPool;
-import org.eclipse.jetty.util.thread.ThreadPool;
 
 public class InMemoryWebDavServer {
 
-	private final Server server;
-	private final ServerConnector localConnector;
-	private final ContextHandlerCollection servletCollection;
-	private final FileSystem inMemoryFileSystem = new InMemoryFileSystem();
-
-	private InMemoryWebDavServer() {
-		final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(10);
-		final ThreadPool tp = new QueuedThreadPool(4, 1, 1, queue);
-		server = new Server(tp);
-		localConnector = new ServerConnector(server);
-		localConnector.setHost("localhost");
-		localConnector.setPort(8080);
-		servletCollection = new ContextHandlerCollection();
-
-		URI servletContextRootUri;
-		try {
-			servletContextRootUri = new URI("http", null, "localhost", 8080, "/", null, null);
-		} catch (URISyntaxException e) {
-			throw new IllegalStateException(e);
-		}
-		final ServletContextHandler servletContext = new ServletContextHandler(servletCollection, "/", ServletContextHandler.SESSIONS);
-		final ServletHolder servletHolder = new ServletHolder("InMemory-WebDAV-Servlet", new WebDavServlet(servletContextRootUri, inMemoryFileSystem));
-		servletContext.addServlet(servletHolder, "/*");
-		servletContext.addFilter(AcceptRangeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
-		servletContext.addFilter(UriNormalizationFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
-		servletCollection.mapContexts();
-
-		server.setConnectors(new Connector[] { localConnector });
-		server.setHandler(servletCollection);
-	}
+	public static void main(String[] args) throws Exception {
+		FileSystem fileSystem = setupFilesystem();
+		FileSystemBasedWebDavServer server = new FileSystemBasedWebDavServer(fileSystem);
 
-	private void start() throws Exception {
 		server.start();
-	}
-
-	private void stop() throws Exception {
+		System.out.println("Server started. Press any key to stop it...");
+		System.in.read();
 		server.stop();
 	}
 
-	public static void main(String[] args) throws Exception {
-		final InMemoryWebDavServer server = new InMemoryWebDavServer();
-
-		server.inMemoryFileSystem.folder("mamals").folder("cats").create();
-		server.inMemoryFileSystem.folder("mamals").folder("dogs").create();
-		try (WritableFile writable = server.inMemoryFileSystem.folder("mamals").folder("cats").file("Garfield.txt").openWritable()) {
+	private static FileSystem setupFilesystem() {
+		FileSystem fileSystem = new InMemoryFileSystem();
+		fileSystem.folder("mamals").folder("cats").create();
+		fileSystem.folder("mamals").folder("dogs").create();
+		try (WritableFile writable = fileSystem.folder("mamals").folder("cats").file("Garfield.txt").openWritable()) {
 			writable.write(ByteBuffer.wrap("meow".getBytes()));
 		}
-
-		server.start();
-		System.out.println("Server started. Press any key to stop it...");
-		System.in.read();
-		server.stop();
+		return fileSystem;
 	}
 
 }

+ 44 - 0
main/jackrabbit-filesystem-adapter/src/test/java/org/cryptomator/webdav/jackrabbitservlet/NioWebDavServer.java

@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Sebastian Stenzel and others.
+ * This file is licensed under the terms of the MIT license.
+ * See the LICENSE.txt file for more info.
+ *
+ * Contributors:
+ *     Sebastian Stenzel - initial API and implementation
+ *******************************************************************************/
+package org.cryptomator.webdav.jackrabbitservlet;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.cryptomator.filesystem.FileSystem;
+import org.cryptomator.filesystem.nio.NioFileSystem;
+
+public class NioWebDavServer {
+
+	public static void main(String[] args) throws Exception {
+		FileSystem fileSystem = setupFilesystem();
+		FileSystemBasedWebDavServer server = new FileSystemBasedWebDavServer(fileSystem);
+
+		server.start();
+		System.out.println("Server started. Press any key to stop it...");
+		System.in.read();
+		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());
+		if (!Files.isDirectory(path)) {
+			throw new RuntimeException("Path is not a directory");
+		}
+		return NioFileSystem.rootedAt(path);
+	}
+
+}

+ 5 - 0
main/pom.xml

@@ -70,6 +70,11 @@
 				<artifactId>filesystem-api</artifactId>
 				<version>${project.version}</version>
 			</dependency>
+			<dependency>
+				<groupId>org.cryptomator</groupId>
+				<artifactId>filesystem-nio</artifactId>
+				<version>${project.version}</version>
+			</dependency>
 			<dependency>
 				<groupId>org.cryptomator</groupId>
 				<artifactId>filesystem-inmemory</artifactId>