Skip to content

Commit

Permalink
Migrate the Node to use the new routes
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Nov 7, 2018
1 parent 0dd0327 commit 1dcd1b9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,18 @@
package org.openqa.selenium.grid.node;

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.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.util.Objects;
import java.util.function.Predicate;

class ForwardWebDriverCommand implements Predicate<HttpRequest>, CommandHandler {
class ForwardWebDriverCommand implements CommandHandler {

private final Node node;
private final Json json;

public ForwardWebDriverCommand(Node node, Json json) {
public ForwardWebDriverCommand(Node node) {
this.node = Objects.requireNonNull(node);
this.json = Objects.requireNonNull(json);
}

@Override
public boolean test(HttpRequest req) {
if (!req.getUri().startsWith("/session/")) {
return false;
}

String[] split = req.getUri().split("/", 4);
SessionId id = new SessionId(split[2]);

return node.isSessionOwner(id);
}

@Override
Expand Down
22 changes: 4 additions & 18 deletions java/server/src/org/openqa/selenium/grid/node/GetNodeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,33 @@
package org.openqa.selenium.grid.node;

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;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.util.Objects;
import java.util.function.Predicate;

class GetNodeSession implements Predicate<HttpRequest>, CommandHandler {
class GetNodeSession implements CommandHandler {

public static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/node/session/{sessionId}");
private final Node node;
private final Json json;
private final SessionId id;

GetNodeSession(Node node, Json json) {
GetNodeSession(Node node, Json json, SessionId id) {
this.node = Objects.requireNonNull(node);
this.json = Objects.requireNonNull(json);
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == GET && TEMPLATE.match(req.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 = node.getSession(id);

resp.setContent(json.toJson(ImmutableMap.of("value", session)).getBytes(UTF_8));
Expand Down
22 changes: 4 additions & 18 deletions java/server/src/org/openqa/selenium/grid/node/IsSessionOwner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,32 @@
package org.openqa.selenium.grid.node;

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.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.io.IOException;
import java.util.Objects;
import java.util.function.Predicate;

class IsSessionOwner implements Predicate<HttpRequest>, CommandHandler {

public static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/node/owner/{sessionId}");
class IsSessionOwner implements CommandHandler {

private final Node node;
private final Json json;
private final SessionId id;

public IsSessionOwner(Node node, Json json) {
public IsSessionOwner(Node node, Json json, SessionId id) {
this.node = Objects.requireNonNull(node);
this.json = Objects.requireNonNull(json);
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == GET && TEMPLATE.match(req.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) {
resp.setContent(json.toJson(ImmutableMap.of("value", false)).getBytes(UTF_8));
}

SessionId id = new SessionId(match.getParameters().get("sessionId"));
resp.setContent(json.toJson(
ImmutableMap.of("value", node.isSessionOwner(id))).getBytes(UTF_8));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.grid.node;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.data.Session;
Expand All @@ -31,9 +30,8 @@
import java.util.HashMap;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

class NewNodeSession implements Predicate<HttpRequest>, CommandHandler {
class NewNodeSession implements CommandHandler {

private final BiConsumer<HttpResponse, Object> encodeJson;
private final Node node;
Expand All @@ -48,11 +46,6 @@ class NewNodeSession implements Predicate<HttpRequest>, CommandHandler {
};
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == POST && "/se/grid/node/session".equals(req.getUri());
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Capabilities caps = json.toType(req.getContentString(), Capabilities.class);
Expand Down
68 changes: 40 additions & 28 deletions java/server/src/org/openqa/selenium/grid/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

package org.openqa.selenium.grid.node;

import static org.openqa.selenium.grid.server.Server.get;

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.get;
import static org.openqa.selenium.grid.web.Routes.matching;
import static org.openqa.selenium.grid.web.Routes.post;

import org.openqa.selenium.Capabilities;
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;
Expand All @@ -36,7 +39,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -88,33 +90,39 @@
*/
public abstract class Node implements Predicate<HttpRequest>, CommandHandler {

private final CompoundHandler handler;
private final UUID id;
private final Injector injector;
private final Routes routes;

protected Node(UUID id) {
this.id = Objects.requireNonNull(id);

Json json = new Json();

NewNodeSession create = new NewNodeSession(this, json);
IsSessionOwner owner = new IsSessionOwner(this, json);
ForwardWebDriverCommand execute = new ForwardWebDriverCommand(this, json);
GetNodeSession read = new GetNodeSession(this, json);
StopNodeSession destroy = new StopNodeSession(this);

handler = new CompoundHandler(
Injector.builder()
.register(this)
.register(new Json())
.build(),
ImmutableMap.<Predicate<HttpRequest>, BiFunction<Injector, HttpRequest, CommandHandler>>builder()
.put(create, (inj, req) -> create)
.put(owner, (inj, req) -> owner)
.put(execute, (inj, req) -> execute)
.put(read, (inj, req) -> read)
.put(destroy, (inj, req) -> destroy)
.put(get("/status"), (inj, req) -> inj.newInstance(StatusHandler.class))
.build());
injector = Injector.builder()
.register(this)
.register(json)
.build();

routes = combine(
get("/se/grid/node/owner/{sessionId}").using(IsSessionOwner.class)
.map("sessionId", SessionId::new),
delete("/se/grid/node/session/{sessionId}").using(StopNodeSession.class)
.map("sessionId", SessionId::new),
get("/se/grid/node/session/{sessionId}").using(GetNodeSession.class)
.map("sessionId", SessionId::new),
post("/se/grid/node/session").using(NewNodeSession.class),
get("/status").using(StatusHandler.class),
matching(req -> {
if (!req.getUri().startsWith("/session/")) {
return false;
}

String[] split = req.getUri().split("/", 4);
SessionId sessionId = new SessionId(split[2]);

return isSessionOwner(sessionId);
}).using(ForwardWebDriverCommand.class)
).build();
}

public UUID getId() {
Expand All @@ -137,11 +145,15 @@ public UUID getId() {

@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<CommandHandler> handler = routes.match(injector, req);
if (!handler.isPresent()) {
throw new HandlerNotFoundException(req);
}
handler.get().execute(req, resp);
}
}
24 changes: 4 additions & 20 deletions java/server/src/org/openqa/selenium/grid/node/StopNodeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,25 @@

package org.openqa.selenium.grid.node;

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.remote.SessionId;
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.function.Predicate;

class StopNodeSession implements Predicate<HttpRequest>, CommandHandler {

public static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/node/session/{sessionId}");
class StopNodeSession implements CommandHandler {
private final Node node;
private final SessionId id;

StopNodeSession(Node node) {
StopNodeSession(Node node, SessionId id) {
this.node = Objects.requireNonNull(node);
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == DELETE && TEMPLATE.match(req.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"));
node.stop(id);
}
}

0 comments on commit 1dcd1b9

Please sign in to comment.