From 5fc74cd7dd3d41cb44b2f4cd1245d663cc88614e Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Wed, 12 Jun 2019 16:11:32 +0100 Subject: [PATCH] Migrate most command handlers to http handlers in session map --- .../grid/sessionmap/AddToSessionMap.java | 15 +++++++-------- .../grid/sessionmap/GetFromSessionMap.java | 13 ++++++------- .../grid/sessionmap/RemoveFromSession.java | 7 ++++--- .../selenium/grid/sessionmap/SessionMap.java | 18 +++++++++++++++--- 4 files changed, 32 insertions(+), 21 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 06173ef2d4530..dee7f666e504a 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/AddToSessionMap.java @@ -17,20 +17,19 @@ package org.openqa.selenium.grid.sessionmap; -import static org.openqa.selenium.remote.http.Contents.string; -import static org.openqa.selenium.remote.http.Contents.utf8String; - import com.google.common.collect.ImmutableMap; - import org.openqa.selenium.grid.data.Session; -import org.openqa.selenium.grid.web.CommandHandler; import org.openqa.selenium.json.Json; +import org.openqa.selenium.remote.http.HttpHandler; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.util.Objects; -class AddToSessionMap implements CommandHandler { +import static org.openqa.selenium.remote.http.Contents.string; +import static org.openqa.selenium.remote.http.Contents.utf8String; + +class AddToSessionMap implements HttpHandler { private final Json json; private final SessionMap sessions; @@ -41,12 +40,12 @@ class AddToSessionMap implements CommandHandler { } @Override - public void execute(HttpRequest req, HttpResponse resp) { + public HttpResponse execute(HttpRequest req) { Session session = json.toType(string(req), Session.class); Objects.requireNonNull(session, "Session to add must be set"); sessions.add(session); - resp.setContent(utf8String(json.toJson(ImmutableMap.of("value", true)))); + return new HttpResponse().setContent(utf8String(json.toJson(ImmutableMap.of("value", true)))); } } 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 11a8882924430..a6f3810e0d85a 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/GetFromSessionMap.java @@ -17,20 +17,19 @@ package org.openqa.selenium.grid.sessionmap; -import static org.openqa.selenium.remote.http.Contents.utf8String; - import com.google.common.collect.ImmutableMap; - import org.openqa.selenium.grid.data.Session; -import org.openqa.selenium.grid.web.CommandHandler; import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.SessionId; +import org.openqa.selenium.remote.http.HttpHandler; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.util.Objects; -class GetFromSessionMap implements CommandHandler { +import static org.openqa.selenium.remote.http.Contents.utf8String; + +class GetFromSessionMap implements HttpHandler { private final Json json; private final SessionMap sessions; @@ -43,9 +42,9 @@ public GetFromSessionMap(Json json, SessionMap sessions, SessionId id) { } @Override - public void execute(HttpRequest req, HttpResponse resp) { + public HttpResponse execute(HttpRequest req) { Session session = sessions.get(id); - resp.setContent(utf8String(json.toJson(ImmutableMap.of("value", session)))); + return new HttpResponse().setContent(utf8String(json.toJson(ImmutableMap.of("value", session)))); } } 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 3ca8b1c7c02f2..cdf5f61afd723 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/RemoveFromSession.java @@ -17,14 +17,14 @@ package org.openqa.selenium.grid.sessionmap; -import org.openqa.selenium.grid.web.CommandHandler; import org.openqa.selenium.remote.SessionId; +import org.openqa.selenium.remote.http.HttpHandler; import org.openqa.selenium.remote.http.HttpRequest; import org.openqa.selenium.remote.http.HttpResponse; import java.util.Objects; -class RemoveFromSession implements CommandHandler { +class RemoveFromSession implements HttpHandler { private final SessionMap sessions; private SessionId id; @@ -35,7 +35,8 @@ public RemoveFromSession(SessionMap sessions, SessionId id) { } @Override - public void execute(HttpRequest req, HttpResponse resp) { + public HttpResponse execute(HttpRequest req) { sessions.remove(id); + return new HttpResponse(); } } 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 cee6cf26a4742..b110d526bf952 100644 --- a/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java +++ b/java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java @@ -22,8 +22,10 @@ import org.openqa.selenium.grid.web.CommandHandler; import org.openqa.selenium.json.Json; import org.openqa.selenium.remote.SessionId; +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.IOException; @@ -66,7 +68,7 @@ * * */ -public abstract class SessionMap implements Predicate, CommandHandler { +public abstract class SessionMap implements Predicate, CommandHandler, Routable, HttpHandler { private final Route routes; @@ -92,7 +94,17 @@ public boolean test(HttpRequest req) { } @Override - public void execute(HttpRequest req, HttpResponse resp) throws IOException { - copyResponse(routes.execute(req), resp); + public boolean matches(HttpRequest req) { + return routes.matches(req); + } + + @Override + public HttpResponse execute(HttpRequest req) { + return routes.execute(req); + } + + @Override + public void execute(HttpRequest req, HttpResponse res) throws IOException { + copyResponse(execute(req), res); } }