Skip to content

Commit c18d680

Browse files
committed
Handle both JSON and W3C end points on the server
We do this by iterating over all known command codecs when decoding a request's URL.
1 parent 7afd033 commit c18d680

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java

+31-15
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222

2323
import org.openqa.selenium.UnsupportedCommandException;
2424
import org.openqa.selenium.remote.Command;
25+
import org.openqa.selenium.remote.CommandCodec;
2526
import org.openqa.selenium.remote.ErrorCodes;
2627
import org.openqa.selenium.remote.Response;
28+
import org.openqa.selenium.remote.ResponseCodec;
2729
import org.openqa.selenium.remote.http.HttpRequest;
2830
import org.openqa.selenium.remote.http.HttpResponse;
2931
import org.openqa.selenium.remote.http.JsonHttpCommandCodec;
3032
import org.openqa.selenium.remote.http.JsonHttpResponseCodec;
33+
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
3134
import org.openqa.selenium.remote.server.handler.AcceptAlert;
3235
import org.openqa.selenium.remote.server.handler.AddConfig;
3336
import org.openqa.selenium.remote.server.handler.AddCookie;
@@ -136,7 +139,9 @@
136139
import org.openqa.selenium.remote.server.rest.ResultConfig;
137140

138141
import java.util.LinkedHashMap;
142+
import java.util.LinkedHashSet;
139143
import java.util.Map;
144+
import java.util.Set;
140145
import java.util.logging.Logger;
141146

142147
public 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

Comments
 (0)