Skip to content

Commit

Permalink
Continue removing the old CommandHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 4, 2019
1 parent 21a2ceb commit d062f62
Show file tree
Hide file tree
Showing 45 changed files with 366 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@

package org.openqa.selenium.grid.distributor;

import static org.openqa.selenium.remote.http.Contents.bytes;
import static org.openqa.selenium.remote.http.Route.delete;
import static org.openqa.selenium.remote.http.Route.get;
import static org.openqa.selenium.remote.http.Route.post;

import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.DistributorStatus;
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.HandlerNotFoundException;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpClient;
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 org.openqa.selenium.remote.tracing.DistributedTracer;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.http.Contents.bytes;
import static org.openqa.selenium.remote.http.Route.delete;
import static org.openqa.selenium.remote.http.Route.get;
import static org.openqa.selenium.remote.http.Route.post;

/**
* Responsible for being the central place where the {@link Node}s on which {@link Session}s run
* are determined.
Expand Down Expand Up @@ -74,7 +73,7 @@
* </tr>
* </table>
*/
public abstract class Distributor implements Predicate<HttpRequest>, CommandHandler {
public abstract class Distributor implements Predicate<HttpRequest>, Routable, HttpHandler {

private final Route routes;

Expand Down Expand Up @@ -108,12 +107,17 @@ public abstract CreateSessionResponse newSession(HttpRequest request)
public abstract DistributorStatus getStatus();

@Override
public boolean test(HttpRequest req) {
public boolean test(HttpRequest httpRequest) {
return matches(httpRequest);
}

@Override
public boolean matches(HttpRequest req) {
return routes.matches(req);
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
copyResponse(routes.execute(req), resp);
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
return routes.execute(req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.grid.data.DistributorStatus;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
package org.openqa.selenium.grid.node;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpHandler;

import java.net.URI;

public interface ActiveSession extends CommandHandler {
public interface ActiveSession extends HttpHandler {

SessionId getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package org.openqa.selenium.grid.node;

import org.openqa.selenium.grid.web.CommandHandler;
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 ForwardWebDriverCommand implements CommandHandler {
class ForwardWebDriverCommand implements HttpHandler {

private final Node node;

Expand All @@ -32,7 +32,7 @@ public ForwardWebDriverCommand(Node node) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
node.executeWebDriverCommand(req, resp);
public HttpResponse execute(HttpRequest req) {
return node.executeWebDriverCommand(req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

package org.openqa.selenium.grid.node;

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.io.UncheckedIOException;
import java.util.Objects;

class GetNodeSession implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.utf8String;

class GetNodeSession implements HttpHandler {

private final Node node;
private final Json json;
Expand All @@ -43,9 +43,9 @@ class GetNodeSession implements CommandHandler {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
Session session = node.getSession(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,19 +17,19 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.Contents.utf8String;

import com.google.common.collect.ImmutableMap;

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.io.UncheckedIOException;
import java.util.Objects;

class IsSessionOwner implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.utf8String;

class IsSessionOwner implements HttpHandler {

private final Node node;
private final Json json;
Expand All @@ -42,8 +42,8 @@ public IsSessionOwner(Node node, Json json, SessionId id) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
resp.setContent(utf8String(json.toJson(
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
return new HttpResponse().setContent(utf8String(json.toJson(
ImmutableMap.of("value", node.isSessionOwner(id)))));
}
}
20 changes: 9 additions & 11 deletions java/server/src/org/openqa/selenium/grid/node/NewNodeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,39 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.Contents.utf8String;

import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.data.CreateSessionResponse;
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.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Objects;
import java.util.function.BiConsumer;

class NewNodeSession implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.Contents.utf8String;

class NewNodeSession implements HttpHandler {

private final BiConsumer<HttpResponse, Object> encodeJson;
private final Node node;
private final Json json;

NewNodeSession(Node node, Json json) {
this.node = Objects.requireNonNull(node);

this.json = Objects.requireNonNull(json);
this.encodeJson = (res, obj) -> res.setContent(utf8String(json.toJson(obj)));
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
CreateSessionRequest incoming = json.toType(string(req), CreateSessionRequest.class);

CreateSessionResponse sessionResponse = node.newSession(incoming).orElse(null);

HashMap<String, Object> value = new HashMap<>();
value.put("value", sessionResponse);
encodeJson.accept(resp, value);

return new HttpResponse().setContent(utf8String(json.toJson(value)));
}
}
15 changes: 7 additions & 8 deletions java/server/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.NodeStatus;
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 org.openqa.selenium.remote.http.Routable;
import org.openqa.selenium.remote.http.Route;
import org.openqa.selenium.remote.tracing.DistributedTracer;

import java.io.IOException;
import java.net.URI;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;

import static org.openqa.selenium.remote.HttpSessionId.getSessionId;
import static org.openqa.selenium.remote.http.Contents.utf8String;
Expand Down Expand Up @@ -94,7 +93,7 @@
* </tr>
* </table>
*/
public abstract class Node implements Predicate<HttpRequest>, CommandHandler {
public abstract class Node implements Routable, HttpHandler {

protected final DistributedTracer tracer;
private final UUID id;
Expand Down Expand Up @@ -134,7 +133,7 @@ public URI getUri() {

public abstract Optional<CreateSessionResponse> newSession(CreateSessionRequest sessionRequest);

public abstract void executeWebDriverCommand(HttpRequest req, HttpResponse resp);
public abstract HttpResponse executeWebDriverCommand(HttpRequest req);

public abstract Session getSession(SessionId id) throws NoSuchSessionException;

Expand All @@ -149,12 +148,12 @@ public URI getUri() {
public abstract HealthCheck getHealthCheck();

@Override
public boolean test(HttpRequest req) {
public boolean matches(HttpRequest req) {
return routes.matches(req);
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
copyResponse(routes.execute(req), resp);
public HttpResponse execute(HttpRequest req) {
return routes.execute(req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.net.URL;
import java.util.Objects;

import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

public abstract class ProtocolConvertingSession extends BaseActiveSession {

private final CommandHandler handler;
private final HttpHandler handler;
private final String killUrl;

protected ProtocolConvertingSession(
Expand All @@ -59,10 +58,11 @@ protected ProtocolConvertingSession(
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
handler.execute(req, resp);
public HttpResponse execute(HttpRequest req) {
HttpResponse res = handler.execute(req);
if (req.getMethod() == DELETE && killUrl.equals(req.getUri())) {
stop();
}
return res;
}
}
14 changes: 7 additions & 7 deletions java/server/src/org/openqa/selenium/grid/node/StatusHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.Contents.utf8String;

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.grid.data.NodeStatus;
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.io.UncheckedIOException;
import java.util.Objects;

public class StatusHandler implements CommandHandler {
import static org.openqa.selenium.remote.http.Contents.utf8String;

public class StatusHandler implements HttpHandler {

private final Node node;
private final Json json;
Expand All @@ -40,7 +40,7 @@ public StatusHandler(Node node, Json json) {
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
NodeStatus status = node.getStatus();

ImmutableMap<String, Object> report = ImmutableMap.of(
Expand All @@ -49,6 +49,6 @@ public void execute(HttpRequest req, HttpResponse resp) {
"message", status.hasCapacity() ? "Ready" : "No free slots available",
"node", status));

resp.setContent(utf8String(json.toJson(report)));
return new HttpResponse().setContent(utf8String(json.toJson(report)));
}
}

0 comments on commit d062f62

Please sign in to comment.