-
Notifications
You must be signed in to change notification settings - Fork 132
/
ParseObjectMetadataFromHeaders.java
110 lines (97 loc) · 4.53 KB
/
ParseObjectMetadataFromHeaders.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.jclouds.s3.functions;
import static com.google.common.io.BaseEncoding.base16;
import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.functions.ParseSystemAndUserMetadataFromHeaders;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.InvocationContext;
import org.jclouds.s3.blobstore.functions.BlobToObjectMetadata;
import org.jclouds.s3.domain.MutableObjectMetadata;
import org.jclouds.s3.domain.ObjectMetadata.StorageClass;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.net.HttpHeaders;
/** This parses {@ link MutableObjectMetadata} from HTTP headers. */
public class ParseObjectMetadataFromHeaders implements Function<HttpResponse, MutableObjectMetadata>,
InvocationContext<ParseObjectMetadataFromHeaders> {
private final ParseSystemAndUserMetadataFromHeaders blobMetadataParser;
private final BlobToObjectMetadata blobToObjectMetadata;
private final String userMdPrefix;
@Inject
public ParseObjectMetadataFromHeaders(ParseSystemAndUserMetadataFromHeaders blobMetadataParser,
BlobToObjectMetadata blobToObjectMetadata, @Named(PROPERTY_USER_METADATA_PREFIX) String userMdPrefix) {
this.blobMetadataParser = blobMetadataParser;
this.blobToObjectMetadata = blobToObjectMetadata;
this.userMdPrefix = userMdPrefix;
}
// eTag pattern can be "a34d7e626b350d2e326196085dfa52f4-1", which is opaque and shouldn't be
// used as content-md5, so filter etags that contain hyphens
static final Pattern MD5_FROM_ETAG = Pattern.compile("^\"?([0-9a-f]+)\"?$");
/** parses the http response headers to create a new {@link MutableObjectMetadata} object. */
public MutableObjectMetadata apply(HttpResponse from) {
BlobMetadata base = blobMetadataParser.apply(from);
MutableObjectMetadata to = blobToObjectMetadata.apply(base);
addETagTo(from, to);
if (to.getContentMetadata().getContentMD5() == null && to.getETag() != null) {
Matcher md5Matcher = MD5_FROM_ETAG.matcher(to.getETag());
if (md5Matcher.find()) {
byte[] md5 = base16().lowerCase().decode(md5Matcher.group(1));
// it is possible others will look at the http payload directly
if (from.getPayload() != null)
from.getPayload().getContentMetadata().setContentMD5(md5);
to.getContentMetadata().setContentMD5(md5);
}
}
// amz has an etag, but matches syntax for usermetadata
to.getUserMetadata().remove("object-etag");
to.setCacheControl(from.getFirstHeaderOrNull(HttpHeaders.CACHE_CONTROL));
String storageClass = from.getFirstHeaderOrNull("x-amz-storage-class");
if (storageClass != null) {
to.setStorageClass(StorageClass.valueOf(storageClass));
}
return to;
}
/**
* ETag == Content-MD5
*/
@VisibleForTesting
protected void addETagTo(HttpResponse from, MutableObjectMetadata metadata) {
if (metadata.getETag() == null) {
String eTagHeader = from.getFirstHeaderOrNull(userMdPrefix + "object-eTag");
if (eTagHeader != null) {
metadata.setETag(eTagHeader);
}
}
}
@Override
public ParseObjectMetadataFromHeaders setContext(HttpRequest request) {
blobMetadataParser.setContext(request);
blobToObjectMetadata.setContext(request);
return this;
}
public ParseObjectMetadataFromHeaders setKey(String key) {
blobMetadataParser.setName(key);
return this;
}
}