Skip to content

Commit

Permalink
Push more common resources into the handler
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 24, 2020
1 parent 620c9a6 commit 36072f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@

package org.openqa.selenium.environment.webserver;

import org.openqa.selenium.build.InProject;
import org.openqa.selenium.grid.web.MergedResource;
import org.openqa.selenium.grid.web.PathResource;
import org.openqa.selenium.grid.web.Resource;
import org.openqa.selenium.grid.web.ResourceHandler;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Routable;
import org.openqa.selenium.remote.http.Route;

import java.io.UncheckedIOException;
import java.nio.file.Path;
Expand All @@ -34,8 +38,21 @@ public class CommonWebResources implements Routable {
private final Routable delegate;

public CommonWebResources() {
Path common = locate("common/src/web").toAbsolutePath();
delegate = new ResourceHandler(new PathResource(common));
Resource resources = new MergedResource(new PathResource(locate("common/src/web")))
.alsoCheck(new PathResource(locate("javascript")))
.alsoCheck(new PathResource(locate("third_party/closure/goog")))
.alsoCheck(new PathResource(locate("third_party/js")));

Path runfiles = InProject.findRunfilesRoot();
if (runfiles != null) {
ResourceHandler handler = new ResourceHandler(new PathResource(runfiles));
delegate = Route.combine(
new ResourceHandler(resources),
Route.prefix("/filez").to(Route.combine(handler))
);
} else {
delegate = new ResourceHandler(resources);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,14 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
handlers = new ContextHandlerCollection();

Path webSrc = locate("common/src/web");
ServletContextHandler defaultContext = addResourceHandler(
DEFAULT_CONTEXT_PATH, webSrc);
ServletContextHandler defaultContext = new ServletContextHandler();
handlers.addHandler(defaultContext);

// Only non-null when running with bazel test.
Path runfiles = InProject.findRunfilesRoot();
if (runfiles != null) {
addResourceHandler(FILEZ_CONTEXT_PATH, runfiles);
}

addJsResourceHandler(JS_SRC_CONTEXT_PATH, "javascript");
addJsResourceHandler(CLOSURE_CONTEXT_PATH, "third_party/closure/goog");
addJsResourceHandler(THIRD_PARTY_JS_CONTEXT_PATH, "third_party/js");
// if (runfiles != null) {
// addResourceHandler(FILEZ_CONTEXT_PATH, runfiles);
// }

TemporaryFilesystem tempFs = TemporaryFilesystem.getDefaultTmpFS();
tempPageDir = tempFs.createTempDir("pages", "test");
Expand Down Expand Up @@ -160,30 +156,24 @@ public JettyAppServer(String hostName, int httpPort, int httpsPort) {
Route.get("/sleep").to(SleepingHandler::new),
Route.post("/upload").to(UploadHandler::new),
Route.matching(req -> req.getUri().startsWith("/utf8/")).to(() -> new Utf8Handler(webSrc, "/utf8/")),
Route.prefix(TEMP_SRC_CONTEXT_PATH).to(Route.combine(generatedPages))
);
Route.prefix(TEMP_SRC_CONTEXT_PATH).to(Route.combine(generatedPages)),
new CommonWebResources());

// If we're not running inside `bazel test` this will be non-null
// if (runfiles != null) {
// route = Route.combine(
// route,
// Route.matching(req -> req.getUri().startsWith(FILEZ_CONTEXT_PATH)).to(new )
// )
// addResourceHandler(FILEZ_CONTEXT_PATH, runfiles);
// }

Route prefixed = Route.prefix(DEFAULT_CONTEXT_PATH).to(route);
defaultContext.addServlet(new ServletHolder(new HttpHandlerServlet(Route.combine(route, prefixed))), "/*");

server.setHandler(handlers);
}

private void addJsResourceHandler(String handlerPath, String dirPath) {
Path path;
try {
path = locate(dirPath);
} catch (WebDriverException e) {
// Ugly hack to get us started with bazel while sorting out missing data dependencies.
if (Boolean.getBoolean(getClass().getPackage().getName() + ".ignoreMissingJsRoots")
&& e.getCause() instanceof FileNotFoundException) {
System.err.println("WARNING: failed to add resource handler " + handlerPath + ": " + e.getCause());
return;
}
throw e;
}
addResourceHandler(handlerPath, path);
}

private static Optional<Integer> getEnvValue(String key) {
return Optional.ofNullable(System.getenv(key)).map(Integer::parseInt);
}
Expand Down

0 comments on commit 36072f2

Please sign in to comment.