From b9f18f46f2ebb314f97e251482b82e9af7cb0ab4 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Wed, 7 Nov 2018 13:43:46 +0000 Subject: [PATCH] Move the session map to the new routes --- .../grid/sessionmap/AddToSessionMap.java | 16 +------- .../grid/sessionmap/GetFromSessionMap.java | 23 ++--------- .../grid/sessionmap/RemoveFromSession.java | 27 ++----------- .../selenium/grid/sessionmap/SessionMap.java | 39 ++++++++++++------- 4 files changed, 34 insertions(+), 71 deletions(-) diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java b/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java index b10d149c6c7ae..1e3ce78022978 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java @@ -18,24 +18,19 @@ package org.openqa.selenium.grid.sessionmap; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.openqa.selenium.remote.http.HttpMethod.POST; import com.google.common.collect.ImmutableMap; -import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.grid.data.Session; import org.openqa.selenium.grid.web.CommandHandler; -import org.openqa.selenium.grid.web.UrlTemplate; import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.util.Objects; -import java.util.function.Predicate; -class AddToSessionMap implements Predicate, CommandHandler { +class AddToSessionMap implements CommandHandler { - private static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/session"); private final Json json; private final SessionMap sessions; @@ -44,17 +39,8 @@ class AddToSessionMap implements Predicate, CommandHandler { this.sessions = Objects.requireNonNull(sessions); } - @Override - public boolean test(HttpRequest req) { - return req.getMethod() == POST && TEMPLATE.match(req.getUri()) != null; - } - @Override public void execute(HttpRequest req, HttpResponse resp) { - if (!test(req)) { - throw new NoSuchSessionException("Session ID not found in URL: " + req.getUri()); - } - Session session = json.toType(req.getContentString(), Session.class); Objects.requireNonNull(session, "Session to add must be set"); diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java b/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java index 79dc8402d7c4d..985092ebc7e14 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java @@ -18,14 +18,11 @@ package org.openqa.selenium.grid.sessionmap; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.openqa.selenium.remote.http.HttpMethod.GET; import com.google.common.collect.ImmutableMap; -import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.grid.data.Session; import org.openqa.selenium.grid.web.CommandHandler; -import org.openqa.selenium.grid.web.UrlTemplate; import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.http.HttpRequest; @@ -33,33 +30,21 @@ import java.io.IOException; import java.util.Objects; -import java.util.function.Predicate; -class GetFromSessionMap implements Predicate, CommandHandler { +class GetFromSessionMap implements CommandHandler { - private static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/session/{sessionId}"); private final Json json; private final SessionMap sessions; + private SessionId id; - public GetFromSessionMap(Json json, SessionMap sessions) { + public GetFromSessionMap(Json json, SessionMap sessions, SessionId id) { this.json = Objects.requireNonNull(json); this.sessions = Objects.requireNonNull(sessions); - } - - @Override - public boolean test(HttpRequest request) { - return request.getMethod() == GET && TEMPLATE.match(request.getUri()) != null; + this.id = Objects.requireNonNull(id); } @Override public void execute(HttpRequest req, HttpResponse resp) throws IOException { - UrlTemplate.Match match = TEMPLATE.match(req.getUri()); - if (match == null || match.getParameters().get("sessionId") == null) { - throw new NoSuchSessionException("Session ID not found in URL: " + req.getUri()); - } - - SessionId id = new SessionId(match.getParameters().get("sessionId")); - Session session = sessions.get(id); resp.setContent(json.toJson(ImmutableMap.of("value", session)).getBytes(UTF_8)); diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java b/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java index 79bc025f96c88..3ca8b1c7c02f2 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java @@ -17,44 +17,25 @@ package org.openqa.selenium.grid.sessionmap; -import static org.openqa.selenium.remote.http.HttpMethod.DELETE; - -import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.grid.web.CommandHandler; -import org.openqa.selenium.grid.web.UrlTemplate; -import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.util.Objects; -import java.util.function.Predicate; -class RemoveFromSession implements Predicate, CommandHandler { +class RemoveFromSession implements CommandHandler { - private static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/session/{sessionId}"); - private final Json json; private final SessionMap sessions; + private SessionId id; - public RemoveFromSession(Json json, SessionMap sessions) { - this.json = Objects.requireNonNull(json); + public RemoveFromSession(SessionMap sessions, SessionId id) { this.sessions = Objects.requireNonNull(sessions); - } - - @Override - public boolean test(HttpRequest request) { - return request.getMethod() == DELETE && TEMPLATE.match(request.getUri()) != null; + this.id = Objects.requireNonNull(id); } @Override public void execute(HttpRequest req, HttpResponse resp) { - UrlTemplate.Match match = TEMPLATE.match(req.getUri()); - if (match == null || match.getParameters().get("sessionId") == null) { - throw new NoSuchSessionException("Session ID not found in URL: " + req.getUri()); - } - - SessionId id = new SessionId(match.getParameters().get("sessionId")); - sessions.remove(id); } } diff --git a/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java b/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java index 1fa7372529daf..77d73b7e86dfc 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java @@ -17,12 +17,15 @@ package org.openqa.selenium.grid.sessionmap; -import com.google.common.collect.ImmutableMap; +import static org.openqa.selenium.grid.web.Routes.combine; +import static org.openqa.selenium.grid.web.Routes.delete; +import static org.openqa.selenium.grid.web.Routes.post; import org.openqa.selenium.NoSuchSessionException; import org.openqa.selenium.grid.data.Session; 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.SessionId; @@ -31,6 +34,7 @@ import java.io.IOException; import java.net.URI; +import java.util.Optional; import java.util.function.Predicate; /** @@ -67,7 +71,8 @@ */ public abstract class SessionMap implements Predicate, CommandHandler { - private final CompoundHandler handler; + private final Routes routes; + private final Injector injector; public abstract boolean add(Session session); @@ -78,25 +83,31 @@ public abstract class SessionMap implements Predicate, CommandHandl public SessionMap() { Json json = new Json(); - AddToSessionMap add = new AddToSessionMap(json, this); - GetFromSessionMap get = new GetFromSessionMap(json, this); - RemoveFromSession remove = new RemoveFromSession(json, this); + injector = Injector.builder() + .register(this) + .register(new Json()) + .build(); - handler = new CompoundHandler( - Injector.builder().build(), - ImmutableMap.of( - get, (inj, req) -> get, // List "get" first because it's most commonly called - add, (inj, req) -> add, - remove, (inj, req) -> remove)); + routes = combine( + post("/se/grid/session").using(AddToSessionMap.class), + Routes.get("/se/grid/session/{sessionId}").using(GetFromSessionMap.class) + .map("sessionId", SessionId::new), + delete("/se/grid/session/{sessionId}").using(RemoveFromSession.class) + .map("sessionId", SessionId::new)) + .build(); } @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); } }