| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.aws.s3.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static org.jclouds.crypto.CryptoStreams.base64; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | import javax.ws.rs.core.HttpHeaders; |
| 28 | |
| 29 | import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix; |
| 30 | import org.jclouds.http.HttpRequest; |
| 31 | import org.jclouds.http.utils.ModifyRequest; |
| 32 | import org.jclouds.rest.Binder; |
| 33 | import org.jclouds.s3.domain.ObjectMetadata; |
| 34 | |
| 35 | import com.google.common.collect.ImmutableMultimap; |
| 36 | import com.google.common.collect.ImmutableMultimap.Builder; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class BindObjectMetadataToRequest implements Binder { |
| 44 | protected final BindMapToHeadersWithPrefix metadataPrefixer; |
| 45 | |
| 46 | @Inject |
| 47 | public BindObjectMetadataToRequest(BindMapToHeadersWithPrefix metadataPrefixer) { |
| 48 | this.metadataPrefixer = checkNotNull(metadataPrefixer, "metadataPrefixer"); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 53 | checkArgument(checkNotNull(input, "input") instanceof ObjectMetadata, |
| 54 | "this binder is only valid for ObjectMetadata!"); |
| 55 | checkNotNull(request, "request"); |
| 56 | |
| 57 | ObjectMetadata md = ObjectMetadata.class.cast(input); |
| 58 | checkArgument(md.getKey() != null, "objectMetadata.getKey() must be set!"); |
| 59 | |
| 60 | request = metadataPrefixer.bindToRequest(request, md.getUserMetadata()); |
| 61 | |
| 62 | Builder<String, String> headers = ImmutableMultimap.<String, String> builder(); |
| 63 | if (md.getCacheControl() != null) { |
| 64 | headers.put(HttpHeaders.CACHE_CONTROL, md.getCacheControl()); |
| 65 | } |
| 66 | |
| 67 | if (md.getContentMetadata().getContentDisposition() != null) { |
| 68 | headers.put("Content-Disposition", md.getContentMetadata().getContentDisposition()); |
| 69 | } |
| 70 | |
| 71 | if (md.getContentMetadata().getContentEncoding() != null) { |
| 72 | headers.put("Content-Encoding", md.getContentMetadata().getContentEncoding()); |
| 73 | } |
| 74 | if (md.getContentMetadata().getContentType() != null) { |
| 75 | headers.put(HttpHeaders.CONTENT_TYPE, md.getContentMetadata().getContentType()); |
| 76 | } else { |
| 77 | headers.put(HttpHeaders.CONTENT_TYPE, "binary/octet-stream"); |
| 78 | } |
| 79 | |
| 80 | if (md.getContentMetadata().getContentMD5() != null) { |
| 81 | headers.put("Content-MD5", base64(md.getContentMetadata().getContentMD5())); |
| 82 | } |
| 83 | |
| 84 | request = ModifyRequest.replaceHeaders(request, headers.build()); |
| 85 | return request; |
| 86 | } |
| 87 | } |