Skip to content

Commit 6005427

Browse files
authored
Request compression interceptor and non-streaming compression (#4124)
* Request compression interceptor and non-streaming compression * Refactoring * Refactoring * Refactoring and implement sync streaming compression * Refactoring * Refactoring * Refactoring and remove sync compression updates
1 parent 95b5a0c commit 6005427

File tree

5 files changed

+467
-0
lines changed

5 files changed

+467
-0
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/client/builder/SdkDefaultClientBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import software.amazon.awssdk.core.internal.interceptor.HttpChecksumInHeaderInterceptor;
7878
import software.amazon.awssdk.core.internal.interceptor.HttpChecksumRequiredInterceptor;
7979
import software.amazon.awssdk.core.internal.interceptor.HttpChecksumValidationInterceptor;
80+
import software.amazon.awssdk.core.internal.interceptor.RequestCompressionInterceptor;
8081
import software.amazon.awssdk.core.internal.interceptor.SyncHttpChecksumInTrailerInterceptor;
8182
import software.amazon.awssdk.core.retry.RetryMode;
8283
import software.amazon.awssdk.core.retry.RetryPolicy;
@@ -444,6 +445,7 @@ private List<ExecutionInterceptor> resolveExecutionInterceptors(SdkClientConfigu
444445
*/
445446
private List<ExecutionInterceptor> sdkInterceptors() {
446447
return Collections.unmodifiableList(Arrays.asList(
448+
new RequestCompressionInterceptor(),
447449
new HttpChecksumRequiredInterceptor(),
448450
new SyncHttpChecksumInTrailerInterceptor(),
449451
new HttpChecksumValidationInterceptor(),
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.compression;
17+
18+
import java.util.EnumSet;
19+
import java.util.Map;
20+
import java.util.Set;
21+
import software.amazon.awssdk.annotations.SdkInternalApi;
22+
import software.amazon.awssdk.utils.internal.EnumUtils;
23+
24+
/**
25+
* The supported compression algorithms for operations with the requestCompression trait. Each supported algorithm will have an
26+
* {@link Compressor} implementation.
27+
*/
28+
@SdkInternalApi
29+
public enum CompressionType {
30+
31+
GZIP("gzip"),
32+
33+
UNKNOWN_TO_SDK_VERSION(null);
34+
35+
private static final Map<String, CompressionType> VALUE_MAP = EnumUtils.uniqueIndex(
36+
CompressionType.class, CompressionType::toString);
37+
38+
private final String value;
39+
40+
CompressionType(String value) {
41+
this.value = value;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return String.valueOf(value);
47+
}
48+
49+
/**
50+
* Use this in place of valueOf to convert the raw string into the enum value.
51+
*
52+
* @param value
53+
* real value
54+
* @return SupportedEncodings corresponding to the value
55+
*/
56+
public static CompressionType fromValue(String value) {
57+
if (value == null) {
58+
return null;
59+
}
60+
return VALUE_MAP.getOrDefault(value, UNKNOWN_TO_SDK_VERSION);
61+
}
62+
63+
/**
64+
* Use this in place of {@link #values()} to return a {@link Set} of all values known to the SDK. This will return
65+
* all known enum values except {@link #UNKNOWN_TO_SDK_VERSION}.
66+
*
67+
* @return a {@link Set} of known {@link CompressionType}s
68+
*/
69+
public static Set<CompressionType> knownValues() {
70+
Set<CompressionType> knownValues = EnumSet.allOf(CompressionType.class);
71+
knownValues.remove(UNKNOWN_TO_SDK_VERSION);
72+
return knownValues;
73+
}
74+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.compression;
17+
18+
import java.io.InputStream;
19+
import java.nio.ByteBuffer;
20+
import org.reactivestreams.Publisher;
21+
import software.amazon.awssdk.annotations.SdkPublicApi;
22+
import software.amazon.awssdk.core.internal.compression.GzipCompressor;
23+
import software.amazon.awssdk.core.internal.interceptor.RequestCompressionInterceptor;
24+
25+
/**
26+
* Interface for compressors to be used by {@link RequestCompressionInterceptor} to compress requests.
27+
*/
28+
@SdkPublicApi
29+
public interface Compressor {
30+
31+
/**
32+
* The compression algorithm type.
33+
*/
34+
String compressorType();
35+
36+
/**
37+
* Compress an {@link InputStream}.
38+
*/
39+
InputStream compress(InputStream inputStream);
40+
41+
/**
42+
* Compress an async stream.
43+
*/
44+
Publisher<ByteBuffer> compressAsyncStream(Publisher<ByteBuffer> publisher);
45+
46+
/**
47+
* Maps the {@link CompressionType} to its corresponding {@link Compressor}.
48+
* TODO: Update mappings here when additional compressors are supported in the future
49+
*/
50+
static Compressor forCompressorType(CompressionType compressionType) {
51+
switch (compressionType) {
52+
case GZIP:
53+
return new GzipCompressor();
54+
default:
55+
throw new IllegalArgumentException("The compresssion type " + compressionType + "does not have an implemenation"
56+
+ " of Compressor.");
57+
}
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.compression;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.ByteBuffer;
23+
import java.util.zip.GZIPOutputStream;
24+
import org.reactivestreams.Publisher;
25+
import software.amazon.awssdk.annotations.SdkInternalApi;
26+
import software.amazon.awssdk.core.compression.Compressor;
27+
import software.amazon.awssdk.core.exception.SdkClientException;
28+
import software.amazon.awssdk.utils.IoUtils;
29+
30+
@SdkInternalApi
31+
public final class GzipCompressor implements Compressor {
32+
33+
private static final String COMPRESSOR_TYPE = "gzip";
34+
35+
@Override
36+
public String compressorType() {
37+
return COMPRESSOR_TYPE;
38+
}
39+
40+
@Override
41+
public InputStream compress(InputStream inputStream) {
42+
try {
43+
byte[] content = IoUtils.toByteArray(inputStream);
44+
ByteArrayOutputStream compressedOutputStream = new ByteArrayOutputStream();
45+
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedOutputStream);
46+
gzipOutputStream.write(content);
47+
gzipOutputStream.close();
48+
49+
return new ByteArrayInputStream(compressedOutputStream.toByteArray());
50+
} catch (IOException e) {
51+
throw SdkClientException.create(e.getMessage(), e);
52+
}
53+
}
54+
55+
@Override
56+
public Publisher<ByteBuffer> compressAsyncStream(Publisher<ByteBuffer> publisher) {
57+
//TODO
58+
throw new UnsupportedOperationException();
59+
}
60+
}

0 commit comments

Comments
 (0)