Skip to content

Commit

Permalink
Add internal config
Browse files Browse the repository at this point in the history
  • Loading branch information
zoewangg committed Apr 28, 2021
1 parent f5916fc commit 31c8c7b
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ public class CustomizationConfig {
*/
private UnderscoresInNameBehavior underscoresInNameBehavior;

private String userAgent;

private CustomizationConfig() {
}

Expand Down Expand Up @@ -470,4 +472,17 @@ public CustomizationConfig withUnderscoresInShapeNameBehavior(UnderscoresInNameB
this.underscoresInNameBehavior = behavior;
return this;
}

public String getUserAgent() {
return userAgent;
}

public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

public CustomizationConfig withUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public TypeSpec poetSpec() {
builder.addMethod(serviceEndpointPrefixMethod());
builder.addMethod(serviceNameMethod());
builder.addMethod(mergeServiceDefaultsMethod());

if (model.getCustomizationConfig().getUserAgent() != null) {
builder.addMethod(mergeInternalDefaultsMethod());
}

builder.addMethod(finalizeServiceConfigurationMethod());
builder.addMethod(defaultSignerMethod());
builder.addMethod(signingNameMethod());
Expand Down Expand Up @@ -170,6 +175,21 @@ private MethodSpec mergeServiceDefaultsMethod() {
return builder.build();
}

private MethodSpec mergeInternalDefaultsMethod() {
String userAgent = model.getCustomizationConfig().getUserAgent();

MethodSpec.Builder builder = MethodSpec.methodBuilder("mergeInternalDefaults")
.addAnnotation(Override.class)
.addModifiers(PROTECTED, FINAL)
.returns(SdkClientConfiguration.class)
.addParameter(SdkClientConfiguration.class, "config")
.addCode("return config.merge(c -> c.option($T.INTERNAL_USER_AGENT, $S)\n",
SdkClientOption.class, userAgent);

builder.addCode(");");
return builder.build();
}

private MethodSpec finalizeServiceConfigurationMethod() {
String requestHandlerDirectory = Utils.packageToDirectory(model.getMetadata().getFullClientPackageName());
String requestHandlerPath = String.format("%s/execution.interceptors", requestHandlerDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public static IntermediateModel endpointDiscoveryModels() {
return new IntermediateModelBuilder(models).build();
}

public static IntermediateModel internalConfigModels() {
File serviceModel = new File(ClientTestModels.class.getResource("client/c2j/internalconfig/service-2.json").getFile());
File customizationModel = new File(ClientTestModels.class.getResource("client/c2j/internalconfig/customization.config").getFile());

C2jModels models = C2jModels.builder()
.serviceModel(getServiceModel(serviceModel))
.customizationConfig(getCustomizationConfig(customizationModel))
.build();

return new IntermediateModelBuilder(models).build();
}

private static ServiceModel getServiceModel(File file) {
return ModelLoaderUtils.loadModel(ServiceModel.class, file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void baseClientBuilderClass() throws Exception {
validateGeneration(BaseClientBuilderClass::new, "test-client-builder-class.java");
}

@Test
public void baseClientBuilderClassWithInternalUserAgent() throws Exception {
assertThat(new BaseClientBuilderClass(ClientTestModels.internalConfigModels()), generatesTo("test-client-builder-internal-user-agent-class.java"));
}

@Test
public void baseQueryClientBuilderClass() throws Exception {
validateQueryGeneration(BaseClientBuilderClass::new, "test-query-client-builder-class.java");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package software.amazon.awssdk.services.json;

import java.util.List;
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder;
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.interceptor.ClasspathInterceptorChainFactory;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.signer.Signer;
import software.amazon.awssdk.utils.CollectionUtils;

/**
* Internal base class for {@link DefaultJsonClientBuilder} and {@link DefaultJsonAsyncClientBuilder}.
*/
@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
abstract class DefaultJsonBaseClientBuilder<B extends JsonBaseClientBuilder<B, C>, C> extends AwsDefaultClientBuilder<B, C> {
@Override
protected final String serviceEndpointPrefix() {
return "json-service-endpoint";
}

@Override
protected final String serviceName() {
return "Json";
}

@Override
protected final SdkClientConfiguration mergeServiceDefaults(SdkClientConfiguration config) {
return config.merge(c -> c.option(SdkAdvancedClientOption.SIGNER, defaultSigner()).option(
SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, false));
}

@Override
protected final SdkClientConfiguration mergeInternalDefaults(SdkClientConfiguration config) {
return config.merge(c -> c.option(SdkClientOption.INTERNAL_USER_AGENT, "md/foobar"));
}

@Override
protected final SdkClientConfiguration finalizeServiceConfiguration(SdkClientConfiguration config) {
ClasspathInterceptorChainFactory interceptorFactory = new ClasspathInterceptorChainFactory();
List<ExecutionInterceptor> interceptors = interceptorFactory
.getInterceptors("software/amazon/awssdk/services/json/execution.interceptors");
interceptors = CollectionUtils.mergeLists(interceptors, config.option(SdkClientOption.EXECUTION_INTERCEPTORS));
return config.toBuilder().option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptors).build();
}

private Signer defaultSigner() {
return Aws4Signer.create();
}

@Override
protected final String signingName() {
return "json-service";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"authPolicyActions" : {
"skip" : true
},
"userAgent": "md/foobar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": "2.0",
"metadata": {
"apiVersion": "2010-05-08",
"endpointPrefix": "json-service-endpoint",
"globalEndpoint": "json-service.amazonaws.com",
"protocol": "rest-json",
"serviceAbbreviation": "Json Service",
"serviceFullName": "Some Service That Uses Json Protocol",
"serviceId": "Json Service",
"signingName": "json-service",
"signatureVersion": "v4",
"uid": "json-service-2010-05-08",
"xmlNamespace": "https://json-service.amazonaws.com/doc/2010-05-08/"
},
"operations":{
"OneOperation":{
"name":"OneOperation",
"http":{
"method":"POST",
"requestUri":"/2016-03-11/oneoperation"
},
"input":{"shape":"OneShape"}
}
},
"shapes": {
"OneShape": {
"type": "structure",
"members": {
"StringMember": {
"shape": "String"
}
}
},
"String":{"type":"string"}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ protected AttributeMap serviceHttpConfig() {
@Override
protected final SdkClientConfiguration mergeChildDefaults(SdkClientConfiguration configuration) {
SdkClientConfiguration config = mergeServiceDefaults(configuration);

return config.merge(c -> c.option(AwsAdvancedClientOption.ENABLE_DEFAULT_REGION_DETECTION, true)
config = config.merge(c -> c.option(AwsAdvancedClientOption.ENABLE_DEFAULT_REGION_DETECTION, true)
.option(SdkAdvancedClientOption.DISABLE_HOST_PREFIX_INJECTION, false)
.option(AwsClientOption.SERVICE_SIGNING_NAME, signingName())
.option(SdkClientOption.SERVICE_NAME, serviceName())
.option(AwsClientOption.ENDPOINT_PREFIX, serviceEndpointPrefix()));
return mergeInternalDefaults(config);
}

/**
Expand All @@ -127,6 +127,13 @@ protected SdkClientConfiguration mergeServiceDefaults(SdkClientConfiguration con
return configuration;
}

/**
* Optionally overridden by child classes to define internal default configuration.
*/
protected SdkClientConfiguration mergeInternalDefaults(SdkClientConfiguration configuration) {
return configuration;
}

@Override
protected final SdkClientConfiguration finalizeChildConfiguration(SdkClientConfiguration configuration) {
configuration = finalizeServiceConfiguration(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public final class SdkClientOption<T> extends ClientOption<T> {
*/
public static final SdkClientOption<ExecutionAttributes> EXECUTION_ATTRIBUTES =
new SdkClientOption<>(new UnsafeValueType(ExecutionAttributes.class));
/**
* Option to specify the internal user agent.
*/
public static final SdkClientOption<String> INTERNAL_USER_AGENT = new SdkClientOption<>(String.class);

private SdkClientOption(Class<T> valueClass) {
super(valueClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.awssdk.core.internal.http.pipeline.stages;

import static software.amazon.awssdk.core.client.config.SdkClientOption.INTERNAL_USER_AGENT;

import java.util.List;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
Expand Down Expand Up @@ -74,6 +76,11 @@ private StringBuilder getUserAgent(SdkClientConfiguration config, List<ApiName>
userAgent.append(COMMA).append(systemUserAgent);
}

String internalUserAgent = StringUtils.trimToEmpty(clientConfig.option(INTERNAL_USER_AGENT));
if (!internalUserAgent.isEmpty()) {
userAgent.append(SPACE).append(internalUserAgent);
}

if (!StringUtils.isEmpty(awsExecutionEnvironment)) {
userAgent.append(SPACE).append(AWS_EXECUTION_ENV_PREFIX).append(awsExecutionEnvironment.trim());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"userAgent":"md/foobar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version":"2.0",
"metadata":{
"apiVersion":"2016-03-11",
"endpointPrefix":"internalconfig",
"jsonVersion":"1.1",
"protocol":"rest-json",
"serviceAbbreviation":"AmazonProtocolRestJson",
"serviceFullName":"Amazon Protocol Rest Json",
"serviceId":"AmazonProtocolRestJsonWithInternalConfig",
"signatureVersion":"v4",
"targetPrefix":"ProtocolTestsService",
"timestampFormat":"unixTimestamp",
"uid":"restjson-2016-03-11"
},
"operations":{
"OneOperation":{
"name":"OneOperation",
"http":{
"method":"POST",
"requestUri":"/2016-03-11/oneoperation"
},
"input":{"shape":"OneShape"}
}
},
"shapes": {
"OneShape": {
"type": "structure",
"members": {
"StringMember": {
"shape": "String"
}
}
},
"String":{"type":"string"}
}
}
Loading

0 comments on commit 31c8c7b

Please sign in to comment.