Skip to content

Commit

Permalink
Move the session map 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 eda8bb8 commit cd4450a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,19 @@
package org.openqa.selenium.grid.sessionmap;

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

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

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

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

private static final UrlTemplate TEMPLATE = new UrlTemplate("/se/grid/session");
private final Json json;
private final SessionMap sessions;

Expand All @@ -44,17 +39,8 @@ class AddToSessionMap implements Predicate<HttpRequest>, CommandHandler {
this.sessions = Objects.requireNonNull(sessions);
}

@Override
public boolean test(HttpRequest req) {
return req.getMethod() == POST && TEMPLATE.match(req.getUri()) != null;
}

@Override
public void execute(HttpRequest req, HttpResponse resp) {
if (!test(req)) {
throw new NoSuchSessionException("Session ID not found in URL: " + req.getUri());
}

Session session = json.toType(req.getContentString(), Session.class);
Objects.requireNonNull(session, "Session to add must be set");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,33 @@
package org.openqa.selenium.grid.sessionmap;

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 GetFromSessionMap implements Predicate<HttpRequest>, CommandHandler {
class GetFromSessionMap implements CommandHandler {

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

public GetFromSessionMap(Json json, SessionMap sessions) {
public GetFromSessionMap(Json json, SessionMap sessions, SessionId id) {
this.json = Objects.requireNonNull(json);
this.sessions = Objects.requireNonNull(sessions);
}

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

resp.setContent(json.toJson(ImmutableMap.of("value", session)).getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,25 @@

package org.openqa.selenium.grid.sessionmap;

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

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

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

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

public RemoveFromSession(Json json, SessionMap sessions) {
this.json = Objects.requireNonNull(json);
public RemoveFromSession(SessionMap sessions, SessionId id) {
this.sessions = Objects.requireNonNull(sessions);
}

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

@Override
public void execute(HttpRequest req, HttpResponse resp) {
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"));

sessions.remove(id);
}
}
39 changes: 25 additions & 14 deletions java/server/src/org/openqa/selenium/grid/sessionmap/SessionMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

package org.openqa.selenium.grid.sessionmap;

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.post;

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 @@ -31,6 +34,7 @@

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

/**
Expand Down Expand Up @@ -67,7 +71,8 @@
*/
public abstract class SessionMap implements Predicate<HttpRequest>, CommandHandler {

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

public abstract boolean add(Session session);

Expand All @@ -78,25 +83,31 @@ public abstract class SessionMap implements Predicate<HttpRequest>, CommandHandl
public SessionMap() {
Json json = new Json();

AddToSessionMap add = new AddToSessionMap(json, this);
GetFromSessionMap get = new GetFromSessionMap(json, this);
RemoveFromSession remove = new RemoveFromSession(json, this);
injector = Injector.builder()
.register(this)
.register(new Json())
.build();

handler = new CompoundHandler(
Injector.builder().build(),
ImmutableMap.of(
get, (inj, req) -> get, // List "get" first because it's most commonly called
add, (inj, req) -> add,
remove, (inj, req) -> remove));
routes = combine(
post("/se/grid/session").using(AddToSessionMap.class),
Routes.get("/se/grid/session/{sessionId}").using(GetFromSessionMap.class)
.map("sessionId", SessionId::new),
delete("/se/grid/session/{sessionId}").using(RemoveFromSession.class)
.map("sessionId", SessionId::new))
.build();
}

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

0 comments on commit cd4450a

Please sign in to comment.