22
22
23
23
import org .openqa .selenium .UnsupportedCommandException ;
24
24
import org .openqa .selenium .remote .Command ;
25
+ import org .openqa .selenium .remote .CommandCodec ;
25
26
import org .openqa .selenium .remote .ErrorCodes ;
26
27
import org .openqa .selenium .remote .Response ;
28
+ import org .openqa .selenium .remote .ResponseCodec ;
27
29
import org .openqa .selenium .remote .http .HttpRequest ;
28
30
import org .openqa .selenium .remote .http .HttpResponse ;
29
31
import org .openqa .selenium .remote .http .JsonHttpCommandCodec ;
30
32
import org .openqa .selenium .remote .http .JsonHttpResponseCodec ;
33
+ import org .openqa .selenium .remote .http .W3CHttpCommandCodec ;
31
34
import org .openqa .selenium .remote .server .handler .AcceptAlert ;
32
35
import org .openqa .selenium .remote .server .handler .AddConfig ;
33
36
import org .openqa .selenium .remote .server .handler .AddCookie ;
136
139
import org .openqa .selenium .remote .server .rest .ResultConfig ;
137
140
138
141
import java .util .LinkedHashMap ;
142
+ import java .util .LinkedHashSet ;
139
143
import java .util .Map ;
144
+ import java .util .Set ;
140
145
import java .util .logging .Logger ;
141
146
142
147
public class JsonHttpCommandHandler {
@@ -145,15 +150,17 @@ public class JsonHttpCommandHandler {
145
150
146
151
private final DriverSessions sessions ;
147
152
private final Logger log ;
148
- private final JsonHttpCommandCodec commandCodec ;
149
- private final JsonHttpResponseCodec responseCodec ;
153
+ private final Set < CommandCodec < HttpRequest >> commandCodecs ;
154
+ private final ResponseCodec < HttpResponse > responseCodec ;
150
155
private final Map <String , ResultConfig > configs = new LinkedHashMap <>();
151
156
private final ErrorCodes errorCodes = new ErrorCodes ();
152
157
153
158
public JsonHttpCommandHandler (DriverSessions sessions , Logger log ) {
154
159
this .sessions = sessions ;
155
160
this .log = log ;
156
- this .commandCodec = new JsonHttpCommandCodec ();
161
+ this .commandCodecs = new LinkedHashSet <>();
162
+ this .commandCodecs .add (new JsonHttpCommandCodec ());
163
+ this .commandCodecs .add (new W3CHttpCommandCodec ());
157
164
this .responseCodec = new JsonHttpResponseCodec ();
158
165
setUpMappings ();
159
166
}
@@ -170,7 +177,7 @@ public HttpResponse handleRequest(HttpRequest request) {
170
177
Command command = null ;
171
178
Response response ;
172
179
try {
173
- command = commandCodec . decode (request );
180
+ command = decode (request );
174
181
ResultConfig config = configs .get (command .getName ());
175
182
if (config == null ) {
176
183
throw new UnsupportedCommandException ();
@@ -191,8 +198,26 @@ public HttpResponse handleRequest(HttpRequest request) {
191
198
return responseCodec .encode (response );
192
199
}
193
200
201
+ private Command decode (HttpRequest request ) {
202
+ UnsupportedCommandException lastException = null ;
203
+ for (CommandCodec <HttpRequest > codec : commandCodecs ) {
204
+ try {
205
+ return codec .decode (request );
206
+ } catch (UnsupportedCommandException e ) {
207
+ lastException = e ;
208
+ }
209
+ }
210
+ if (lastException != null ) {
211
+ throw lastException ;
212
+ }
213
+ throw new UnsupportedOperationException ("Cannot find command for: " + request .getUri ());
214
+ }
215
+
194
216
private void setUpMappings () {
195
- commandCodec .defineCommand (ADD_CONFIG_COMMAND_NAME , POST , "/config/drivers" );
217
+ for (CommandCodec <HttpRequest > codec : commandCodecs ) {
218
+ codec .defineCommand (ADD_CONFIG_COMMAND_NAME , POST , "/config/drivers" );
219
+ }
220
+
196
221
addNewMapping (ADD_CONFIG_COMMAND_NAME , AddConfig .class );
197
222
198
223
addNewMapping (STATUS , Status .class );
@@ -328,17 +353,8 @@ private void setUpMappings() {
328
353
addNewMapping (SET_NETWORK_CONNECTION , SetNetworkConnection .class );
329
354
330
355
// Deprecated end points. Will be removed.
331
- addNewMapping ("getCurrentWindowHandleW3C" , GetCurrentWindowHandle .class );
332
- addNewMapping ("getWindowHandlesW3C" , GetAllWindowHandles .class );
333
-
334
- addNewMapping ("dimissAlertW3C" , DismissAlert .class );
335
- addNewMapping ("acceptAlertW3C" , AcceptAlert .class );
336
- addNewMapping ("getAlertTextW3C" , GetAlertText .class );
337
- addNewMapping ("setAlertValueW3C" , SetAlertText .class );
338
- addNewMapping ("executeScriptW3C" , ExecuteScript .class );
339
- addNewMapping ("executeAsyncScriptW3C" , ExecuteAsyncScript .class );
340
356
addNewMapping ("getWindowSize" , GetWindowSize .class );
341
357
addNewMapping ("setWindowSize" , SetWindowSize .class );
342
358
addNewMapping ("maximizeWindow" , MaximizeWindow .class );
343
- }
359
+ }
344
360
}
0 commit comments