forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultErrorResponseHandler.java
More file actions
108 lines (96 loc) · 4.27 KB
/
DefaultErrorResponseHandler.java
File metadata and controls
108 lines (96 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2010-2013 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 com.amazonaws.http;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXParseException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.util.XpathUtils;
/**
* Implementation of HttpResponseHandler that handles only error responses from
* Amazon Web Services. A list of unmarshallers is passed into the constructor,
* and while handling a response, each unmarshaller is tried, in order, until
* one is found that can successfully unmarshall the error response. If no
* unmarshaller is found that can unmarshall the error response, a generic
* AmazonServiceException is created and populated with the AWS error response
* information (error message, AWS error code, AWS request ID, etc).
*/
public class DefaultErrorResponseHandler
implements HttpResponseHandler<AmazonServiceException> {
/**
* The list of error response unmarshallers to try to apply to error
* responses.
*/
private List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList;
/**
* Constructs a new DefaultErrorResponseHandler that will handle error
* responses from Amazon services using the specified list of unmarshallers.
* Each unmarshaller will be tried, in order, until one is found that can
* unmarshall the error response.
*
* @param unmarshallerList
* The list of unmarshallers to try using when handling an error
* response.
*/
public DefaultErrorResponseHandler(
List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList) {
this.unmarshallerList = unmarshallerList;
}
/* (non-Javadoc)
* @see com.amazonaws.http.HttpResponseHandler#handle(com.amazonaws.http.HttpResponse)
*/
public AmazonServiceException handle(HttpResponse errorResponse)
throws Exception {
Document document;
try {
document = XpathUtils.documentFrom(errorResponse.getContent());
} catch (SAXParseException e) {
AmazonServiceException exception =
new AmazonServiceException(String.format("Unable to unmarshall error response (%s)", e.getMessage()), e);
exception.setErrorCode(String.format("%s %s", errorResponse.getStatusCode(), errorResponse.getStatusText()));
exception.setErrorType(AmazonServiceException.ErrorType.Unknown);
exception.setStatusCode(errorResponse.getStatusCode());
return exception;
}
/*
* We need to select which exception unmarshaller is the correct one to
* use from all the possible exceptions this operation can throw.
* Currently we rely on the unmarshallers to return null if they can't
* unmarshall the response, but we might need something a little more
* sophisticated in the future.
*/
for (Unmarshaller<AmazonServiceException, Node> unmarshaller : unmarshallerList) {
AmazonServiceException ase = unmarshaller.unmarshall(document);
if (ase != null) {
ase.setStatusCode(errorResponse.getStatusCode());
return ase;
}
}
throw new AmazonClientException("Unable to unmarshall error response from service");
}
/**
* Since this response handler completely consumes all the data from the
* underlying HTTP connection during the handle method, we don't need to
* keep the HTTP connection open.
*
* @see com.amazonaws.http.HttpResponseHandler#needsConnectionLeftOpen()
*/
public boolean needsConnectionLeftOpen() {
return false;
}
}