|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 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.awscore.http.response; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.awscore.internal.DefaultAwsResponseMetadata.AWS_REQUEST_ID; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | +import software.amazon.awssdk.annotations.SdkProtectedApi; |
| 24 | +import software.amazon.awssdk.awscore.AwsResponse; |
| 25 | +import software.amazon.awssdk.awscore.AwsResponseMetadata; |
| 26 | +import software.amazon.awssdk.awscore.internal.DefaultAwsResponseMetadata; |
| 27 | +import software.amazon.awssdk.core.SdkStandardLogger; |
| 28 | +import software.amazon.awssdk.core.http.HttpResponseHandler; |
| 29 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 30 | +import software.amazon.awssdk.core.internal.protocol.query.unmarshall.QueryProtocolUnmarshaller; |
| 31 | +import software.amazon.awssdk.core.protocol.SdkPojo; |
| 32 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 33 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 34 | +import software.amazon.awssdk.utils.Logger; |
| 35 | +import software.amazon.awssdk.utils.Pair; |
| 36 | + |
| 37 | +/** |
| 38 | + * Response handler for AWS/Query services and Amazon EC2 which is a dialect of the Query protocol. |
| 39 | + * |
| 40 | + * @param <T> Indicates the type being unmarshalled by this response handler. |
| 41 | + */ |
| 42 | +@SdkProtectedApi |
| 43 | +public final class AwsQueryResponseHandler<T extends AwsResponse> implements HttpResponseHandler<T> { |
| 44 | + |
| 45 | + private static final Logger log = Logger.loggerFor(StaxResponseHandler.class); |
| 46 | + |
| 47 | + private final QueryProtocolUnmarshaller<T> unmarshaller; |
| 48 | + private final Function<SdkHttpFullResponse, SdkPojo> pojoSupplier; |
| 49 | + |
| 50 | + |
| 51 | + public AwsQueryResponseHandler(QueryProtocolUnmarshaller<T> unmarshaller, |
| 52 | + Function<SdkHttpFullResponse, SdkPojo> pojoSupplier) { |
| 53 | + this.unmarshaller = unmarshaller; |
| 54 | + this.pojoSupplier = pojoSupplier; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public T handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception { |
| 59 | + try { |
| 60 | + return unmarshallResponse(response); |
| 61 | + } finally { |
| 62 | + response.content().ifPresent(i -> { |
| 63 | + try { |
| 64 | + i.close(); |
| 65 | + } catch (IOException e) { |
| 66 | + log.warn(() -> "Error closing HTTP content.", e); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + private T unmarshallResponse(SdkHttpFullResponse response) throws Exception { |
| 74 | + SdkStandardLogger.REQUEST_LOGGER.trace(() -> "Parsing service response XML."); |
| 75 | + Pair<T, Map<String, String>> result = unmarshaller.unmarshall(pojoSupplier.apply(response), response); |
| 76 | + SdkStandardLogger.REQUEST_LOGGER.trace(() -> "Done parsing service response."); |
| 77 | + AwsResponseMetadata responseMetadata = generateResponseMetadata(response, result.right()); |
| 78 | + return (T) result.left().toBuilder().responseMetadata(responseMetadata).build(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Create the default {@link AwsResponseMetadata}. Subclasses may override this to create a |
| 83 | + * subclass of {@link AwsResponseMetadata}. |
| 84 | + */ |
| 85 | + private AwsResponseMetadata generateResponseMetadata(SdkHttpResponse response, Map<String, String> metadata) { |
| 86 | + if (!metadata.containsKey(AWS_REQUEST_ID)) { |
| 87 | + metadata.put(AWS_REQUEST_ID, |
| 88 | + response.firstMatchingHeader(X_AMZN_REQUEST_ID_HEADER).orElse(null)); |
| 89 | + } |
| 90 | + |
| 91 | + response.headers().forEach((key, value) -> metadata.put(key, value.get(0))); |
| 92 | + return DefaultAwsResponseMetadata.create(metadata); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean needsConnectionLeftOpen() { |
| 97 | + // Query doesn't support streaming so this is always false |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments