| 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; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.atmos.options.PutOptions.Builder.publicRead; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Provider; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.atmos.AtmosAsyncClient; |
| 32 | import org.jclouds.atmos.AtmosClient; |
| 33 | import org.jclouds.atmos.blobstore.functions.BlobStoreListOptionsToListOptions; |
| 34 | import org.jclouds.atmos.blobstore.functions.BlobToObject; |
| 35 | import org.jclouds.atmos.blobstore.functions.DirectoryEntryListToResourceMetadataList; |
| 36 | import org.jclouds.atmos.blobstore.functions.ObjectToBlob; |
| 37 | import org.jclouds.atmos.blobstore.functions.ObjectToBlobMetadata; |
| 38 | import org.jclouds.atmos.options.ListOptions; |
| 39 | import org.jclouds.atmos.util.AtmosUtils; |
| 40 | import org.jclouds.blobstore.BlobStoreContext; |
| 41 | import org.jclouds.blobstore.domain.Blob; |
| 42 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 43 | import org.jclouds.blobstore.domain.PageSet; |
| 44 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 45 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 46 | import org.jclouds.blobstore.internal.BaseBlobStore; |
| 47 | import org.jclouds.blobstore.options.CreateContainerOptions; |
| 48 | import org.jclouds.blobstore.options.PutOptions; |
| 49 | import org.jclouds.blobstore.strategy.internal.FetchBlobMetadata; |
| 50 | import org.jclouds.blobstore.util.BlobUtils; |
| 51 | import org.jclouds.collect.Memoized; |
| 52 | import org.jclouds.crypto.Crypto; |
| 53 | import org.jclouds.domain.Location; |
| 54 | import org.jclouds.http.options.GetOptions; |
| 55 | |
| 56 | import com.google.common.base.Supplier; |
| 57 | |
| 58 | /** |
| 59 | * @author Adrian Cole |
| 60 | */ |
| 61 | @Singleton |
| 62 | public class AtmosBlobStore extends BaseBlobStore { |
| 63 | private final AtmosClient sync; |
| 64 | private final ObjectToBlob object2Blob; |
| 65 | private final ObjectToBlobMetadata object2BlobMd; |
| 66 | private final BlobToObject blob2Object; |
| 67 | private final BlobStoreListOptionsToListOptions container2ContainerListOptions; |
| 68 | private final DirectoryEntryListToResourceMetadataList container2ResourceList; |
| 69 | private final Crypto crypto; |
| 70 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 71 | private final Provider<FetchBlobMetadata> fetchBlobMetadataProvider; |
| 72 | private final Map<String, Boolean> isPublic; |
| 73 | |
| 74 | @Inject |
| 75 | AtmosBlobStore(BlobStoreContext context, BlobUtils blobUtils, Supplier<Location> defaultLocation, |
| 76 | @Memoized Supplier<Set<? extends Location>> locations, AtmosClient sync, ObjectToBlob object2Blob, |
| 77 | ObjectToBlobMetadata object2BlobMd, BlobToObject blob2Object, |
| 78 | BlobStoreListOptionsToListOptions container2ContainerListOptions, |
| 79 | DirectoryEntryListToResourceMetadataList container2ResourceList, Crypto crypto, |
| 80 | BlobToHttpGetOptions blob2ObjectGetOptions, Provider<FetchBlobMetadata> fetchBlobMetadataProvider, |
| 81 | Map<String, Boolean> isPublic) { |
| 82 | super(context, blobUtils, defaultLocation, locations); |
| 83 | this.blob2ObjectGetOptions = checkNotNull(blob2ObjectGetOptions, "blob2ObjectGetOptions"); |
| 84 | this.sync = checkNotNull(sync, "sync"); |
| 85 | this.container2ContainerListOptions = checkNotNull(container2ContainerListOptions, |
| 86 | "container2ContainerListOptions"); |
| 87 | this.container2ResourceList = checkNotNull(container2ResourceList, "container2ResourceList"); |
| 88 | this.object2Blob = checkNotNull(object2Blob, "object2Blob"); |
| 89 | this.blob2Object = checkNotNull(blob2Object, "blob2Object"); |
| 90 | this.object2BlobMd = checkNotNull(object2BlobMd, "object2BlobMd"); |
| 91 | this.crypto = checkNotNull(crypto, "crypto"); |
| 92 | this.fetchBlobMetadataProvider = checkNotNull(fetchBlobMetadataProvider, "fetchBlobMetadataProvider"); |
| 93 | this.isPublic = checkNotNull(isPublic, "isPublic"); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * This implementation invokes {@link AtmosClient#headFile} |
| 98 | */ |
| 99 | @Override |
| 100 | public BlobMetadata blobMetadata(String container, String key) { |
| 101 | return object2BlobMd.apply(sync.headFile(container + "/" + key)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * This implementation invokes {@link AtmosAsyncClient#deletePath} followed by |
| 106 | * {@link AtmosAsyncClient#pathExists} until it is true. |
| 107 | */ |
| 108 | protected boolean deleteAndVerifyContainerGone(final String container) { |
| 109 | sync.deletePath(container + "/"); |
| 110 | return !sync.pathExists(container + "/"); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * This implementation invokes {@link AtmosClient#createDirectory} |
| 115 | * |
| 116 | * @param location |
| 117 | * currently ignored |
| 118 | * @param container |
| 119 | * directory name |
| 120 | */ |
| 121 | @Override |
| 122 | public boolean createContainerInLocation(Location location, String container) { |
| 123 | sync.createDirectory(container); |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * This implementation invokes {@link AtmosClient#createDirectory} |
| 129 | * |
| 130 | * @param container |
| 131 | * directory name |
| 132 | */ |
| 133 | @Override |
| 134 | public void createDirectory(String container, String directory) { |
| 135 | sync.createDirectory(container + "/" + directory); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * This implementation invokes {@link #removeBlob} |
| 140 | */ |
| 141 | @Override |
| 142 | public void deleteDirectory(String containerName, String directory) { |
| 143 | removeBlob(containerName, directory + "/"); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * This implementation invokes {@link AtmosClient#pathExists} |
| 148 | */ |
| 149 | @Override |
| 150 | public boolean containerExists(String container) { |
| 151 | return sync.pathExists(container + "/"); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This implementation invokes {@link AtmosClient#pathExists} |
| 156 | */ |
| 157 | @Override |
| 158 | public boolean directoryExists(String container, String directory) { |
| 159 | return sync.pathExists(container + "/" + directory + "/"); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * This implementation invokes {@link AtmosClient#pathExists} |
| 164 | * |
| 165 | * @param container |
| 166 | * container |
| 167 | * @param key |
| 168 | * file name |
| 169 | */ |
| 170 | @Override |
| 171 | public boolean blobExists(String container, String key) { |
| 172 | return sync.pathExists(container + "/" + key); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * This implementation invokes {@link AtmosClient#readFile} |
| 177 | */ |
| 178 | @Override |
| 179 | public Blob getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions options) { |
| 180 | GetOptions httpOptions = blob2ObjectGetOptions.apply(options); |
| 181 | return object2Blob.apply(sync.readFile(container + "/" + key, httpOptions)); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * This implementation invokes {@link AtmosClient#listDirectories} |
| 186 | */ |
| 187 | @Override |
| 188 | public PageSet<? extends StorageMetadata> list() { |
| 189 | return container2ResourceList.apply(sync.listDirectories()); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * This implementation invokes {@link AtmosClient#listDirectory} |
| 194 | */ |
| 195 | @Override |
| 196 | public PageSet<? extends StorageMetadata> list(String container, |
| 197 | org.jclouds.blobstore.options.ListContainerOptions options) { |
| 198 | container = AtmosUtils.adjustContainerIfDirOptionPresent(container, options); |
| 199 | ListOptions nativeOptions = container2ContainerListOptions.apply(options); |
| 200 | // until includeMeta() option works for namespace interface |
| 201 | PageSet<? extends StorageMetadata> list = container2ResourceList.apply(sync.listDirectory(container, |
| 202 | nativeOptions)); |
| 203 | return options.isDetailed() ? fetchBlobMetadataProvider.get().setContainerName(container).apply(list) : list; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * This implementation invokes {@link AtmosClient#createFile} |
| 208 | * <p/> |
| 209 | * Since there is no etag support in atmos, we just return the path. |
| 210 | */ |
| 211 | @Override |
| 212 | public String putBlob(final String container, final Blob blob) { |
| 213 | final org.jclouds.atmos.options.PutOptions options = new org.jclouds.atmos.options.PutOptions(); |
| 214 | try { |
| 215 | if (isPublic.get(container + "/")) |
| 216 | options.publicRead(); |
| 217 | } catch (NullPointerException e) { |
| 218 | // MapMaker |
| 219 | } |
| 220 | return AtmosUtils.putBlob(sync, crypto, blob2Object, container, blob, options); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * This implementation invokes {@link AtmosClient#createFile} |
| 225 | * <p/> |
| 226 | * Since there is no etag support in atmos, we just return the path. |
| 227 | */ |
| 228 | @Override |
| 229 | public String putBlob(String container, Blob blob, PutOptions options) { |
| 230 | // TODO implement options |
| 231 | return putBlob(container, blob); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * This implementation invokes {@link AtmosClient#deletePath} |
| 236 | */ |
| 237 | @Override |
| 238 | public void removeBlob(String container, String key) { |
| 239 | sync.deletePath(container + "/" + key); |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public boolean createContainerInLocation(Location location, String container, CreateContainerOptions options) { |
| 244 | if (options.isPublicRead()) { |
| 245 | sync.createDirectory(container, publicRead()); |
| 246 | return true; |
| 247 | } |
| 248 | return createContainerInLocation(location, container); |
| 249 | } |
| 250 | } |