Skip to content

Commit 18e6cb4

Browse files
authored
support file write in binding-filesystem & binding-http-filesystem (#1300)
1 parent 019f8d9 commit 18e6cb4

File tree

51 files changed

+2596
-55
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2596
-55
lines changed

runtime/binding-filesystem/src/main/java/io/aklivity/zilla/runtime/binding/filesystem/internal/stream/FileSystemServerFactory.java

+566-6
Large diffs are not rendered by default.

runtime/binding-filesystem/src/test/java/io/aklivity/zilla/runtime/binding/filesystem/internal/stream/FileSystemServerIT.java

+103-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public void shouldReadFilePayloadModifiedMultiClient() throws Exception
137137
@Test
138138
@Configuration("server.yaml")
139139
@Specification({
140-
"${app}/read.file.payload.etag.not.matched/client"
140+
"${app}/read.file.payload.tag.not.matched/client"
141141
})
142-
public void shouldReadFilePayloadEtagNotMatched() throws Exception
142+
public void shouldReadFilePayloadTagNotMatched() throws Exception
143143
{
144144
k3po.finish();
145145
}
@@ -252,4 +252,105 @@ public void shouldReceiveClientReadAbort() throws Exception
252252
{
253253
k3po.finish();
254254
}
255+
256+
@Test
257+
@Configuration("server.yaml")
258+
@Specification({
259+
"${app}/create.file.payload/client",
260+
})
261+
public void shouldCreateFilePayloadOnly() throws Exception
262+
{
263+
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
264+
Path indexFile = targetDirectory.resolve("index_write.html");
265+
266+
Files.deleteIfExists(indexFile);
267+
268+
k3po.finish();
269+
}
270+
271+
@Test
272+
@Configuration("server.yaml")
273+
@Specification({
274+
"${app}/write.file.payload.modified/client",
275+
})
276+
public void shouldWriteFilePayloadModified() throws Exception
277+
{
278+
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
279+
Path indexFile = targetDirectory.resolve("index_write.html");
280+
281+
Files.createDirectories(targetDirectory);
282+
283+
Files.write(indexFile, """
284+
<html>
285+
<head><title>Welcome</title></head>
286+
<body>Hello, world</body>
287+
</html>
288+
""".getBytes());
289+
290+
k3po.finish();
291+
}
292+
293+
@Test
294+
@Configuration("server.yaml")
295+
@Specification({
296+
"${app}/write.file.payload.modified.abort/client",
297+
})
298+
public void shouldWriteFilePayloadModifiedAbort() throws Exception
299+
{
300+
k3po.finish();
301+
}
302+
303+
@Test
304+
@Configuration("server.yaml")
305+
@Specification({
306+
"${app}/write.file.payload.interrupt/client",
307+
})
308+
public void shouldWriteFilePayloadInterrupt() throws Exception
309+
{
310+
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
311+
Path indexFile = targetDirectory.resolve("index_write.html");
312+
313+
Files.createDirectories(targetDirectory);
314+
315+
Files.write(indexFile, """
316+
<html>
317+
<head><title>Welcome</title></head>
318+
<body>Hello, world</body>
319+
</html>
320+
""".getBytes());
321+
322+
k3po.finish();
323+
}
324+
325+
@Test
326+
@Configuration("server.yaml")
327+
@Specification({
328+
"${app}/delete.file.payload/client",
329+
})
330+
public void shouldDeleteFilePayload() throws Exception
331+
{
332+
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
333+
Path indexFile = targetDirectory.resolve("error.html");
334+
335+
Files.createDirectories(targetDirectory);
336+
337+
Files.write(indexFile, """
338+
<html>
339+
<head><title>Welcome</title></head>
340+
<body>Hello, world</body>
341+
</html>
342+
""".getBytes());
343+
344+
k3po.finish();
345+
}
346+
347+
@Test
348+
@Configuration("server.yaml")
349+
@Specification({
350+
"${app}/delete.file.payload.failed/client",
351+
})
352+
public void shouldRejectDeleteFilePayload() throws Exception
353+
{
354+
k3po.finish();
355+
}
255356
}

runtime/binding-http-filesystem/src/main/java/io/aklivity/zilla/runtime/binding/http/filesystem/internal/config/HttpFileSystemWithResolver.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
*/
1515
package io.aklivity.zilla.runtime.binding.http.filesystem.internal.config;
1616

