Skip to content

Support for new HttpChecksum trait for requests and responses #3038

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 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-bb17f86.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": "Support adding http checksum for requests and validation of http checksum for responses based on HttpChecksum traits defined in the model."
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public Map<String, OperationModel> constructOperations() {
operationModel.setEndpointDiscovery(op.getEndpointdiscovery());
operationModel.setEndpointTrait(op.getEndpoint());
operationModel.setHttpChecksumRequired(op.isHttpChecksumRequired());
operationModel.setHttpChecksum(op.getHttpChecksum());

Input input = op.getInput();
if (input != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.checksum;

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

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

private boolean requestChecksumRequired;

private String requestAlgorithmMember;

private String requestValidationModeMember;

private List<String> responseAlgorithms;

public boolean isRequestChecksumRequired() {
return requestChecksumRequired;
}

public void setRequestChecksumRequired(boolean requestChecksumRequired) {
this.requestChecksumRequired = requestChecksumRequired;
}

public String getRequestAlgorithmMember() {
return requestAlgorithmMember;
}

public void setRequestAlgorithmMember(String requestAlgorithmMember) {
this.requestAlgorithmMember = requestAlgorithmMember;
}

public String getRequestValidationModeMember() {
return requestValidationModeMember;
}

public void setRequestValidationModeMember(String requestValidationModeMember) {
this.requestValidationModeMember = requestValidationModeMember;
}

public List<String> getResponseAlgorithms() {
return responseAlgorithms;
}

public void setResponseAlgorithms(List<String> responseAlgorithms) {
this.responseAlgorithms = responseAlgorithms;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.ArrayList;
import java.util.List;
import software.amazon.awssdk.codegen.checksum.HttpChecksum;
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 @@ -66,6 +67,8 @@ public class OperationModel extends DocumentationModel {

private boolean httpChecksumRequired;

private HttpChecksum httpChecksum;

public String getOperationName() {
return operationName;
}
Expand Down Expand Up @@ -292,4 +295,12 @@ public boolean isHttpChecksumRequired() {
public void setHttpChecksumRequired(boolean httpChecksumRequired) {
this.httpChecksumRequired = httpChecksumRequired;
}

public HttpChecksum getHttpChecksum() {
return httpChecksum;
}

public void setHttpChecksum(HttpChecksum httpChecksum) {
this.httpChecksum = httpChecksum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.codegen.model.service;

import java.util.List;
import software.amazon.awssdk.codegen.checksum.HttpChecksum;
import software.amazon.awssdk.codegen.model.intermediate.EndpointDiscovery;

public class Operation {
Expand Down Expand Up @@ -48,6 +49,8 @@ public class Operation {

private boolean httpChecksumRequired;

private HttpChecksum httpChecksum;

public String getName() {
return name;
}
Expand Down Expand Up @@ -174,4 +177,12 @@ public boolean isHttpChecksumRequired() {
public void setHttpChecksumRequired(boolean httpChecksumRequired) {
this.httpChecksumRequired = httpChecksumRequired;
}

public HttpChecksum getHttpChecksum() {
return httpChecksum;
}

public void setHttpChecksum(HttpChecksum httpChecksum) {
this.httpChecksum = httpChecksum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
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 @@ -175,7 +176,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(discoveredEndpoint(opModel))
.add(".withInput($L)\n", opModel.getInput().getVariableName())
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel));
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));

if (opModel.hasStreamingInput()) {
codeBlock.add(".withRequestBody(requestBody)")
Expand Down Expand Up @@ -242,6 +244,7 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(discoveredEndpoint(opModel))
.add(asyncRequestBody)
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.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 @@ -29,6 +29,7 @@
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
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 @@ -111,7 +112,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(discoveredEndpoint(opModel))
.add(".withInput($L)", opModel.getInput().getVariableName())
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel));
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));

if (opModel.hasStreamingInput()) {
return codeBlock.add(".withRequestBody(requestBody)")
Expand Down Expand Up @@ -142,7 +144,8 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(".withResponseHandler(responseHandler)\n")
.add(".withErrorResponseHandler(errorResponseHandler)\n")
.add(".withMetricCollector(apiCallMetricCollector)\n")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel));
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));

builder.add(hostPrefixExpression(opModel) + asyncRequestBody + ".withInput($L)$L);",
opModel.getInput().getVariableName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.poet.PoetExtensions;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
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 @@ -130,7 +131,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
hostPrefixExpression(opModel) +
discoveredEndpoint(opModel))
.add(".withInput($L)", opModel.getInput().getVariableName())
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel));
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));

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

Expand Down Expand Up @@ -204,7 +206,8 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
builder.add(hostPrefixExpression(opModel))
.add(".withMetricCollector(apiCallMetricCollector)\n")
.add(asyncRequestBody(opModel))
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel));
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.MemberModel;
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.interceptor.trait.HttpChecksum;

