Skip to content

Commit

Permalink
support file write in binding-filesystem & binding-http-filesystem (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitk-me authored Nov 4, 2024
1 parent 019f8d9 commit 18e6cb4
Show file tree
Hide file tree
Showing 51 changed files with 2,596 additions and 55 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public void shouldReadFilePayloadModifiedMultiClient() throws Exception
@Test
@Configuration("server.yaml")
@Specification({
"${app}/read.file.payload.etag.not.matched/client"
"${app}/read.file.payload.tag.not.matched/client"
})
public void shouldReadFilePayloadEtagNotMatched() throws Exception
public void shouldReadFilePayloadTagNotMatched() throws Exception
{
k3po.finish();
}
Expand Down Expand Up @@ -252,4 +252,105 @@ public void shouldReceiveClientReadAbort() throws Exception
{
k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/create.file.payload/client",
})
public void shouldCreateFilePayloadOnly() throws Exception
{
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
Path indexFile = targetDirectory.resolve("index_write.html");

Files.deleteIfExists(indexFile);

k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/write.file.payload.modified/client",
})
public void shouldWriteFilePayloadModified() throws Exception
{
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
Path indexFile = targetDirectory.resolve("index_write.html");

Files.createDirectories(targetDirectory);

Files.write(indexFile, """
<html>
<head><title>Welcome</title></head>
<body>Hello, world</body>
</html>
""".getBytes());

k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/write.file.payload.modified.abort/client",
})
public void shouldWriteFilePayloadModifiedAbort() throws Exception
{
k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/write.file.payload.interrupt/client",
})
public void shouldWriteFilePayloadInterrupt() throws Exception
{
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
Path indexFile = targetDirectory.resolve("index_write.html");

Files.createDirectories(targetDirectory);

Files.write(indexFile, """
<html>
<head><title>Welcome</title></head>
<body>Hello, world</body>
</html>
""".getBytes());

k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/delete.file.payload/client",
})
public void shouldDeleteFilePayload() throws Exception
{
Path targetDirectory = Paths.get("target/files").toAbsolutePath();
Path indexFile = targetDirectory.resolve("error.html");

Files.createDirectories(targetDirectory);

Files.write(indexFile, """
<html>
<head><title>Welcome</title></head>
<body>Hello, world</body>
</html>
""".getBytes());

k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/delete.file.payload.failed/client",
})
public void shouldRejectDeleteFilePayload() throws Exception
{
k3po.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
*/
package io.aklivity.zilla.runtime.binding.http.filesystem.internal.config;

import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.CREATE_PAYLOAD;
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.DELETE_PAYLOAD;
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.READ_EXTENSION;
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.READ_PAYLOAD;
import static io.aklivity.zilla.runtime.binding.http.filesystem.internal.types.FileSystemCapabilities.WRITE_PAYLOAD;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand All @@ -32,14 +35,21 @@ public final class HttpFileSystemWithResolver
{
public static final int HEADER_METHOD_MASK_HEAD = 1 << READ_EXTENSION.ordinal();
private static final int HEADER_METHOD_MASK_GET = 1 << READ_PAYLOAD.ordinal() | 1 << READ_EXTENSION.ordinal();
public static final int HEADER_METHOD_MASK_POST = 1 << CREATE_PAYLOAD.ordinal();
public static final int HEADER_METHOD_MASK_PUT = 1 << WRITE_PAYLOAD.ordinal();
public static final int HEADER_METHOD_MASK_DELETE = 1 << DELETE_PAYLOAD.ordinal();

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

private final String16FW etagRO = new String16FW();
private final HttpFileSystemWithConfig with;
Expand Down Expand Up @@ -73,6 +83,7 @@ public HttpFileSystemWithResult resolve(
path0 = pathMatcher.replaceAll(replacer);
}
String16FW path = new String16FW(path0);
String16FW etag = new String16FW("");

HttpHeaderFW method = httpBeginEx.headers().matchFirst(h -> HEADER_METHOD_NAME.equals(h.name()));
int capabilities = 0;
Expand All @@ -86,14 +97,27 @@ else if (HEADER_METHOD_VALUE_GET.equals(method.value()))
{
capabilities = HEADER_METHOD_MASK_GET;
}
else if (HEADER_METHOD_VALUE_POST.equals(method.value()))
{
capabilities = HEADER_METHOD_MASK_POST;
}
else if (HEADER_METHOD_VALUE_PUT.equals(method.value()))
{
capabilities = HEADER_METHOD_MASK_PUT;
}
else if (HEADER_METHOD_VALUE_DELETE.equals(method.value()))
{
capabilities = HEADER_METHOD_MASK_DELETE;
}
}
HttpHeaderFW ifNotMatched = httpBeginEx.headers().matchFirst(h -> HEADER_IF_NONE_MATCH_NAME.equals(h.name()));
String16FW etag = new String16FW("");
if (ifNotMatched != null)
HttpHeaderFW tag = httpBeginEx.headers().matchFirst(h ->
HEADER_IF_MATCH_NAME.equals(h.name()) || HEADER_IF_NONE_MATCH_NAME.equals(h.name()));
if (tag != null)
{
String16FW value = ifNotMatched.value();
String16FW value = tag.value();
etag = etagRO.wrap(value.buffer(), value.offset(), value.limit());
}

HttpHeaderFW prefer = httpBeginEx.headers().matchFirst(h -> HEADER_PREFER_NAME.equals(h.name()));
int wait = 0;
if (prefer != null)
Expand Down
Loading

0 comments on commit 18e6cb4

Please sign in to comment.