1 /* 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 16 package software.amazon.awssdk.core.internal.async; 17 18 import java.nio.ByteBuffer; 19 import java.util.concurrent.CompletableFuture; 20 import software.amazon.awssdk.annotations.SdkInternalApi; 21 import software.amazon.awssdk.core.SdkResponse; 22 import software.amazon.awssdk.core.async.AsyncResponseTransformer; 23 import software.amazon.awssdk.core.async.ResponsePublisher; 24 import software.amazon.awssdk.core.async.SdkPublisher; 25 26 /** 27 * Transforms a {@link ResponseT} and {@link ByteBuffer} {@link SdkPublisher} into a {@link ResponsePublisher}. 28 * 29 * @param <ResponseT> Pojo response type. 30 * @see AsyncResponseTransformer#toPublisher() 31 */ 32 @SdkInternalApi 33 public final class PublisherAsyncResponseTransformer<ResponseT extends SdkResponse> 34 implements AsyncResponseTransformer<ResponseT, ResponsePublisher<ResponseT>> { 35 36 private volatile CompletableFuture<ResponsePublisher<ResponseT>> future; 37 private volatile ResponseT response; 38 39 @Override prepare()40 public CompletableFuture<ResponsePublisher<ResponseT>> prepare() { 41 CompletableFuture<ResponsePublisher<ResponseT>> f = new CompletableFuture<>(); 42 this.future = f; 43 return f; 44 } 45 46 @Override onResponse(ResponseT response)47 public void onResponse(ResponseT response) { 48 this.response = response; 49 } 50 51 @Override onStream(SdkPublisher<ByteBuffer> publisher)52 public void onStream(SdkPublisher<ByteBuffer> publisher) { 53 future.complete(new ResponsePublisher<>(response, publisher)); 54 } 55 56 @Override exceptionOccurred(Throwable error)57 public void exceptionOccurred(Throwable error) { 58 future.completeExceptionally(error); 59 } 60 } 61