Skip to content

Commit

Permalink
Migrate most command handlers to http handlers in session map
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 4, 2019
1 parent c5b8a70 commit 5fc74cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +68,7 @@
* </tr>
* </table>
*/
public abstract class SessionMap implements Predicate<HttpRequest>, CommandHandler {
public abstract class SessionMap implements Predicate<HttpRequest>, CommandHandler, Routable, HttpHandler {

private final Route routes;

Expand All @@ -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);
}
}

0 comments on commit 5fc74cd

Please sign in to comment.