Skip to content

Request compression trait #4371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 5, 2023
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-1a64dc3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "feature",
"description": "Add support for RequestCompression trait to GZIP compress requests."
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public Map<String, OperationModel> constructOperations() {
operationModel.setEndpointTrait(op.getEndpoint());
operationModel.setHttpChecksumRequired(op.isHttpChecksumRequired());
operationModel.setHttpChecksum(op.getHttpChecksum());
operationModel.setRequestCompression(op.getRequestCompression());
operationModel.setStaticContextParams(op.getStaticContextParams());

Input input = op.getInput();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.codegen.compression;

import java.util.List;
import software.amazon.awssdk.annotations.SdkInternalApi;

/**
* Class to map the RequestCompression trait of an operation.
*/
@SdkInternalApi
public class RequestCompression {

private List<String> encodings;

public List<String> getEncodings() {
return encodings;
}

public void setEncodings(List<String> encodings) {
this.encodings = encodings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.codegen.checksum.HttpChecksum;
import software.amazon.awssdk.codegen.compression.RequestCompression;
import software.amazon.awssdk.codegen.docs.ClientType;
import software.amazon.awssdk.codegen.docs.DocConfiguration;
import software.amazon.awssdk.codegen.docs.OperationDocs;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class OperationModel extends DocumentationModel {

private HttpChecksum httpChecksum;

private RequestCompression requestCompression;

@JsonIgnore
private Map<String, StaticContextParam> staticContextParams;

Expand Down Expand Up @@ -309,6 +312,14 @@ public void setHttpChecksum(HttpChecksum httpChecksum) {
this.httpChecksum = httpChecksum;
}

public RequestCompression getRequestCompression() {
return requestCompression;
}

public void setRequestCompression(RequestCompression requestCompression) {
this.requestCompression = requestCompression;
}

public Map<String, StaticContextParam> getStaticContextParams() {
return staticContextParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.codegen.checksum.HttpChecksum;
import software.amazon.awssdk.codegen.compression.RequestCompression;
import software.amazon.awssdk.codegen.model.intermediate.EndpointDiscovery;

public class Operation {
Expand Down Expand Up @@ -52,6 +53,8 @@ public class Operation {

private HttpChecksum httpChecksum;

private RequestCompression requestCompression;

private Map<String, StaticContextParam> staticContextParams;

public String getName() {
Expand Down Expand Up @@ -189,6 +192,14 @@ public void setHttpChecksum(HttpChecksum httpChecksum) {
this.httpChecksum = httpChecksum;
}

public RequestCompression getRequestCompression() {
return requestCompression;
}

public void setRequestCompression(RequestCompression requestCompression) {
this.requestCompression = requestCompression;
}

public Map<String, StaticContextParam> getStaticContextParams() {
return staticContextParams;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.codegen.poet.client.traits.RequestCompressionTrait;
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.EventStreamSpecHelper;
import software.amazon.awssdk.core.SdkPojoBuilder;
Expand Down Expand Up @@ -187,7 +188,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, model));

if (opModel.hasStreamingInput()) {
codeBlock.add(".withRequestBody(requestBody)")
Expand Down Expand Up @@ -257,6 +259,7 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, model))
.add(".withInput($L)$L);",
opModel.getInput().getVariableName(), asyncResponseTransformerVariable(isStreaming, isRestJson, opModel));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.codegen.poet.client.traits.RequestCompressionTrait;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.http.HttpResponseHandler;
Expand Down Expand Up @@ -116,7 +117,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, intermediateModel));


if (opModel.hasStreamingInput()) {
Expand Down Expand Up @@ -151,7 +153,8 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(".withMetricCollector(apiCallMetricCollector)\n")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, intermediateModel));


builder.add(hostPrefixExpression(opModel) + asyncRequestBody + ".withInput($L)$L);",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.codegen.poet.client.traits.RequestCompressionTrait;
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.EventStreamSpecHelper;
import software.amazon.awssdk.core.SdkPojoBuilder;
Expand Down Expand Up @@ -135,7 +136,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(".withInput($L)", opModel.getInput().getVariableName())
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, model));


s3ArnableFields(opModel, model).ifPresent(codeBlock::add);
Expand Down Expand Up @@ -213,7 +215,8 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(asyncRequestBody(opModel))
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(RequestCompressionTrait.create(opModel, model));

s3ArnableFields(opModel, model).ifPresent(builder::add);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.codegen.poet.client.traits;

import com.squareup.javapoet.CodeBlock;
import java.util.List;
import java.util.stream.Collectors;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.internal.interceptor.trait.RequestCompression;

/**
* The logic for handling the "requestCompression" trait within the code generator.
*/
public class RequestCompressionTrait {

private RequestCompressionTrait() {
}

/**
* Generate a ".putExecutionAttribute(...)" code-block for the provided operation model. This should be used within the
* context of initializing {@link ClientExecutionParams}. If request compression is not required by the operation, this will
* return an empty code-block.
*/
public static CodeBlock create(OperationModel operationModel, IntermediateModel model) {
if (operationModel.getRequestCompression() == null) {
return CodeBlock.of("");
}

// TODO : remove once:
// 1) S3 checksum interceptors are moved to occur after CompressRequestStage
// 2) Transfer-Encoding:chunked is supported in S3
if (model.getMetadata().getServiceName().equals("S3")) {
throw new IllegalStateException("Request compression for S3 is not yet supported in the AWS SDK for Java.");
}

List<String> encodings = operationModel.getRequestCompression().getEncodings();

return CodeBlock.of(".putExecutionAttribute($T.REQUEST_COMPRESSION, "
+ "$T.builder().encodings($L).isStreaming($L).build())",
SdkInternalExecutionAttribute.class, RequestCompression.class,
encodings.stream().collect(Collectors.joining("\", \"", "\"", "\"")),
operationModel.hasStreamingInput());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
},
"authtype": "none"
},
"OperationWithRequestCompression": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"requestCompression": {
"encodings": ["gzip"]
}
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
},
"authtype": "none"
},
"OperationWithRequestCompression": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"requestCompression": {
"encodings": ["gzip"]
}
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
},
"httpChecksumRequired": true
},
"OperationWithRequestCompression": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"requestCompression": {
"encodings": ["gzip"]
}
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
},
"authtype": "none"
},
"OperationWithRequestCompression": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"requestCompression": {
"encodings": ["gzip"]
}
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import software.amazon.awssdk.services.json.model.JsonRequest;
import software.amazon.awssdk.services.json.model.OperationWithChecksumRequiredRequest;
import software.amazon.awssdk.services.json.model.OperationWithChecksumRequiredResponse;
import software.amazon.awssdk.services.json.model.OperationWithRequestCompressionRequest;
import software.amazon.awssdk.services.json.model.OperationWithRequestCompressionResponse;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithResultKeyRequest;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithResultKeyResponse;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithoutResultKeyRequest;
Expand Down Expand Up @@ -305,6 +307,33 @@ public CompletableFuture<OperationWithChecksumRequiredResponse> operationWithChe
return invokeOperation(operationWithChecksumRequiredRequest, request -> delegate.operationWithChecksumRequired(request));
}

