Skip to content

Commit 9484ac0

Browse files
committed
Updated with request body example
1 parent 084eaf1 commit 9484ac0

File tree

4 files changed

+223
-159
lines changed

4 files changed

+223
-159
lines changed

pom.xml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,27 @@
9393
<scope>test</scope>
9494
</dependency>
9595

96+
97+
<!-- wire logging -->
9698
<dependency>
97-
<groupId>org.slf4j</groupId>
98-
<artifactId>slf4j-simple</artifactId>
99-
<version>1.7.30</version>
100-
<scope>test</scope>
99+
<groupId>org.apache.logging.log4j</groupId>
100+
<artifactId>log4j-core</artifactId>
101+
<version>2.13.3</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.apache.logging.log4j</groupId>
105+
<artifactId>log4j-api</artifactId>
106+
<version>2.13.3</version>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.apache.logging.log4j</groupId>
110+
<artifactId>log4j-slf4j-impl</artifactId>
111+
<version>2.13.3</version>
112+
</dependency>
113+
<dependency>
114+
<groupId>org.apache.logging.log4j</groupId>
115+
<artifactId>log4j-1.2-api</artifactId>
116+
<version>2.13.3</version>
101117
</dependency>
102118

103119

Lines changed: 157 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.time.Duration;
12
import java.util.Collections;
23
import java.util.LinkedHashMap;
34
import java.util.List;
@@ -25,7 +26,9 @@
2526
import software.amazon.awssdk.core.internal.http.CombinedResponseHandler;
2627
import software.amazon.awssdk.core.internal.http.loader.DefaultSdkHttpClientBuilder;
2728
import software.amazon.awssdk.core.retry.RetryPolicy;
29+
import software.amazon.awssdk.core.sync.RequestBody;
2830
import software.amazon.awssdk.http.SdkHttpClient;
31+
import software.amazon.awssdk.http.SdkHttpConfigurationOption;
2932
import software.amazon.awssdk.http.SdkHttpFullRequest;
3033
import software.amazon.awssdk.http.SdkHttpFullResponse;
3134
import software.amazon.awssdk.metrics.NoOpMetricCollector;
@@ -36,146 +39,157 @@
3639

