org.cryptomator.ui.util.mount.MacOsXWebDavMounter.java Source code

Java tutorial

Introduction

Here is the source code for org.cryptomator.ui.util.mount.MacOsXWebDavMounter.java

Source

/*******************************************************************************
 * Copyright (c) 2014 Sebastian Stenzel, Markus Kreusch
 * 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, strategy fine tuning
 *     Markus Kreusch - Refactored WebDavMounter to use strategy pattern
 ******************************************************************************/
package org.cryptomator.ui.util.mount;

import java.net.URI;

import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.ui.util.command.Script;

final class MacOsXWebDavMounter implements WebDavMounterStrategy {

    @Override
    public boolean shouldWork() {
        return SystemUtils.IS_OS_MAC_OSX;
    }

    @Override
    public WebDavMount mount(URI uri) throws CommandFailedException {
        final String path = "/Volumes/Cryptomator" + uri.getRawPath().replace('/', '_');
        final Script mountScript = Script
                .fromLines("mkdir \"$MOUNT_PATH\"",
                        "mount_webdav -S -v Cryptomator \"[::1]:$PORT$DAV_PATH\" \"$MOUNT_PATH\"",
                        "open \"$MOUNT_PATH\"")
                .addEnv("PORT", String.valueOf(uri.getPort())).addEnv("DAV_PATH", uri.getRawPath())
                .addEnv("MOUNT_PATH", path);
        final Script unmountScript = Script.fromLines("umount $MOUNT_PATH").addEnv("MOUNT_PATH", path);
        mountScript.execute();
        return new WebDavMount() {
            @Override
            public void unmount() throws CommandFailedException {
                unmountScript.execute();
            }
        };
    }

}