forked from apple/servicetalk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: There are no examples demonstrating serving response payload body from file.
- Loading branch information
1 parent
0a7f539
commit 659eaf0
Showing
10 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
apply plugin: "java" | ||
apply from: "../../gradle/idea.gradle" | ||
|
||
dependencies { | ||
implementation project(":servicetalk-http-netty") | ||
implementation project(":servicetalk-http-utils") | ||
|
||
runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion" | ||
} |
51 changes: 51 additions & 0 deletions
51
...alk-examples/http/files/src/main/java/io/servicetalk/examples/http/files/FilesClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.files; | ||
|
||
import io.servicetalk.http.api.HttpClient; | ||
import io.servicetalk.http.netty.HttpClients; | ||
import io.servicetalk.http.utils.TimeoutHttpRequesterFilter; | ||
|
||
import java.time.Duration; | ||
|
||
import static io.servicetalk.http.api.HttpSerializers.textSerializerUtf8; | ||
|
||
/** | ||
* Extends the async 'Hello World!' example to demonstrate use of timeout filters and timeout operators. If a single | ||
* timeout can be applied to all transactions then the timeout should be applied using the | ||
* {@link TimeoutHttpRequesterFilter}. If only some transactions require a timeout then the timeout should be applied | ||
* using a {@link io.servicetalk.concurrent.api.Single#timeout(Duration)} Single.timeout()} or a | ||
* {@link io.servicetalk.concurrent.api.Publisher#timeoutTerminal(Duration)} (Duration)} Publisher.timeoutTerminal()} | ||
* operator. | ||
*/ | ||
public final class FilesClient { | ||
public static void main(String[] args) throws Exception { | ||
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080) | ||
.build()) { | ||
// first request, with default timeout from HttpClient (this will succeed) | ||
client.request(client.get("/")) | ||
.whenOnError(System.err::println) | ||
.whenOnSuccess(resp -> { | ||
System.out.println(resp.toString((name, value) -> value)); | ||
System.out.println(resp.payloadBody(textSerializerUtf8())); | ||
}) | ||
// This example is demonstrating asynchronous execution, but needs to prevent the main thread from exiting | ||
// before the response has been processed. This isn't typical usage for an asynchronous API but is useful | ||
// for demonstration purposes. | ||
.toFuture().get(); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...alk-examples/http/files/src/main/java/io/servicetalk/examples/http/files/FilesServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.examples.http.files; | ||
|
||
import io.servicetalk.buffer.api.BufferAllocator; | ||
import io.servicetalk.http.api.StreamingHttpResponse; | ||
import io.servicetalk.http.netty.HttpServers; | ||
import io.servicetalk.transport.api.IoExecutor; | ||
|
||
import java.io.InputStream; | ||
|
||
import static io.servicetalk.concurrent.api.Publisher.from; | ||
import static io.servicetalk.concurrent.api.Publisher.fromInputStream; | ||
import static io.servicetalk.concurrent.api.Single.succeeded; | ||
import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE; | ||
import static io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8; | ||
|
||
/** | ||
* A simple server that serves content from a file via {@link InputStream}. Note reading from file into application | ||
* memory maybe blocking and this example uses the default execution strategy which consumes from the | ||
* {@link InputStream} on a non-{@link IoExecutor} thread to avoid blocking EventLoop threads. | ||
*/ | ||
public final class FilesServer { | ||
public static void main(String[] args) throws Exception { | ||
HttpServers.forPort(8080) | ||
.listenStreamingAndAwait((ctx, request, responseFactory) -> { | ||
// InputStream lifetime ownership is transferred to ServiceTalk (e.g. it will call close) because | ||
// we create a new InputStream per request and always pass it to ServiceTalk as the response payload | ||
// body (if not null). | ||
final InputStream responseStream = FilesServer.class.getClassLoader() | ||
.getResourceAsStream("response_payload.txt"); | ||
final BufferAllocator allocator = ctx.executionContext().bufferAllocator(); | ||
final StreamingHttpResponse response = responseStream == null ? | ||
responseFactory.notFound().payloadBody( | ||
from(allocator.fromAscii("file not found, please rebuild the project"))) : | ||
responseFactory.ok().payloadBody(fromInputStream(responseStream, allocator::wrap)); | ||
response.headers().set(CONTENT_TYPE, TEXT_PLAIN_UTF_8); | ||
return succeeded(response); | ||
}).awaitShutdown(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
servicetalk-examples/http/files/src/main/resources/log4j2.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright © 2024 Apple Inc. and the ServiceTalk project authors | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
<Configuration status="info"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d %30t [%-5level] %-30logger{1} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<!-- Prints server start and shutdown --> | ||
<Logger name="io.servicetalk.http.netty.NettyHttpServer" level="DEBUG"/> | ||
|
||
<!-- Prints default subscriber errors--> | ||
<Logger name="io.servicetalk.concurrent.api" level="DEBUG"/> | ||
|
||
<!-- Use `-Dservicetalk.logger.level=DEBUG` to change the root logger level via command line --> | ||
<Root level="${sys:servicetalk.logger.level:-INFO}"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
5 changes: 5 additions & 0 deletions
5
servicetalk-examples/http/files/src/main/resources/response_payload.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
response payload from file! | ||
|
||
warning: serving arbitrary files from your server maybe a security risk. You | ||
should take care if user input is used to infer file paths or names and ensure | ||
appropriate access control and auditing is in place. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
servicetalk-examples/http/jaxrs/src/main/resources/response_payload.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
response payload from file! | ||
|
||
warning: serving arbitrary files from your server maybe a security risk. You | ||
should take care if user input is used to infer file paths or names and ensure | ||
appropriate access control and auditing is in place. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters