2222
2323import org .openqa .selenium .UnsupportedCommandException ;
2424import org .openqa .selenium .remote .Command ;
25+ import org .openqa .selenium .remote .CommandCodec ;
2526import org .openqa .selenium .remote .ErrorCodes ;
2627import org .openqa .selenium .remote .Response ;
28+ import org .openqa .selenium .remote .ResponseCodec ;
2729import org .openqa .selenium .remote .http .HttpRequest ;
2830import org .openqa .selenium .remote .http .HttpResponse ;
2931import org .openqa .selenium .remote .http .JsonHttpCommandCodec ;
3032import org .openqa .selenium .remote .http .JsonHttpResponseCodec ;
33+ import org .openqa .selenium .remote .http .W3CHttpCommandCodec ;
3134import org .openqa .selenium .remote .server .handler .AcceptAlert ;
3235import org .openqa .selenium .remote .server .handler .AddConfig ;
3336import org .openqa .selenium .remote .server .handler .AddCookie ;
136139import org .openqa .selenium .remote .server .rest .ResultConfig ;
137140
138141import java .util .LinkedHashMap ;
142+ import java .util .LinkedHashSet ;
139143import java .util .Map ;
144+ import java .util .Set ;
140145import java .util .logging .Logger ;
141146
142147public class JsonHttpCommandHandler {
@@ -145,15 +150,17 @@ public class JsonHttpCommandHandler {
145150
146151 private final DriverSessions sessions ;
147152 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 ;
150155 private final Map <String , ResultConfig > configs = new LinkedHashMap <>();
151156 private final ErrorCodes errorCodes = new ErrorCodes ();
152157
153158 public JsonHttpCommandHandler (DriverSessions sessions , Logger log ) {
154159 this .sessions = sessions ;
155160 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 ());
157164 this .responseCodec = new JsonHttpResponseCodec ();
158165 setUpMappings ();
159166 }
@@ -170,7 +177,7 @@ public HttpResponse handleRequest(HttpRequest request) {
170177 Command command = null ;
171178 Response response ;
172179 try {
173- command = commandCodec . decode (request );
180+ command = decode (request );
174181 ResultConfig config = configs .get (command .getName ());
175182 if (config == null ) {
176183 throw new UnsupportedCommandException ();
@@ -191,8 +198,26 @@ public HttpResponse handleRequest(HttpRequest request) {
191198 return responseCodec .encode (response );
192199 }
193200
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+
194216 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+
196221 addNewMapping (ADD_CONFIG_COMMAND_NAME , AddConfig .class );
197222
198223 addNewMapping (STATUS , Status .class );
@@ -328,17 +353,8 @@ private void setUpMappings() {
328353 addNewMapping (SET_NETWORK_CONNECTION , SetNetworkConnection .class );
329354
330355 // 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 );
340356 addNewMapping ("getWindowSize" , GetWindowSize .class );
341357 addNewMapping ("setWindowSize" , SetWindowSize .class );
342358 addNewMapping ("maximizeWindow" , MaximizeWindow .class );
343- }
359+ }
344360}
0 commit comments