| 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.blobstore.strategy.internal; |
| 20 | |
| 21 | import javax.inject.Singleton; |
| 22 | |
| 23 | import org.jclouds.blobstore.BlobStore; |
| 24 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 25 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 26 | import org.jclouds.blobstore.functions.ResourceMetadataToRelativePathResourceMetadata; |
| 27 | import org.jclouds.blobstore.reference.BlobStoreConstants; |
| 28 | import org.jclouds.blobstore.strategy.GetDirectoryStrategy; |
| 29 | |
| 30 | import com.google.inject.Inject; |
| 31 | |
| 32 | /** |
| 33 | * Key-value implementations of BlobStore, such as S3, do not have directories. In following the |
| 34 | * rackspace cloud files project, we use an empty object '#{dirpath}' with content type set to |
| 35 | * 'application/directory'. |
| 36 | * |
| 37 | * <p/> |
| 38 | * To interoperate with other S3 tools, we accept the following ways to tell if the directory |
| 39 | * exists: |
| 40 | * <ul> |
| 41 | * <li>an object named '#{dirpath}_$folder$' or '#{dirpath}/' denoting a directory marker</li> |
| 42 | * <li>an object with content type set to 'application/directory' denoting a directory marker</li> |
| 43 | * <li>if there exists any objects with the prefix "#{dirpath}/", then the directory is said to |
| 44 | * exist</li> |
| 45 | * <li>if both a file with the name of a directory and a marker for that directory exists, then the |
| 46 | * *file masks the directory*, and the directory is never returned.</li> |
| 47 | * </ul> |
| 48 | * |
| 49 | * @see MarkerFileMkdirStrategy |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class MarkersGetDirectoryStrategy implements GetDirectoryStrategy { |
| 54 | |
| 55 | protected final ResourceMetadataToRelativePathResourceMetadata resource2Directory; |
| 56 | private final BlobStore connection; |
| 57 | |
| 58 | @Inject |
| 59 | public MarkersGetDirectoryStrategy(BlobStore connection, |
| 60 | ResourceMetadataToRelativePathResourceMetadata resource2Directory) { |
| 61 | this.connection = connection; |
| 62 | this.resource2Directory = resource2Directory; |
| 63 | } |
| 64 | |
| 65 | public StorageMetadata execute(String containerName, String directory) { |
| 66 | BlobMetadata md = connection.blobMetadata(containerName, directory); |
| 67 | if (md != null && md.getContentMetadata().getContentType().equals("application/directory")) |
| 68 | return resource2Directory.apply(md); |
| 69 | for (String suffix : BlobStoreConstants.DIRECTORY_SUFFIXES) { |
| 70 | md = connection.blobMetadata(containerName, directory + suffix); |
| 71 | if (md != null) |
| 72 | return resource2Directory.apply(md); |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 | } |