|
18 | 18 |
|
19 | 19 | package org.openqa.grid.web.servlet.handler;
|
20 | 20 |
|
| 21 | +import com.google.gson.Gson; |
| 22 | +import com.google.gson.JsonElement; |
21 | 23 | import com.google.gson.JsonObject;
|
22 | 24 | import com.google.gson.JsonParser;
|
23 | 25 |
|
24 | 26 | import org.openqa.grid.common.exception.GridException;
|
25 | 27 | import org.openqa.grid.internal.ExternalSessionKey;
|
26 | 28 | import org.openqa.grid.internal.Registry;
|
| 29 | +import org.openqa.selenium.remote.BeanToJsonConverter; |
27 | 30 | import org.openqa.selenium.remote.JsonToBeanConverter;
|
28 | 31 |
|
29 | 32 | import java.util.Map;
|
|
32 | 35 |
|
33 | 36 | public class WebDriverRequest extends SeleniumBasedRequest {
|
34 | 37 |
|
| 38 | + private static final String CAPABILITIES = "capabilities"; |
| 39 | + private static final String DESIRED_CAPABILITIES = "desiredCapabilities"; |
| 40 | + |
35 | 41 | public WebDriverRequest(HttpServletRequest httpServletRequest, Registry registry) {
|
36 | 42 | super(httpServletRequest, registry);
|
37 | 43 | }
|
@@ -65,17 +71,41 @@ public Map<String, Object> extractDesiredCapability() {
|
65 | 71 | JsonObject map = new JsonParser().parse(json).getAsJsonObject();
|
66 | 72 | // Current W3C has required / desired capabilities wrapped in a 'capabilities' object.
|
67 | 73 | // This will need to be updated if/when https://github.com/w3c/webdriver/pull/327 gets merged
|
68 |
| - if (map.has("capabilities")) { |
69 |
| - JsonObject outerCapabilities = map.getAsJsonObject("capabilities"); |
70 |
| - if (outerCapabilities.has("desiredCapabilities")) { |
71 |
| - JsonObject desiredCapabilities = outerCapabilities.getAsJsonObject("desiredCapabilities"); |
| 74 | + if (map.has(CAPABILITIES)) { |
| 75 | + JsonObject outerCapabilities = map.getAsJsonObject(CAPABILITIES); |
| 76 | + if (outerCapabilities.has(DESIRED_CAPABILITIES)) { |
| 77 | + JsonObject desiredCapabilities = outerCapabilities.getAsJsonObject(DESIRED_CAPABILITIES); |
72 | 78 | return new JsonToBeanConverter().convert(Map.class, desiredCapabilities);
|
73 | 79 | }
|
74 | 80 | }
|
75 |
| - JsonObject dc = map.get("desiredCapabilities").getAsJsonObject(); |
| 81 | + JsonObject dc = map.get(DESIRED_CAPABILITIES).getAsJsonObject(); |
76 | 82 | return new JsonToBeanConverter().convert(Map.class, dc);
|
| 83 | + |
77 | 84 | } catch (Exception e) {
|
78 | 85 | throw new GridException("Cannot extract a capabilities from the request: " + json, e);
|
79 | 86 | }
|
80 | 87 | }
|
| 88 | + |
| 89 | + @Override |
| 90 | + public String getBody() { |
| 91 | + String json = super.getBody(); |
| 92 | + try { |
| 93 | + Map<String, Object> capsMap = getDesiredCapabilities(); |
| 94 | + if (capsMap == null) { |
| 95 | + return json; |
| 96 | + } |
| 97 | + JsonObject map = new JsonParser().parse(json).getAsJsonObject(); |
| 98 | + JsonElement dc = new BeanToJsonConverter().convertObject(capsMap); |
| 99 | + if (map.has(CAPABILITIES)) { |
| 100 | + map.getAsJsonObject(CAPABILITIES) |
| 101 | + .add(DESIRED_CAPABILITIES, new BeanToJsonConverter().convertObject(dc)); |
| 102 | + } else { |
| 103 | + map.add(DESIRED_CAPABILITIES, dc); |
| 104 | + } |
| 105 | + return new Gson().toJson(map); |
| 106 | + } catch (Exception e) { |
| 107 | + return json; |
| 108 | + } |
| 109 | + } |
| 110 | + |
81 | 111 | }
|
0 commit comments