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

3.x: Fix #43: streaming example fails to start #45

Merged
merged 3 commits into from
Apr 16, 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
2 changes: 1 addition & 1 deletion 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`:
```bash
curl --data-binary "@target/classes/large-file.bin" http://localhost:8080/upload
curl --data-binary "@large-file.bin" http://localhost:8080/upload
curl http://localhost:8080/download
```
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,45 +18,32 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.net.URL;
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 ScheduledExecutorService executor = ScheduledThreadPoolSupplier.create().get();
private final Path filePath;
private volatile Path filePath;

StreamingService() {
URL resource = getClass().getResource(LARGE_FILE_RESOURCE);
if (resource == null) {
throw new IllegalStateException("Resource not found: " + LARGE_FILE_RESOURCE);
}
try {
filePath = Paths.get(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
Expand All @@ -68,23 +55,32 @@ public void update(Routing.Rules routingRules) {
private void upload(ServerRequest request, ServerResponse response) {
LOGGER.info("Entering upload ... " + Thread.currentThread());
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());
.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().contentLength(length);
response.send(IoMulti.multiFromByteChannelBuilder(newByteChannel(filePath))
.executor(executor)
.build());
LOGGER.info("Exiting download ...");
.build()
.map(DataChunk::create));
LOGGER.info("Exiting download. Returning " + length + " bytes...");
}

@SuppressWarnings("SameParameterValue")
Expand Down
Loading
Loading