forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial rework of the existing DriverServlet
This introduces the scaffolding for later work, but does not introduce any new functionality.
- Loading branch information
Showing
13 changed files
with
863 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
java/server/src/org/openqa/selenium/remote/server/ActiveSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
java/server/src/org/openqa/selenium/remote/server/AllHandlers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.