Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AsyncResponseTransformer.toBlockingInputStream. (#3562)
* Added AsyncResponseTransformer.toBlockingInputStream, allowing streaming operation responses to be read as if they're an InputStream. * Fix sonarcloud issue.
- Loading branch information
Showing
11 changed files
with
1,088 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"type": "feature", | ||
"description": "Added AsyncResponseTransformer.toBlockingInputStream, allowing streaming operation responses to be read as if they're an InputStream." | ||
} |
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
62 changes: 62 additions & 0 deletions
62
.../main/java/software/amazon/awssdk/core/internal/async/InputStreamResponseTransformer.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,62 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.core.internal.async; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.ResponseInputStream; | ||
import software.amazon.awssdk.core.SdkResponse; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
import software.amazon.awssdk.utils.async.InputStreamSubscriber; | ||
|
||
/** | ||
* A {@link AsyncResponseTransformer} that allows performing blocking reads on the response data. | ||
* <p> | ||
* Created with {@link AsyncResponseTransformer#toBlockingInputStream()}. | ||
*/ | ||
@SdkInternalApi | ||
public class InputStreamResponseTransformer<ResponseT extends SdkResponse> | ||
implements AsyncResponseTransformer<ResponseT, ResponseInputStream<ResponseT>> { | ||
|
||
private volatile CompletableFuture<ResponseInputStream<ResponseT>> future; | ||
private volatile ResponseT response; | ||
|
||
@Override | ||
public CompletableFuture<ResponseInputStream<ResponseT>> prepare() { | ||
CompletableFuture<ResponseInputStream<ResponseT>> result = new CompletableFuture<>(); | ||
this.future = result; | ||
return result; | ||
} | ||
|
||
@Override | ||
public void onResponse(ResponseT response) { | ||
this.response = response; | ||
} | ||
|
||
@Override | ||
public void onStream(SdkPublisher<ByteBuffer> publisher) { | ||
InputStreamSubscriber inputStreamSubscriber = new InputStreamSubscriber(); | ||
publisher.subscribe(inputStreamSubscriber); | ||
future.complete(new ResponseInputStream<>(response, inputStreamSubscriber)); | ||
} | ||
|
||
@Override | ||
public void exceptionOccurred(Throwable error) { | ||
future.completeExceptionally(error); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...t/java/software/amazon/awssdk/core/internal/async/InputStreamResponseTransformerTest.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,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.core.internal.async; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.core.ResponseInputStream; | ||
import software.amazon.awssdk.core.SdkResponse; | ||
import software.amazon.awssdk.core.async.SdkPublisher; | ||
import software.amazon.awssdk.core.protocol.VoidSdkResponse; | ||
import software.amazon.awssdk.utils.async.SimplePublisher; | ||
|
||
class InputStreamResponseTransformerTest { | ||
private SimplePublisher<ByteBuffer> publisher; | ||
private InputStreamResponseTransformer<SdkResponse> transformer; | ||
private SdkResponse response; | ||
private CompletableFuture<ResponseInputStream<SdkResponse>> resultFuture; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
publisher = new SimplePublisher<>(); | ||
transformer = new InputStreamResponseTransformer<>(); | ||
resultFuture = transformer.prepare(); | ||
response = VoidSdkResponse.builder().build(); | ||
|
||
transformer.onResponse(response); | ||
|
||
assertThat(resultFuture).isNotDone(); | ||
|
||
transformer.onStream(SdkPublisher.adapt(publisher)); | ||
|
||
assertThat(resultFuture).isCompleted(); | ||
assertThat(resultFuture.join().response()).isEqualTo(response); | ||
} | ||
|
||
@Test | ||
public void inputStreamReadsAreFromPublisher() throws IOException { | ||
InputStream stream = resultFuture.join(); | ||
|
||
publisher.send(ByteBuffer.wrap(new byte[] { 0, 1, 2 })); | ||
publisher.complete(); | ||
|
||
assertThat(stream.read()).isEqualTo(0); | ||
assertThat(stream.read()).isEqualTo(1); | ||
assertThat(stream.read()).isEqualTo(2); | ||
assertThat(stream.read()).isEqualTo(-1); | ||
} | ||
|
||
@Test | ||
public void inputStreamArrayReadsAreFromPublisher() throws IOException { | ||
InputStream stream = resultFuture.join(); | ||
|
||
publisher.send(ByteBuffer.wrap(new byte[] { 0, 1, 2 })); | ||
publisher.complete(); | ||
|
||
byte[] data = new byte[3]; | ||
assertThat(stream.read(data)).isEqualTo(3); | ||
|
||
assertThat(data[0]).isEqualTo((byte) 0); | ||
assertThat(data[1]).isEqualTo((byte) 1); | ||
assertThat(data[2]).isEqualTo((byte) 2); | ||
assertThat(stream.read(data)).isEqualTo(-1); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...t/src/test/java/software/amazon/awssdk/services/BlockingAsyncRequestResponseBodyTest.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,89 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.services; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Timeout; | ||
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; | ||
import software.amazon.awssdk.core.ResponseInputStream; | ||
import software.amazon.awssdk.core.async.AsyncResponseTransformer; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; | ||
import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationRequest; | ||
import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse; | ||
|
||
@Timeout(5) | ||
public class BlockingAsyncRequestResponseBodyTest { | ||
private final WireMockServer wireMock = new WireMockServer(0); | ||
private ProtocolRestJsonAsyncClient client; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
wireMock.start(); | ||
client = ProtocolRestJsonAsyncClient.builder() | ||
.region(Region.US_WEST_2) | ||
.credentialsProvider(AnonymousCredentialsProvider.create()) | ||
.endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void blockingResponseTransformer_readsRightValue() { | ||
wireMock.stubFor(post(anyUrl()).willReturn(aResponse().withStatus(200).withBody("hello"))); | ||
|
||
CompletableFuture<ResponseInputStream<StreamingOutputOperationResponse>> responseFuture = | ||
client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), | ||
AsyncResponseTransformer.toBlockingInputStream()); | ||
ResponseInputStream<StreamingOutputOperationResponse> responseStream = responseFuture.join(); | ||
|
||
assertThat(responseStream).asString(StandardCharsets.UTF_8).isEqualTo("hello"); | ||
assertThat(responseStream.response().sdkHttpResponse().statusCode()).isEqualTo(200); | ||
} | ||
|
||
@Test | ||
public void blockingResponseTransformer_abortCloseDoesNotThrow() throws IOException { | ||
wireMock.stubFor(post(anyUrl()).willReturn(aResponse().withStatus(200).withBody("hello"))); | ||
|
||
CompletableFuture<ResponseInputStream<StreamingOutputOperationResponse>> responseFuture = | ||
client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), | ||
AsyncResponseTransformer.toBlockingInputStream()); | ||
ResponseInputStream<StreamingOutputOperationResponse> responseStream = responseFuture.join(); | ||
responseStream.abort(); | ||
responseStream.close(); | ||
} | ||
|
||
@Test | ||
public void blockingResponseTransformer_closeDoesNotThrow() throws IOException { | ||
wireMock.stubFor(post(anyUrl()).willReturn(aResponse().withStatus(200).withBody("hello"))); | ||
|
||
CompletableFuture<ResponseInputStream<StreamingOutputOperationResponse>> responseFuture = | ||
client.streamingOutputOperation(StreamingOutputOperationRequest.builder().build(), | ||
AsyncResponseTransformer.toBlockingInputStream()); | ||
ResponseInputStream<StreamingOutputOperationResponse> responseStream = responseFuture.join(); | ||
responseStream.close(); | ||
} | ||
} |
Oops, something went wrong.