-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
RelayOptions.java
176 lines (158 loc) · 6.19 KB
/
RelayOptions.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
// 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 com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.Tracer;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
public class RelayOptions {
static final String RELAY_SECTION = "relay";
private static final Logger LOG = Logger.getLogger(RelayOptions.class.getName());
private static final Json JSON = new Json();
private final Config config;
public RelayOptions(Config config) {
this.config = Require.nonNull("Config", config);
}
public URI getServiceUri() {
try {
Optional<String> possibleUri = config.get(RELAY_SECTION, "url");
if (possibleUri.isPresent()) {
return new URI(possibleUri.get());
}
Optional<String> possibleHost = config.get(RELAY_SECTION, "host");
Optional<Integer> possiblePort = config.getInt(RELAY_SECTION, "port");
if (possibleHost.isPresent() && possiblePort.isPresent()) {
String host = possibleHost.get();
int port = possiblePort.get();
if (!host.startsWith("http")) {
host = String.format("http://%s:%s", host, port);
} else {
host = String.format("%s:%s", host, port);
}
URI uri = new URI(host);
return new URI(
uri.getScheme(),
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
uri.getPath(),
null,
null);
}
throw new ConfigException("Unable to determine the service url");
} catch (URISyntaxException e) {
throw new ConfigException("Unable to determine the service url", e);
}
}
public URI getServiceStatusUri() {
try {
if (!config.get(RELAY_SECTION, "status-endpoint").isPresent()) {
return null;
}
String statusEndpoint = config.get(RELAY_SECTION, "status-endpoint").orElse("/status");
if (!statusEndpoint.startsWith("/")) {
statusEndpoint = "/" + statusEndpoint;
}
URI serviceUri = getServiceUri();
return new URI(serviceUri.toString() + statusEndpoint);
} catch (URISyntaxException e) {
throw new ConfigException("Unable to determine the service status url", e);
}
}
// Method being used in SessionSlot
@SuppressWarnings("unused")
private boolean isServiceUp(HttpClient client) {
URI serviceStatusUri = getServiceStatusUri();
if (serviceStatusUri == null) {
// If no status endpoint was configured, we assume the server is up.
return true;
}
try {
HttpResponse response = client.execute(new HttpRequest(GET, serviceStatusUri.toString()));
LOG.fine(string(response));
return 200 == response.getStatus();
} catch (Exception e) {
throw new ConfigException("Unable to reach the service at " + getServiceUri(), e);
}
}
public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
Tracer tracer,
HttpClient.Factory clientFactory,
Duration sessionTimeout) {
List<String> allConfigs = config.getAll(RELAY_SECTION, "configs")
.orElseThrow(() -> new ConfigException("Unable to find configs for " + getServiceUri()));
Multimap<Integer, Capabilities> parsedConfigs = HashMultimap.create();
for (int i = 0; i < allConfigs.size(); i++) {
int maxSessions;
try {
maxSessions = Integer.parseInt(extractConfiguredValue(allConfigs.get(i)));
} catch (NumberFormatException e) {
throw new ConfigException("Unable parse value as number. " + allConfigs.get(i));
}
i++;
if (i == allConfigs.size()) {
throw new ConfigException("Unable to find stereotype config. " + allConfigs);
}
Capabilities stereotype = JSON.toType(
extractConfiguredValue(allConfigs.get(i)),
Capabilities.class);
parsedConfigs.put(maxSessions, stereotype);
}
ImmutableMultimap.Builder<Capabilities, SessionFactory> factories = ImmutableMultimap.builder();
LOG.info(String.format("Adding relay configs for %s", getServiceUri()));
parsedConfigs.forEach((maxSessions, stereotype) -> {
for (int i = 0; i < maxSessions; i++) {
factories.put(
stereotype,
new RelaySessionFactory(
tracer,
clientFactory,
sessionTimeout,
getServiceUri(),
getServiceStatusUri(),
stereotype));
}
LOG.info(String.format("Mapping %s, %d times", stereotype, maxSessions));
});
return factories.build().asMap();
}
private String extractConfiguredValue(String keyValue) {
if (keyValue.contains("=")) {
return keyValue.substring(keyValue.indexOf("=") + 1);
}
return keyValue;
}
}