Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide default directory for js plugins #3070

Merged
merged 2 commits into from
Jan 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions server/jetty/src/main/java/io/deephaven/server/jetty/JsPlugins.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,69 @@
package io.deephaven.server.jetty;

import io.deephaven.configuration.ConfigDir;
import io.deephaven.configuration.Configuration;
import io.deephaven.internal.log.LoggerFactory;
import io.deephaven.io.logger.Logger;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;

import static org.eclipse.jetty.servlet.ServletContextHandler.NO_SESSIONS;

class JsPlugins {

private static final Logger log = LoggerFactory.getLogger(JsPlugins.class);

public static void maybeAdd(Consumer<Handler> addHandler) {
resource()
mofojed marked this conversation as resolved.
Show resolved Hide resolved
.map(ControlledCacheResource::wrap)
.ifPresent(resource -> addResource(addHandler, resource));
}

private static void addResource(Consumer<Handler> addHandler, ControlledCacheResource resource) {
log.info().append("Creating JsPlugins context with resource '").append(resource.toString()).append('\'').endl();
WebAppContext context =
new WebAppContext(null, "/js-plugins/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
context.setBaseResource(resource);
context.setInitParameter(DefaultServlet.CONTEXT_INIT + "dirAllowed", "false");
// Suppress warnings about security handlers
context.setSecurityHandler(new ConstraintSecurityHandler());
addHandler.accept(context);
}

private static Optional<Resource> resource() {
// Note: this would probably be better to live in JettyConfig - but until we establish more formal expectations
// for js plugin configuration and workflows, we'll keep this here.
final String resourceBase =
Configuration.getInstance().getStringWithDefault("deephaven.jsPlugins.resourceBase", null);
if (resourceBase == null) {
return;
// defaults to "<configDir>/js-plugins/" if it exists
return defaultJsPluginsDirectory()
.filter(Files::exists)
.map(PathResource::new);
}
try {
Resource resource = ControlledCacheResource.wrap(Resource.newResource(resourceBase));
WebAppContext context =
new WebAppContext(null, "/js-plugins/", null, null, null, new ErrorPageErrorHandler(), NO_SESSIONS);
context.setBaseResource(resource);
context.setInitParameter(DefaultServlet.CONTEXT_INIT + "dirAllowed", "false");
// Suppress warnings about security handlers
context.setSecurityHandler(new ConstraintSecurityHandler());
addHandler.accept(context);
return Optional.of(Resource.newResource(resourceBase));
} catch (IOException e) {
throw new IllegalStateException(String.format("Unable to resolve resourceBase '%s'", resourceBase), e);
}
}

private static Optional<Path> defaultJsPluginsDirectory() {
return ConfigDir.get().map(JsPlugins::jsPluginsDir);
}

private static Path jsPluginsDir(Path configDir) {
return configDir.resolve("js-plugins");
}
}