Skip to content

Commit

Permalink
Initial rework of the existing DriverServlet
Browse files Browse the repository at this point in the history
This introduces the scaffolding for later work, but
does not introduce any new functionality.
  • Loading branch information
shs96c committed May 20, 2017
1 parent 8ae565d commit ea3fa47
Show file tree
Hide file tree
Showing 13 changed files with 863 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.openqa.selenium.remote.server;

import org.openqa.selenium.remote.SessionId;

import java.util.Map;

interface ActiveSession extends CommandHandler {

/**
* Used to provide logging information and thread names.
*/
String getDescription();

SessionId getId();

/**
* Desribe the current webdriver session's capabilities.
*/
Map<String, Object> getCapabilities();

void stop();
}
86 changes: 86 additions & 0 deletions java/server/src/org/openqa/selenium/remote/server/AllHandlers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.openqa.selenium.remote.server;

import static com.google.common.net.MediaType.JAVASCRIPT_UTF_8;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openqa.selenium.remote.ErrorCodes.UNKNOWN_COMMAND;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.cache.Cache;
import com.google.common.collect.ImmutableMap;
import com.google.gson.GsonBuilder;

import org.openqa.selenium.remote.SessionId;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


class AllHandlers {

private final Cache<SessionId, ActiveSession> allSessions;
private final DriverSessions legacySessions;

public AllHandlers(Cache<SessionId, ActiveSession> allSessions, DriverSessions legacySessions) {
this.allSessions = allSessions;
this.legacySessions = legacySessions;
}

public CommandHandler match(HttpServletRequest req) {
String path = Strings.isNullOrEmpty(req.getPathInfo()) ? "/" : req.getPathInfo();

// All commands that take a session id expect that as the path fragment immediately after "/session".
SessionId id = null;
List<String> fragments = Splitter.on('/').limit(4).splitToList(path);
if (fragments.size() > 2) {
if ("session".equals(fragments.get(1))) {
id = new SessionId(fragments.get(2));
}
}

if (id != null) {
ActiveSession session = allSessions.getIfPresent(id);
if (session != null) {
return session;
}
}

if ("POST".equalsIgnoreCase(req.getMethod()) && "/session".equals(path)) {
return new BeginSession(allSessions, legacySessions);
}

return new NoHandler();
}

private static class NoHandler implements CommandHandler {

@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.reset();

byte[] payload = new GsonBuilder().serializeNulls().create().toJson(ImmutableMap.of(
"sessionId", null,
"status", UNKNOWN_COMMAND,
"value", ImmutableMap.of(
"error", "unknown command",
"message", String.format(
"Unable to find command matching %s to %s",
req.getMethod(),
req.getPathInfo()),
"stacktrace", ""))).getBytes(UTF_8);

resp.setStatus(HTTP_NOT_FOUND);
resp.setContentType(JAVASCRIPT_UTF_8.toString());
resp.setContentLengthLong(payload.length);

try (OutputStream out = resp.getOutputStream()) {
out.write(payload);
}
}
}
}
24 changes: 24 additions & 0 deletions java/server/src/org/openqa/selenium/remote/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ java_library(name = 'server',
],
)

java_library(
name = 'webdriver-servlet',
srcs = [
'ActiveSession.java',
'BeginSession.java',
'AllHandlers.java',
'CommandHandler.java',
'ExceptionHandler.java',
'InMemorySession.java',
'WebDriverServlet.java',
],
provided_deps = [
'//third_party/java/servlet:servlet-api',
],
deps = [
':server',
':sessions',
'//java/client/src/org/openqa/selenium/remote:remote',
'//third_party/java/gson:gson',
'//third_party/java/guava:guava',
]
)

export_file(name = 'client',
src = '//javascript/remote:client',
out = 'client.js',
Expand All @@ -82,6 +105,7 @@ java_library(name = 'standalone-server-lib',
deps = [
':server',
':sessions',
':webdriver-servlet',
'//java/client/src/org/openqa/selenium/chrome:chrome',
'//java/client/src/org/openqa/selenium/edge:edge',
'//java/client/src/org/openqa/selenium/firefox:firefox',
Expand Down
Loading

0 comments on commit ea3fa47

Please sign in to comment.