-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
RelaySessionFactory.java
250 lines (228 loc) · 10.4 KB
/
RelaySessionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.grid.node.relay;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
import static org.openqa.selenium.remote.tracing.AttributeKey.DOWNSTREAM_DIALECT;
import static org.openqa.selenium.remote.tracing.AttributeKey.DRIVER_RESPONSE;
import static org.openqa.selenium.remote.tracing.AttributeKey.DRIVER_URL;
import static org.openqa.selenium.remote.tracing.AttributeKey.EXCEPTION_EVENT;
import static org.openqa.selenium.remote.tracing.AttributeKey.EXCEPTION_MESSAGE;
import static org.openqa.selenium.remote.tracing.AttributeKey.LOGGER_CLASS;
import static org.openqa.selenium.remote.tracing.AttributeKey.UPSTREAM_DIALECT;
import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.node.ActiveSession;
import org.openqa.selenium.grid.node.DefaultActiveSession;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Debug;
import org.openqa.selenium.internal.Either;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ProtocolHandshake;
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.AttributeKey;
import org.openqa.selenium.remote.tracing.AttributeMap;
import org.openqa.selenium.remote.tracing.Span;
import org.openqa.selenium.remote.tracing.Status;
import org.openqa.selenium.remote.tracing.Tracer;
public class RelaySessionFactory implements SessionFactory {
private static final Logger LOG = Logger.getLogger(RelaySessionFactory.class.getName());
private final Tracer tracer;
private final HttpClient.Factory clientFactory;
private final Duration sessionTimeout;
private final URL serviceUrl;
private final URL serviceStatusUrl;
private final String serviceProtocolVersion;
private final Capabilities stereotype;
public RelaySessionFactory(
Tracer tracer,
HttpClient.Factory clientFactory,
Duration sessionTimeout,
URI serviceUri,
URI serviceStatusUri,
String serviceProtocolVersion,
Capabilities stereotype) {
this.tracer = Require.nonNull("Tracer", tracer);
this.clientFactory = Require.nonNull("HTTP client", clientFactory);
this.sessionTimeout = Require.nonNull("Session timeout", sessionTimeout);
this.serviceUrl = createUrlFromUri(Require.nonNull("Service URL", serviceUri));
this.serviceStatusUrl = createUrlFromUri(serviceStatusUri);
this.serviceProtocolVersion =
Require.nonNull("Service protocol version", serviceProtocolVersion);
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
}
@Override
public Capabilities getStereotype() {
return stereotype;
}
@Override
public boolean test(Capabilities capabilities) {
// If a request reaches this point is because the basic match of W3C caps has already been done.
// Custom matching in case a platformVersion is requested
boolean platformVersionMatch =
capabilities.getCapabilityNames().stream()
.filter(name -> name.contains("platformVersion"))
.map(
platformVersionCapName ->
Objects.equals(
stereotype.getCapability(platformVersionCapName),
capabilities.getCapability(platformVersionCapName)))
.reduce(Boolean::logicalAnd)
.orElse(true);
return platformVersionMatch
&& stereotype.getCapabilityNames().stream()
.filter(name -> capabilities.asMap().containsKey(name))
.map(
name -> {
if (capabilities.getCapability(name) instanceof String) {
return stereotype
.getCapability(name)
.toString()
.equalsIgnoreCase(capabilities.getCapability(name).toString());
} else {
return capabilities.getCapability(name) == null
|| Objects.equals(
stereotype.getCapability(name), capabilities.getCapability(name));
}
})
.reduce(Boolean::logicalAnd)
.orElse(false);
}
@Override
public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sessionRequest) {
Capabilities capabilities = sessionRequest.getDesiredCapabilities();
if (!test(capabilities)) {
return Either.left(
new SessionNotCreatedException(
"New session request capabilities do not " + "match the stereotype."));
}
capabilities = capabilities.merge(stereotype);
LOG.info("Starting session for " + capabilities);
try (Span span = tracer.getCurrentContext().createSpan("relay_session_factory.apply")) {
AttributeMap attributeMap = tracer.createAttributeMap();
CAPABILITIES.accept(span, capabilities);
CAPABILITIES_EVENT.accept(attributeMap, capabilities);
attributeMap.put(LOGGER_CLASS.getKey(), this.getClass().getName());
attributeMap.put(DRIVER_URL.getKey(), serviceUrl.toString());
ClientConfig clientConfig =
ClientConfig.defaultConfig().readTimeout(sessionTimeout).baseUrl(serviceUrl);
if (!serviceProtocolVersion.isEmpty()) {
clientConfig = clientConfig.version(serviceProtocolVersion);
}
HttpClient client = clientFactory.createClient(clientConfig);
Command command = new Command(null, DriverCommand.NEW_SESSION(capabilities));
try {
ProtocolHandshake.Result result = new ProtocolHandshake().createSession(client, command);
Set<Dialect> downstreamDialects = sessionRequest.getDownstreamDialects();
Dialect upstream = result.getDialect();
Dialect downstream =
downstreamDialects.contains(result.getDialect())
? result.getDialect()
: downstreamDialects.iterator().next();
Response response = result.createResponse();
attributeMap.put(UPSTREAM_DIALECT.getKey(), upstream.toString());
attributeMap.put(DOWNSTREAM_DIALECT.getKey(), downstream.toString());
attributeMap.put(DRIVER_RESPONSE.getKey(), response.toString());
Capabilities responseCaps = new ImmutableCapabilities((Map<?, ?>) response.getValue());
Capabilities mergedCapabilities = capabilities.merge(responseCaps);
span.addEvent("Relay service created session", attributeMap);
LOG.fine(String.format("Created session: %s - %s", response.getSessionId(), capabilities));
return Either.right(
new DefaultActiveSession(
tracer,
client,
new SessionId(response.getSessionId()),
serviceUrl,
downstream,
upstream,
stereotype,
mergedCapabilities,
Instant.now()) {});
} catch (Exception e) {
span.setAttribute(AttributeKey.ERROR.getKey(), true);
span.setStatus(Status.CANCELLED);
EXCEPTION.accept(attributeMap, e);
String errorMessage =
String.format(
"Error while creating session with the service %s. %s", serviceUrl, e.getMessage());
attributeMap.put(EXCEPTION_MESSAGE.getKey(), errorMessage);
span.addEvent(EXCEPTION_EVENT.getKey(), attributeMap);
return Either.left(new SessionNotCreatedException(errorMessage));
}
} catch (Exception e) {
return Either.left(new SessionNotCreatedException(e.getMessage()));
}
}
public boolean isServiceUp() {
if (serviceStatusUrl == null) {
// If no status endpoint was configured, we assume the server is up.
return true;
}
ClientConfig clientConfig = ClientConfig.defaultConfig().baseUrl(serviceStatusUrl);
if (!serviceProtocolVersion.isEmpty()) {
clientConfig = clientConfig.version(serviceProtocolVersion);
}
try (HttpClient client = clientFactory.createClient(clientConfig)) {
HttpResponse response =
client.execute(new HttpRequest(HttpMethod.GET, serviceStatusUrl.toString()));
LOG.log(Debug.getDebugLogLevel(), () -> Contents.string(response));
return response.getStatus() == 200;
} catch (Exception e) {
LOG.log(
Level.WARNING,
() ->
String.format(
"Error checking service status %s. %s", serviceStatusUrl, e.getMessage()));
LOG.log(Debug.getDebugLogLevel(), "Error checking service status " + serviceStatusUrl, e);
}
return false;
}
private URL createUrlFromUri(URI uri) {
if (uri == null) {
return null;
}
try {
return uri.toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}