Skip to content

Commit afdf13b

Browse files
committed
Implementing /wd/hub/sessions endpoint in selenium server
1 parent 312a8af commit afdf13b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.remote.server.log.LoggingManager;
2626
import org.openqa.selenium.remote.server.log.PerSessionLogHandler;
2727

28+
import java.util.Collection;
2829
import java.util.LinkedList;
2930
import java.util.List;
3031
import java.util.concurrent.TimeUnit;
@@ -85,6 +86,10 @@ public void invalidate(SessionId id) {
8586
allSessions.invalidate(id);
8687
}
8788

89+
public Collection<ActiveSession> getAllSessions() {
90+
return allSessions.asMap().values();
91+
}
92+
8893
public void addListener(ActiveSessionListener listener) {
8994
listeners.add(listener);
9095
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openqa.selenium.remote.server.commandhandler.NoHandler;
3434
import org.openqa.selenium.remote.server.commandhandler.NoSessionHandler;
3535
import org.openqa.selenium.remote.server.commandhandler.Status;
36+
import org.openqa.selenium.remote.server.commandhandler.GetAllSessions;
3637

3738
import java.lang.reflect.Constructor;
3839
import java.util.List;
@@ -61,6 +62,7 @@ public AllHandlers(ActiveSessions allSessions) {
6162
HttpMethod.DELETE, ImmutableList.of(),
6263
HttpMethod.GET, ImmutableList.of(
6364
handler("/session/{sessionId}/log/types", GetLogTypes.class),
65+
handler("/sessions", GetAllSessions.class),
6466
handler("/status", Status.class)
6567
),
6668
HttpMethod.POST, ImmutableList.of(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.server.commandhandler;
19+
20+
import static com.google.common.net.MediaType.JSON_UTF_8;
21+
import static java.net.HttpURLConnection.HTTP_OK;
22+
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static org.openqa.selenium.remote.ErrorCodes.SUCCESS;
24+
25+
import com.google.common.collect.ImmutableMap;
26+
import com.google.gson.GsonBuilder;
27+
28+
import org.openqa.selenium.remote.http.HttpRequest;
29+
import org.openqa.selenium.remote.http.HttpResponse;
30+
import org.openqa.selenium.remote.server.ActiveSessions;
31+
import org.openqa.selenium.remote.server.CommandHandler;
32+
33+
import java.io.IOException;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
public class GetAllSessions implements CommandHandler {
39+
40+
private volatile ActiveSessions allSessions;
41+
42+
public GetAllSessions(ActiveSessions allSessions) {
43+
this.allSessions = allSessions;
44+
}
45+
46+
@Override
47+
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
48+
List<Map<String, Object>> value = new ArrayList<>();
49+
50+
allSessions.getAllSessions().forEach(s -> value.add(
51+
ImmutableMap.of("id", s.getId().toString(), "capabilities", s.getCapabilities())));
52+
53+
Map<String, Object> payloadObj = ImmutableMap.of(
54+
"status", SUCCESS,
55+
"value", value);
56+
57+
// Write out a minimal W3C status response.
58+
byte[] payload = new GsonBuilder()
59+
.serializeNulls()
60+
.create()
61+
.toJson(payloadObj).getBytes(UTF_8);
62+
63+
resp.setStatus(HTTP_OK);
64+
resp.setHeader("Content-Type", JSON_UTF_8.toString());
65+
resp.setHeader("Content-Length", String.valueOf(payload.length));
66+
67+
resp.setContent(payload);
68+
}
69+
}

0 commit comments

Comments
 (0)