forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractS3ResponseHandler.java
More file actions
145 lines (132 loc) · 5.96 KB
/
AbstractS3ResponseHandler.java
File metadata and controls
145 lines (132 loc) · 5.96 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* 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.services.s3.internal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.amazonaws.AmazonWebServiceResponse;
import com.amazonaws.ResponseMetadata;
import com.amazonaws.http.HttpResponse;
import com.amazonaws.http.HttpResponseHandler;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.S3ResponseMetadata;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.util.DateUtils;
/**
* Abstract HTTP response handler for Amazon S3 responses. Provides common
* utilities that other specialized S3 response handlers need to share such as
* pulling common response metadata (ex: request IDs) out of headers.
*
* @param <T>
* The output type resulting from handling a response.
*/
public abstract class AbstractS3ResponseHandler<T>
implements HttpResponseHandler<AmazonWebServiceResponse<T>> {
/** Shared logger */
private static final Log log = LogFactory.getLog(S3MetadataResponseHandler.class);
/** The set of response headers that aren't part of the object's metadata */
private static final Set<String> ignoredHeaders;
static {
ignoredHeaders = new HashSet<String>();
ignoredHeaders.add(Headers.DATE);
ignoredHeaders.add(Headers.SERVER);
ignoredHeaders.add(Headers.REQUEST_ID);
ignoredHeaders.add(Headers.EXTENDED_REQUEST_ID);
}
/**
* The majority of S3 response handlers read the complete response while
* handling it, and don't need to manually manage the underlying HTTP
* connection.
*
* @see com.amazonaws.http.HttpResponseHandler#needsConnectionLeftOpen()
*/
public boolean needsConnectionLeftOpen() {
return false;
}
/**
* Parses the S3 response metadata (ex: AWS request ID) from the specified
* response, and returns a AmazonWebServiceResponse<T> object ready for the
* result to be plugged in.
*
* @param response
* The response containing the response metadata to pull out.
*
* @return A new, populated AmazonWebServiceResponse<T> object, ready for
* the result to be plugged in.
*/
protected AmazonWebServiceResponse<T> parseResponseMetadata(HttpResponse response) {
AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>();
String awsRequestId = response.getHeaders().get(Headers.REQUEST_ID);
String hostId = response.getHeaders().get(Headers.EXTENDED_REQUEST_ID);
Map<String, String> metadataMap = new HashMap<String, String>();
metadataMap.put(ResponseMetadata.AWS_REQUEST_ID, awsRequestId);
metadataMap.put(S3ResponseMetadata.HOST_ID, hostId);
awsResponse.setResponseMetadata(new S3ResponseMetadata(metadataMap));
return awsResponse;
}
/**
* Populates the specified S3ObjectMetadata object with all object metadata
* pulled from the headers in the specified response.
*
* @param response
* The HTTP response containing the object metadata within the
* headers.
* @param metadata
* The metadata object to populate from the response's headers.
*/
protected void populateObjectMetadata(HttpResponse response, ObjectMetadata metadata) {
for (Entry<String, String> header : response.getHeaders().entrySet()) {
String key = header.getKey();
if (key.startsWith(Headers.S3_USER_METADATA_PREFIX)) {
key = key.substring(Headers.S3_USER_METADATA_PREFIX.length());
metadata.addUserMetadata(key, header.getValue());
} else if (ignoredHeaders.contains(key)) {
// ignore...
} else if (key.equals(Headers.LAST_MODIFIED)) {
try {
metadata.setHeader(key, ServiceUtils.parseRfc822Date(header.getValue()));
} catch (ParseException pe) {
log.warn("Unable to parse last modified date: " + header.getValue(), pe);
}
} else if (key.equals(Headers.CONTENT_LENGTH)) {
try {
metadata.setHeader(key, Long.parseLong(header.getValue()));
} catch (NumberFormatException nfe) {
log.warn("Unable to parse content length: " + header.getValue(), nfe);
}
} else if (key.equals(Headers.ETAG)) {
metadata.setHeader(key, ServiceUtils.removeQuotes(header.getValue()));
} else if (key.equals(Headers.EXPIRES)) {
try {
metadata.setHttpExpiresDate(new DateUtils().parseRfc822Date(header.getValue()));
} catch (ParseException pe) {
log.warn("Unable to parse http expiration date: " + header.getValue(), pe);
}
} else if (key.equals(Headers.EXPIRATION)) {
new ObjectExpirationHeaderHandler<ObjectMetadata>().handle(metadata, response);
} else if (key.equals(Headers.RESTORE)) {
new ObjectRestoreHeaderHandler<ObjectRestoreResult>().handle(metadata, response);
} else {
metadata.setHeader(key, header.getValue());
}
}
}
}