25
25
import com .google .common .collect .ImmutableMap ;
26
26
27
27
import org .openqa .selenium .NoSuchSessionException ;
28
+ import org .openqa .selenium .SessionNotCreatedException ;
28
29
import org .openqa .selenium .UnsupportedCommandException ;
29
30
import org .openqa .selenium .WebDriverException ;
30
31
import org .openqa .selenium .logging .LocalLogs ;
35
36
import org .openqa .selenium .remote .http .HttpClient ;
36
37
import org .openqa .selenium .remote .http .HttpRequest ;
37
38
import org .openqa .selenium .remote .http .HttpResponse ;
38
- import org .openqa .selenium .remote .http .JsonHttpCommandCodec ;
39
- import org .openqa .selenium .remote .http .JsonHttpResponseCodec ;
40
39
import org .openqa .selenium .remote .internal .ApacheHttpClient ;
41
40
42
41
import java .io .IOException ;
@@ -50,8 +49,9 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs {
50
49
51
50
private final URL remoteServer ;
52
51
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 ;
55
55
56
56
private LocalLogs logs = LocalLogs .getNullLogger ();
57
57
@@ -83,13 +83,8 @@ public HttpCommandExecutor(
83
83
throw new WebDriverException (e );
84
84
}
85
85
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 );
93
88
}
94
89
95
90
private static synchronized HttpClient .Factory getDefaultClientFactory () {
@@ -137,6 +132,28 @@ public Response execute(Command command) throws IOException {
137
132
}
138
133
}
139
134
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
+
140
157
HttpRequest httpRequest = commandCodec .encode (command );
141
158
try {
142
159
log (LogType .PROFILER , new HttpProfilerLogEntry (command .getName (), true ));
0 commit comments