Skip to content

Commit fcd736e

Browse files
committed
Fixing checkstyle issues
1 parent 454d392 commit fcd736e

File tree

4 files changed

+681
-4
lines changed

4 files changed

+681
-4
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/XmlProtocolSpec.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public CodeBlock responseHandler(IntermediateModel model,
9191
return CodeBlock.builder()
9292
.addStatement("\n\n$T responseHandler = protocolFactory.createCombinedResponseHandler($T::builder, "
9393
+ "new $T().withHasStreamingSuccessResponse($L))",
94-
handlerType, responseType, XmlOperationMetadata.class, opModel.hasStreamingOutput())
94+
handlerType, responseType, XmlOperationMetadata.class, opModel.hasStreamingOutput())
9595
.build();
9696
}
9797

Original file line numberDiff line numberDiff line change
@@ -1 +1,205 @@
1-
/* * 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.protocols.query;import static java.util.Collections.unmodifiableList;import java.net.URI;import java.util.ArrayList;import java.util.List;import java.util.Optional;import java.util.function.Supplier;import software.amazon.awssdk.annotations.SdkProtectedApi;import software.amazon.awssdk.awscore.AwsResponse;import software.amazon.awssdk.awscore.exception.AwsServiceException;import software.amazon.awssdk.core.ClientEndpointProvider;import software.amazon.awssdk.core.SdkPojo;import software.amazon.awssdk.core.client.config.SdkClientConfiguration;import software.amazon.awssdk.core.client.config.SdkClientOption;import software.amazon.awssdk.core.http.HttpResponseHandler;import software.amazon.awssdk.core.http.MetricCollectingHttpResponseHandler;import software.amazon.awssdk.core.metrics.CoreMetric;import software.amazon.awssdk.http.SdkHttpFullRequest;import software.amazon.awssdk.protocols.core.ExceptionMetadata;import software.amazon.awssdk.protocols.core.OperationInfo;import software.amazon.awssdk.protocols.core.ProtocolMarshaller;import software.amazon.awssdk.protocols.query.internal.marshall.QueryProtocolMarshaller;import software.amazon.awssdk.protocols.query.internal.unmarshall.AwsQueryResponseHandler;import software.amazon.awssdk.protocols.query.internal.unmarshall.QueryProtocolUnmarshaller;import software.amazon.awssdk.protocols.query.unmarshall.AwsXmlErrorProtocolUnmarshaller;import software.amazon.awssdk.protocols.query.unmarshall.XmlElement;/** * Protocol factory for the AWS/Query protocol. */@SdkProtectedApipublic class AwsQueryProtocolFactory { private final SdkClientConfiguration clientConfiguration; private final List<ExceptionMetadata> modeledExceptions; private final Supplier<SdkPojo> defaultServiceExceptionSupplier; private final MetricCollectingHttpResponseHandler<AwsServiceException> errorUnmarshaller; AwsQueryProtocolFactory(Builder<?> builder) { this.clientConfiguration = builder.clientConfiguration; this.modeledExceptions = unmodifiableList(builder.modeledExceptions); this.defaultServiceExceptionSupplier = builder.defaultServiceExceptionSupplier; this.errorUnmarshaller = timeUnmarshalling(AwsXmlErrorProtocolUnmarshaller .builder() .defaultExceptionSupplier(defaultServiceExceptionSupplier) .exceptions(modeledExceptions) // We don't set result wrapper since that's handled by the errorRootExtractor .errorUnmarshaller(QueryProtocolUnmarshaller.builder().build()) .errorRootExtractor(this::getErrorRoot) .build()); } /** * Creates a new marshaller for the given request. * * @param operationInfo Object containing metadata about the operation. * @return New {@link ProtocolMarshaller}. */ public final ProtocolMarshaller<SdkHttpFullRequest> createProtocolMarshaller( OperationInfo operationInfo) { return QueryProtocolMarshaller.builder() .endpoint(endpoint(clientConfiguration)) .operationInfo(operationInfo) .isEc2(isEc2()) .build(); } private URI endpoint(SdkClientConfiguration clientConfiguration) { ClientEndpointProvider endpointProvider = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER); if (endpointProvider != null) { return endpointProvider.clientEndpoint(); } // Some old client versions may not use the endpoint provider. In that case, use the legacy endpoint field. return clientConfiguration.option(SdkClientOption.ENDPOINT); } /** * Creates the success response handler to unmarshall the response into a POJO. * * @param pojoSupplier Supplier of the POJO builder we are unmarshalling into. * @param <T> Type being unmarshalled. * @return New {@link HttpResponseHandler} for success responses. */ public final <T extends AwsResponse> HttpResponseHandler<T> createResponseHandler(Supplier<SdkPojo> pojoSupplier) { return timeUnmarshalling(new AwsQueryResponseHandler<>(QueryProtocolUnmarshaller.builder() .hasResultWrapper(!isEc2()) .build(), r -> pojoSupplier.get())); } /** * @return A {@link HttpResponseHandler} that will unmarshall the service exceptional response into * a modeled exception or the service base exception. */ public final HttpResponseHandler<AwsServiceException> createErrorResponseHandler() { return errorUnmarshaller; } private <T> MetricCollectingHttpResponseHandler<T> timeUnmarshalling(HttpResponseHandler<T> delegate) { return MetricCollectingHttpResponseHandler.create(CoreMetric.UNMARSHALLING_DURATION, delegate); } /** * Extracts the <Error/> element from the root XML document. Method is protected as EC2 has a slightly * different location. * * @param document Root XML document. * @return If error root is found than a fulfilled {@link Optional}, otherwise an empty one. */ Optional<XmlElement> getErrorRoot(XmlElement document) { return document.getOptionalElementByName("Error"); } /** * EC2 has a few distinct differences from query so we wire things up a bit differently. */ boolean isEc2() { return false; } /** * @return New Builder instance. */ public static Builder builder() { return new Builder(); } /** * Builder for {@link AwsQueryProtocolFactory}. * * @param <SubclassT> Subclass of Builder for fluent method chaining. */ public static class Builder<SubclassT extends Builder> { private final List<ExceptionMetadata> modeledExceptions = new ArrayList<>(); private SdkClientConfiguration clientConfiguration; private Supplier<SdkPojo> defaultServiceExceptionSupplier; Builder() { } /** * Sets the {@link SdkClientConfiguration} which contains the service endpoint. * * @param clientConfiguration Configuration of the client. * @return This builder for method chaining. */ public final SubclassT clientConfiguration(SdkClientConfiguration clientConfiguration) { this.clientConfiguration = clientConfiguration; return getSubclass(); } /** * Registers a new modeled exception by the error code. * * @param errorMetadata metadata for unmarshalling the exceptions * @return This builder for method chaining. */ public final SubclassT registerModeledException(ExceptionMetadata errorMetadata) { modeledExceptions.add(errorMetadata); return getSubclass(); } /** * A supplier for the services base exception builder. This is used when we can't identify any modeled * exception to unmarshall into. * * @param exceptionBuilderSupplier Suppplier of the base service exceptions Builder. * @return This builder for method chaining. */ public final SubclassT defaultServiceExceptionSupplier(Supplier<SdkPojo> exceptionBuilderSupplier) { this.defaultServiceExceptionSupplier = exceptionBuilderSupplier; return getSubclass(); } @SuppressWarnings("unchecked") private SubclassT getSubclass() { return (SubclassT) this; } /** * @return New instance of {@link AwsQueryProtocolFactory}. */ public AwsQueryProtocolFactory build() { return new AwsQueryProtocolFactory(this); } }}
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.protocols.query;
17+
18+
import static java.util.Collections.unmodifiableList;
19+
20+
import java.net.URI;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Optional;
24+
import java.util.function.Supplier;
25+
import software.amazon.awssdk.annotations.SdkProtectedApi;
26+
import software.amazon.awssdk.awscore.AwsResponse;
27+
import software.amazon.awssdk.awscore.exception.AwsServiceException;
28+
import software.amazon.awssdk.core.ClientEndpointProvider;
29+
import software.amazon.awssdk.core.SdkPojo;
30+
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
31+
import software.amazon.awssdk.core.client.config.SdkClientOption;
32+
import software.amazon.awssdk.core.http.HttpResponseHandler;
33+
import software.amazon.awssdk.core.http.MetricCollectingHttpResponseHandler;
34+
import software.amazon.awssdk.core.metrics.CoreMetric;
35+
import software.amazon.awssdk.http.SdkHttpFullRequest;
36+
import software.amazon.awssdk.protocols.core.ExceptionMetadata;
37+
import software.amazon.awssdk.protocols.core.OperationInfo;
38+
import software.amazon.awssdk.protocols.core.ProtocolMarshaller;
39+
import software.amazon.awssdk.protocols.query.internal.marshall.QueryProtocolMarshaller;
40+
import software.amazon.awssdk.protocols.query.internal.unmarshall.AwsQueryResponseHandler;
41+
import software.amazon.awssdk.protocols.query.internal.unmarshall.QueryProtocolUnmarshaller;
42+
import software.amazon.awssdk.protocols.query.unmarshall.AwsXmlErrorProtocolUnmarshaller;
43+
import software.amazon.awssdk.protocols.query.unmarshall.XmlElement;
44+
45+
/**
46+
* Protocol factory for the AWS/Query protocol.
47+
*/
48+
@SdkProtectedApi
49+
public class AwsQueryProtocolFactory {
50+
51+
private final SdkClientConfiguration clientConfiguration;
52+
private final List<ExceptionMetadata> modeledExceptions;
53+
private final Supplier<SdkPojo> defaultServiceExceptionSupplier;
54+
private final MetricCollectingHttpResponseHandler<AwsServiceException> errorUnmarshaller;
55+
56+
AwsQueryProtocolFactory(Builder<?> builder) {
57+
this.clientConfiguration = builder.clientConfiguration;
58+
this.modeledExceptions = unmodifiableList(builder.modeledExceptions);
59+
this.defaultServiceExceptionSupplier = builder.defaultServiceExceptionSupplier;
60+
this.errorUnmarshaller = timeUnmarshalling(AwsXmlErrorProtocolUnmarshaller
61+
.builder()
62+
.defaultExceptionSupplier(defaultServiceExceptionSupplier)
63+
.exceptions(modeledExceptions)
64+
// We don't set result wrapper since that's handled by the errorRootExtractor
65+
.errorUnmarshaller(QueryProtocolUnmarshaller.builder().build())
66+
.errorRootExtractor(this::getErrorRoot)
67+
.build());
68+
}
69+
70+
/**
71+
* Creates a new marshaller for the given request.
72+
*
73+
* @param operationInfo Object containing metadata about the operation.
74+
* @return New {@link ProtocolMarshaller}.
75+
*/
76+
public final ProtocolMarshaller<SdkHttpFullRequest> createProtocolMarshaller(
77+
OperationInfo operationInfo) {
78+
return QueryProtocolMarshaller.builder()
79+
.endpoint(endpoint(clientConfiguration))
80+
.operationInfo(operationInfo)
81+
.isEc2(isEc2())
82+
.build();
83+
}
84+
85+
private URI endpoint(SdkClientConfiguration clientConfiguration) {
86+
ClientEndpointProvider endpointProvider = clientConfiguration.option(SdkClientOption.CLIENT_ENDPOINT_PROVIDER);
87+
if (endpointProvider != null) {
88+
return endpointProvider.clientEndpoint();
89+
}
90+
91+
// Some old client versions may not use the endpoint provider. In that case, use the legacy endpoint field.
92+
return clientConfiguration.option(SdkClientOption.ENDPOINT);
93+
}
94+
95+
/**
96+
* Creates the success response handler to unmarshall the response into a POJO.
97+
*
98+
* @param pojoSupplier Supplier of the POJO builder we are unmarshalling into.
99+
* @param <T> Type being unmarshalled.
100+
* @return New {@link HttpResponseHandler} for success responses.
101+
*/
102+
public final <T extends AwsResponse> HttpResponseHandler<T> createResponseHandler(Supplier<SdkPojo> pojoSupplier) {
103+
return timeUnmarshalling(new AwsQueryResponseHandler<>(QueryProtocolUnmarshaller.builder()
104+
.hasResultWrapper(!isEc2())
105+
.build(), r -> pojoSupplier.get()));
106+
}
107+
108+
/**
109+
* @return A {@link HttpResponseHandler} that will unmarshall the service exceptional response into
110+
* a modeled exception or the service base exception.
111+
*/
112+
public final HttpResponseHandler<AwsServiceException> createErrorResponseHandler() {
113+
return errorUnmarshaller;
114+
}
115+
116+
private <T> MetricCollectingHttpResponseHandler<T> timeUnmarshalling(HttpResponseHandler<T> delegate) {
117+
return MetricCollectingHttpResponseHandler.create(CoreMetric.UNMARSHALLING_DURATION, delegate);
118+
}
119+
120+
/**
121+
* Extracts the <Error/> element from the root XML document. Method is protected as EC2 has a slightly
122+
* different location.
123+
*
124+
* @param document Root XML document.
125+
* @return If error root is found than a fulfilled {@link Optional}, otherwise an empty one.
126+
*/
127+
Optional<XmlElement> getErrorRoot(XmlElement document) {
128+
return document.getOptionalElementByName("Error");
129+
}
130+
131+
/**
132+
* EC2 has a few distinct differences from query so we wire things up a bit differently.
133+
*/
134+
boolean isEc2() {
135+
return false;
136+
}
137+
138+
/**
139+
* @return New Builder instance.
140+
*/
141+
public static Builder builder() {
142+
return new Builder();
143+
}
144+
145+
/**
146+
* Builder for {@link AwsQueryProtocolFactory}.
147+
*
148+
* @param <SubclassT> Subclass of Builder for fluent method chaining.
149+
*/
150+
public static class Builder<SubclassT extends Builder> {
151+
152+
private final List<ExceptionMetadata> modeledExceptions = new ArrayList<>();
153+
private SdkClientConfiguration clientConfiguration;
154+
private Supplier<SdkPojo> defaultServiceExceptionSupplier;
155+
156+
Builder() {
157+
}
158+
159+
/**
160+
* Sets the {@link SdkClientConfiguration} which contains the service endpoint.
161+
*
162+
* @param clientConfiguration Configuration of the client.
163+
* @return This builder for method chaining.
164+
*/
165+
public final SubclassT clientConfiguration(SdkClientConfiguration clientConfiguration) {
166+
this.clientConfiguration = clientConfiguration;
167+
return getSubclass();
168+
}
169+
170+
/**
171+
* Registers a new modeled exception by the error code.
172+
*
173+
* @param errorMetadata metadata for unmarshalling the exceptions
174+
* @return This builder for method chaining.
175+
*/
176+
public final SubclassT registerModeledException(ExceptionMetadata errorMetadata) {
177+
modeledExceptions.add(errorMetadata);
178+
return getSubclass();
179+
}
180+
181+
/**
182+
* A supplier for the services base exception builder. This is used when we can't identify any modeled
183+
* exception to unmarshall into.
184+
*
185+
* @param exceptionBuilderSupplier Suppplier of the base service exceptions Builder.
186+
* @return This builder for method chaining.
187+
*/
188+
public final SubclassT defaultServiceExceptionSupplier(Supplier<SdkPojo> exceptionBuilderSupplier) {
189+
this.defaultServiceExceptionSupplier = exceptionBuilderSupplier;
190+
return getSubclass();
191+
}
192+
193+
@SuppressWarnings("unchecked")
194+
private SubclassT getSubclass() {
195+
return (SubclassT) this;
196+
}
197+
198+
/**
199+
* @return New instance of {@link AwsQueryProtocolFactory}.
200+
*/
201+
public AwsQueryProtocolFactory build() {
202+
return new AwsQueryProtocolFactory(this);
203+
}
204+
}
205+
}

0 commit comments

Comments
 (0)