|
| 1 | +package software.amazon.awssdk.services.h2; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.function.Consumer; |
| 7 | +import software.amazon.awssdk.annotations.Generated; |
| 8 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 9 | +import software.amazon.awssdk.auth.signer.Aws4Signer; |
| 10 | +import software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder; |
| 11 | +import software.amazon.awssdk.awscore.client.config.AwsClientOption; |
| 12 | +import software.amazon.awssdk.awscore.endpoint.AwsClientEndpointProvider; |
| 13 | +import software.amazon.awssdk.awscore.retry.AwsRetryStrategy; |
| 14 | +import software.amazon.awssdk.core.SdkPlugin; |
| 15 | +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; |
| 16 | +import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; |
| 17 | +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
| 18 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 19 | +import software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory; |
| 20 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
| 21 | +import software.amazon.awssdk.core.retry.RetryMode; |
| 22 | +import software.amazon.awssdk.core.signer.Signer; |
| 23 | +import software.amazon.awssdk.http.Protocol; |
| 24 | +import software.amazon.awssdk.http.ProtocolNegotiation; |
| 25 | +import software.amazon.awssdk.http.SdkHttpConfigurationOption; |
| 26 | +import software.amazon.awssdk.identity.spi.IdentityProvider; |
| 27 | +import software.amazon.awssdk.identity.spi.IdentityProviders; |
| 28 | +import software.amazon.awssdk.regions.ServiceMetadataAdvancedOption; |
| 29 | +import software.amazon.awssdk.retries.api.RetryStrategy; |
| 30 | +import software.amazon.awssdk.services.h2.endpoints.H2EndpointProvider; |
| 31 | +import software.amazon.awssdk.services.h2.endpoints.internal.H2RequestSetEndpointInterceptor; |
| 32 | +import software.amazon.awssdk.services.h2.endpoints.internal.H2ResolveEndpointInterceptor; |
| 33 | +import software.amazon.awssdk.services.h2.internal.H2ServiceClientConfigurationBuilder; |
| 34 | +import software.amazon.awssdk.utils.AttributeMap; |
| 35 | +import software.amazon.awssdk.utils.CollectionUtils; |
| 36 | +import software.amazon.awssdk.utils.Validate; |
| 37 | + |
| 38 | +/** |
| 39 | + * Internal base class for {@link DefaultH2ClientBuilder} and {@link DefaultH2AsyncClientBuilder}. |
| 40 | + */ |
| 41 | +@Generated("software.amazon.awssdk:codegen") |
| 42 | +@SdkInternalApi |
| 43 | +abstract class DefaultH2BaseClientBuilder<B extends H2BaseClientBuilder<B, C>, C> extends AwsDefaultClientBuilder<B, C> { |
| 44 | + @Override |
| 45 | + protected final String serviceEndpointPrefix() { |
| 46 | + return "h2-service"; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected final String serviceName() { |
| 51 | + return "H2"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected final SdkClientConfiguration mergeServiceDefaults(SdkClientConfiguration config) { |
| 56 | + return config.merge(c -> c.option(SdkClientOption.ENDPOINT_PROVIDER, defaultEndpointProvider()) |
| 57 | + .option(SdkAdvancedClientOption.SIGNER, defaultSigner()) |
| 58 | + .option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, false)); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientConfiguration config) { |
| 63 | + List<ExecutionInterceptor> endpointInterceptors = new ArrayList<>(); |
| 64 | + endpointInterceptors.add(new H2ResolveEndpointInterceptor()); |
| 65 | + endpointInterceptors.add(new H2RequestSetEndpointInterceptor()); |
| 66 | + ClasspathInterceptorChainFactory interceptorFactory = new ClasspathInterceptorChainFactory(); |
| 67 | + List<ExecutionInterceptor> interceptors = interceptorFactory |
| 68 | + .getInterceptors("software/amazon/awssdk/services/h2/execution.interceptors"); |
| 69 | + List<ExecutionInterceptor> additionalInterceptors = new ArrayList<>(); |
| 70 | + interceptors = CollectionUtils.mergeLists(endpointInterceptors, interceptors); |
| 71 | + interceptors = CollectionUtils.mergeLists(interceptors, additionalInterceptors); |
| 72 | + interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS)); |
| 73 | + SdkClientConfiguration.Builder builder = config.toBuilder(); |
| 74 | + builder.lazyOption(SdkClientOption.IDENTITY_PROVIDERS, c -> { |
| 75 | + IdentityProviders.Builder result = IdentityProviders.builder(); |
| 76 | + IdentityProvider<?> credentialsIdentityProvider = c.get(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER); |
| 77 | + if (credentialsIdentityProvider != null) { |
| 78 | + result.putIdentityProvider(credentialsIdentityProvider); |
| 79 | + } |
| 80 | + return result.build(); |
| 81 | + }); |
| 82 | + builder.option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors); |
| 83 | + builder.lazyOptionIfAbsent( |
| 84 | + SdkClientOption.CLIENT_ENDPOINT_PROVIDER, |
| 85 | + c -> AwsClientEndpointProvider |
| 86 | + .builder() |
| 87 | + .serviceEndpointOverrideEnvironmentVariable("AWS_ENDPOINT_URL_H2_SERVICE") |
| 88 | + .serviceEndpointOverrideSystemProperty("aws.endpointUrlH2") |
| 89 | + .serviceProfileProperty("h2_service") |
| 90 | + .serviceEndpointPrefix(serviceEndpointPrefix()) |
| 91 | + .defaultProtocol("https") |
| 92 | + .region(c.get(AwsClientOption.AWS_REGION)) |
| 93 | + .profileFile(c.get(SdkClientOption.PROFILE_FILE_SUPPLIER)) |
| 94 | + .profileName(c.get(SdkClientOption.PROFILE_NAME)) |
| 95 | + .putAdvancedOption(ServiceMetadataAdvancedOption.DEFAULT_S3_US_EAST_1_REGIONAL_ENDPOINT, |
| 96 | + c.get(ServiceMetadataAdvancedOption.DEFAULT_S3_US_EAST_1_REGIONAL_ENDPOINT)) |
| 97 | + .dualstackEnabled(c.get(AwsClientOption.DUALSTACK_ENDPOINT_ENABLED)) |
| 98 | + .fipsEnabled(c.get(AwsClientOption.FIPS_ENDPOINT_ENABLED)).build()); |
| 99 | + return builder.build(); |
| 100 | + } |
| 101 | + |
| 102 | + private Signer defaultSigner() { |
| 103 | + return Aws4Signer.create(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + protected final String signingName() { |
| 108 | + return "h2-service"; |
| 109 | + } |
| 110 | + |
| 111 | + private H2EndpointProvider defaultEndpointProvider() { |
| 112 | + return H2EndpointProvider.defaultProvider(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + protected final AttributeMap serviceHttpConfig() { |
| 117 | + AttributeMap result = AttributeMap.empty(); |
| 118 | + return result.merge(AttributeMap.builder().put(SdkHttpConfigurationOption.PROTOCOL, Protocol.HTTP2) |
| 119 | + .put(SdkHttpConfigurationOption.PROTOCOL_NEGOTIATION, ProtocolNegotiation.ALPN).build()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected SdkClientConfiguration invokePlugins(SdkClientConfiguration config) { |
| 124 | + List<SdkPlugin> internalPlugins = internalPlugins(config); |
| 125 | + List<SdkPlugin> externalPlugins = plugins(); |
| 126 | + if (internalPlugins.isEmpty() && externalPlugins.isEmpty()) { |
| 127 | + return config; |
| 128 | + } |
| 129 | + List<SdkPlugin> plugins = CollectionUtils.mergeLists(internalPlugins, externalPlugins); |
| 130 | + SdkClientConfiguration.Builder configuration = config.toBuilder(); |
| 131 | + H2ServiceClientConfigurationBuilder serviceConfigBuilder = new H2ServiceClientConfigurationBuilder(configuration); |
| 132 | + for (SdkPlugin plugin : plugins) { |
| 133 | + plugin.configureClient(serviceConfigBuilder); |
| 134 | + } |
| 135 | + updateRetryStrategyClientConfiguration(configuration); |
| 136 | + return configuration.build(); |
| 137 | + } |
| 138 | + |
| 139 | + private void updateRetryStrategyClientConfiguration(SdkClientConfiguration.Builder configuration) { |
| 140 | + ClientOverrideConfiguration.Builder builder = configuration.asOverrideConfigurationBuilder(); |
| 141 | + RetryMode retryMode = builder.retryMode(); |
| 142 | + if (retryMode != null) { |
| 143 | + configuration.option(SdkClientOption.RETRY_STRATEGY, AwsRetryStrategy.forRetryMode(retryMode)); |
| 144 | + } else { |
| 145 | + Consumer<RetryStrategy.Builder<?, ?>> configurator = builder.retryStrategyConfigurator(); |
| 146 | + if (configurator != null) { |
| 147 | + RetryStrategy.Builder<?, ?> defaultBuilder = AwsRetryStrategy.defaultRetryStrategy().toBuilder(); |
| 148 | + configurator.accept(defaultBuilder); |
| 149 | + configuration.option(SdkClientOption.RETRY_STRATEGY, defaultBuilder.build()); |
| 150 | + } else { |
| 151 | + RetryStrategy retryStrategy = builder.retryStrategy(); |
| 152 | + if (retryStrategy != null) { |
| 153 | + configuration.option(SdkClientOption.RETRY_STRATEGY, retryStrategy); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + configuration.option(SdkClientOption.CONFIGURED_RETRY_MODE, null); |
| 158 | + configuration.option(SdkClientOption.CONFIGURED_RETRY_STRATEGY, null); |
| 159 | + configuration.option(SdkClientOption.CONFIGURED_RETRY_CONFIGURATOR, null); |
| 160 | + } |
| 161 | + |
| 162 | + private List<SdkPlugin> internalPlugins(SdkClientConfiguration config) { |
| 163 | + return Collections.emptyList(); |
| 164 | + } |
| 165 | + |
| 166 | + protected static void validateClientOptions(SdkClientConfiguration c) { |
| 167 | + Validate.notNull(c.option(SdkAdvancedClientOption.SIGNER), |
| 168 | + "The 'overrideConfiguration.advancedOption[SIGNER]' must be configured in the client builder."); |
| 169 | + } |
| 170 | +} |
0 commit comments