Skip to content

Commit dfecced

Browse files
committed
Move handlers out of the WebDriverServlet
1 parent f2d8d33 commit dfecced

File tree

4 files changed

+211
-117
lines changed

4 files changed

+211
-117
lines changed

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

Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717

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

20-
import static com.google.common.net.MediaType.JSON_UTF_8;
21-
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
22-
import static java.net.HttpURLConnection.HTTP_OK;
23-
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static org.openqa.selenium.remote.ErrorCodes.NO_SUCH_SESSION;
25-
import static org.openqa.selenium.remote.ErrorCodes.SUCCESS;
26-
import static org.openqa.selenium.remote.ErrorCodes.UNKNOWN_COMMAND;
27-
2820
import com.google.common.annotations.VisibleForTesting;
2921
import com.google.common.base.Splitter;
3022
import com.google.common.base.Strings;
@@ -34,17 +26,14 @@
3426
import com.google.gson.Gson;
3527
import com.google.gson.GsonBuilder;
3628

37-
import org.openqa.selenium.internal.BuildInfo;
3829
import org.openqa.selenium.remote.SessionId;
3930
import org.openqa.selenium.remote.http.HttpMethod;
40-
import org.openqa.selenium.remote.http.HttpRequest;
41-
import org.openqa.selenium.remote.http.HttpResponse;
4231
import org.openqa.selenium.remote.server.commandhandler.GetLogTypes;
32+
import org.openqa.selenium.remote.server.commandhandler.NoHandler;
33+
import org.openqa.selenium.remote.server.commandhandler.NoSessionHandler;
34+
import org.openqa.selenium.remote.server.commandhandler.Status;
4335

