Skip to content

Commit

Permalink
Move the Distributor to the new routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Stewart committed Nov 7, 2018
1 parent 1dcd1b9 commit eda8bb8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.grid.distributor;

import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
Expand All @@ -39,10 +38,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class AddNode implements Predicate<HttpRequest>, CommandHandler {
public class AddNode implements CommandHandler {

private final Distributor distributor;
private final Json json;
Expand All @@ -54,11 +52,6 @@ public AddNode(Distributor distributor, Json json, HttpClient.Factory httpFactor
this.httpFactory = Objects.requireNonNull(httpFactory);
}

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

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
Map<String, Object> raw = json.toType(req.getContentString(), MAP_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.grid.distributor;

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

import com.google.common.collect.ImmutableMap;

Expand All @@ -33,9 +32,8 @@
import java.io.Reader;
import java.io.StringReader;
import java.util.Objects;
import java.util.function.Predicate;

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

private final Distributor distributor;
private final Json json;
Expand All @@ -45,11 +43,6 @@ public CreateSession(Distributor distributor, Json json) {
this.json = Objects.requireNonNull(json);
}

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

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
try (Reader reader = new StringReader(req.getContentString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

package org.openqa.selenium.grid.distributor;

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

import com.google.common.collect.ImmutableMap;
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.post;

import org.openqa.selenium.SessionNotCreatedException;
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.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.NewSessionPayload;
Expand All @@ -34,8 +35,8 @@
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -71,23 +72,22 @@
*/
public abstract class Distributor implements Predicate<HttpRequest>, CommandHandler {

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

protected Distributor() {
Json json = new Json();

CreateSession create = new CreateSession(this, json);
AddNode addNode = new AddNode(this, json, HttpClient.Factory.createDefault());
RemoveNode removeNode = new RemoveNode(this);

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

routes = Routes.combine(
post("/session").using(CreateSession.class),
post("/se/grid/distributor/node").using(AddNode.class),
delete("/se/grid/distributor/node/{nodeId}").using(RemoveNode.class).map("nodeId", UUID::fromString),
get("/status").using(StatusHandler.class)
).build();
}

public abstract Session newSession(NewSessionPayload payload) throws SessionNotCreatedException;
Expand All @@ -100,11 +100,15 @@ protected Distributor() {

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,26 @@

package org.openqa.selenium.grid.distributor;

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

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.UrlTemplate;
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.UUID;
import java.util.function.Predicate;

class RemoveNode implements Predicate<HttpRequest>, CommandHandler {

public static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/distributor/node/{nodeId}");
class RemoveNode implements CommandHandler {

private final Distributor distributor;
private final UUID nodeId;

public RemoveNode(Distributor distributor) {
public RemoveNode(Distributor distributor, UUID nodeId) {
this.distributor = Objects.requireNonNull(distributor);
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == DELETE && TEMPLATE.match(req.getUri()) != null;
this.nodeId = Objects.requireNonNull(nodeId);
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
UrlTemplate.Match match = TEMPLATE.match(req.getUri());
if (match == null || match.getParameters().get("nodeId") == null) {
throw new WebDriverException("Node ID not found in URL: " + req.getUri());
}

UUID nodeId = UUID.fromString(match.getParameters().get("nodeId"));
distributor.remove(nodeId);
}
}

0 comments on commit eda8bb8

Please sign in to comment.