/**
* Invokes the OperationWithRequestCompression operation asynchronously.
*
* @param operationWithRequestCompressionRequest
* @return A Java Future containing the result of the OperationWithRequestCompression operation returned by the
* service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample JsonAsyncClient.OperationWithRequestCompression
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/OperationWithRequestCompression"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<OperationWithRequestCompressionResponse> operationWithRequestCompression(
OperationWithRequestCompressionRequest operationWithRequestCompressionRequest) {
return invokeOperation(operationWithRequestCompressionRequest,
request -> delegate.operationWithRequestCompression(request));
}

/**
* Some paginated operation with result_key in paginators.json file
*
Expand Down Expand Up @@ -468,7 +497,7 @@ public <ReturnT> CompletableFuture<ReturnT> streamingInputOutputOperation(
StreamingInputOutputOperationRequest streamingInputOutputOperationRequest, AsyncRequestBody requestBody,
AsyncResponseTransformer<StreamingInputOutputOperationResponse, ReturnT> asyncResponseTransformer) {
return invokeOperation(streamingInputOutputOperationRequest,
request -> delegate.streamingInputOutputOperation(request, requestBody, asyncResponseTransformer));
request -> delegate.streamingInputOutputOperation(request, requestBody, asyncResponseTransformer));
}

/**
Expand Down
Loading