| 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.atmos.blobstore.functions; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | import java.util.Map.Entry; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.atmos.domain.AtmosObject; |
| 29 | import org.jclouds.atmos.domain.FileType; |
| 30 | import org.jclouds.atmos.filters.ShareUrl; |
| 31 | import org.jclouds.atmos.functions.AtmosObjectName; |
| 32 | import org.jclouds.blobstore.domain.MutableBlobMetadata; |
| 33 | import org.jclouds.blobstore.domain.StorageType; |
| 34 | import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl; |
| 35 | import org.jclouds.http.HttpUtils; |
| 36 | |
| 37 | import com.google.common.base.Function; |
| 38 | import com.google.common.base.Splitter; |
| 39 | import com.google.common.collect.ImmutableSet; |
| 40 | import com.google.common.collect.Iterables; |
| 41 | import com.google.common.collect.Maps; |
| 42 | |
| 43 | /** |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class ObjectToBlobMetadata implements Function<AtmosObject, MutableBlobMetadata> { |
| 48 | private final AtmosObjectName objectName; |
| 49 | private final ShareUrl shareUrl; |
| 50 | |
| 51 | private static final Set<String> systemMetadata = ImmutableSet.of("atime", "mtime", "ctime", "itime", "type", "uid", |
| 52 | "gid", "objectid", "objname", "size", "nlink", "policyname", "content-md5"); |
| 53 | |
| 54 | @Inject |
| 55 | protected ObjectToBlobMetadata(AtmosObjectName objectName, ShareUrl shareUrl) |
| 56 | throws SecurityException, NoSuchMethodException { |
| 57 | this.objectName = objectName; |
| 58 | this.shareUrl = shareUrl; |
| 59 | } |
| 60 | |
| 61 | public MutableBlobMetadata apply(AtmosObject from) { |
| 62 | if (from == null) |
| 63 | return null; |
| 64 | MutableBlobMetadata to = new MutableBlobMetadataImpl(); |
| 65 | to.setId(from.getSystemMetadata().getObjectID()); |
| 66 | to.setLastModified(from.getSystemMetadata().getLastUserDataModification()); |
| 67 | HttpUtils.copy(from.getContentMetadata(), to.getContentMetadata()); |
| 68 | to.setName(objectName.apply(from)); |
| 69 | to.setUri(from.getContentMetadata().getUri()); |
| 70 | to.setContainer(Iterables.get(Splitter.on('/').split(from.getContentMetadata().getPath()),0)); |
| 71 | if (from.getAllHeaders().containsEntry("x-emc-groupacl", "other=READ")) |
| 72 | to.setPublicUri(shareUrl.apply(from.getContentMetadata().getPath())); |
| 73 | if (from.getSystemMetadata().getType() == FileType.DIRECTORY) { |
| 74 | to.setType(StorageType.FOLDER); |
| 75 | } else { |
| 76 | to.setType(StorageType.BLOB); |
| 77 | } |
| 78 | Map<String, String> lowerKeyMetadata = Maps.newHashMap(); |
| 79 | for (Entry<String, String> entry : from.getUserMetadata().getMetadata().entrySet()) { |
| 80 | String key = entry.getKey().toLowerCase(); |
| 81 | if (!systemMetadata.contains(key)) |
| 82 | lowerKeyMetadata.put(key, entry.getValue()); |
| 83 | } |
| 84 | to.setUserMetadata(lowerKeyMetadata); |
| 85 | return to; |
| 86 | } |
| 87 | } |