1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.openstack.swift.blobstore.functions; |
20 | |
21 | import java.util.Map.Entry; |
22 | |
23 | import javax.inject.Singleton; |
24 | |
25 | import org.jclouds.blobstore.domain.BlobMetadata; |
26 | import org.jclouds.blobstore.domain.StorageMetadata; |
27 | import org.jclouds.blobstore.domain.StorageType; |
28 | import org.jclouds.crypto.CryptoStreams; |
29 | import org.jclouds.openstack.swift.domain.MutableObjectInfoWithMetadata; |
30 | import org.jclouds.openstack.swift.domain.internal.MutableObjectInfoWithMetadataImpl; |
31 | |
32 | import com.google.common.base.Function; |
33 | |
34 | /** |
35 | * @author Adrian Cole |
36 | */ |
37 | @Singleton |
38 | public class ResourceToObjectInfo implements Function<StorageMetadata, MutableObjectInfoWithMetadata> { |
39 | |
40 | public MutableObjectInfoWithMetadata apply(StorageMetadata from) { |
41 | if (from == null) |
42 | return null; |
43 | MutableObjectInfoWithMetadata to = new MutableObjectInfoWithMetadataImpl(); |
44 | if (from.getType() == StorageType.BLOB) { |
45 | to.setContentType(((BlobMetadata) from).getContentMetadata().getContentType()); |
46 | to.setBytes(((BlobMetadata) from).getContentMetadata().getContentLength()); |
47 | to.setHash(((BlobMetadata) from).getContentMetadata().getContentMD5()); |
48 | } else if (from.getType() == StorageType.RELATIVE_PATH) { |
49 | to.setContentType("application/directory"); |
50 | } |
51 | if (from.getETag() != null && to.getHash() == null) |
52 | to.setHash(CryptoStreams.hex(from.getETag())); |
53 | to.setName(from.getName()); |
54 | to.setLastModified(from.getLastModified()); |
55 | if (from.getUserMetadata() != null) { |
56 | for (Entry<String, String> entry : from.getUserMetadata().entrySet()) |
57 | to.getMetadata().put(entry.getKey().toLowerCase(), entry.getValue()); |
58 | } |
59 | return to; |
60 | } |
61 | |
62 | } |