| 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.s3.blobstore.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | |
| 23 | import java.util.Map.Entry; |
| 24 | |
| 25 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 26 | import org.jclouds.http.HttpRequest; |
| 27 | import org.jclouds.http.HttpUtils; |
| 28 | import org.jclouds.rest.InvocationContext; |
| 29 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 30 | import org.jclouds.s3.domain.MutableObjectMetadata; |
| 31 | import org.jclouds.s3.domain.internal.MutableObjectMetadataImpl; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | |
| 35 | /** |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | public class BlobToObjectMetadata implements Function<BlobMetadata, MutableObjectMetadata>, |
| 39 | InvocationContext<BlobToObjectMetadata> { |
| 40 | private String bucket; |
| 41 | |
| 42 | public MutableObjectMetadata apply(BlobMetadata from) { |
| 43 | if (from == null) |
| 44 | return null; |
| 45 | MutableObjectMetadata to = new MutableObjectMetadataImpl(); |
| 46 | HttpUtils.copy(from.getContentMetadata(), to.getContentMetadata()); |
| 47 | to.setUri(from.getUri()); |
| 48 | to.setETag(from.getETag()); |
| 49 | to.setKey(from.getName()); |
| 50 | to.setBucket(bucket); |
| 51 | to.setLastModified(from.getLastModified()); |
| 52 | if (from.getUserMetadata() != null) { |
| 53 | for (Entry<String, String> entry : from.getUserMetadata().entrySet()) |
| 54 | to.getUserMetadata().put(entry.getKey().toLowerCase(), entry.getValue()); |
| 55 | } |
| 56 | return to; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public BlobToObjectMetadata setContext(HttpRequest request) { |
| 61 | checkArgument(request instanceof GeneratedHttpRequest<?>, "note this handler requires a GeneratedHttpRequest"); |
| 62 | return setBucket(GeneratedHttpRequest.class.cast(request).getArgs().get(0).toString()); |
| 63 | } |
| 64 | |
| 65 | private BlobToObjectMetadata setBucket(String bucket) { |
| 66 | this.bucket = bucket; |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | } |