52
52
*
53
53
* @see <a href="https://w3.org/tr/webdriver">W3C WebDriver spec</a>
54
54
*/
55
- public class AbstractHttpCommandCodec implements CommandCodec <HttpRequest > {
55
+ public abstract class AbstractHttpCommandCodec implements CommandCodec <HttpRequest > {
56
56
private static final Splitter PATH_SPLITTER = Splitter .on ('/' ).omitEmptyStrings ();
57
57
private static final String SESSION_ID_PARAM = "sessionId" ;
58
58
@@ -79,17 +79,9 @@ public AbstractHttpCommandCodec() {
79
79
defineCommand (SWITCH_TO_WINDOW , post ("/session/:sessionId/window" ));
80
80
81
81
defineCommand (GET_WINDOW_HANDLES , get ("/session/:sessionId/window/handles" ));
82
- defineCommand (MAXIMIZE_WINDOW , post ("/session/:sessionId/window/:windowHandle/maximize" ));
83
- defineCommand (GET_WINDOW_SIZE , get ("/session/:sessionId/window/:windowHandle/size" ));
84
- defineCommand (SET_WINDOW_SIZE , post ("/session/:sessionId/window/:windowHandle/size" ));
85
82
defineCommand (GET_WINDOW_POSITION , get ("/session/:sessionId/window/:windowHandle/position" ));
86
- defineCommand (SET_WINDOW_POSITION , post ("/session/:sessionId/window/:windowHandle/position" ));
87
83
defineCommand (GET_CURRENT_WINDOW_HANDLE , get ("/session/:sessionId/window" ));
88
-
89
- defineCommand (MAXIMIZE_CURRENT_WINDOW , post ("/session/:sessionId/window/maximize" ));
90
84
defineCommand (FULLSCREEN_CURRENT_WINDOW , post ("/session/:sessionId/window/fullscreen" ));
91
- defineCommand (GET_CURRENT_WINDOW_SIZE , get ("/session/:sessionId/window/size" ));
92
- defineCommand (SET_CURRENT_WINDOW_SIZE , post ("/session/:sessionId/window/size" ));
93
85
94
86
defineCommand (GET_CURRENT_URL , get ("/session/:sessionId/url" ));
95
87
defineCommand (GET , post ("/session/:sessionId/url" ));
@@ -204,12 +196,14 @@ public HttpRequest encode(Command command) {
204
196
if (spec == null ) {
205
197
throw new UnsupportedCommandException (command .getName ());
206
198
}
207
- String uri = buildUri (command , spec );
199
+ Map <String , ?> parameters = amendParameters (command .getName (), command .getParameters ());
200
+ String uri = buildUri (command .getName (), command .getSessionId (), parameters , spec );
208
201
209
202
HttpRequest request = new HttpRequest (spec .method , uri );
210
203
211
204
if (HttpMethod .POST == spec .method ) {
212
- String content = beanToJsonConverter .convert (command .getParameters ());
205
+
206
+ String content = beanToJsonConverter .convert (parameters );
213
207
byte [] data = content .getBytes (UTF_8 );
214
208
215
209
request .setHeader (CONTENT_LENGTH , String .valueOf (data .length ));
@@ -224,6 +218,8 @@ public HttpRequest encode(Command command) {
224
218
return request ;
225
219
}
226
220
221
+ protected abstract Map <String ,?> amendParameters (String name , Map <String , ?> parameters );
222
+
227
223
@ Override
228
224
public Command decode (final HttpRequest encodedCommand ) {
229
225
final String path = Strings .isNullOrEmpty (encodedCommand .getUri ())
@@ -293,7 +289,11 @@ protected static CommandSpec post(String path) {
293
289
return new CommandSpec (HttpMethod .POST , path );
294
290
}
295
291
296
- private String buildUri (Command command , CommandSpec spec ) {
292
+ private String buildUri (
293
+ String commandName ,
294
+ SessionId sessionId ,
295
+ Map <String , ?> parameters ,
296
+ CommandSpec spec ) {
297
297
StringBuilder builder = new StringBuilder ();
298
298
for (String part : spec .pathSegments ) {
299
299
if (part .isEmpty ()) {
@@ -302,24 +302,28 @@ private String buildUri(Command command, CommandSpec spec) {
302
302
303
303
builder .append ("/" );
304
304
if (part .startsWith (":" )) {
305
- builder .append (getParameter (part .substring (1 ), command ));
305
+ builder .append (getParameter (part .substring (1 ), commandName , sessionId , parameters ));
306
306
} else {
307
307
builder .append (part );
308
308
}
309
309
}
310
310
return builder .toString ();
311
311
}
312
312
313
- private String getParameter (String parameterName , Command command ) {
313
+ private String getParameter (
314
+ String parameterName ,
315
+ String commandName ,
316
+ SessionId sessionId ,
317
+ Map <String , ?> parameters ) {
314
318
if ("sessionId" .equals (parameterName )) {
315
- SessionId id = command . getSessionId () ;
316
- checkArgument (id != null , "Session ID may not be null for command %s" , command . getName () );
319
+ SessionId id = sessionId ;
320
+ checkArgument (id != null , "Session ID may not be null for command %s" , commandName );
317
321
return id .toString ();
318
322
}
319
323
320
- Object value = command . getParameters () .get (parameterName );
324
+ Object value = parameters .get (parameterName );
321
325
checkArgument (value != null ,
322
- "Missing required parameter \" %s\" for command %s" , parameterName , command . getName () );
326
+ "Missing required parameter \" %s\" for command %s" , parameterName , commandName );
323
327
return Urls .urlEncode (String .valueOf (value ));
324
328
}
325
329
0 commit comments