From fa10a92fa46835b5c272bb16bf3fdb3debd63b77 Mon Sep 17 00:00:00 2001 From: infeo Date: Sat, 3 Feb 2018 16:44:07 +0100 Subject: [PATCH] commit of the webDavNioAdapter --- .../ui/model/WebDavNioAdapter.java | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 main/ui/src/main/java/org/cryptomator/ui/model/WebDavNioAdapter.java diff --git a/main/ui/src/main/java/org/cryptomator/ui/model/WebDavNioAdapter.java b/main/ui/src/main/java/org/cryptomator/ui/model/WebDavNioAdapter.java new file mode 100644 index 000000000..7dc805ed7 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/model/WebDavNioAdapter.java @@ -0,0 +1,115 @@ +package org.cryptomator.ui.model; + + +import org.cryptomator.common.settings.Settings; +import org.cryptomator.common.settings.VaultSettings; +import org.cryptomator.cryptofs.CryptoFileSystem; +import org.cryptomator.frontend.webdav.WebDavServer; +import org.cryptomator.frontend.webdav.mount.MountParams; +import org.cryptomator.frontend.webdav.mount.Mounter; +import org.cryptomator.frontend.webdav.servlet.WebDavServletController; + +import javax.inject.Inject; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.function.Function; + +@VaultModule.PerVault +public class WebDavNioAdapter implements NioAdapter { + + private static final String LOCALHOST_ALIAS = "cryptomator-vault"; + + private final WebDavServer server; + private final VaultSettings vaultSettings; + private final Settings settings; + + private WebDavServletController servlet; + private Mounter.Mount mount; + + @Inject + public WebDavNioAdapter(WebDavServer server, VaultSettings vaultSettings, Settings settings){ + this.server = server; + this.vaultSettings = vaultSettings; + this.settings = settings; + } + + @Override + public void unlock(CryptoFileSystem fs){ + if(!server.isRunning()){ + server.start(); + } + servlet = server.createWebDavServlet(fs.getPath("/"), vaultSettings.getId()+ "/" + vaultSettings.mountName().get()); + servlet.start(); + } + + @Override + public void mount() throws CommandFailedException { + if (servlet == null) { + throw new IllegalStateException("Mounting requires unlocked WebDAV servlet."); + } + MountParams mountParams = MountParams.create() // + .withWindowsDriveLetter(vaultSettings.winDriveLetter().get()) // + .withPreferredGvfsScheme(settings.preferredGvfsScheme().get())// + .withWebdavHostname(getLocalhostAliasOrNull()) // + .build(); + try { + this.mount = servlet.mount(mountParams); // might block this thread for a while + } catch (Mounter.CommandFailedException e) { + e.printStackTrace(); + throw new CommandFailedException(e); + } + } + + @Override + public synchronized void unmount() throws CommandFailedException { + try { + mount.unmount(); + } + catch (Mounter.CommandFailedException e){ + throw new CommandFailedException(e); + } + } + + @Override + public synchronized void unmountForced(){ + mount.forced(); + } + + private String getLocalhostAliasOrNull() { + try { + InetAddress alias = InetAddress.getByName(LOCALHOST_ALIAS); + if (alias.getHostAddress().equals("127.0.0.1")) { + return LOCALHOST_ALIAS; + } else { + return null; + } + } catch (UnknownHostException e) { + return null; + } + } + + @Override + public void stop() { + if(servlet != null){ + servlet.stop(); + } + + } + + public synchronized String getFilesystemRootUrl() { + return servlet.getServletRootUri().toString(); + } + + /** + * TODO: what to check wether it is implemented? + * @return + */ + @Override + public boolean isSupported() { + return true; + } + + public boolean supportsForcedUnmount() { + return mount != null && mount.forced().isPresent(); + } +}