17+
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.CREATE_PAYLOAD;
18+
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.DELETE_PAYLOAD;
1719
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.READ_EXTENSION;
1820
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.READ_PAYLOAD;
21+
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.WRITE_PAYLOAD;
1922

2023
import java.util.concurrent.TimeUnit;
2124
import java.util.function.Function;
@@ -32,14 +35,21 @@ public final class HttpFileSystemWithResolver
3235
{
3336
public static final int HEADER_METHOD_MASK_HEAD = 1 << READ_EXTENSION.ordinal();
3437
private static final int HEADER_METHOD_MASK_GET = 1 << READ_PAYLOAD.ordinal() | 1 << READ_EXTENSION.ordinal();
38+
public static final int HEADER_METHOD_MASK_POST = 1 << CREATE_PAYLOAD.ordinal();
39+
public static final int HEADER_METHOD_MASK_PUT = 1 << WRITE_PAYLOAD.ordinal();
40+
public static final int HEADER_METHOD_MASK_DELETE = 1 << DELETE_PAYLOAD.ordinal();
3541

3642
private static final Pattern PARAMS_PATTERN = Pattern.compile("\\$\\{params\\.([a-zA-Z_]+)\\}");
3743
private static final Pattern PREFER_WAIT_PATTERN = Pattern.compile("wait=(\\d+)");
3844
private static final String8FW HEADER_METHOD_NAME = new String8FW(":method");
3945
private static final String8FW HEADER_IF_NONE_MATCH_NAME = new String8FW("if-none-match");
46+
private static final String8FW HEADER_IF_MATCH_NAME = new String8FW("if-match");
4047
private static final String8FW HEADER_PREFER_NAME = new String8FW("prefer");
4148
private static final String16FW HEADER_METHOD_VALUE_GET = new String16FW("GET");
4249
private static final String16FW HEADER_METHOD_VALUE_HEAD = new String16FW("HEAD");
50+
private static final String16FW HEADER_METHOD_VALUE_POST = new String16FW("POST");
51+
private static final String16FW HEADER_METHOD_VALUE_PUT = new String16FW("PUT");
52+
private static final String16FW HEADER_METHOD_VALUE_DELETE = new String16FW("DELETE");
4353

4454
private final String16FW etagRO = new String16FW();
4555
private final HttpFileSystemWithConfig with;
@@ -73,6 +83,7 @@ public HttpFileSystemWithResult resolve(
7383
path0 = pathMatcher.replaceAll(replacer);
7484
}
7585
String16FW path = new String16FW(path0);
86+
String16FW etag = new String16FW("");
7687

7788
HttpHeaderFW method = httpBeginEx.headers().matchFirst(h -> HEADER_METHOD_NAME.equals(h.name()));
7889
int capabilities = 0;
@@ -86,14 +97,27 @@ else if (HEADER_METHOD_VALUE_GET.equals(method.value()))
8697
{
8798
capabilities = HEADER_METHOD_MASK_GET;
8899
}
100+
else if (HEADER_METHOD_VALUE_POST.equals(method.value()))
101+
{
102+
capabilities = HEADER_METHOD_MASK_POST;
103+
}
104+
else if (HEADER_METHOD_VALUE_PUT.equals(method.value()))
105+
{
106+
capabilities = HEADER_METHOD_MASK_PUT;
107+
}
108+
else if (HEADER_METHOD_VALUE_DELETE.equals(method.value()))
109+
{
110+
capabilities = HEADER_METHOD_MASK_DELETE;
111+
}
89112
}
90-
HttpHeaderFW ifNotMatched = httpBeginEx.headers().matchFirst(h -> HEADER_IF_NONE_MATCH_NAME.equals(h.name()));
91-
String16FW etag = new String16FW("");
92-
if (ifNotMatched != null)
113+
HttpHeaderFW tag = httpBeginEx.headers().matchFirst(h ->
114+
HEADER_IF_MATCH_NAME.equals(h.name()) || HEADER_IF_NONE_MATCH_NAME.equals(h.name()));
115+
if (tag != null)
93116
{
94-
String16FW value = ifNotMatched.value();
117+
String16FW value = tag.value();
95118
etag = etagRO.wrap(value.buffer(), value.offset(), value.limit());
96119
}
120+
97121
HttpHeaderFW prefer = httpBeginEx.headers().matchFirst(h -> HEADER_PREFER_NAME.equals(h.name()));
98122
int wait = 0;
99123
if (prefer != null)

0 commit comments

Comments
 (0)