44-
import java.io.IOException;
4536
import java.lang.reflect.Constructor;
46-
import java.util.Collections;
47-
import java.util.HashMap;
4837
import java.util.List;
4938
import java.util.Map;
5039
import java.util.Objects;
@@ -71,7 +60,7 @@ public AllHandlers(ActiveSessions allSessions) {
7160
HttpMethod.DELETE, ImmutableList.of(),
7261
HttpMethod.GET, ImmutableList.of(
7362
handler("/session/{sessionId}/log/types", GetLogTypes.class),
74-
handler("/status", StatusHandler.class)
63+
handler("/status", Status.class)
7564
),
7665
HttpMethod.POST, ImmutableList.of(
7766
handler("/session", BeginSession.class)
@@ -140,108 +129,6 @@ private <H extends CommandHandler> Function<String, CommandHandler> handler(
140129
};
141130
}
142131

143-
private static class NoHandler implements CommandHandler {
144-
145-
@Override
146-
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
147-
// We're not using ImmutableMap for the outer map because it disallows null values.
148-
Map<String, Object> responseMap = new HashMap<>();
149-
responseMap.put("sessionId", null);
150-
responseMap.put("status", UNKNOWN_COMMAND);
151-
responseMap.put("value", ImmutableMap.of(
152-
"error", "unknown command",
153-
"message", String.format(
154-
"Unable to find command matching %s to %s",
155-
req.getMethod(),
156-
req.getUri()),
157-
"stacktrace", ""));
158-
responseMap = Collections.unmodifiableMap(responseMap);
159-
160-
byte[] payload = new GsonBuilder().serializeNulls().create().toJson(responseMap)
161-
.getBytes(UTF_8);
162-
163-
resp.setStatus(HTTP_NOT_FOUND);
164-
resp.setHeader("Content-Type", JSON_UTF_8.toString());
165-
resp.setHeader("Content-Length", String.valueOf(payload.length));
166-
167-
resp.setContent(payload);
168-
}
169-
}
170-
171-
private static class NoSessionHandler implements CommandHandler {
172-
173-
private final SessionId sessionId;
174-
175-
public NoSessionHandler(SessionId sessionId) {
176-
this.sessionId = sessionId;
177-
}
178-
179-
@Override
180-
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
181-
// We're not using ImmutableMap for the outer map because it disallows null values.
182-
Map<String, Object> responseMap = new HashMap<>();
183-
responseMap.put("sessionId", sessionId.toString());
184-
responseMap.put("status", NO_SUCH_SESSION);
185-
responseMap.put("value", ImmutableMap.of(
186-
"error", "invalid session id",
187-
"message", String.format("No active session with ID %s", sessionId),
188-
"stacktrace", ""));
189-
responseMap = Collections.unmodifiableMap(responseMap);
190-
191-
byte[] payload = new GsonBuilder().serializeNulls().create().toJson(responseMap)
192-
.getBytes(UTF_8);
193-
194-
resp.setStatus(HTTP_NOT_FOUND);
195-
resp.setHeader("Content-Type", JSON_UTF_8.toString());
196-
resp.setHeader("Content-Length", String.valueOf(payload.length));
197-
198-
resp.setContent(payload);
199-
}
200-
}
201-
202-
private static class StatusHandler implements CommandHandler {
203-
204-
@Override
205-
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
206-
ImmutableMap.Builder<String, Object> value = ImmutableMap.builder();
207-
208-
// W3C spec
209-
value.put("ready", true);
210-
value.put("message", "Server is running");
211-
212-
// And now more information
213-
BuildInfo buildInfo = new BuildInfo();
214-
value.put("build", ImmutableMap.of(
215-
// We need to fix the BuildInfo to properly fill out these values.
216-
// "revision", buildInfo.getBuildRevision(),
217-
// "time", buildInfo.getBuildTime(),
218-
"version", buildInfo.getReleaseLabel()));
219-
220-
value.put("os", ImmutableMap.of(
221-
"arch", System.getProperty("os.arch"),
222-
"name", System.getProperty("os.name"),
223-
"version", System.getProperty("os.version")));
224-
225-
value.put("java", ImmutableMap.of("version", System.getProperty("java.version")));
226-
227-
Map<String, Object> payloadObj = ImmutableMap.of(
228-
"status", SUCCESS,
229-
"value", value.build());
230-
231-
// Write out a minimal W3C status response.
232-
byte[] payload = new GsonBuilder()
233-
.serializeNulls()
234-
.create()
235-
.toJson(payloadObj).getBytes(UTF_8);
236-
237-
resp.setStatus(HTTP_OK);
238-
resp.setHeader("Content-Type", JSON_UTF_8.toString());
239-
resp.setHeader("Content-Length", String.valueOf(payload.length));
240-
241-
resp.setContent(payload);
242-
}
243-
}
244-
245132
@VisibleForTesting
246133
<T extends CommandHandler> T create(Class<T> toCreate, Set<Object> args) {
247134
Constructor<?> constructor = Stream.of(toCreate.getDeclaredConstructors())
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_NOT_FOUND;
22+
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static org.openqa.selenium.remote.ErrorCodes.UNKNOWN_COMMAND;
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.CommandHandler;
31+
32+
import java.io.IOException;
33+
import java.util.Collections;
34+
import java.util.HashMap;
35+
import java.util.Map;
36+
37+
public class NoHandler implements CommandHandler {
38+
39+
@Override
40+
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
41+
// We're not using ImmutableMap for the outer map because it disallows null values.
42+
Map<String, Object> responseMap = new HashMap<>();
43+
responseMap.put("sessionId", null);
44+
responseMap.put("status", UNKNOWN_COMMAND);
45+
responseMap.put("value", ImmutableMap.of(
46+
"error", "unknown command",
47+
"message", String.format(
48+
"Unable to find command matching %s to %s",
49+
req.getMethod(),
50+
req.getUri()),
51+
"stacktrace", ""));
52+
responseMap = Collections.unmodifiableMap(responseMap);
53+
54+
byte[] payload = new GsonBuilder().serializeNulls().create().toJson(responseMap)
55+
.getBytes(UTF_8);
56+
57+
resp.setStatus(HTTP_NOT_FOUND);
58+
resp.setHeader("Content-Type", JSON_UTF_8.toString());
59+
resp.setHeader("Content-Length", String.valueOf(payload.length));
60+
61+
resp.setContent(payload);
62+
}
63+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_NOT_FOUND;
22+
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static org.openqa.selenium.remote.ErrorCodes.NO_SUCH_SESSION;
24+
25+
import com.google.common.collect.ImmutableMap;
26+
import com.google.gson.GsonBuilder;
27+
28+
import org.openqa.selenium.remote.SessionId;
29+
import org.openqa.selenium.remote.http.HttpRequest;
30+
import org.openqa.selenium.remote.http.HttpResponse;
31+
import org.openqa.selenium.remote.server.CommandHandler;
32+
33+
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
38+
public class NoSessionHandler implements CommandHandler {
39+
40+
private final SessionId sessionId;
41+
42+
public NoSessionHandler(SessionId sessionId) {
43+
this.sessionId = sessionId;
44+
}
45+
46+
@Override
47+
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
48+
// We're not using ImmutableMap for the outer map because it disallows null values.
49+
Map<String, Object> responseMap = new HashMap<>();
50+
responseMap.put("sessionId", sessionId.toString());
51+
responseMap.put("status", NO_SUCH_SESSION);
52+
responseMap.put("value", ImmutableMap.of(
53+
"error", "invalid session id",
54+
"message", String.format("No active session with ID %s", sessionId),
55+
"stacktrace", ""));
56+
responseMap = Collections.unmodifiableMap(responseMap);
57+
58+
byte[] payload = new GsonBuilder().serializeNulls().create().toJson(responseMap)
59+
.getBytes(UTF_8);
60+
61+
resp.setStatus(HTTP_NOT_FOUND);
62+
resp.setHeader("Content-Type", JSON_UTF_8.toString());
63+
resp.setHeader("Content-Length", String.valueOf(payload.length));
64+
65+
resp.setContent(payload);
66+
}
67+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.internal.BuildInfo;
29+
import org.openqa.selenium.remote.http.HttpRequest;
30+
import org.openqa.selenium.remote.http.HttpResponse;
31+
import org.openqa.selenium.remote.server.CommandHandler;
32+
33+
import java.io.IOException;
34+
import java.util.Map;
35+
36+
public class Status implements CommandHandler {
37+
38+
@Override
39+
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
40+
ImmutableMap.Builder<String, Object> value = ImmutableMap.builder();
41+
42+
// W3C spec
43+
value.put("ready", true);
44+
value.put("message", "Server is running");
45+
46+
// And now more information
47+
BuildInfo buildInfo = new BuildInfo();
48+
value.put("build", ImmutableMap.of(
49+
// We need to fix the BuildInfo to properly fill out these values.
50+
// "revision", buildInfo.getBuildRevision(),
51+
// "time", buildInfo.getBuildTime(),
52+
"version", buildInfo.getReleaseLabel()));
53+
54+
value.put("os", ImmutableMap.of(
55+
"arch", System.getProperty("os.arch"),
56+
"name", System.getProperty("os.name"),
57+
"version", System.getProperty("os.version")));
58+
59+
value.put("java", ImmutableMap.of("version", System.getProperty("java.version")));
60+
61+
Map<String, Object> payloadObj = ImmutableMap.of(
62+
"status", SUCCESS,
63+
"value", value.build());
64+
65+
// Write out a minimal W3C status response.
66+
byte[] payload = new GsonBuilder()
67+
.serializeNulls()
68+
.create()
69+
.toJson(payloadObj).getBytes(UTF_8);
70+
71+
resp.setStatus(HTTP_OK);
72+
resp.setHeader("Content-Type", JSON_UTF_8.toString());
73+
resp.setHeader("Content-Length", String.valueOf(payload.length));
74+
75+
resp.setContent(payload);
76+
}
77+
}

0 commit comments

Comments
 (0)