Skip to content

Commit 9bc8c9a

Browse files
committed
Make ActiveSession track the protocol dialect
This allows us to correctly format responses when necessary. Closes #4119
1 parent 2dc48fc commit 9bc8c9a

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.remote.server;
1919

20+
import org.openqa.selenium.remote.Dialect;
2021
import org.openqa.selenium.remote.SessionId;
2122

2223
import java.util.Map;
@@ -25,6 +26,10 @@ interface ActiveSession extends CommandHandler {
2526

2627
SessionId getId();
2728

29+
Dialect getUpstreamDialect();
30+
31+
Dialect getDownstreamDialect();
32+
2833
/**
2934
* Desribe the current webdriver session's capabilities.
3035
*/

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import com.google.common.collect.ImmutableMap;
4040
import com.google.common.collect.ImmutableSet;
4141
import com.google.common.net.MediaType;
42-
import com.google.gson.Gson;
43-
import com.google.gson.GsonBuilder;
4442
import com.google.gson.stream.JsonReader;
4543
import com.google.gson.stream.JsonToken;
4644

@@ -73,23 +71,17 @@
7371
class BeginSession implements CommandHandler {
7472

7573
private final static Logger LOG = Logger.getLogger(BeginSession.class.getName());
76-
private final static Gson gson = new GsonBuilder().serializeNulls().create();
7774

7875
private final Cache<SessionId, ActiveSession> allSessions;
79-
private final DriverSessions legacySessions;
8076
private final Map<String, SessionFactory> factories;
81-
private final Function<Path, ActiveSession> defaultFactory;
8277

8378
public BeginSession(Cache<SessionId, ActiveSession> allSessions, DriverSessions legacySessions) {
8479
this.allSessions = allSessions;
85-
this.legacySessions = legacySessions;
8680

8781
this.factories = ImmutableMap.of(
8882
chrome().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.chrome.ChromeDriverService"),
8983
firefox().getBrowserName(), new ServicedSession.Factory("org.openqa.selenium.firefox.GeckoDriverService"),
9084
htmlUnit().getBrowserName(), new InMemorySession.Factory(legacySessions));
91-
92-
defaultFactory = null;
9385
}
9486

9587
@Override
@@ -133,18 +125,24 @@ public void execute(HttpRequest req, HttpResponse resp) throws IOException {
133125
allSessions.put(session.getId(), session);
134126

135127
Object toConvert;
136-
if (firstMatch.isEmpty() && alwaysMatch.isEmpty()) {
137-
// JSON Wire Protocol response
138-
toConvert = ImmutableMap.of(
139-
"status", 0,
140-
"sessionId", session.getId().toString(),
141-
"value", session.getCapabilities());
142-
} else {
143-
// W3C protocol response
144-
toConvert = ImmutableMap.of(
145-
"value", ImmutableMap.of(
146-
"sessionId", session.getId().toString(),
147-
"capabilities", session.getCapabilities()));
128+
switch (session.getDownstreamDialect()) {
129+
case OSS:
130+
toConvert = ImmutableMap.of(
131+
"status", 0,
132+
"sessionId", session.getId().toString(),
133+
"value", session.getCapabilities());
134+
break;
135+
136+
case W3C:
137+
toConvert = ImmutableMap.of(
138+
"value", ImmutableMap.of(
139+
"sessionId", session.getId().toString(),
140+
"capabilities", session.getCapabilities()));
141+
break;
142+
143+
default:
144+
throw new SessionNotCreatedException(
145+
"Unrecognized downstream dialect: " + session.getDownstreamDialect());
148146
}
149147

150148
byte[] payload = new BeanToJsonConverter().convert(toConvert).getBytes(UTF_8);

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.openqa.selenium.remote.server;
1919

2020
import static java.nio.charset.StandardCharsets.UTF_8;
21+
import static org.openqa.selenium.remote.Dialect.OSS;
2122

2223
import com.google.common.base.Preconditions;
24+
import com.google.common.collect.Iterables;
2325
import com.google.common.io.CharStreams;
2426

2527
import org.openqa.selenium.Capabilities;
@@ -54,18 +56,34 @@ class InMemorySession implements ActiveSession {
5456
private final SessionId id;
5557
private final Session session;
5658
private JsonHttpCommandHandler commandHandler;
59+
private Dialect downstreamDialect;
5760

58-
public InMemorySession(SessionId id, Session session, JsonHttpCommandHandler commandHandler) {
61+
public InMemorySession(
62+
SessionId id,
63+
Session session,
64+
JsonHttpCommandHandler commandHandler,
65+
Dialect downstreamDialect) {
5966
this.id = id;
6067
this.session = session;
6168
this.commandHandler = commandHandler;
69+
this.downstreamDialect = downstreamDialect;
6270
}
6371

6472
@Override
6573
public SessionId getId() {
6674
return id;
6775
}
6876

77+
@Override
78+
public Dialect getUpstreamDialect() {
79+
return OSS;
80+
}
81+
82+
@Override
83+
public Dialect getDownstreamDialect() {
84+
return downstreamDialect;
85+
}
86+
6987
@Override
7088
public Map<String, Object> getCapabilities() {
7189
return session.getCapabilities().asMap().entrySet().stream()
@@ -108,7 +126,12 @@ public ActiveSession apply(Path path, Set<Dialect> downstreamDialects) {
108126
SessionId sessionId = legacySessions.newSession(caps);
109127
Session session = legacySessions.get(sessionId);
110128

111-
return new InMemorySession(sessionId, session, jsonHttpCommandHandler);
129+
// Force OSS dialect if downstream speaks it
130+
Dialect downstream = downstreamDialects.contains(OSS) ?
131+
OSS :
132+
Iterables.getFirst(downstreamDialects, null);
133+
134+
return new InMemorySession(sessionId, session, jsonHttpCommandHandler, downstream);
112135
} catch (Exception e) {
113136
throw new SessionNotCreatedException("Unable to create session", e);
114137
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.common.io.CharStreams;
2525
import com.google.common.net.MediaType;
2626

27+
import org.openqa.selenium.remote.Dialect;
2728
import org.openqa.selenium.remote.http.HttpRequest;
2829
import org.openqa.selenium.remote.http.HttpResponse;
2930

@@ -58,9 +59,11 @@ class Passthrough implements SessionCodec {
5859
.build();
5960

6061
private final URL upstream;
62+
private Dialect dialect;
6163

6264
public Passthrough(URL upstream) {
6365
this.upstream = upstream;
66+
this.dialect = dialect;
6467
}
6568

6669
@Override

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,21 @@ class ServicedSession implements ActiveSession {
6060

6161
private final DriverService service;
6262
private final SessionId id;
63+
private Dialect downstream;
64+
private Dialect upstream;
6365
private final SessionCodec codec;
6466
private final Map<String, Object> capabilities;
6567

6668
public ServicedSession(
6769
DriverService service,
70+
Dialect downstream,
71+
Dialect upstream,
6872
SessionCodec codec,
6973
SessionId id,
7074
Map<String, Object> capabilities) {
7175
this.service = service;
76+
this.downstream = downstream;
77+
this.upstream = upstream;
7278
this.codec = codec;
7379
this.id = id;
7480
this.capabilities = capabilities;
@@ -85,6 +91,16 @@ public SessionId getId() {
8591
return id;
8692
}
8793

94+
@Override
95+
public Dialect getUpstreamDialect() {
96+
return upstream;
97+
}
98+
99+
@Override
100+
public Dialect getDownstreamDialect() {
101+
return downstream;
102+
}
103+
88104
@Override
89105
public Map<String, Object> getCapabilities() {
90106
return capabilities;
@@ -138,24 +154,28 @@ public ActiveSession apply(Path path, Set<Dialect> downstreamDialects) {
138154
.orElseThrow(() -> new SessionNotCreatedException("Unable to create session"));
139155

140156
SessionCodec codec;
157+
Dialect upstream = result.getDialect();
158+
Dialect downstream;
141159
if (downstreamDialects.contains(result.getDialect())) {
142160
codec = new Passthrough(url);
161+
downstream = upstream;
143162
} else {
144-
145-
Dialect dialact = downstreamDialects.iterator().next();
163+
downstream = downstreamDialects.iterator().next();
146164

147165
codec = new ProtocolConverter(
148166
url,
149-
getCommandCodec(dialact),
150-
getResponseCodec(dialact),
151-
getCommandCodec(result.getDialect()),
152-
getResponseCodec(result.getDialect()));
167+
getCommandCodec(downstream),
168+
getResponseCodec(downstream),
169+
getCommandCodec(upstream),
170+
getResponseCodec(upstream));
153171
}
154172

155173
Response response = result.createResponse();
156174
//noinspection unchecked
157175
return new ServicedSession(
158176
service,
177+
downstream,
178+
upstream,
159179
codec,
160180
new SessionId(response.getSessionId()),
161181
(Map<String, Object>) response.getValue());

0 commit comments

Comments
 (0)