You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* * 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. */packagesoftware.amazon.awssdk.protocols.query;importstaticjava.util.Collections.unmodifiableList;importjava.net.URI;importjava.util.ArrayList;importjava.util.List;importjava.util.Optional;importjava.util.function.Supplier;importsoftware.amazon.awssdk.annotations.SdkProtectedApi;importsoftware.amazon.awssdk.awscore.AwsResponse;importsoftware.amazon.awssdk.awscore.exception.AwsServiceException;importsoftware.amazon.awssdk.core.ClientEndpointProvider;importsoftware.amazon.awssdk.core.SdkPojo;importsoftware.amazon.awssdk.core.client.config.SdkClientConfiguration;importsoftware.amazon.awssdk.core.client.config.SdkClientOption;importsoftware.amazon.awssdk.core.http.HttpResponseHandler;importsoftware.amazon.awssdk.core.http.MetricCollectingHttpResponseHandler;importsoftware.amazon.awssdk.core.metrics.CoreMetric;importsoftware.amazon.awssdk.http.SdkHttpFullRequest;importsoftware.amazon.awssdk.protocols.core.ExceptionMetadata;importsoftware.amazon.awssdk.protocols.core.OperationInfo;importsoftware.amazon.awssdk.protocols.core.ProtocolMarshaller;importsoftware.amazon.awssdk.protocols.query.internal.marshall.QueryProtocolMarshaller;importsoftware.amazon.awssdk.protocols.query.internal.unmarshall.AwsQueryResponseHandler;importsoftware.amazon.awssdk.protocols.query.internal.unmarshall.QueryProtocolUnmarshaller;importsoftware.amazon.awssdk.protocols.query.unmarshall.AwsXmlErrorProtocolUnmarshaller;importsoftware.amazon.awssdk.protocols.query.unmarshall.XmlElement;/** * Protocol factory for the AWS/Query protocol. */@SdkProtectedApipublicclassAwsQueryProtocolFactory { privatefinalSdkClientConfigurationclientConfiguration; privatefinalList<ExceptionMetadata> modeledExceptions; privatefinalSupplier<SdkPojo> defaultServiceExceptionSupplier; privatefinalMetricCollectingHttpResponseHandler<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
0 commit comments