|
18 | 18 | import static javax.lang.model.element.Modifier.FINAL;
|
19 | 19 | import static javax.lang.model.element.Modifier.PRIVATE;
|
20 | 20 | import static javax.lang.model.element.Modifier.PROTECTED;
|
| 21 | +import static software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait.hasRequestAlgorithmMember; |
| 22 | +import static software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait.hasResponseAlgorithms; |
21 | 23 |
|
22 | 24 | import com.squareup.javapoet.ClassName;
|
23 | 25 | import com.squareup.javapoet.CodeBlock;
|
|
59 | 61 | import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
|
60 | 62 | import software.amazon.awssdk.codegen.utils.AuthUtils;
|
61 | 63 | import software.amazon.awssdk.core.SdkPlugin;
|
| 64 | +import software.amazon.awssdk.core.checksums.RequestChecksumCalculation; |
| 65 | +import software.amazon.awssdk.core.checksums.RequestChecksumCalculationResolver; |
| 66 | +import software.amazon.awssdk.core.checksums.ResponseChecksumValidation; |
| 67 | +import software.amazon.awssdk.core.checksums.ResponseChecksumValidationResolver; |
62 | 68 | import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
|
63 | 69 | import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
|
64 | 70 | import software.amazon.awssdk.core.client.config.SdkClientOption;
|
@@ -157,6 +163,14 @@ public TypeSpec poetSpec() {
|
157 | 163 | builder.addMethod(authSchemesMethod());
|
158 | 164 | }
|
159 | 165 |
|
| 166 | + if (hasRequestAlgorithmMember(model)) { |
| 167 | + builder.addMethod(requestChecksumCalculationMethod()); |
| 168 | + } |
| 169 | + |
| 170 | + if (hasResponseAlgorithms(model)) { |
| 171 | + builder.addMethod(responseChecksumValidationMethod()); |
| 172 | + } |
| 173 | + |
160 | 174 | if (hasClientContextParams()) {
|
161 | 175 | model.getClientContextParams().forEach((n, m) -> {
|
162 | 176 | builder.addMethod(clientContextParamSetter(n, m));
|
@@ -189,6 +203,14 @@ public TypeSpec poetSpec() {
|
189 | 203 |
|
190 | 204 | endpointParamsKnowledgeIndex.resolveAccountIdEndpointModeMethod().ifPresent(builder::addMethod);
|
191 | 205 |
|
| 206 | + if (hasRequestAlgorithmMember(model)) { |
| 207 | + builder.addMethod(resolveRequestChecksumCalculationMethod()); |
| 208 | + } |
| 209 | + |
| 210 | + if (hasResponseAlgorithms(model)) { |
| 211 | + builder.addMethod(resolveResponseChecksumValidationMethod()); |
| 212 | + } |
| 213 | + |
192 | 214 | builder.addMethod(validateClientOptionsMethod());
|
193 | 215 |
|
194 | 216 | return builder.build();
|
@@ -487,6 +509,19 @@ private MethodSpec finalizeServiceConfigurationMethod() {
|
487 | 509 | }
|
488 | 510 | }
|
489 | 511 |
|
| 512 | + if (hasRequestAlgorithmMember(model) || hasResponseAlgorithms(model)) { |
| 513 | + builder.addStatement("$T clientConfig = config", SdkClientConfiguration.class); |
| 514 | + |
| 515 | + if (hasRequestAlgorithmMember(model)) { |
| 516 | + builder.addStatement("builder.lazyOption($T.REQUEST_CHECKSUM_CALCULATION, " |
| 517 | + + "c -> resolveRequestChecksumCalculation(clientConfig))", SdkClientOption.class); |
| 518 | + } |
| 519 | + if (hasResponseAlgorithms(model)) { |
| 520 | + builder.addStatement("builder.lazyOption($T.RESPONSE_CHECKSUM_VALIDATION, " |
| 521 | + + "c -> resolveResponseChecksumValidation(clientConfig))", SdkClientOption.class); |
| 522 | + } |
| 523 | + } |
| 524 | + |
490 | 525 | builder.addStatement("return builder.build()");
|
491 | 526 | return builder.build();
|
492 | 527 | }
|
@@ -576,6 +611,51 @@ private void mergeServiceConfiguration(MethodSpec.Builder builder, String client
|
576 | 611 | .addCode("}");
|
577 | 612 | }
|
578 | 613 |
|
| 614 | + boolean hasRequestAlgorithmMember = hasRequestAlgorithmMember(model); |
| 615 | + boolean hasResponseAlgorithms = hasResponseAlgorithms(model); |
| 616 | + if (model.getCustomizationConfig().getServiceConfig().hasChecksumValidationEnabledProperty() |
| 617 | + && (hasRequestAlgorithmMember || hasResponseAlgorithms)) { |
| 618 | + builder.addStatement("$T checksumValidationEnabled = serviceConfigBuilder.checksumValidationEnabled()", Boolean.class) |
| 619 | + .beginControlFlow("if (checksumValidationEnabled != null)"); |
| 620 | + if (hasRequestAlgorithmMember) { |
| 621 | + builder.addCode("$T.validState(config.option($T.REQUEST_CHECKSUM_CALCULATION) == null, ", |
| 622 | + Validate.class, SdkClientOption.class) |
| 623 | + .addStatement("\"Checksum behavior has been configured on both $L and the client/global level. Please " |
| 624 | + + "limit checksum behavior configuration to one location.\")", clientConfigClassName); |
| 625 | + } |
| 626 | + if (hasResponseAlgorithms) { |
| 627 | + builder.addCode("$T.validState(config.option($T.RESPONSE_CHECKSUM_VALIDATION) == null, ", |
| 628 | + Validate.class, SdkClientOption.class) |
| 629 | + .addStatement("\"Checksum behavior has been configured on both $L and the client/global level. Please " |
| 630 | + + "limit checksum behavior configuration to one location.\")", clientConfigClassName); |
| 631 | + } |
| 632 | + builder.beginControlFlow("if (checksumValidationEnabled)") |
| 633 | + .addCode("config = config.toBuilder()"); |
| 634 | + if (hasRequestAlgorithmMember) { |
| 635 | + builder.addCode(".option($T.REQUEST_CHECKSUM_CALCULATION, $T.WHEN_SUPPORTED)", |
| 636 | + SdkClientOption.class, RequestChecksumCalculation.class); |
| 637 | + } |
| 638 | + if (hasResponseAlgorithms) { |
| 639 | + builder.addCode(".option($T.RESPONSE_CHECKSUM_VALIDATION, $T.WHEN_SUPPORTED)", |
| 640 | + SdkClientOption.class, ResponseChecksumValidation.class); |
| 641 | + } |
| 642 | + builder.addStatement(".build()") |
| 643 | + .nextControlFlow("else") |
| 644 | + .addCode("config = config.toBuilder()"); |
| 645 | + |
| 646 | + if (hasRequestAlgorithmMember) { |
| 647 | + builder.addCode(".option($T.REQUEST_CHECKSUM_CALCULATION, $T.WHEN_REQUIRED)", |
| 648 | + SdkClientOption.class, RequestChecksumCalculation.class); |
| 649 | + } |
| 650 | + if (hasResponseAlgorithms) { |
| 651 | + builder.addCode(".option($T.RESPONSE_CHECKSUM_VALIDATION, $T.WHEN_REQUIRED)", |
| 652 | + SdkClientOption.class, ResponseChecksumValidation.class); |
| 653 | + } |
| 654 | + builder.addStatement(".build()") |
| 655 | + .endControlFlow() |
| 656 | + .endControlFlow(); |
| 657 | + } |
| 658 | + |
579 | 659 | builder.addStatement("$T finalServiceConfig = serviceConfigBuilder.build()", clientConfigClass);
|
580 | 660 |
|
581 | 661 | if (model.getCustomizationConfig().getServiceConfig().hasUseArnRegionProperty()) {
|
@@ -740,6 +820,28 @@ private MethodSpec putAuthSchemeMethod() {
|
740 | 820 | .build();
|
741 | 821 | }
|
742 | 822 |
|
| 823 | + private MethodSpec requestChecksumCalculationMethod() { |
| 824 | + return MethodSpec.methodBuilder("requestChecksumCalculation") |
| 825 | + .addModifiers(Modifier.PUBLIC) |
| 826 | + .returns(TypeVariableName.get("B")) |
| 827 | + .addParameter(RequestChecksumCalculation.class, "requestChecksumCalculation") |
| 828 | + .addStatement("clientConfiguration.option($T.REQUEST_CHECKSUM_CALCULATION, requestChecksumCalculation)", |
| 829 | + SdkClientOption.class) |
| 830 | + .addStatement("return thisBuilder()") |
| 831 | + .build(); |
| 832 | + } |
| 833 | + |
| 834 | + private MethodSpec responseChecksumValidationMethod() { |
| 835 | + return MethodSpec.methodBuilder("responseChecksumValidation") |
| 836 | + .addModifiers(Modifier.PUBLIC) |
| 837 | + .returns(TypeVariableName.get("B")) |
| 838 | + .addParameter(ResponseChecksumValidation.class, "responseChecksumValidation") |
| 839 | + .addStatement("clientConfiguration.option($T.RESPONSE_CHECKSUM_VALIDATION, responseChecksumValidation)", |
| 840 | + SdkClientOption.class) |
| 841 | + .addStatement("return thisBuilder()") |
| 842 | + .build(); |
| 843 | + } |
| 844 | + |
743 | 845 | private MethodSpec clientContextParamSetter(String name, ClientContextParam param) {
|
744 | 846 | String setterName = endpointRulesSpecUtils.paramMethodName(name);
|
745 | 847 | String keyName = model.getNamingStrategy().getEnumValueName(name);
|
@@ -854,6 +956,50 @@ private MethodSpec internalPluginsMethod() {
|
854 | 956 | return builder.build();
|
855 | 957 | }
|
856 | 958 |
|
| 959 | + private MethodSpec resolveRequestChecksumCalculationMethod() { |
| 960 | + |
| 961 | + MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveRequestChecksumCalculation") |
| 962 | + .addModifiers(PRIVATE) |
| 963 | + .addParameter(SdkClientConfiguration.class, "config") |
| 964 | + .returns(RequestChecksumCalculation.class); |
| 965 | + |
| 966 | + builder.addStatement("$T configuredChecksumCalculation = config.option($T.REQUEST_CHECKSUM_CALCULATION)", |
| 967 | + RequestChecksumCalculation.class, SdkClientOption.class); |
| 968 | + |
| 969 | + builder.beginControlFlow("if (configuredChecksumCalculation == null)"); |
| 970 | + builder.addCode("configuredChecksumCalculation = $T.create()", RequestChecksumCalculationResolver.class); |
| 971 | + builder.addCode(".profileFile(config.option($T.PROFILE_FILE_SUPPLIER))", SdkClientOption.class); |
| 972 | + builder.addCode(".profileName(config.option($T.PROFILE_NAME))", SdkClientOption.class); |
| 973 | + builder.addCode(".defaultChecksumCalculation($T.WHEN_SUPPORTED)", RequestChecksumCalculation.class); |
| 974 | + builder.addStatement(".resolve()"); |
| 975 | + builder.endControlFlow(); |
| 976 | + |
| 977 | + builder.addStatement("return configuredChecksumCalculation"); |
| 978 | + return builder.build(); |
| 979 | + } |
| 980 | + |
| 981 | + private MethodSpec resolveResponseChecksumValidationMethod() { |
| 982 | + |
| 983 | + MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveResponseChecksumValidation") |
| 984 | + .addModifiers(PRIVATE) |
| 985 | + .addParameter(SdkClientConfiguration.class, "config") |
| 986 | + .returns(ResponseChecksumValidation.class); |
| 987 | + |
| 988 | + builder.addStatement("$T configuredChecksumValidation = config.option($T.RESPONSE_CHECKSUM_VALIDATION)", |
| 989 | + ResponseChecksumValidation.class, SdkClientOption.class); |
| 990 | + |
| 991 | + builder.beginControlFlow("if (configuredChecksumValidation == null)"); |
| 992 | + builder.addCode("configuredChecksumValidation = $T.create()", ResponseChecksumValidationResolver.class); |
| 993 | + builder.addCode(".profileFile(config.option($T.PROFILE_FILE_SUPPLIER))", SdkClientOption.class); |
| 994 | + builder.addCode(".profileName(config.option($T.PROFILE_NAME))", SdkClientOption.class); |
| 995 | + builder.addCode(".defaultChecksumValidation($T.WHEN_SUPPORTED)", ResponseChecksumValidation.class); |
| 996 | + builder.addStatement(".resolve()"); |
| 997 | + builder.endControlFlow(); |
| 998 | + |
| 999 | + builder.addStatement("return configuredChecksumValidation"); |
| 1000 | + return builder.build(); |
| 1001 | + } |
| 1002 | + |
857 | 1003 | private String internalPluginClass(String internalPlugin) {
|
858 | 1004 | int openParenthesisIndex = internalPlugin.indexOf('(');
|
859 | 1005 | if (openParenthesisIndex == -1) {
|
|
0 commit comments