/**
* The logic for handling the Flexible "httpChecksum" trait within the code generator.
*/
public class HttpChecksumTrait {

private HttpChecksumTrait() {
}


/**
* Generate a ".putExecutionAttribute(...)" code-block for the provided operation model. This should be used within the
* context of initializing {@link ClientExecutionParams}.
* If HTTP checksums are not required by the operation, this will return an empty code-block.
*/
public static CodeBlock create(OperationModel operationModel) {

if (operationModel.getHttpChecksum() != null) {

CodeBlock.Builder codeBuilder = CodeBlock.builder();
codeBuilder.add(CodeBlock.of(".putExecutionAttribute($T.HTTP_CHECKSUM, $T.builder().requestChecksumRequired($L)",
SdkInternalExecutionAttribute.class, HttpChecksum.class,
operationModel.getHttpChecksum().isRequestChecksumRequired()));

addFluentGetterToBuilder(operationModel,
codeBuilder,
operationModel.getHttpChecksum().getRequestAlgorithmMember(),
"requestAlgorithm");

addFluentGetterToBuilder(operationModel,
codeBuilder,
operationModel.getHttpChecksum().getRequestValidationModeMember(),
"requestValidationMode");

// loop to get the comma separated strings \"literals\"
List<String> responseAlgorithms = operationModel.getHttpChecksum().getResponseAlgorithms();
if (responseAlgorithms != null && !responseAlgorithms.isEmpty()) {

codeBuilder.add(CodeBlock.of(".responseAlgorithms("))
.add(
CodeBlock.of("$L",
responseAlgorithms.stream().collect(
Collectors.joining("\", \"", "\"", "\""))))
.add(CodeBlock.of(")"));
}
codeBuilder.add(CodeBlock.of(".isRequestStreaming($L)", operationModel.getInputShape().isHasStreamingMember()));
return codeBuilder.add(CodeBlock.of(".build())")).build();
} else {
return CodeBlock.of("");
}
}

private static void addFluentGetterToBuilder(OperationModel operationModel, CodeBlock.Builder codeBuilder,
String requestValidationModeMemberInModel, String memberBuilderName) {

if (requestValidationModeMemberInModel != null) {
MemberModel requestValidationModeMember =
operationModel.getInputShape().tryFindMemberModelByC2jName(
requestValidationModeMemberInModel, true);

if (requestValidationModeMember == null) {
throw new IllegalStateException(requestValidationModeMemberInModel
+ " is not a member in "
+ operationModel.getInputShape().getShapeName());
}
codeBuilder.add(".$L($N.$N())",
memberBuilderName,
operationModel.getInput().getVariableName(),
requestValidationModeMember.getFluentGetterMethodName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import software.amazon.awssdk.codegen.poet.ClientTestModels;

public class PoetClientFunctionalTests {

@Test
public void asyncClientClass() throws Exception {
AsyncClientClass asyncClientClass = createAsyncClientClass(ClientTestModels.restJsonServiceModels());
Expand All @@ -41,7 +40,7 @@ public void asyncClientInterface() throws Exception {
@Test
public void simpleMethodsIntegClass() throws Exception {
ClientSimpleMethodsIntegrationTests simpleMethodsClass = new ClientSimpleMethodsIntegrationTests(
ClientTestModels.restJsonServiceModels());
ClientTestModels.restJsonServiceModels());
assertThat(simpleMethodsClass, generatesTo("test-simple-methods-integ-class.java"));
}

Expand Down Expand Up @@ -104,14 +103,14 @@ public void syncClientEndpointDiscovery() throws Exception {
@Test
public void asyncClientEndpointDiscovery() throws Exception {
ClassSpec asyncClientEndpointDiscovery = new AsyncClientClass(
GeneratorTaskParams.create(ClientTestModels.endpointDiscoveryModels(), "sources/", "tests/"));
GeneratorTaskParams.create(ClientTestModels.endpointDiscoveryModels(), "sources/", "tests/"));
assertThat(asyncClientEndpointDiscovery, generatesTo("test-endpoint-discovery-async.java"));
}

@Test
public void asyncClientCustomServiceMetaData() throws Exception {
ClassSpec asyncClientCustomServiceMetaData = new AsyncClientClass(
GeneratorTaskParams.create(ClientTestModels.customContentTypeModels(), "sources/", "tests/"));
GeneratorTaskParams.create(ClientTestModels.customContentTypeModels(), "sources/", "tests/"));
assertThat(asyncClientCustomServiceMetaData, generatesTo("test-customservicemetadata-async.java"));
}

Expand All @@ -120,4 +119,5 @@ public void syncClientCustomServiceMetaData() throws Exception {
ClassSpec syncClientCustomServiceMetaData = createSyncClientClass(ClientTestModels.customContentTypeModels());
assertThat(syncClientCustomServiceMetaData, generatesTo("test-customservicemetadata-sync.java"));
}

}
Loading