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

2.x: Fix #44: streaming example fails to start #46

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/webserver/streaming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ java -jar target/helidon-examples-webserver-streaming.jar

Upload a file and download it back with `curl`:
```shell
curl --data-binary "@target/classes/large-file.bin" http://localhost:8080/upload
curl http://localhost:8080/download
curl --data-binary "@large-file.bin" http://localhost:8080/upload
curl http://localhost:8080/download --output myfile.bin
```
9 changes: 7 additions & 2 deletions examples/webserver/streaming/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-test-support</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
public class Main {

static final String LARGE_FILE_RESOURCE = "/large-file.bin";

private Main() {
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,87 @@

package io.helidon.webserver.examples.streaming;

import java.net.URISyntaxException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;

import io.helidon.common.configurable.ScheduledThreadPoolSupplier;
import io.helidon.common.http.DataChunk;
import io.helidon.common.http.Http;
import io.helidon.common.reactive.IoMulti;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;

import static io.helidon.webserver.examples.streaming.Main.LARGE_FILE_RESOURCE;

/**
* StreamingService class. Uses a {@code Subscriber<RequestChunk>} and a
* {@code Publisher<ResponseChunk>} for uploading and downloading files.
*/
public class StreamingService implements Service {
private static final Logger LOGGER = Logger.getLogger(StreamingService.class.getName());

private final Path filePath;
private final ScheduledExecutorService executor = ScheduledThreadPoolSupplier.create().get();
private volatile Path filePath;

StreamingService() {
try {
filePath = Paths.get(getClass().getResource(LARGE_FILE_RESOURCE).toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
public void update(Routing.Rules routingRules) {
routingRules.get("/download", this::download)
.post("/upload", this::upload);
.post("/upload", this::upload);
}

private void upload(ServerRequest request, ServerResponse response) {
LOGGER.info("Entering upload ... " + Thread.currentThread());
request.content().subscribe(new ServerFileWriter(response));
Path tempFilePath = createTempFile("large-file", ".tmp");
filePath = tempFilePath;
LOGGER.info("Storing upload as " + tempFilePath);
request.content()
.map(DataChunk::data)
.flatMapIterable(Arrays::asList)
.to(IoMulti.writeToFile(tempFilePath)
.executor(executor)
.build())
.thenRun(() -> response.status(Http.Status.OK_200).send());
LOGGER.info("Exiting upload ...");
}

private void download(ServerRequest request, ServerResponse response) {
LOGGER.info("Entering download ..." + Thread.currentThread());
if (filePath == null) {
LOGGER.warning("No file to download.");
response.status(Http.Status.BAD_REQUEST_400).send("No file to download. Please upload file first.");
return;
}
long length = filePath.toFile().length();
response.headers().add("Content-Length", String.valueOf(length));
response.send(new ServerFileReader(filePath));
LOGGER.info("Exiting download ...");
response.headers().contentLength(length);
response.send(IoMulti.multiFromByteChannelBuilder(newByteChannel(filePath))
.executor(executor)
.build()
.map(DataChunk::create));
LOGGER.info("Exiting download. Returning " + length + " bytes...");
}

@SuppressWarnings("SameParameterValue")
private static Path createTempFile(String prefix, String suffix) {
try {
return Files.createTempFile(prefix, suffix);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}

private static ReadableByteChannel newByteChannel(Path path) {
try {
return Files.newByteChannel(path);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}
Loading