3740
public class AWSSignerHttpClient implements AutoCloseable {
3841

39-
private String serviceName;
40-
private Region region;
41-
private AwsCredentialsProvider awsCredentialsProvider;
42-
private SdkHttpClient sdkClient;
43-
// required by client to avoid NPE
44-
private SdkRequest sdkRequest = new ServiceSDKRequest();
45-
private ExecutionInterceptorChain execInterceptorChain = new ExecutionInterceptorChain(Collections.emptyList());
46-
private AmazonSyncHttpClient awsClient;
47-
private Aws4Signer signer;
48-
49-
private AWSSignerHttpClient() {
50-
51-
}
52-
53-
public static Builder builder() {
54-
return new Builder();
55-
}
56-
57-
public <T> T execute(SdkHttpFullRequest httpRequest, HttpResponseHandler<T> responseHandler) {
58-
return execute(httpRequest, responseHandler, new ErrorHandler());
59-
}
60-
61-
public <T extends JsonStructure> T execute(SdkHttpFullRequest httpRequest) {
62-
return execute(httpRequest, new JsonHandler<T>(), new ErrorHandler());
63-
}
64-
65-
public <T> T execute(SdkHttpFullRequest httpRequest, HttpResponseHandler<T> responseHandler, HttpResponseHandler<? extends SdkException> errorHandler) {
66-
InterceptorContext incerceptorContext = InterceptorContext.builder().request(sdkRequest).httpRequest(httpRequest).build();
67-
ExecutionContext.Builder execContextBuilder = ExecutionContext.builder();
68-
execContextBuilder.signer(signer);
69-
execContextBuilder.interceptorChain(execInterceptorChain);
70-
execContextBuilder.metricCollector(NoOpMetricCollector.create());
71-
ExecutionAttributes executionAttributes = new ExecutionAttributes();
72-
executionAttributes.putAttribute(AwsSignerExecutionAttribute.AWS_CREDENTIALS, awsCredentialsProvider.resolveCredentials());
73-
executionAttributes.putAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME, serviceName);
74-
executionAttributes.putAttribute(AwsSignerExecutionAttribute.SIGNING_REGION, region);
75-
execContextBuilder.executionAttributes(executionAttributes);
76-
execContextBuilder.interceptorContext(incerceptorContext).build();
77-
ExecutionContext execContext = execContextBuilder.build();
78-
return awsClient.requestExecutionBuilder().executionContext(execContext).originalRequest(sdkRequest).request(httpRequest).execute(new CombinedResponseHandler<T>(responseHandler, errorHandler));
79-
}
80-
81-
public static class Builder {
82-
AWSSignerHttpClient client = new AWSSignerHttpClient();
83-
84-
public AWSSignerHttpClient build() {
85-
if (client.awsCredentialsProvider == null) {
86-
DefaultCredentialsProvider provider = DefaultCredentialsProvider.create();
87-
client.awsCredentialsProvider = provider;
88-
}
89-
if (client.sdkClient == null) {
90-
client.sdkClient = new DefaultSdkHttpClientBuilder().buildWithDefaults(AttributeMap.empty());
91-
}
92-
93-
if (client.region == null) {
94-
client.region = new DefaultAwsRegionProviderChain().getRegion();
95-
}
96-
client.signer = Aws4Signer.create();
97-
// signer.setRegionName(client.region.value());
98-
// signer.setServiceName(client.serviceName);
99-
// client.signingProvider = StaticSignerProvider.create(signer);
100-
SdkClientConfiguration clientConfiguration = SdkClientConfiguration.builder().option(SdkClientOption.ADDITIONAL_HTTP_HEADERS, new LinkedHashMap<>()).option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, true).option(SdkClientOption.SYNC_HTTP_CLIENT, client.sdkClient).option(SdkClientOption.RETRY_POLICY, RetryPolicy.none()).build();
101-
client.awsClient = new AmazonSyncHttpClient(clientConfiguration);
102-
return client;
103-
}
104-
105-
public Builder sdkClient(SdkHttpClient sdkClient) {
106-
client.sdkClient = sdkClient;
107-
return this;
108-
}
109-
110-
public Builder serviceName(String serviceName) {
111-
client.serviceName = serviceName;
112-
return this;
113-
}
114-
115-
public Builder region(Region region) {
116-
client.region = region;
117-
return this;
118-
}
119-
120-
public Builder awsCredentials(AwsCredentialsProvider awsCredentialsProvider) {
121-
client.awsCredentialsProvider = awsCredentialsProvider;
122-
return this;
123-
}
124-
}
125-
126-
public static class ServiceSDKRequest extends SdkRequest {
127-
128-
@Override
129-
public Optional<? extends RequestOverrideConfiguration> overrideConfiguration() {
130-
return Optional.empty();
131-
}
132-
133-
@Override
134-
public Builder toBuilder() {
135-
return new Builder() {
136-
137-
@Override
138-
public RequestOverrideConfiguration overrideConfiguration() {
139-
return null;
140-
}
141-
142-
@Override
143-
public SdkRequest build() {
144-
return new ServiceSDKRequest();
145-
}
146-
147-
};
148-
}
149-
150-
@Override
151-
public List<SdkField<?>> sdkFields() {
152-
return Collections.unmodifiableList(Collections.emptyList());
153-
}
154-
155-
}
156-
157-
public static class ErrorHandler implements HttpResponseHandler<SdkException> {
158-
159-
@Override
160-
public SdkException handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
161-
String responseMsg = response.content().isPresent() ? IoUtils.toUtf8String(response.content().get()) : "";
162-
return SdkException.builder().message(String.format("%d: %s", response.statusCode(), responseMsg)).build();
163-
}
164-
165-
}
166-
167-
public static class JsonHandler<T extends JsonStructure> implements HttpResponseHandler<T> {
168-
169-
@Override
170-
public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
171-
return response.content().isPresent() ? (T) Json.createReader(response.content().get()).read() : null;
172-
}
173-
174-
}
175-
176-
@Override
177-
public void close() {
178-
awsClient.close();
179-
180-
}
181-
}
42+
private String serviceName;
43+
private Region region;
44+
private AwsCredentialsProvider awsCredentialsProvider;
45+
private Duration readTimeOut;
46+
private SdkHttpClient sdkClient;
47+
// required by client to avoid NPE
48+
private SdkRequest sdkRequest = new ServiceSDKRequest();
49+
private ExecutionInterceptorChain execInterceptorChain = new ExecutionInterceptorChain(Collections.emptyList());
50+
private AmazonSyncHttpClient awsClient;
51+
private Aws4Signer signer;
52+
53+
private AWSSignerHttpClient() {
54+
55+
}
56+
57+
public static Builder builder() {
58+
return new Builder();
59+
}
60+
61+
public <T> T execute(SdkHttpFullRequest httpRequest, RequestBody requestBody, HttpResponseHandler<T> responseHandler) {
62+
return execute(httpRequest, requestBody, responseHandler, new ErrorHandler());
63+
}
64+
65+
public <T extends JsonStructure> T execute(SdkHttpFullRequest httpRequest, RequestBody requestBody) {
66+
return execute(httpRequest, requestBody, new JsonHandler<T>(), new ErrorHandler());
67+
}
68+
69+
public <T extends JsonStructure> T execute(SdkHttpFullRequest httpRequest) {
70+
return execute(httpRequest, null, new JsonHandler<T>(), new ErrorHandler());
71+
}
72+
73+
public <T> T execute(SdkHttpFullRequest httpRequest, RequestBody requestBody, HttpResponseHandler<T> responseHandler, HttpResponseHandler<? extends SdkException> errorHandler) {
74+
InterceptorContext incerceptorContext = InterceptorContext.builder().requestBody(requestBody).request(sdkRequest).httpRequest(httpRequest).build();
75+
ExecutionContext.Builder execContextBuilder = ExecutionContext.builder();
76+
execContextBuilder.signer(signer);
77+
execContextBuilder.interceptorChain(execInterceptorChain);
78+
execContextBuilder.metricCollector(NoOpMetricCollector.create());
79+
ExecutionAttributes executionAttributes = new ExecutionAttributes();
80+
executionAttributes.putAttribute(AwsSignerExecutionAttribute.AWS_CREDENTIALS, awsCredentialsProvider.resolveCredentials());
81+
executionAttributes.putAttribute(AwsSignerExecutionAttribute.SERVICE_SIGNING_NAME, serviceName);
82+
executionAttributes.putAttribute(AwsSignerExecutionAttribute.SIGNING_REGION, region);
83+
execContextBuilder.executionAttributes(executionAttributes);
84+
execContextBuilder.interceptorContext(incerceptorContext).build();
85+
ExecutionContext execContext = execContextBuilder.build();
86+
return awsClient.requestExecutionBuilder().executionContext(execContext).originalRequest(sdkRequest).request(httpRequest).execute(new CombinedResponseHandler<T>(responseHandler, errorHandler));
87+
}
88+
89+
public static class Builder {
90+
AWSSignerHttpClient client = new AWSSignerHttpClient();
91+
92+
public AWSSignerHttpClient build() {
93+
if (client.awsCredentialsProvider == null) {
94+
DefaultCredentialsProvider provider = DefaultCredentialsProvider.create();
95+
client.awsCredentialsProvider = provider;
96+
}
97+
if (client.sdkClient == null) {
98+
AttributeMap options = client.readTimeOut != null ? AttributeMap.builder().put(SdkHttpConfigurationOption.READ_TIMEOUT, client.readTimeOut).build() : AttributeMap.empty();
99+
client.sdkClient = new DefaultSdkHttpClientBuilder().buildWithDefaults(options);
100+
}
101+
102+
if (client.region == null) {
103+
client.region = new DefaultAwsRegionProviderChain().getRegion();
104+
}
105+
client.signer = Aws4Signer.create();
106+
// signer.setRegionName(client.region.value());
107+
// signer.setServiceName(client.serviceName);
108+
// client.signingProvider = StaticSignerProvider.create(signer);
109+
SdkClientConfiguration clientConfiguration = SdkClientConfiguration.builder().option(SdkClientOption.ADDITIONAL_HTTP_HEADERS, new LinkedHashMap<>()).option(SdkClientOption.CRC32_FROM_COMPRESSED_DATA_ENABLED, true).option(SdkClientOption.SYNC_HTTP_CLIENT, client.sdkClient).option(SdkClientOption.RETRY_POLICY, RetryPolicy.none()).build();
110+
client.awsClient = new AmazonSyncHttpClient(clientConfiguration);
111+
return client;
112+
}
113+
114+
public Builder sdkClient(SdkHttpClient sdkClient) {
115+
client.sdkClient = sdkClient;
116+
return this;
117+
}
118+
119+
public Builder serviceName(String serviceName) {
120+
client.serviceName = serviceName;
121+
return this;
122+
}
123+
124+
public Builder region(Region region) {
125+
client.region = region;
126+
return this;
127+
}
128+
129+
public Builder awsCredentials(AwsCredentialsProvider awsCredentialsProvider) {
130+
client.awsCredentialsProvider = awsCredentialsProvider;
131+
return this;
132+
}
133+
134+
public Builder readTimeout(Duration readTimeOut) {
135+
client.readTimeOut = readTimeOut;
136+
return this;
137+
}
138+
}
139+
140+
public static class ServiceSDKRequest extends SdkRequest {
141+
142+
@Override
143+
public Optional<? extends RequestOverrideConfiguration> overrideConfiguration() {
144+
return Optional.empty();
145+
}
146+
147+
@Override
148+
public Builder toBuilder() {
149+
return new Builder() {
150+
151+
@Override
152+
public RequestOverrideConfiguration overrideConfiguration() {
153+
return null;
154+
}
155+
156+
@Override
157+
public SdkRequest build() {
158+
return new ServiceSDKRequest();
159+
}
160+
161+
};
162+
}
163+
164+
@Override
165+
public List<SdkField<?>> sdkFields() {
166+
return Collections.unmodifiableList(Collections.emptyList());
167+
}
168+
169+
}
170+
171+
public static class ErrorHandler implements HttpResponseHandler<SdkException> {
172+
173+
@Override
174+
public SdkException handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
175+
String responseMsg = response.content().isPresent() ? IoUtils.toUtf8String(response.content().get()) : "";
176+
return SdkException.builder().message(String.format("%d: %s", response.statusCode(), responseMsg)).build();
177+
}
178+
179+
}
180+
181+
public static class JsonHandler<T extends JsonStructure> implements HttpResponseHandler<T> {
182+
183+
@Override
184+
public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
185+
return response.content().isPresent() ? (T) Json.createReader(response.content().get()).read() : null;
186+
}
187+
188+
}
189+
190+
@Override
191+
public void close() {
192+
awsClient.close();
193+
194+
}
195+
}

0 commit comments

Comments
 (0)