/
BaseClientBuilderClass.java
608 lines (517 loc) · 31.6 KB
/
BaseClientBuilderClass.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* 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.builder;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PROTECTED;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider;
import software.amazon.awssdk.auth.token.credentials.aws.DefaultAwsTokenProvider;
import software.amazon.awssdk.auth.token.signer.aws.BearerTokenSigner;
import software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.model.service.AuthType;
import software.amazon.awssdk.codegen.model.service.ClientContextParam;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.codegen.utils.BearerAuthUtils;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.endpointdiscovery.providers.DefaultEndpointDiscoveryProviderChain;
import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.signer.Signer;
import software.amazon.awssdk.http.Protocol;
import software.amazon.awssdk.http.SdkHttpConfigurationOption;
import software.amazon.awssdk.protocols.query.interceptor.QueryParametersToBodyInterceptor;
import software.amazon.awssdk.utils.AttributeMap;
import software.amazon.awssdk.utils.CollectionUtils;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.Validate;
public class BaseClientBuilderClass implements ClassSpec {
private final IntermediateModel model;
private final ClassName builderInterfaceName;
private final ClassName builderClassName;
private final String basePackage;
private final EndpointRulesSpecUtils endpointRulesSpecUtils;
public BaseClientBuilderClass(IntermediateModel model) {
this.model = model;
this.basePackage = model.getMetadata().getFullClientPackageName();
this.builderInterfaceName = ClassName.get(basePackage, model.getMetadata().getBaseBuilderInterface());
this.builderClassName = ClassName.get(basePackage, model.getMetadata().getBaseBuilder());
this.endpointRulesSpecUtils = new EndpointRulesSpecUtils(model);
}
@Override
public TypeSpec poetSpec() {
TypeSpec.Builder builder =
PoetUtils.createClassBuilder(builderClassName)
.addModifiers(Modifier.ABSTRACT)
.addAnnotation(SdkInternalApi.class)
.addTypeVariable(PoetUtils.createBoundedTypeVariableName("B", builderInterfaceName, "B", "C"))
.addTypeVariable(TypeVariableName.get("C"))
.superclass(PoetUtils.createParameterizedTypeName(AwsDefaultClientBuilder.class, "B", "C"))
.addJavadoc("Internal base class for {@link $T} and {@link $T}.",
ClassName.get(basePackage, model.getMetadata().getSyncBuilder()),
ClassName.get(basePackage, model.getMetadata().getAsyncBuilder()));
// Only services that require endpoint discovery for at least one of their operations get a default value of
// 'true'
if (model.getEndpointOperation().isPresent()) {
builder.addField(FieldSpec.builder(boolean.class, "endpointDiscoveryEnabled")
.addModifiers(PROTECTED)
.initializer(resolveDefaultEndpointDiscovery() ? "true" : "false")
.build());
}
builder.addMethod(serviceEndpointPrefixMethod());
builder.addMethod(serviceNameMethod());
builder.addMethod(mergeServiceDefaultsMethod());
mergeInternalDefaultsMethod().ifPresent(builder::addMethod);
builder.addMethod(finalizeServiceConfigurationMethod());
defaultAwsAuthSignerMethod().ifPresent(builder::addMethod);
builder.addMethod(signingNameMethod());
if (endpointRulesSpecUtils.isEndpointRulesEnabled()) {
builder.addMethod(defaultEndpointProviderMethod());
}
if (hasClientContextParams() && endpointRulesSpecUtils.isEndpointRulesEnabled()) {
model.getClientContextParams().forEach((n, m) -> {
builder.addMethod(clientContextParamSetter(n, m));
});
}
if (model.getCustomizationConfig().getServiceConfig().getClassName() != null) {
builder.addMethod(setServiceConfigurationMethod())
.addMethod(beanStyleSetServiceConfigurationMethod());
}
if (BearerAuthUtils.usesBearerAuth(model)) {
builder.addMethod(defaultBearerTokenProviderMethod());
builder.addMethod(defaultTokenAuthSignerMethod());
}
addServiceHttpConfigIfNeeded(builder, model);
return builder.build();
}
private boolean resolveDefaultEndpointDiscovery() {
return model.getEndpointOperation()
.map(OperationModel::isEndpointCacheRequired)
.orElse(false);
}
private MethodSpec signingNameMethod() {
return MethodSpec.methodBuilder("signingName")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(String.class)
.addCode("return $S;", model.getMetadata().getSigningName())
.build();
}
private Optional<MethodSpec> defaultAwsAuthSignerMethod() {
return awsAuthSignerDefinitionMethodBody().map(body -> MethodSpec.methodBuilder("defaultSigner")
.returns(Signer.class)
.addModifiers(PRIVATE)
.addCode(body)
.build());
}
private MethodSpec serviceEndpointPrefixMethod() {
return MethodSpec.methodBuilder("serviceEndpointPrefix")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(String.class)
.addCode("return $S;", model.getMetadata().getEndpointPrefix())
.build();
}
private MethodSpec serviceNameMethod() {
return MethodSpec.methodBuilder("serviceName")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(String.class)
.addCode("return $S;", model.getMetadata().getServiceName())
.build();
}
private MethodSpec mergeServiceDefaultsMethod() {
boolean crc32FromCompressedDataEnabled = model.getCustomizationConfig().isCalculateCrc32FromCompressedData();
MethodSpec.Builder builder = MethodSpec.methodBuilder("mergeServiceDefaults")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(SdkClientConfiguration.class)
.addParameter(SdkClientConfiguration.class, "config")
.addCode("return config.merge(c -> c");
if (endpointRulesSpecUtils.isEndpointRulesEnabled()) {
builder.addCode(".option($T.ENDPOINT_PROVIDER, defaultEndpointProvider())", SdkClientOption.class);
}
if (defaultAwsAuthSignerMethod().isPresent()) {
builder.addCode(".option($T.SIGNER, defaultSigner())\n", SdkAdvancedClientOption.class);
}
builder.addCode(".option($T.CRC32_FROM_COMPRESSED_DATA_ENABLED, $L)\n",
SdkClientOption.class, crc32FromCompressedDataEnabled);
String clientConfigClassName = model.getCustomizationConfig().getServiceConfig().getClassName();
if (StringUtils.isNotBlank(clientConfigClassName)) {
builder.addCode(".option($T.SERVICE_CONFIGURATION, $T.builder().build())\n",
SdkClientOption.class, ClassName.bestGuess(clientConfigClassName));
}
if (BearerAuthUtils.usesBearerAuth(model)) {
builder.addCode(".option($T.TOKEN_PROVIDER, defaultTokenProvider())\n", AwsClientOption.class);
builder.addCode(".option($T.TOKEN_SIGNER, defaultTokenSigner())", SdkAdvancedClientOption.class);
}
builder.addCode(");");
return builder.build();
}
private Optional<MethodSpec> mergeInternalDefaultsMethod() {
String userAgent = model.getCustomizationConfig().getUserAgent();
RetryMode defaultRetryMode = model.getCustomizationConfig().getDefaultRetryMode();
// If none of the options are customized, then we do not need to bother overriding the method
if (userAgent == null && defaultRetryMode == null) {
return Optional.empty();
}
MethodSpec.Builder builder = MethodSpec.methodBuilder("mergeInternalDefaults")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(SdkClientConfiguration.class)
.addParameter(SdkClientConfiguration.class, "config")
.addCode("return config.merge(c -> {\n");
if (userAgent != null) {
builder.addCode("c.option($T.INTERNAL_USER_AGENT, $S);\n",
SdkClientOption.class, userAgent);
}
if (defaultRetryMode != null) {
builder.addCode("c.option($T.DEFAULT_RETRY_MODE, $T.$L);\n",
SdkClientOption.class, RetryMode.class, defaultRetryMode.name());
}
builder.addCode("});\n");
return Optional.of(builder.build());
}
private MethodSpec finalizeServiceConfigurationMethod() {
String requestHandlerDirectory = Utils.packageToDirectory(model.getMetadata().getFullClientPackageName());
String requestHandlerPath = String.format("%s/execution.interceptors", requestHandlerDirectory);
MethodSpec.Builder builder = MethodSpec.methodBuilder("finalizeServiceConfiguration")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(SdkClientConfiguration.class)
.addParameter(SdkClientConfiguration.class, "config");
// Initialize configuration values
builder.addStatement("$T endpointInterceptors = new $T<>()",
ParameterizedTypeName.get(List.class, ExecutionInterceptor.class),
ArrayList.class);
if (endpointRulesSpecUtils.isEndpointRulesEnabled()) {
builder.addStatement("endpointInterceptors.add(new $T())", endpointRulesSpecUtils.resolverInterceptorName());
builder.addStatement("endpointInterceptors.add(new $T())", endpointRulesSpecUtils.authSchemesInterceptorName());
builder.addStatement("endpointInterceptors.add(new $T())", endpointRulesSpecUtils.requestModifierInterceptorName());
}
builder.addCode("$1T interceptorFactory = new $1T();\n", ClasspathInterceptorChainFactory.class)
.addCode("$T<$T> interceptors = interceptorFactory.getInterceptors($S);\n",
List.class, ExecutionInterceptor.class, requestHandlerPath);
builder.addStatement("$T additionalInterceptors = new $T<>()",
ParameterizedTypeName.get(List.class,
ExecutionInterceptor.class),
ArrayList.class);
if (model.getMetadata().isQueryProtocol()) {
builder.addStatement("additionalInterceptors.add(new $T())", QueryParametersToBodyInterceptor.class);
}
builder.addStatement("interceptors = $T.mergeLists(endpointInterceptors, interceptors)",
CollectionUtils.class);
builder.addStatement("interceptors = $T.mergeLists(interceptors, additionalInterceptors)",
CollectionUtils.class);
builder.addCode("interceptors = $T.mergeLists(interceptors, config.option($T.EXECUTION_INTERCEPTORS));\n",
CollectionUtils.class, SdkClientOption.class);
if (model.getEndpointOperation().isPresent()) {
builder.beginControlFlow("if (!endpointDiscoveryEnabled)")
.addStatement("$1T chain = new $1T(config)", DefaultEndpointDiscoveryProviderChain.class)
.addStatement("endpointDiscoveryEnabled = chain.resolveEndpointDiscovery()")
.endControlFlow();
}
String clientConfigClassName = model.getCustomizationConfig().getServiceConfig().getClassName();
if (StringUtils.isNotBlank(clientConfigClassName)) {
mergeServiceConfiguration(builder, clientConfigClassName);
}
if (model.getCustomizationConfig().useGlobalEndpoint()) {
builder.addStatement("$T resolver = new UseGlobalEndpointResolver(config)",
ClassName.get("software.amazon.awssdk.services.s3.internal.endpoints",
"UseGlobalEndpointResolver"));
}
// Update configuration
builder.addCode("return config.toBuilder()\n");
if (model.getCustomizationConfig().getServiceConfig().hasDualstackProperty()) {
builder.addCode(".option($T.DUALSTACK_ENDPOINT_ENABLED, finalServiceConfig.dualstackEnabled())",
AwsClientOption.class);
}
if (model.getCustomizationConfig().getServiceConfig().hasFipsProperty()) {
builder.addCode(".option($T.FIPS_ENDPOINT_ENABLED, finalServiceConfig.fipsModeEnabled())", AwsClientOption.class);
}
if (model.getEndpointOperation().isPresent()) {
builder.addCode(".option($T.ENDPOINT_DISCOVERY_ENABLED, endpointDiscoveryEnabled)\n",
SdkClientOption.class);
}
builder.addCode(".option($1T.EXECUTION_INTERCEPTORS, interceptors)", SdkClientOption.class);
if (StringUtils.isNotBlank(model.getCustomizationConfig().getCustomRetryPolicy())) {
builder.addCode(".option($1T.RETRY_POLICY, $2T.resolveRetryPolicy(config))",
SdkClientOption.class,
PoetUtils.classNameFromFqcn(model.getCustomizationConfig().getCustomRetryPolicy()));
}
if (StringUtils.isNotBlank(clientConfigClassName)) {
builder.addCode(".option($T.SERVICE_CONFIGURATION, finalServiceConfig)", SdkClientOption.class);
}
if (model.getCustomizationConfig().useGlobalEndpoint()) {
builder.addCode(".option($1T.USE_GLOBAL_ENDPOINT, resolver.resolve(config.option($1T.AWS_REGION)))",
AwsClientOption.class);
}
if (hasClientContextParams()) {
builder.addCode(".option($T.CLIENT_CONTEXT_PARAMS, clientContextParams.build())", SdkClientOption.class);
}
builder.addCode(".build();");
return builder.build();
}
private void mergeServiceConfiguration(MethodSpec.Builder builder, String clientConfigClassName) {
ClassName clientConfigClass = ClassName.bestGuess(clientConfigClassName);
builder.addCode("$1T.Builder serviceConfigBuilder = (($1T) config.option($2T.SERVICE_CONFIGURATION)).toBuilder();" +
"serviceConfigBuilder.profileFile(serviceConfigBuilder.profileFile() "
+ "!= null ? serviceConfigBuilder.profileFile() : config.option($2T.PROFILE_FILE));" +
"serviceConfigBuilder.profileName(serviceConfigBuilder.profileName() "
+ "!= null ? serviceConfigBuilder.profileName() : config.option($2T.PROFILE_NAME));",
clientConfigClass, SdkClientOption.class);
if (model.getCustomizationConfig().getServiceConfig().hasDualstackProperty()) {
builder.addCode("if (serviceConfigBuilder.dualstackEnabled() != null) {")
.addCode(" $T.validState(config.option($T.DUALSTACK_ENDPOINT_ENABLED) == null, \"Dualstack has been "
+ "configured on both $L and the client/global level. Please limit dualstack configuration to "
+ "one location.\");",
Validate.class, AwsClientOption.class, clientConfigClassName)
.addCode("} else {")
.addCode(" serviceConfigBuilder.dualstackEnabled(config.option($T.DUALSTACK_ENDPOINT_ENABLED));",
AwsClientOption.class)
.addCode("}");
}
if (model.getCustomizationConfig().getServiceConfig().hasFipsProperty()) {
builder.addCode("if (serviceConfigBuilder.fipsModeEnabled() != null) {")
.addCode(" $T.validState(config.option($T.FIPS_ENDPOINT_ENABLED) == null, \"Fips has been "
+ "configured on both $L and the client/global level. Please limit fips configuration to "
+ "one location.\");",
Validate.class, AwsClientOption.class, clientConfigClassName)
.addCode("} else {")
.addCode(" serviceConfigBuilder.fipsModeEnabled(config.option($T.FIPS_ENDPOINT_ENABLED));",
AwsClientOption.class)
.addCode("}");
}
if (model.getCustomizationConfig().getServiceConfig().hasUseArnRegionProperty()) {
builder.addCode("if (serviceConfigBuilder.useArnRegionEnabled() != null) {")
.addCode(" $T.validState(clientContextParams.get($T.USE_ARN_REGION) == null, \"UseArnRegion has been "
+ "configured on both $L and the client/global level. Please limit UseArnRegion configuration to "
+ "one location.\");",
Validate.class, endpointRulesSpecUtils.clientContextParamsName(), clientConfigClassName)
.addCode("} else {")
.addCode(" serviceConfigBuilder.useArnRegionEnabled(clientContextParams.get($T.USE_ARN_REGION));",
endpointRulesSpecUtils.clientContextParamsName())
.addCode("}");
}
if (model.getCustomizationConfig().getServiceConfig().hasMultiRegionEnabledProperty()) {
builder.addCode("if (serviceConfigBuilder.multiRegionEnabled() != null) {")
.addCode(" $T.validState(clientContextParams.get($T.DISABLE_MULTI_REGION_ACCESS_POINTS) == null, "
+ "\"DisableMultiRegionAccessPoints has been configured on both $L and the client/global level. "
+ "Please limit DisableMultiRegionAccessPoints configuration to one location.\");",
Validate.class, endpointRulesSpecUtils.clientContextParamsName(), clientConfigClassName)
.addCode("} else if (clientContextParams.get($T.DISABLE_MULTI_REGION_ACCESS_POINTS) != null) {",
endpointRulesSpecUtils.clientContextParamsName())
.addCode(" serviceConfigBuilder.multiRegionEnabled(!clientContextParams.get($T"
+ ".DISABLE_MULTI_REGION_ACCESS_POINTS));",
endpointRulesSpecUtils.clientContextParamsName())
.addCode("}");
}
if (model.getCustomizationConfig().getServiceConfig().hasForcePathTypeEnabledProperty()) {
builder.addCode("if (serviceConfigBuilder.pathStyleAccessEnabled() != null) {")
.addCode(" $T.validState(clientContextParams.get($T.FORCE_PATH_STYLE) == null, "
+ "\"ForcePathStyle has been configured on both $L and the client/global level. "
+ "Please limit ForcePathStyle configuration to one location.\");",
Validate.class, endpointRulesSpecUtils.clientContextParamsName(), clientConfigClassName)
.addCode("} else {")
.addCode(" serviceConfigBuilder.pathStyleAccessEnabled(clientContextParams.get($T"
+ ".FORCE_PATH_STYLE));",
endpointRulesSpecUtils.clientContextParamsName())
.addCode("}");
}
if (model.getCustomizationConfig().getServiceConfig().hasAccelerateModeEnabledProperty()) {
builder.addCode("if (serviceConfigBuilder.accelerateModeEnabled() != null) {")
.addCode(" $T.validState(clientContextParams.get($T.ACCELERATE) == null, "
+ "\"Accelerate has been configured on both $L and the client/global level. "
+ "Please limit Accelerate configuration to one location.\");",
Validate.class, endpointRulesSpecUtils.clientContextParamsName(), clientConfigClassName)
.addCode("} else {")
.addCode(" serviceConfigBuilder.accelerateModeEnabled(clientContextParams.get($T"
+ ".ACCELERATE));",
endpointRulesSpecUtils.clientContextParamsName())
.addCode("}");
}
builder.addStatement("$T finalServiceConfig = serviceConfigBuilder.build()", clientConfigClass);
if (model.getCustomizationConfig().getServiceConfig().hasUseArnRegionProperty()) {
builder.addCode(
CodeBlock.builder()
.addStatement("clientContextParams.put($T.USE_ARN_REGION, finalServiceConfig.useArnRegionEnabled())",
endpointRulesSpecUtils.clientContextParamsName())
.build());
}
if (model.getCustomizationConfig().getServiceConfig().hasMultiRegionEnabledProperty()) {
builder.addCode(
CodeBlock.builder()
.addStatement("clientContextParams.put($T.DISABLE_MULTI_REGION_ACCESS_POINTS, !finalServiceConfig"
+ ".multiRegionEnabled())",
endpointRulesSpecUtils.clientContextParamsName())
.build());
}
if (model.getCustomizationConfig().getServiceConfig().hasForcePathTypeEnabledProperty()) {
builder.addCode(CodeBlock.builder()
.addStatement("clientContextParams.put($T.FORCE_PATH_STYLE, finalServiceConfig"
+ ".pathStyleAccessEnabled())",
endpointRulesSpecUtils.clientContextParamsName())
.build());
}
if (model.getCustomizationConfig().getServiceConfig().hasAccelerateModeEnabledProperty()) {
builder.addCode(CodeBlock.builder()
.addStatement("clientContextParams.put($T.ACCELERATE, finalServiceConfig"
+ ".accelerateModeEnabled())",
endpointRulesSpecUtils.clientContextParamsName())
.build());
}
}
private MethodSpec setServiceConfigurationMethod() {
ClassName serviceConfiguration = ClassName.get(basePackage,
model.getCustomizationConfig().getServiceConfig().getClassName());
return MethodSpec.methodBuilder("serviceConfiguration")
.addModifiers(Modifier.PUBLIC)
.returns(TypeVariableName.get("B"))
.addParameter(serviceConfiguration, "serviceConfiguration")
.addStatement("clientConfiguration.option($T.SERVICE_CONFIGURATION, serviceConfiguration)",
SdkClientOption.class)
.addStatement("return thisBuilder()")
.build();
}
private MethodSpec beanStyleSetServiceConfigurationMethod() {
ClassName serviceConfiguration = ClassName.get(basePackage,
model.getCustomizationConfig().getServiceConfig().getClassName());
return MethodSpec.methodBuilder("setServiceConfiguration")
.addModifiers(Modifier.PUBLIC)
.addParameter(serviceConfiguration, "serviceConfiguration")
.addStatement("serviceConfiguration(serviceConfiguration)")
.build();
}
private void addServiceHttpConfigIfNeeded(TypeSpec.Builder builder, IntermediateModel model) {
String serviceDefaultFqcn = model.getCustomizationConfig().getServiceSpecificHttpConfig();
boolean supportsH2 = model.getMetadata().supportsH2();
if (serviceDefaultFqcn != null || supportsH2) {
builder.addMethod(serviceSpecificHttpConfigMethod(serviceDefaultFqcn, supportsH2));
}
}
private MethodSpec serviceSpecificHttpConfigMethod(String serviceDefaultFqcn, boolean supportsH2) {
return MethodSpec.methodBuilder("serviceHttpConfig")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(AttributeMap.class)
.addCode(serviceSpecificHttpConfigMethodBody(serviceDefaultFqcn, supportsH2))
.build();
}
private CodeBlock serviceSpecificHttpConfigMethodBody(String serviceDefaultFqcn, boolean supportsH2) {
CodeBlock.Builder builder = CodeBlock.builder();
if (serviceDefaultFqcn != null) {
builder.addStatement("$T result = $T.defaultHttpConfig()",
AttributeMap.class,
PoetUtils.classNameFromFqcn(model.getCustomizationConfig().getServiceSpecificHttpConfig()));
} else {
builder.addStatement("$1T result = $1T.empty()", AttributeMap.class);
}
if (supportsH2) {
builder.addStatement("return result.merge(AttributeMap.builder()"
+ ".put($T.PROTOCOL, $T.HTTP2)"
+ ".build())",
SdkHttpConfigurationOption.class, Protocol.class);
} else {
builder.addStatement("return result");
}
return builder.build();
}
private Optional<CodeBlock> awsAuthSignerDefinitionMethodBody() {
AuthType authType = model.getMetadata().getAuthType();
switch (authType) {
case V4:
return Optional.of(v4SignerDefinitionMethodBody());
case S3:
case S3V4:
return Optional.of(s3SignerDefinitionMethodBody());
case BEARER:
return Optional.empty();
default:
throw new UnsupportedOperationException("Unsupported signer type: " + authType);
}
}
private CodeBlock v4SignerDefinitionMethodBody() {
return CodeBlock.of("return $T.create();", Aws4Signer.class);
}
private CodeBlock s3SignerDefinitionMethodBody() {
return CodeBlock.of("return $T.create();\n",
ClassName.get("software.amazon.awssdk.auth.signer", "AwsS3V4Signer"));
}
private MethodSpec defaultEndpointProviderMethod() {
return MethodSpec.methodBuilder("defaultEndpointProvider")
.addModifiers(PRIVATE)
.returns(endpointRulesSpecUtils.providerInterfaceName())
.addStatement("return $T.defaultProvider()", endpointRulesSpecUtils.providerInterfaceName())
.build();
}
private MethodSpec clientContextParamSetter(String name, ClientContextParam param) {
String setterName = endpointRulesSpecUtils.paramMethodName(name);
String keyName = model.getNamingStrategy().getEnumValueName(name);
TypeName type = endpointRulesSpecUtils.toJavaType(param.getType());
return MethodSpec.methodBuilder(setterName)
.addModifiers(Modifier.PUBLIC)
.returns(TypeVariableName.get("B"))
.addParameter(type, setterName)
.addStatement("clientContextParams.put($T.$N, $N)", endpointRulesSpecUtils.clientContextParamsName(),
keyName, setterName)
.addStatement("return thisBuilder()")
.build();
}
private MethodSpec defaultBearerTokenProviderMethod() {
return MethodSpec.methodBuilder("defaultTokenProvider")
.returns(SdkTokenProvider.class)
.addModifiers(PRIVATE)
.addStatement("return $T.create()", DefaultAwsTokenProvider.class)
.build();
}
private MethodSpec defaultTokenAuthSignerMethod() {
return MethodSpec.methodBuilder("defaultTokenSigner")
.returns(Signer.class)
.addModifiers(PRIVATE)
.addStatement("return $T.create()", BearerTokenSigner.class)
.build();
}
@Override
public ClassName className() {
return builderClassName;
}
private boolean hasClientContextParams() {
Map<String, ClientContextParam> clientContextParams = model.getClientContextParams();
return clientContextParams != null && !clientContextParams.isEmpty();
}
}