| 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 java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.s3.domain.ListBucketResponse; |
| 28 | import org.jclouds.blobstore.domain.PageSet; |
| 29 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 30 | import org.jclouds.blobstore.domain.StorageType; |
| 31 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 32 | |
| 33 | import com.google.common.base.Function; |
| 34 | import com.google.common.collect.Iterables; |
| 35 | import com.google.common.collect.Maps; |
| 36 | import com.google.common.collect.Sets; |
| 37 | |
| 38 | /** |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | @Singleton |
| 42 | public class BucketToResourceList implements |
| 43 | Function<ListBucketResponse, PageSet<? extends StorageMetadata>> { |
| 44 | private final ObjectToBlobMetadata object2blobMd; |
| 45 | private final CommonPrefixesToResourceMetadata prefix2ResourceMd; |
| 46 | |
| 47 | protected final Function<StorageMetadata, String> indexer = new Function<StorageMetadata, String>() { |
| 48 | @Override |
| 49 | public String apply(StorageMetadata from) { |
| 50 | return from.getName(); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | @Inject |
| 55 | public BucketToResourceList(ObjectToBlobMetadata object2blobMd, |
| 56 | CommonPrefixesToResourceMetadata prefix2ResourceMd) { |
| 57 | this.object2blobMd = object2blobMd; |
| 58 | this.prefix2ResourceMd = prefix2ResourceMd; |
| 59 | } |
| 60 | |
| 61 | public PageSet<? extends StorageMetadata> apply(ListBucketResponse from) { |
| 62 | Set<StorageMetadata> contents = Sets.<StorageMetadata> newHashSet(Iterables.transform(from, |
| 63 | object2blobMd)); |
| 64 | |
| 65 | Map<String, StorageMetadata> nameToMd = Maps.uniqueIndex(contents, indexer); |
| 66 | for (String prefix : from.getCommonPrefixes()) { |
| 67 | prefix = prefix.endsWith("/") ? prefix.substring(0, prefix.lastIndexOf('/')) : prefix; |
| 68 | if (!nameToMd.containsKey(prefix) |
| 69 | || nameToMd.get(prefix).getType() != StorageType.RELATIVE_PATH) |
| 70 | contents.add(prefix2ResourceMd.apply(prefix)); |
| 71 | } |
| 72 | return new PageSetImpl<StorageMetadata>(contents, from.getNextMarker()); |
| 73 | } |
| 74 | } |