Skip to content

Commit

Permalink
2.x: Fix #42: streaming example fails to start
Browse files Browse the repository at this point in the history
* Fix #43: streaming example fails to start
  • Loading branch information
barchetta committed Apr 16, 2024
1 parent 213af58 commit 0455585
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 196 deletions.
8 changes: 4 additions & 4 deletions examples/webserver/streaming/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ to the size of the file being uploaded or downloaded.

## Build and run

```shell
```bash
mvn package
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
```bash
curl --data-binary "@large-file.bin" http://localhost:8080/upload
curl http://localhost:8080/download --output myfile.bin
```
File renamed without changes.
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 Expand Up @@ -58,4 +56,5 @@ public static void main(String[] args) {
System.out.println("Streaming service is 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

0 comments on commit 0455585

Please sign in to comment.