Skip to content

Commit

Permalink
Make Routable also be an HttpHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 4, 2019
1 parent 644776d commit 6be6012
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 36 deletions.
21 changes: 21 additions & 0 deletions java/client/src/org/openqa/selenium/remote/http/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.remote.http;

import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.function.Function;

/**
Expand All @@ -42,10 +44,29 @@
public interface Filter extends Function<HttpHandler, HttpHandler> {

default Filter andThen(Filter next) {
Objects.requireNonNull(next, "Next filter must be set.");

return req -> apply(next.apply(req));
}

default HttpHandler andFinally(HttpHandler end) {
Objects.requireNonNull(end, "HTTP handler must be set.");

return request -> Filter.this.apply(end).execute(request);
}

default Routable andFinally(Routable end) {
return new Routable() {

@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
return Filter.this.apply(end).execute(req);
}

@Override
public boolean matches(HttpRequest req) {
return end.matches(req);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.openqa.selenium.remote.http;

public interface Routable {
public interface Routable extends HttpHandler {

boolean matches(HttpRequest req);

Expand Down
12 changes: 6 additions & 6 deletions java/client/src/org/openqa/selenium/remote/http/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public static NestedRouteConfig prefix(String prefix) {
return new NestedRouteConfig(prefix);
}

public static <T extends Routable & HttpHandler> Route combine(T first, T... others) {
public static Route combine(Routable first, Routable... others) {
Objects.requireNonNull(first, "At least one route must be set.");
return new CombinedRoute(Stream.concat(Stream.of(first), Stream.of(others)));
}

public static <T extends Routable & HttpHandler> Route combine(Iterable<T> routes) {
public static Route combine(Iterable<Routable> routes) {
Objects.requireNonNull(routes, "At least one route must be set.");

return new CombinedRoute(StreamSupport.stream(routes.spliterator(), false));
Expand Down Expand Up @@ -271,11 +271,11 @@ private HttpRequest transform(HttpRequest request) {
}
}

private static class CombinedRoute<T extends Routable & HttpHandler> extends Route {
private static class CombinedRoute extends Route {

private final List<T> allRoutes;
private final List<Routable> allRoutes;

public CombinedRoute(Stream<T> routes) {
private CombinedRoute(Stream<Routable> routes) {
// We want later routes to have a greater chance of being called so that we can override
// routes as necessary.
allRoutes = routes.collect(ImmutableList.toImmutableList()).reverse();
Expand Down Expand Up @@ -318,7 +318,7 @@ private static class PredicatedRoute extends Route {
private final Predicate<HttpRequest> predicate;
private final Supplier<HttpHandler> supplier;

public PredicatedRoute(Predicate<HttpRequest> predicate, Supplier<HttpHandler> supplier) {
private PredicatedRoute(Predicate<HttpRequest> predicate, Supplier<HttpHandler> supplier) {
this.predicate = Objects.requireNonNull(predicate);
this.supplier = Objects.requireNonNull(supplier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CombinedHandler implements Predicate<HttpRequest>, Routable, HttpHa

private final Map<Routable, HttpHandler> handlers = new HashMap<>();

public <X extends Routable & HttpHandler> void addHandler(X handler) {
public void addHandler(Routable handler) {
Objects.requireNonNull(handler);
handlers.put(handler, handler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setUp() throws MalformedURLException {
local = new LocalDistributor(tracer, bus, HttpClient.Factory.createDefault(), sessions);
distributor = new RemoteDistributor(
tracer,
new PassthroughHttpClient.Factory<>(local),
new PassthroughHttpClient.Factory(local),
new URL("http://does.not.exist/"));

caps = new ImmutableCapabilities("browserName", "cheese");
Expand All @@ -117,7 +117,7 @@ public void shouldBeAbleToAddANodeAndCreateASession() throws URISyntaxException
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(node),
new PassthroughHttpClient.Factory(node),
sessions);
distributor.add(node);

Expand All @@ -144,7 +144,7 @@ public void creatingASessionAddsItToTheSessionMap() throws URISyntaxException {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(node),
new PassthroughHttpClient.Factory(node),
sessions);
distributor.add(node);

Expand Down Expand Up @@ -172,11 +172,11 @@ public void shouldBeAbleToRemoveANode() throws URISyntaxException, MalformedURLE
Distributor local = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(node),
new PassthroughHttpClient.Factory(node),
sessions);
distributor = new RemoteDistributor(
tracer,
new PassthroughHttpClient.Factory<>(local),
new PassthroughHttpClient.Factory(local),
new URL("http://does.not.exist"));
distributor.add(node);
distributor.remove(node.getId());
Expand Down Expand Up @@ -226,7 +226,7 @@ public void theMostLightlyLoadedNodeIsSelectedFirst() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions)
.add(heavy)
.add(medium)
Expand All @@ -252,7 +252,7 @@ public void shouldUseLastSessionCreatedTimeAsTieBreaker() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions)
.add(leastRecent);
try (NewSessionPayload payload = NewSessionPayload.create(caps)) {
Expand Down Expand Up @@ -319,7 +319,7 @@ public void shouldIncludeHostsThatAreUpInHostList() {
LocalDistributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);
distributor.add(alwaysDown);
Expand All @@ -344,7 +344,7 @@ public void shouldNotScheduleAJobIfAllSlotsAreBeingUsed() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);

Expand Down Expand Up @@ -372,7 +372,7 @@ public void shouldReleaseSlotOnceSessionEnds() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);

Expand Down Expand Up @@ -420,7 +420,7 @@ public void shouldNotStartASessionIfTheCapabilitiesAreNotSupported() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);

Expand Down Expand Up @@ -453,7 +453,7 @@ public void attemptingToStartASessionWhichFailsMarksAsTheSlotAsAvailable() {
Distributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);
distributor.add(node);
Expand Down Expand Up @@ -486,7 +486,7 @@ public void shouldReturnNodesThatWereDownToPoolOfNodesOnceTheyMarkTheirHealthChe
LocalDistributor distributor = new LocalDistributor(
tracer,
bus,
new PassthroughHttpClient.Factory<>(handler),
new PassthroughHttpClient.Factory(handler),
sessions);
handler.addHandler(distributor);
distributor.add(node);
Expand Down
6 changes: 3 additions & 3 deletions java/server/test/org/openqa/selenium/grid/node/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {

node = new RemoteNode(
tracer,
new PassthroughHttpClient.Factory<>(local),
new PassthroughHttpClient.Factory(local),
UUID.randomUUID(),
uri,
ImmutableSet.of(caps));
Expand All @@ -115,7 +115,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
@Test
public void shouldRefuseToCreateASessionIfNoFactoriesAttached() {
Node local = LocalNode.builder(tracer, bus, clientFactory, uri).build();
HttpClient.Factory clientFactory = new PassthroughHttpClient.Factory<>(local);
HttpClient.Factory clientFactory = new PassthroughHttpClient.Factory(local);
Node node = new RemoteNode(tracer, clientFactory, UUID.randomUUID(), uri, ImmutableSet.of());

Optional<Session> session = node.newSession(createSessionRequest(caps))
Expand Down Expand Up @@ -232,7 +232,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
.build();
Node remote = new RemoteNode(
tracer,
new PassthroughHttpClient.Factory<>(local),
new PassthroughHttpClient.Factory(local),
UUID.randomUUID(),
uri,
ImmutableSet.of(caps));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void setUp() {
bus = new GuavaEventBus();

handler = new CombinedHandler();
clientFactory = new PassthroughHttpClient.Factory<>(handler);
clientFactory = new PassthroughHttpClient.Factory(handler);

sessions = new LocalSessionMap(tracer, bus);
handler.addHandler(sessions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setUp() throws URISyntaxException {
local = new LocalSessionMap(
DistributedTracer.builder().build(),
bus);
client = new PassthroughHttpClient<>(local);
client = new PassthroughHttpClient(local);
remote = new RemoteSessionMap(client);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.grid.testing;

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;
Expand All @@ -28,12 +27,11 @@
import java.io.UncheckedIOException;
import java.net.URL;

public class PassthroughHttpClient<T extends Routable & HttpHandler>
implements HttpClient {
public class PassthroughHttpClient implements HttpClient {

private final T handler;
private final Routable handler;

public PassthroughHttpClient(T handler) {
public PassthroughHttpClient(Routable handler) {
this.handler = handler;
}

Expand All @@ -51,12 +49,11 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
throw new UnsupportedOperationException("openSocket");
}

public static class Factory<T extends Routable & HttpHandler>
implements HttpClient.Factory {
public static class Factory implements HttpClient.Factory {

private final T handler;
private final Routable handler;

public Factory(T handler) {
public Factory(Routable handler) {
this.handler = handler;
}

Expand All @@ -65,7 +62,7 @@ public Builder builder() {
return new Builder() {
@Override
public HttpClient createClient(URL url) {
return new PassthroughHttpClient<>(handler);
return new PassthroughHttpClient(handler);
}
};
}
Expand Down

0 comments on commit 6be6012

Please sign in to comment.