|
| 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 static software.amazon.awssdk.core.internal.io.AwsChunkedInputStream.DEFAULT_CHUNK_SIZE; |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | +import java.util.concurrent.atomic.AtomicLong; |
| 25 | +import org.reactivestreams.Subscriber; |
| 26 | +import org.reactivestreams.Subscription; |
| 27 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 28 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 29 | +import software.amazon.awssdk.core.async.SdkPublisher; |
| 30 | +import software.amazon.awssdk.core.internal.compression.Compressor; |
| 31 | +import software.amazon.awssdk.utils.Validate; |
| 32 | +import software.amazon.awssdk.utils.async.DelegatingSubscriber; |
| 33 | +import software.amazon.awssdk.utils.async.FlatteningSubscriber; |
| 34 | +import software.amazon.awssdk.utils.builder.SdkBuilder; |
| 35 | + |
| 36 | +/** |
| 37 | + * Wrapper class to wrap an AsyncRequestBody. |
| 38 | + * This will chunk and compress the payload with the provided {@link Compressor}. |
| 39 | + */ |
| 40 | +@SdkInternalApi |
| 41 | +public class CompressionAsyncRequestBody implements AsyncRequestBody { |
| 42 | + |
| 43 | + private final AsyncRequestBody wrapped; |
| 44 | + private final Compressor compressor; |
| 45 | + private final int chunkSize; |
| 46 | + |
| 47 | + private CompressionAsyncRequestBody(DefaultBuilder builder) { |
| 48 | + this.wrapped = Validate.paramNotNull(builder.asyncRequestBody, "asyncRequestBody"); |
| 49 | + this.compressor = Validate.paramNotNull(builder.compressor, "compressor"); |
| 50 | + this.chunkSize = builder.chunkSize != null ? builder.chunkSize : DEFAULT_CHUNK_SIZE; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void subscribe(Subscriber<? super ByteBuffer> s) { |
| 55 | + Validate.notNull(s, "Subscription MUST NOT be null."); |
| 56 | + |
| 57 | + SdkPublisher<Iterable<ByteBuffer>> split = split(wrapped); |
| 58 | + SdkPublisher<ByteBuffer> flattening = flattening(split); |
| 59 | + flattening.map(compressor::compress).subscribe(s); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Optional<Long> contentLength() { |
| 64 | + return wrapped.contentLength(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String contentType() { |
| 69 | + return wrapped.contentType(); |
| 70 | + } |
| 71 | + |
| 72 | + private SdkPublisher<Iterable<ByteBuffer>> split(SdkPublisher<ByteBuffer> source) { |
| 73 | + return subscriber -> source.subscribe(new SplittingSubscriber(subscriber, chunkSize)); |
| 74 | + } |
| 75 | + |
| 76 | + private SdkPublisher<ByteBuffer> flattening(SdkPublisher<Iterable<ByteBuffer>> source) { |
| 77 | + return subscriber -> source.subscribe(new FlatteningSubscriber<>(subscriber)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return Builder instance to construct a {@link CompressionAsyncRequestBody}. |
| 82 | + */ |
| 83 | + public static Builder builder() { |
| 84 | + return new DefaultBuilder(); |
| 85 | + } |
| 86 | + |
| 87 | + public interface Builder extends SdkBuilder<CompressionAsyncRequestBody.Builder, CompressionAsyncRequestBody> { |
| 88 | + |
| 89 | + /** |
| 90 | + * Sets the AsyncRequestBody that will be wrapped. |
| 91 | + * @param asyncRequestBody |
| 92 | + * @return This builder for method chaining. |
| 93 | + */ |
| 94 | + Builder asyncRequestBody(AsyncRequestBody asyncRequestBody); |
| 95 | + |
| 96 | + /** |
| 97 | + * Sets the compressor to compress the request. |
| 98 | + * @param compressor |
| 99 | + * @return This builder for method chaining. |
| 100 | + */ |
| 101 | + Builder compressor(Compressor compressor); |
| 102 | + |
| 103 | + /** |
| 104 | + * Sets the chunk size. Default size is 128 * 1024. |
| 105 | + * @param chunkSize |
| 106 | + * @return This builder for method chaining. |
| 107 | + */ |
| 108 | + Builder chunkSize(Integer chunkSize); |
| 109 | + } |
| 110 | + |
| 111 | + private static final class DefaultBuilder implements Builder { |
| 112 | + |
| 113 | + private AsyncRequestBody asyncRequestBody; |
| 114 | + private Compressor compressor; |
| 115 | + private Integer chunkSize; |
| 116 | + |
| 117 | + @Override |
| 118 | + public CompressionAsyncRequestBody build() { |
| 119 | + return new CompressionAsyncRequestBody(this); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Builder asyncRequestBody(AsyncRequestBody asyncRequestBody) { |
| 124 | + this.asyncRequestBody = asyncRequestBody; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Builder compressor(Compressor compressor) { |
| 130 | + this.compressor = compressor; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Builder chunkSize(Integer chunkSize) { |
| 136 | + this.chunkSize = chunkSize; |
| 137 | + return this; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static final class SplittingSubscriber extends DelegatingSubscriber<ByteBuffer, Iterable<ByteBuffer>> { |
| 142 | + private final ChunkBuffer chunkBuffer; |
| 143 | + private final AtomicBoolean upstreamDone = new AtomicBoolean(false); |
| 144 | + private final AtomicLong downstreamDemand = new AtomicLong(); |
| 145 | + private final Object lock = new Object(); |
| 146 | + private volatile boolean sentFinalChunk = false; |
| 147 | + |
| 148 | + protected SplittingSubscriber(Subscriber<? super Iterable<ByteBuffer>> subscriber, int chunkSize) { |
| 149 | + super(subscriber); |
| 150 | + this.chunkBuffer = ChunkBuffer.builder() |
| 151 | + .bufferSize(chunkSize) |
| 152 | + .build(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void onSubscribe(Subscription s) { |
| 157 | + subscriber.onSubscribe(new Subscription() { |
| 158 | + @Override |
| 159 | + public void request(long n) { |
| 160 | + if (n <= 0) { |
| 161 | + throw new IllegalArgumentException("n > 0 required but it was " + n); |
| 162 | + } |
| 163 | + |
| 164 | + downstreamDemand.getAndAdd(n); |
| 165 | + |
| 166 | + if (upstreamDone.get()) { |
| 167 | + sendFinalChunk(); |
| 168 | + } else { |
| 169 | + s.request(n); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void cancel() { |
| 175 | + s.cancel(); |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onNext(ByteBuffer byteBuffer) { |
| 182 | + downstreamDemand.decrementAndGet(); |
| 183 | + Iterable<ByteBuffer> buffers = chunkBuffer.split(byteBuffer); |
| 184 | + subscriber.onNext(buffers); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void onComplete() { |
| 189 | + upstreamDone.compareAndSet(false, true); |
| 190 | + if (downstreamDemand.get() > 0) { |
| 191 | + sendFinalChunk(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public void onError(Throwable t) { |
| 197 | + upstreamDone.compareAndSet(false, true); |
| 198 | + super.onError(t); |
| 199 | + } |
| 200 | + |
| 201 | + private void sendFinalChunk() { |
| 202 | + synchronized (lock) { |
| 203 | + if (!sentFinalChunk) { |
| 204 | + sentFinalChunk = true; |
| 205 | + Optional<ByteBuffer> byteBuffer = chunkBuffer.getBufferedData(); |
| 206 | + byteBuffer.ifPresent(buffer -> subscriber.onNext(Collections.singletonList(buffer))); |
| 207 | + subscriber.onComplete(); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments