Skip to content

Commit d725150

Browse files
committed
Use the ProtocolHandshake to determine remote end protocol version
At the moment, all protocols are modelled using the Json*Codecs. A follow up diff will clean this up.
1 parent 1eb94ab commit d725150

File tree

6 files changed

+606
-13
lines changed

6 files changed

+606
-13
lines changed

java/client/src/org/openqa/selenium/remote/BUCK

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ java_library(name = 'remote-lib',
103103
'http/HttpResponse.java',
104104
'http/JsonHttpCommandCodec.java',
105105
'http/JsonHttpResponseCodec.java',
106+
'http/W3CHttpCommandCodec.java',
107+
'http/W3CHttpResponseCodec.java',
106108
'internal/ApacheHttpClient.java',
107109
'internal/HttpClientFactory.java',
108110
'internal/JsonToWebElementConverter.java',

java/client/src/org/openqa/selenium/remote/CommandCodec.java

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.openqa.selenium.UnsupportedCommandException;
2121
import org.openqa.selenium.remote.Command;
22+
import org.openqa.selenium.remote.http.HttpMethod;
2223

2324
/**
2425
* Converts {@link Command} objects to and from another representation.
@@ -44,4 +45,9 @@ public interface CommandCodec<T> {
4445
* @throws UnsupportedCommandException If the command is not supported by this codec.
4546
*/
4647
Command decode(T encodedCommand);
48+
49+
/**
50+
* Enhance this command codec with additional commands.
51+
*/
52+
void defineCommand(String name, HttpMethod method, String pathPattern);
4753
}

java/client/src/org/openqa/selenium/remote/Dialect.java

+32-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,37 @@
1717

1818
package org.openqa.selenium.remote;
1919

20+
import org.openqa.selenium.remote.http.HttpRequest;
21+
import org.openqa.selenium.remote.http.HttpResponse;
22+
import org.openqa.selenium.remote.http.JsonHttpCommandCodec;
23+
import org.openqa.selenium.remote.http.JsonHttpResponseCodec;
24+
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
25+
import org.openqa.selenium.remote.http.W3CHttpResponseCodec;
26+
2027
public enum Dialect {
21-
OSS,
22-
W3C;
28+
OSS {
29+
@Override
30+
public CommandCodec<HttpRequest> getCommandCodec() {
31+
return new JsonHttpCommandCodec();
32+
}
33+
34+
@Override
35+
public ResponseCodec<HttpResponse> getResponseCodec() {
36+
return new JsonHttpResponseCodec();
37+
}
38+
},
39+
W3C {
40+
@Override
41+
public CommandCodec<HttpRequest> getCommandCodec() {
42+
return new W3CHttpCommandCodec();
43+
}
44+
45+
@Override
46+
public ResponseCodec<HttpResponse> getResponseCodec() {
47+
return new W3CHttpResponseCodec();
48+
}
49+
};
50+
51+
public abstract CommandCodec<HttpRequest> getCommandCodec();
52+
public abstract ResponseCodec<HttpResponse> getResponseCodec();
2353
}

java/client/src/org/openqa/selenium/remote/HttpCommandExecutor.java

+28-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.common.collect.ImmutableMap;
2626

2727
import org.openqa.selenium.NoSuchSessionException;
28+
import org.openqa.selenium.SessionNotCreatedException;
2829
import org.openqa.selenium.UnsupportedCommandException;
2930
import org.openqa.selenium.WebDriverException;
3031
import org.openqa.selenium.logging.LocalLogs;
@@ -35,8 +36,6 @@
3536
import org.openqa.selenium.remote.http.HttpClient;
3637
import org.openqa.selenium.remote.http.HttpRequest;
3738
import org.openqa.selenium.remote.http.HttpResponse;
38-
import org.openqa.selenium.remote.http.JsonHttpCommandCodec;
39-
import org.openqa.selenium.remote.http.JsonHttpResponseCodec;
4039
import org.openqa.selenium.remote.internal.ApacheHttpClient;
4140

4241
import java.io.IOException;
@@ -50,8 +49,9 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs {
5049

5150
private final URL remoteServer;
5251
private final HttpClient client;
53-
private final JsonHttpCommandCodec commandCodec;
54-
private final JsonHttpResponseCodec responseCodec;
52+
private final Map<String, CommandInfo> additionalCommands;
53+
private CommandCodec<HttpRequest> commandCodec;
54+
private ResponseCodec<HttpResponse> responseCodec;
5555

5656
private LocalLogs logs = LocalLogs.getNullLogger();
5757

@@ -83,13 +83,8 @@ public HttpCommandExecutor(
8383
throw new WebDriverException(e);
8484
}
8585

86-
commandCodec = new JsonHttpCommandCodec();
87-
responseCodec = new JsonHttpResponseCodec();
88-
client = httpClientFactory.createClient(remoteServer);
89-
90-
for (Map.Entry<String, CommandInfo> entry : additionalCommands.entrySet()) {
91-
defineCommand(entry.getKey(), entry.getValue());
92-
}
86+
this.additionalCommands = additionalCommands;
87+
this.client = httpClientFactory.createClient(remoteServer);
9388
}
9489

9590
private static synchronized HttpClient.Factory getDefaultClientFactory() {
@@ -137,6 +132,28 @@ public Response execute(Command command) throws IOException {
137132
}
138133
}
139134

135+
if (NEW_SESSION.equals(command.getName())) {
136+
if (commandCodec != null) {
137+
throw new SessionNotCreatedException("Session already exists");
138+
}
139+
ProtocolHandshake handshake = new ProtocolHandshake();
140+
log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));
141+
ProtocolHandshake.Result result = handshake.createSession(client, command);
142+
Dialect dialect = result.getDialect();
143+
commandCodec = dialect.getCommandCodec();
144+
for (Map.Entry<String, CommandInfo> entry : additionalCommands.entrySet()) {
145+
defineCommand(entry.getKey(), entry.getValue());
146+
}
147+
responseCodec = dialect.getResponseCodec();
148+
log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), false));
149+
return result.createResponse();
150+
}
151+
152+
if (commandCodec == null || responseCodec == null) {
153+
throw new WebDriverException(
154+
"No command or response codec has been defined. Unable to proceed");
155+
}
156+
140157
HttpRequest httpRequest = commandCodec.encode(command);
141158
try {
142159
log(LogType.PROFILER, new HttpProfilerLogEntry(command.getName(), true));

0 commit comments

Comments
 (0)