Skip to content

Commit fe86b4c

Browse files
committed
Implemented cryptomator#363
1 parent a583afe commit fe86b4c

19 files changed

Lines changed: 278 additions & 103 deletions

File tree

‎main/filesystem-charsets/src/main/java/org/cryptomator/filesystem/charsets/NormalizedNameFolder.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public NormalizedNameFile file(String name) throws UncheckedIOException {
4040
NormalizedNameFile nfcFile = super.file(nfcName);
4141
NormalizedNameFile nfdFile = super.file(nfdName);
4242
if (!nfcName.equals(nfdName) && nfcFile.exists() && nfdFile.exists()) {
43-
LOG.warn("Ambiguous file names \"" + nfcName + "\" (NFC) vs. \"" + nfdName + "\" (NFD). Both files exist. Using \"" + nfcName + "\" (NFC).");
43+
LOG.debug("Ambiguous file names \"" + nfcName + "\" (NFC) vs. \"" + nfdName + "\" (NFD). Both files exist. Using \"" + nfcName + "\" (NFC).");
4444
} else if (!nfcName.equals(nfdName) && !nfcFile.exists() && nfdFile.exists()) {
45-
LOG.info("Moving file from \"" + nfcName + "\" (NFD) to \"" + nfdName + "\" (NFC).");
45+
LOG.debug("Moving file from \"" + nfcName + "\" (NFD) to \"" + nfdName + "\" (NFC).");
4646
nfdFile.moveTo(nfcFile);
4747
}
4848
return nfcFile;
@@ -60,9 +60,9 @@ public NormalizedNameFolder folder(String name) throws UncheckedIOException {
6060
NormalizedNameFolder nfcFolder = super.folder(nfcName);
6161
NormalizedNameFolder nfdFolder = super.folder(nfdName);
6262
if (!nfcName.equals(nfdName) && nfcFolder.exists() && nfdFolder.exists()) {
63-
LOG.warn("Ambiguous folder names \"" + nfcName + "\" (NFC) vs. \"" + nfdName + "\" (NFD). Both files exist. Using \"" + nfcName + "\" (NFC).");
63+
LOG.debug("Ambiguous folder names \"" + nfcName + "\" (NFC) vs. \"" + nfdName + "\" (NFD). Both files exist. Using \"" + nfcName + "\" (NFC).");
6464
} else if (!nfcName.equals(nfdName) && !nfcFolder.exists() && nfdFolder.exists()) {
65-
LOG.info("Moving folder from \"" + nfcName + "\" (NFD) to \"" + nfdName + "\" (NFC).");
65+
LOG.debug("Moving folder from \"" + nfcName + "\" (NFD) to \"" + nfdName + "\" (NFC).");
6666
nfdFolder.moveTo(nfcFolder);
6767
}
6868
return nfcFolder;

‎main/filesystem-crypto/src/main/java/org/cryptomator/filesystem/crypto/ConflictResolver.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private File resolveConflict(File conflictingFile, MatchResult matchResult) {
6868
String alternativeCiphertext = nameEncryptor.apply(alternativeCleartext).get();
6969
alternativeFile = folder.file(isDirectory ? DIR_PREFIX + alternativeCiphertext : alternativeCiphertext);
7070
} while (alternativeFile.exists());
71-
LOG.info("Detected conflict {}:\n{}\n{}", conflictId, canonicalFile, conflictingFile);
71+
LOG.debug("Detected conflict {}:\n{}\n{}", conflictId, canonicalFile, conflictingFile);
7272
conflictingFile.moveTo(alternativeFile);
7373
return alternativeFile;
7474
}

‎main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/ContextPathBuilder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static String from(FrontendId id, String name) {
1818
return format("/%s/%s", id, name);
1919
}
2020

21+
public static String removeFrontendId(String path) {
22+
return path.replaceAll("/" + FRONTEND_ID_PATTERN + "/", "/[...]/");
23+
}
24+
2125
public static Optional<FrontendId> extractFrontendId(String path) {
2226
Matcher matcher = SERVLET_PATH_WITH_FRONTEND_ID_PATTERN.matcher(path);
2327
if (matcher.matches()) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.cryptomator.frontend.webdav;
2+
3+
import org.eclipse.jetty.server.HandlerContainer;
4+
import org.eclipse.jetty.servlet.ServletContextHandler;
5+
6+
class FontendIdHidingServletContextHandler extends ServletContextHandler {
7+
8+
public FontendIdHidingServletContextHandler(HandlerContainer parent, String contextPath, int options) {
9+
super(parent, contextPath, options);
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return ContextPaths.removeFrontendId(super.toString());
15+
}
16+
17+
}

‎main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/WebDavServer.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*******************************************************************************/
99
package org.cryptomator.frontend.webdav;
1010

11-
import static java.lang.String.format;
12-
1311
import java.net.URI;
1412
import java.net.URISyntaxException;
1513
import java.util.Collection;
@@ -110,18 +108,18 @@ ServletContextHandler addServlet(Folder root, URI contextRoot) {
110108

111109
@Override
112110
public Frontend create(Folder root, FrontendId id, String name) throws FrontendCreationFailedException {
113-
String contextPath = format("/%s/%s", id, name);
111+
String contextPath = ContextPaths.from(id, name);
114112
final URI uri;
115113
try {
116114
uri = new URI("http", null, "localhost", getPort(), contextPath, null, null);
117115
} catch (URISyntaxException e) {
118116
throw new IllegalStateException(e);
119117
}
120118
final ServletContextHandler handler = addServlet(root, uri);
121-
LOG.info("Servlet available under " + uri);
119+
LOG.info("Servlet available under " + ContextPaths.removeFrontendId(uri.toString()));
122120
return new WebDavFrontend(webdavMounterProvider, handler, uri);
123121
}
124-
122+
125123
public void setValidFrontendIds(Collection<FrontendId> validFrontendIds) {
126124
tarpit.setValidFrontendIds(validFrontendIds);
127125
}

‎main/frontend-webdav/src/main/java/org/cryptomator/frontend/webdav/WebDavServletContextFactory.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@
3535
class WebDavServletContextFactory {
3636

3737
private static final String WILDCARD = "/*";
38-
38+
3939
@Inject
40-
public WebDavServletContextFactory() {}
40+
public WebDavServletContextFactory() {
41+
}
4142

4243
/**
4344
* Creates a new Jetty ServletContextHandler, that can be be added to a servletCollection as follows:
@@ -63,7 +64,7 @@ public ServletContextHandler create(URI contextRoot, Folder root) {
6364
}
6465
};
6566
final String contextPath = StringUtils.removeEnd(contextRoot.getPath(), "/");
66-
final ServletContextHandler servletContext = new ServletContextHandler(null, contextPath, ServletContextHandler.SESSIONS);
67+
final ServletContextHandler servletContext = new FontendIdHidingServletContextHandler(null, contextPath, ServletContextHandler.SESSIONS);
6768
final ServletHolder servletHolder = new ServletHolder(contextPath, new WebDavServlet(contextRoot, root));
6869
servletContext.addServlet(servletHolder, WILDCARD);
6970
servletContext.addFilter(LoopbackFilter.class, WILDCARD, EnumSet.of(DispatcherType.REQUEST));

‎main/ui/src/main/java/org/cryptomator/ui/Cryptomator.java‎

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.function.Consumer;
2121

2222
import org.apache.commons.lang3.SystemUtils;
23+
import org.cryptomator.ui.util.ApplicationVersion;
2324
import org.cryptomator.ui.util.SingleInstanceManager;
2425
import org.cryptomator.ui.util.SingleInstanceManager.RemoteInstance;
2526
import org.eclipse.jetty.util.ConcurrentHashSet;
@@ -33,60 +34,59 @@ public class Cryptomator {
3334
public static final CompletableFuture<Consumer<File>> OPEN_FILE_HANDLER = new CompletableFuture<>();
3435
private static final Logger LOG = LoggerFactory.getLogger(Cryptomator.class);
3536
private static final Set<Runnable> SHUTDOWN_TASKS = new ConcurrentHashSet<>();
36-
private static final CleanShutdownPerformer CLEAN_SHUTDOWN_PERFORMER = new CleanShutdownPerformer();
3737

3838
public static void main(String[] args) {
39-
String cryptomatorVersion = Optional.ofNullable(Cryptomator.class.getPackage().getImplementationVersion()).orElse("SNAPSHOT");
40-
LOG.info("Starting Cryptomator {} on {} {} ({})", cryptomatorVersion, SystemUtils.OS_NAME, SystemUtils.OS_VERSION, SystemUtils.OS_ARCH);
39+
LOG.info("Starting Cryptomator {} on {} {} ({})", ApplicationVersion.orElse("SNAPSHOT"), SystemUtils.OS_NAME, SystemUtils.OS_VERSION, SystemUtils.OS_ARCH);
40+
4141
if (SystemUtils.IS_OS_MAC_OSX) {
42-
/*
43-
* On OSX we're in an awkward position. We need to register a handler in the main thread of this application. However, we can't
44-
* even pass objects to the application, so we're forced to use a static CompletableFuture for the handler, which actually opens
45-
* the file in the application.
46-
*
47-
* Code taken from https://github.com/axet/desktop/blob/master/src/main/java/com/github/axet/desktop/os/mac/AppleHandlers.java
48-
*/
49-
try {
50-
final Class<?> applicationClass = Class.forName("com.apple.eawt.Application");
51-
final Class<?> openFilesHandlerClass = Class.forName("com.apple.eawt.OpenFilesHandler");
52-
final Method getApplication = applicationClass.getMethod("getApplication");
53-
final Object application = getApplication.invoke(null);
54-
final Method setOpenFileHandler = applicationClass.getMethod("setOpenFileHandler", openFilesHandlerClass);
55-
56-
final ClassLoader openFilesHandlerClassLoader = openFilesHandlerClass.getClassLoader();
57-
final OpenFilesHandlerClassHandler openFilesHandlerHandler = new OpenFilesHandlerClassHandler();
58-
final Object openFilesHandlerObject = Proxy.newProxyInstance(openFilesHandlerClassLoader, new Class<?>[] {openFilesHandlerClass}, openFilesHandlerHandler);
59-
60-
setOpenFileHandler.invoke(application, openFilesHandlerObject);
61-
} catch (ReflectiveOperationException | RuntimeException e) {
62-
// Since we're trying to call OS-specific code, we'll just have
63-
// to hope for the best.
64-
LOG.error("exception adding OSX file open handler", e);
65-
}
42+
addOsxFileOpenHandler();
6643
}
6744

68-
/*
69-
* Perform certain things on VM termination.
70-
*/
71-
Runtime.getRuntime().addShutdownHook(CLEAN_SHUTDOWN_PERFORMER);
45+
new CleanShutdownPerformer().registerShutdownHook();
46+
47+
final Optional<RemoteInstance> runningInstance = SingleInstanceManager.getRemoteInstance(MainApplication.APPLICATION_KEY);
48+
if (runningInstance.isPresent()) {
49+
sendArgsToRunningInstance(args, runningInstance);
50+
} else {
51+
Application.launch(MainApplication.class, args);
52+
}
53+
}
7254

55+
private static void addOsxFileOpenHandler() {
7356
/*
74-
* Before starting the application, we check if there is already an instance running on this computer. If so, we send our command
75-
* line arguments to that instance and quit.
57+
* On OSX we're in an awkward position. We need to register a handler in the main thread of this application. However, we can't
58+
* even pass objects to the application, so we're forced to use a static CompletableFuture for the handler, which actually opens
59+
* the file in the application.
60+
*
61+
* Code taken from https://github.com/axet/desktop/blob/master/src/main/java/com/github/axet/desktop/os/mac/AppleHandlers.java
7662
*/
77-
final Optional<RemoteInstance> remoteInstance = SingleInstanceManager.getRemoteInstance(MainApplication.APPLICATION_KEY);
63+
try {
64+
final Class<?> applicationClass = Class.forName("com.apple.eawt.Application");
65+
final Class<?> openFilesHandlerClass = Class.forName("com.apple.eawt.OpenFilesHandler");
66+
final Method getApplication = applicationClass.getMethod("getApplication");
67+
final Object application = getApplication.invoke(null);
68+
final Method setOpenFileHandler = applicationClass.getMethod("setOpenFileHandler", openFilesHandlerClass);
69+
70+
final ClassLoader openFilesHandlerClassLoader = openFilesHandlerClass.getClassLoader();
71+
final OpenFilesHandlerClassHandler openFilesHandlerHandler = new OpenFilesHandlerClassHandler();
72+
final Object openFilesHandlerObject = Proxy.newProxyInstance(openFilesHandlerClassLoader, new Class<?>[] {openFilesHandlerClass}, openFilesHandlerHandler);
73+
74+
setOpenFileHandler.invoke(application, openFilesHandlerObject);
75+
} catch (ReflectiveOperationException | RuntimeException e) {
76+
// Since we're trying to call OS-specific code, we'll just have
77+
// to hope for the best.
78+
LOG.error("exception adding OSX file open handler", e);
79+
}
80+
}
7881

79-
if (remoteInstance.isPresent()) {
80-
try (RemoteInstance instance = remoteInstance.get()) {
81-
LOG.info("An instance of Cryptomator is already running at {}.", instance.getRemotePort());
82-
for (int i = 0; i < args.length; i++) {
83-
remoteInstance.get().sendMessage(args[i], 100);
84-
}
85-
} catch (Exception e) {
86-
LOG.error("Error forwarding arguments to remote instance", e);
82+
private static void sendArgsToRunningInstance(String[] args, final Optional<RemoteInstance> remoteInstance) {
83+
try (RemoteInstance instance = remoteInstance.get()) {
84+
LOG.info("An instance of Cryptomator is already running at {}.", instance.getRemotePort());
85+
for (int i = 0; i < args.length; i++) {
86+
remoteInstance.get().sendMessage(args[i], 100);
8787
}
88-
} else {
89-
Application.launch(MainApplication.class, args);
88+
} catch (Exception e) {
89+
LOG.error("Error forwarding arguments to remote instance", e);
9090
}
9191
}
9292

@@ -111,6 +111,10 @@ public void run() {
111111
});
112112
SHUTDOWN_TASKS.clear();
113113
}
114+
115+
public void registerShutdownHook() {
116+
Runtime.getRuntime().addShutdownHook(this);
117+
}
114118
}
115119

116120
private static void handleOpenFileRequest(File file) {

‎main/ui/src/main/java/org/cryptomator/ui/CryptomatorComponent.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ interface CryptomatorComponent {
3737

3838
ExitUtil exitUtil();
3939

40+
DebugMode debugMode();
41+
4042
Optional<MacFunctions> nativeMacFunctions();
4143

4244
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.cryptomator.ui;
2+
3+
import static java.util.Arrays.asList;
4+
import static org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME;
5+
6+
import java.util.Collection;
7+
8+
import javax.inject.Inject;
9+
import javax.inject.Singleton;
10+
11+
import org.apache.logging.log4j.Level;
12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.core.LoggerContext;
14+
import org.apache.logging.log4j.core.config.Configuration;
15+
import org.apache.logging.log4j.core.config.LoggerConfig;
16+
import org.cryptomator.ui.settings.Settings;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
@Singleton
21+
public class DebugMode {
22+
23+
private static final Logger LOG = LoggerFactory.getLogger(DebugMode.class);
24+
25+
private static final Collection<LoggerUpgrade> LOGGER_UPGRADES = asList( //
26+
loggerUpgrade(ROOT_LOGGER_NAME, Level.INFO), //
27+
loggerUpgrade("org.cryptomator", Level.TRACE), //
28+
loggerUpgrade("org.eclipse.jetty.server.Server", Level.DEBUG) //
29+
);
30+
31+
private final Settings settings;
32+
33+
@Inject
34+
public DebugMode(Settings settings) {
35+
this.settings = settings;
36+
}
37+
38+
public void initialize() {
39+
if (settings.getDebugMode()) {
40+
enable();
41+
LOG.debug("Debug mode initialized");
42+
}
43+
}
44+
45+
private void enable() {
46+
LoggerContext context = (LoggerContext) LogManager.getContext(false);
47+
Configuration config = context.getConfiguration();
48+
LOGGER_UPGRADES.forEach(loggerUpgrade -> loggerUpgrade.execute(config));
49+
context.updateLoggers();
50+
}
51+
52+
private static LoggerUpgrade loggerUpgrade(String loggerName, Level minLevel) {
53+
return new LoggerUpgrade(loggerName, minLevel);
54+
}
55+
56+
private static class LoggerUpgrade {
57+
58+
private final Level level;
59+
private final String loggerName;
60+
61+
public LoggerUpgrade(String loggerName, Level minLevel) {
62+
this.loggerName = loggerName;
63+
this.level = minLevel;
64+
}
65+
66+
public void execute(Configuration config) {
67+
LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
68+
if (loggerConfig.getLevel().isMoreSpecificThan(level)) {
69+
loggerConfig.setLevel(level);
70+
}
71+
}
72+
73+
}
74+
75+
}

0 commit comments

Comments
 (0)