Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support file write in binding-filesystem & binding-http-filesystem #1300

Merged
merged 13 commits into from
Nov 4, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Files;
Expand Down Expand Up @@ -73,6 +74,7 @@ public final class FileSystemServerFactory implements FileSystemStreamFactory
private static final OctetsFW EMPTY_EXTENSION = new OctetsFW().wrap(new UnsafeBuffer(new byte[0]), 0, 0);

private static final int READ_PAYLOAD_MASK = 1 << FileSystemCapabilities.READ_PAYLOAD.ordinal();
private static final int WRITE_PAYLOAD_MASK = 1 << FileSystemCapabilities.WRITE_PAYLOAD.ordinal();
private static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
private static final int TIMEOUT_EXPIRED_SIGNAL_ID = 0;
public static final int FILE_CHANGED_SIGNAL_ID = 1;
Expand Down Expand Up @@ -410,6 +412,20 @@ private InputStream getInputStream()
return input;
}

private OutputStream getOutputStream()
{
OutputStream output = null;
try
{
output = canWritePayload(capabilities) ? Files.newOutputStream(resolvedPath, symlinks) : null;
}
catch (IOException ex)
{
// reject
}
return output;
}

private String calculateHash(
byte[] input,
int offset,
Expand Down Expand Up @@ -862,4 +878,10 @@ private boolean canReadPayload(
{
return (capabilities & READ_PAYLOAD_MASK) != 0;
}

private boolean canWritePayload(
int capabilities)
{
return (capabilities & WRITE_PAYLOAD_MASK) != 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,14 @@ public void shouldReceiveClientReadAbort() throws Exception
{
k3po.finish();
}

@Test
@Configuration("server.yaml")
@Specification({
"${app}/write.file.payload/client",
})
public void shouldWriteFilePayloadOnly() throws Exception
{
k3po.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ scope filesystem
{
READ_PAYLOAD,
READ_EXTENSION,
READ_CHANGES
READ_CHANGES,
WRITE_PAYLOAD,
WRITE_EXTENSION,
WRITE_CHANGES
}

scope stream
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

connect "zilla://streams/app0"
option zilla:window 8192
option zilla:transmission "half-duplex"

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD")
.path("index.html")
.type("text/html")
.payloadSize(77)
.tag("f2c912a30f38a124c3249f8e802e0d90")
.build()}
connected

write "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"
write flush

write close

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD")
.path("index.html")
.build()}

read closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

accept "zilla://streams/app0"
option zilla:window 8192
option zilla:transmission "half-duplex"
accepted

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD")
.path("index.html")
.type("text/html")
.payloadSize(77)
.tag("f2c912a30f38a124c3249f8e802e0d90")
.build()}
connected

read "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"

read closed

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD")
.path("index.html")
.build()}

write flush

write close
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,14 @@ public void shouldReceiveClientReadAbort() throws Exception
{
k3po.finish();
}

@Test
@Specification({
"${app}/write.file.payload/client",
"${app}/write.file.payload/server",
})
public void shouldWriteFilePayloadOnly() throws Exception
{
k3po.finish();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

connect "zilla://streams/filesystem0"
option zilla:window 8192
option zilla:transmission "half-duplex"

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD", "WRITE_EXTENSION")
.path("index.html")
.type("text/html")
.payloadSize(77)
.tag("c7183509522eb56e5cf927a3b2e8c15a")
.build()}
connected

write "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"
write flush

write close

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD", "WRITE_EXTENSION")
.path("index.html")
.build()}

read closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

accept "zilla://streams/filesystem0"
option zilla:window 8192
option zilla:transmission "half-duplex"
accepted

read zilla:begin.ext ${filesystem:matchBeginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD", "WRITE_EXTENSION")
.path("index.html")
.type("text/html")
.payloadSize(77)
.tag("c7183509522eb56e5cf927a3b2e8c15a")
.build()}
connected

read "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"

read closed

write zilla:begin.ext ${filesystem:beginEx()
.typeId(zilla:id("filesystem"))
.capabilities("WRITE_PAYLOAD", "WRITE_EXTENSION")
.path("index.html")
.build()}
write flush

write close
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

connect "zilla://streams/http0"
option zilla:window 8192
option zilla:transmission "half-duplex"

write zilla:begin.ext ${http:beginEx()
.typeId(zilla:id("http"))
.header(":method", "PUT")
.header(":scheme", "https")
.header(":authority", "example.com:9090")
.header(":path", "/index.html")
.build()}

connected

write "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"

write flush

write close

read zilla:begin.ext ${http:beginEx()
.typeId(zilla:id("http"))
.header(":status", "200")
.build()}

read closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright 2021-2023 Aklivity Inc
#
# Licensed under the Aklivity Community License (the "License"); you may not use
# this file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://www.aklivity.io/aklivity-community-license/
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#

accept "zilla://streams/http0"
option zilla:window 8192
option zilla:transmission "half-duplex"
accepted

read zilla:begin.ext ${http:beginEx()
.typeId(zilla:id("http"))
.header(":method", "PUT")
.header(":scheme", "https")
.header(":authority", "example.com:9090")
.header(":path", "/index.html")
.build()}

connected

read "<html>\n"
"<head><title>Welcome</title></head>\n"
"<body>Hello, world</body>\n"
"</html>\n"

read closed

write zilla:begin.ext ${http:beginEx()
.typeId(zilla:id("http"))
.header(":status", "200")
.build()}

write flush

write close
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,13 @@ public void shouldReceiveClientSentAbort() throws Exception
{
k3po.finish();
}

@Test
@Specification({
"${filesystem}/client.write.file/client",
"${filesystem}/client.write.file/server"})
public void shouldReceiveClientWriteFile() throws Exception
{
k3po.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,13 @@ public void shouldReceiveServerSentFlush() throws Exception
{
k3po.finish();
}

@Test
@Specification({
"${http}/client.write.file/client",
"${http}/client.write.file/server"})
public void shouldReceiveClientWriteFile() throws Exception
{
k3po.finish();
}
}
Loading