From eda8bb8e09bf7838b40d4c2244ff2f8cb63cb21f Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Wed, 7 Nov 2018 13:36:57 +0000 Subject: [PATCH] Move the Distributor to the new routes --- .../selenium/grid/distributor/AddNode.java | 9 +--- .../grid/distributor/CreateSession.java | 9 +--- .../grid/distributor/Distributor.java | 48 ++++++++++--------- .../selenium/grid/distributor/RemoveNode.java | 24 ++-------- 4 files changed, 32 insertions(+), 58 deletions(-) diff --git a/java/server/src/org/openqa/selenium/grid/distributor/AddNode.java b/java/server/src/org/openqa/selenium/grid/distributor/AddNode.java index 5383b2c206f2d..0e77184439d2a 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/AddNode.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/AddNode.java @@ -18,7 +18,6 @@ package org.openqa.selenium.grid.distributor; import static org.openqa.selenium.json.Json.MAP_TYPE; -import static org.openqa.selenium.remote.http.HttpMethod.POST; import org.openqa.selenium.Capabilities; import org.openqa.selenium.ImmutableCapabilities; @@ -39,10 +38,9 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; -import java.util.function.Predicate; import java.util.stream.Collectors; -public class AddNode implements Predicate, CommandHandler { +public class AddNode implements CommandHandler { private final Distributor distributor; private final Json json; @@ -54,11 +52,6 @@ public AddNode(Distributor distributor, Json json, HttpClient.Factory httpFactor this.httpFactory = Objects.requireNonNull(httpFactory); } - @Override - public boolean test(HttpRequest req) { - return req.getMethod() == POST && "/se/grid/distributor/node".equals(req.getUri()); - } - @Override public void execute(HttpRequest req, HttpResponse resp) throws IOException { Map raw = json.toType(req.getContentString(), MAP_TYPE); diff --git a/java/server/src/org/openqa/selenium/grid/distributor/CreateSession.java b/java/server/src/org/openqa/selenium/grid/distributor/CreateSession.java index c0999fb6867c5..0b08b06b40473 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/CreateSession.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/CreateSession.java @@ -18,7 +18,6 @@ package org.openqa.selenium.grid.distributor; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.openqa.selenium.remote.http.HttpMethod.POST; import com.google.common.collect.ImmutableMap; @@ -33,9 +32,8 @@ import java.io.Reader; import java.io.StringReader; import java.util.Objects; -import java.util.function.Predicate; -class CreateSession implements Predicate, CommandHandler { +class CreateSession implements CommandHandler { private final Distributor distributor; private final Json json; @@ -45,11 +43,6 @@ public CreateSession(Distributor distributor, Json json) { this.json = Objects.requireNonNull(json); } - @Override - public boolean test(HttpRequest req) { - return req.getMethod() == POST && "/session".equals(req.getUri()); - } - @Override public void execute(HttpRequest req, HttpResponse resp) throws IOException { try (Reader reader = new StringReader(req.getContentString()); diff --git a/java/server/src/org/openqa/selenium/grid/distributor/Distributor.java b/java/server/src/org/openqa/selenium/grid/distributor/Distributor.java index 551f3a56e681b..b7a2e935cbc96 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/Distributor.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/Distributor.java @@ -17,15 +17,16 @@ package org.openqa.selenium.grid.distributor; -import static org.openqa.selenium.grid.server.Server.get; - -import com.google.common.collect.ImmutableMap; +import static org.openqa.selenium.grid.web.Routes.delete; +import static org.openqa.selenium.grid.web.Routes.get; +import static org.openqa.selenium.grid.web.Routes.post; import org.openqa.selenium.SessionNotCreatedException; import org.openqa.selenium.grid.data.Session; import org.openqa.selenium.grid.node.Node; import org.openqa.selenium.grid.web.CommandHandler; -import org.openqa.selenium.grid.web.CompoundHandler; +import org.openqa.selenium.grid.web.HandlerNotFoundException; +import org.openqa.selenium.grid.web.Routes; import org.openqa.selenium.injector.Injector; import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.NewSessionPayload; @@ -34,8 +35,8 @@ import org.openqa.selenium.remote.http.HttpResponse; import java.io.IOException; +import java.util.Optional; import java.util.UUID; -import java.util.function.BiFunction; import java.util.function.Predicate; /** @@ -71,23 +72,22 @@ */ public abstract class Distributor implements Predicate, CommandHandler { - private final CompoundHandler handler; + private final Routes routes; + private final Injector injector; protected Distributor() { - Json json = new Json(); - - CreateSession create = new CreateSession(this, json); - AddNode addNode = new AddNode(this, json, HttpClient.Factory.createDefault()); - RemoveNode removeNode = new RemoveNode(this); - - handler = new CompoundHandler( - Injector.builder().register(this).register(json).build(), - ImmutableMap., BiFunction>builder() - .put(create, (inj, req) -> create) - .put(addNode, (inj, req) -> addNode) - .put(removeNode, (inj, req) -> removeNode) - .put(get("/status"), (Injector inj, HttpRequest req) -> inj.newInstance(StatusHandler.class)) - .build()); + injector = Injector.builder() + .register(this) + .register(new Json()) + .register(HttpClient.Factory.createDefault()) + .build(); + + routes = Routes.combine( + post("/session").using(CreateSession.class), + post("/se/grid/distributor/node").using(AddNode.class), + delete("/se/grid/distributor/node/{nodeId}").using(RemoveNode.class).map("nodeId", UUID::fromString), + get("/status").using(StatusHandler.class) + ).build(); } public abstract Session newSession(NewSessionPayload payload) throws SessionNotCreatedException; @@ -100,11 +100,15 @@ protected Distributor() { @Override public boolean test(HttpRequest req) { - return handler.test(req); + return routes.match(injector, req).isPresent(); } @Override public void execute(HttpRequest req, HttpResponse resp) throws IOException { - handler.execute(req, resp); + Optional handler = routes.match(injector, req); + if (!handler.isPresent()) { + throw new HandlerNotFoundException(req); + } + handler.get().execute(req, resp); } } diff --git a/java/server/src/org/openqa/selenium/grid/distributor/RemoveNode.java b/java/server/src/org/openqa/selenium/grid/distributor/RemoveNode.java index 11e48465bea61..b8579c9cdc543 100644 --- a/java/server/src/org/openqa/selenium/grid/distributor/RemoveNode.java +++ b/java/server/src/org/openqa/selenium/grid/distributor/RemoveNode.java @@ -17,42 +17,26 @@ package org.openqa.selenium.grid.distributor; -import static org.openqa.selenium.remote.http.HttpMethod.DELETE; - -import org.openqa.selenium.WebDriverException; import org.openqa.selenium.grid.web.CommandHandler; -import org.openqa.selenium.grid.web.UrlTemplate; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.io.IOException; import java.util.Objects; import java.util.UUID; -import java.util.function.Predicate; - -class RemoveNode implements Predicate, CommandHandler { - public static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/distributor/node/{nodeId}"); +class RemoveNode implements CommandHandler { private final Distributor distributor; + private final UUID nodeId; - public RemoveNode(Distributor distributor) { + public RemoveNode(Distributor distributor, UUID nodeId) { this.distributor = Objects.requireNonNull(distributor); - } - - @Override - public boolean test(HttpRequest req) { - return req.getMethod() == DELETE && TEMPLATE.match(req.getUri()) != null; + this.nodeId = Objects.requireNonNull(nodeId); } @Override public void execute(HttpRequest req, HttpResponse resp) throws IOException { - UrlTemplate.Match match = TEMPLATE.match(req.getUri()); - if (match == null || match.getParameters().get("nodeId") == null) { - throw new WebDriverException("Node ID not found in URL: " + req.getUri()); - } - - UUID nodeId = UUID.fromString(match.getParameters().get("nodeId")); distributor.remove(nodeId); } }