Skip to content

Commit b514a74

Browse files
committed
[grid] Adding file upload endpoint to the new grid
1 parent 8264a3a commit b514a74

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

java/server/src/org/openqa/selenium/grid/node/Node.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ protected Node(Tracer tracer, UUID id, URI uri) {
112112
// route the is checked.
113113
matching(req -> getSessionId(req.getUri()).map(SessionId::new).map(this::isSessionOwner).orElse(false))
114114
.to(() -> new ForwardWebDriverCommand(this)).with(new SpanDecorator(tracer, req -> "node.forward_command")),
115+
post("/session/{sessionId}/file").to(() -> new UploadFile(this, json)).with(new SpanDecorator(tracer, req -> "node.upload_file")),
115116
get("/se/grid/node/owner/{sessionId}")
116117
.to(params -> new IsSessionOwner(this, json, new SessionId(params.get("sessionId")))).with(new SpanDecorator(tracer, req -> "node.is_session_owner")),
117118
delete("/se/grid/node/session/{sessionId}")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.grid.node;
19+
20+
import static org.openqa.selenium.remote.http.Contents.string;
21+
import static org.openqa.selenium.remote.http.Contents.utf8String;
22+
23+
import com.google.common.collect.ImmutableMap;
24+
25+
import org.openqa.selenium.WebDriverException;
26+
import org.openqa.selenium.grid.data.CreateSessionRequest;
27+
import org.openqa.selenium.grid.data.CreateSessionResponse;
28+
import org.openqa.selenium.io.TemporaryFilesystem;
29+
import org.openqa.selenium.io.Zip;
30+
import org.openqa.selenium.json.Json;
31+
import org.openqa.selenium.remote.http.HttpHandler;
32+
import org.openqa.selenium.remote.http.HttpRequest;
33+
import org.openqa.selenium.remote.http.HttpResponse;
34+
35+
import java.io.File;
36+
import java.io.IOException;
37+
import java.io.UncheckedIOException;
38+
import java.util.HashMap;
39+
import java.util.Map;
40+
import java.util.Objects;
41+
42+
class UploadFile implements HttpHandler {
43+
44+
private final Node node;
45+
private final Json json;
46+
47+
UploadFile(Node node, Json json) {
48+
this.node = Objects.requireNonNull(node);
49+
this.json = Objects.requireNonNull(json);
50+
}
51+
52+
@Override
53+
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
54+
Map<String, Object> incoming = json.toType(string(req), Json.MAP_TYPE);
55+
56+
TemporaryFilesystem tempfs = TemporaryFilesystem.getDefaultTmpFS();
57+
File tempDir = tempfs.createTempDir("upload", "file");
58+
59+
try {
60+
Zip.unzip((String) incoming.get("file"), tempDir);
61+
} catch (IOException e) {
62+
throw new UncheckedIOException(e);
63+
}
64+
// Select the first file
65+
File[] allFiles = tempDir.listFiles();
66+
if (allFiles == null || allFiles.length != 1) {
67+
throw new WebDriverException(
68+
String.format("Expected there to be only 1 file. There were: %s", allFiles.length));
69+
}
70+
71+
ImmutableMap<String, Object> result = ImmutableMap.of(
72+
"value", allFiles[0].getAbsolutePath());
73+
74+
return new HttpResponse().setContent(utf8String(json.toJson(result)));
75+
}
76+
}

0 commit comments

Comments
 (0)