| 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.openstack.swift.blobstore; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.blobstore.util.BlobStoreUtils.createParentIfNeededAsync; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Provider; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.blobstore.BlobStoreContext; |
| 31 | import org.jclouds.blobstore.domain.Blob; |
| 32 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 33 | import org.jclouds.blobstore.domain.PageSet; |
| 34 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 35 | import org.jclouds.blobstore.domain.internal.PageSetImpl; |
| 36 | import org.jclouds.blobstore.functions.BlobToHttpGetOptions; |
| 37 | import org.jclouds.blobstore.internal.BaseBlobStore; |
| 38 | import org.jclouds.blobstore.options.CreateContainerOptions; |
| 39 | import org.jclouds.blobstore.options.ListContainerOptions; |
| 40 | import org.jclouds.blobstore.options.PutOptions; |
| 41 | import org.jclouds.blobstore.strategy.internal.FetchBlobMetadata; |
| 42 | import org.jclouds.blobstore.util.BlobUtils; |
| 43 | import org.jclouds.collect.Memoized; |
| 44 | import org.jclouds.domain.Location; |
| 45 | import org.jclouds.http.options.GetOptions; |
| 46 | import org.jclouds.openstack.swift.CommonSwiftClient; |
| 47 | import org.jclouds.openstack.swift.blobstore.functions.BlobStoreListContainerOptionsToListContainerOptions; |
| 48 | import org.jclouds.openstack.swift.blobstore.functions.BlobToObject; |
| 49 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceList; |
| 50 | import org.jclouds.openstack.swift.blobstore.functions.ContainerToResourceMetadata; |
| 51 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlob; |
| 52 | import org.jclouds.openstack.swift.blobstore.functions.ObjectToBlobMetadata; |
| 53 | import org.jclouds.openstack.swift.domain.ContainerMetadata; |
| 54 | |
| 55 | import com.google.common.base.Function; |
| 56 | import com.google.common.base.Supplier; |
| 57 | import com.google.common.collect.Iterables; |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | * @author Adrian Cole |
| 62 | */ |
| 63 | @Singleton |
| 64 | public class SwiftBlobStore extends BaseBlobStore { |
| 65 | private final CommonSwiftClient sync; |
| 66 | private final ContainerToResourceMetadata container2ResourceMd; |
| 67 | private final BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions; |
| 68 | private final ContainerToResourceList container2ResourceList; |
| 69 | private final ObjectToBlob object2Blob; |
| 70 | private final BlobToObject blob2Object; |
| 71 | private final ObjectToBlobMetadata object2BlobMd; |
| 72 | private final BlobToHttpGetOptions blob2ObjectGetOptions; |
| 73 | private final Provider<FetchBlobMetadata> fetchBlobMetadataProvider; |
| 74 | |
| 75 | @Inject |
| 76 | protected SwiftBlobStore(BlobStoreContext context, BlobUtils blobUtils, Supplier<Location> defaultLocation, |
| 77 | @Memoized Supplier<Set<? extends Location>> locations, CommonSwiftClient sync, |
| 78 | ContainerToResourceMetadata container2ResourceMd, |
| 79 | BlobStoreListContainerOptionsToListContainerOptions container2ContainerListOptions, |
| 80 | ContainerToResourceList container2ResourceList, ObjectToBlob object2Blob, BlobToObject blob2Object, |
| 81 | ObjectToBlobMetadata object2BlobMd, BlobToHttpGetOptions blob2ObjectGetOptions, |
| 82 | Provider<FetchBlobMetadata> fetchBlobMetadataProvider) { |
| 83 | super(context, blobUtils, defaultLocation, locations); |
| 84 | this.sync = sync; |
| 85 | this.container2ResourceMd = container2ResourceMd; |
| 86 | this.container2ContainerListOptions = container2ContainerListOptions; |
| 87 | this.container2ResourceList = container2ResourceList; |
| 88 | this.object2Blob = object2Blob; |
| 89 | this.blob2Object = blob2Object; |
| 90 | this.object2BlobMd = object2BlobMd; |
| 91 | this.blob2ObjectGetOptions = blob2ObjectGetOptions; |
| 92 | this.fetchBlobMetadataProvider = checkNotNull(fetchBlobMetadataProvider, "fetchBlobMetadataProvider"); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * This implementation invokes {@link CommonSwiftClient#listContainers} |
| 97 | */ |
| 98 | @Override |
| 99 | public PageSet<? extends StorageMetadata> list() { |
| 100 | return new Function<Set<ContainerMetadata>, org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata>>() { |
| 101 | public org.jclouds.blobstore.domain.PageSet<? extends StorageMetadata> apply(Set<ContainerMetadata> from) { |
| 102 | return new PageSetImpl<StorageMetadata>(Iterables.transform(from, container2ResourceMd), null); |
| 103 | } |
| 104 | }.apply(sync.listContainers()); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * This implementation invokes {@link CommonSwiftClient#containerExists} |
| 109 | * |
| 110 | * @param container |
| 111 | * container name |
| 112 | */ |
| 113 | @Override |
| 114 | public boolean containerExists(String container) { |
| 115 | return sync.containerExists(container); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * This implementation invokes {@link CommonSwiftClient#putBucketInRegion} |
| 120 | * |
| 121 | * @param location |
| 122 | * currently ignored |
| 123 | * @param container |
| 124 | * container name |
| 125 | */ |
| 126 | @Override |
| 127 | public boolean createContainerInLocation(Location location, String container) { |
| 128 | return sync.createContainer(container); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * This implementation invokes {@link CommonSwiftClient#listObjects} |
| 133 | * |
| 134 | * @param container |
| 135 | * container name |
| 136 | */ |
| 137 | @Override |
| 138 | public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) { |
| 139 | org.jclouds.openstack.swift.options.ListContainerOptions httpOptions = container2ContainerListOptions |
| 140 | .apply(options); |
| 141 | PageSet<? extends StorageMetadata> list = container2ResourceList.apply(sync.listObjects(container, httpOptions)); |
| 142 | return options.isDetailed() ? fetchBlobMetadataProvider.get().setContainerName(container).apply(list) : list; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * This implementation invokes {@link CommonSwiftClient#blobExists} |
| 147 | * |
| 148 | * @param container |
| 149 | * container name |
| 150 | * @param key |
| 151 | * file name |
| 152 | */ |
| 153 | @Override |
| 154 | public boolean blobExists(String container, String key) { |
| 155 | return sync.objectExists(container, key); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * This implementation invokes {@link CommonSwiftClient#getObjectInfo} |
| 160 | * |
| 161 | * @param container |
| 162 | * container name |
| 163 | * @param key |
| 164 | * file name |
| 165 | */ |
| 166 | @Override |
| 167 | public BlobMetadata blobMetadata(String container, String key) { |
| 168 | return object2BlobMd.apply(sync.getObjectInfo(container, key)); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This implementation invokes {@link CommonSwiftClient#getObject} |
| 173 | * |
| 174 | * @param container |
| 175 | * container name |
| 176 | * @param key |
| 177 | * file name |
| 178 | */ |
| 179 | @Override |
| 180 | public Blob getBlob(String container, String key, org.jclouds.blobstore.options.GetOptions optionsList) { |
| 181 | GetOptions httpOptions = blob2ObjectGetOptions.apply(optionsList); |
| 182 | return object2Blob.apply(sync.getObject(container, key, httpOptions)); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * This implementation invokes {@link CommonSwiftClient#putObject} |
| 187 | * |
| 188 | * @param container |
| 189 | * container name |
| 190 | * @param blob |
| 191 | * object |
| 192 | */ |
| 193 | @Override |
| 194 | public String putBlob(String container, Blob blob) { |
| 195 | createParentIfNeededAsync(context.getAsyncBlobStore(), container, blob); |
| 196 | return sync.putObject(container, blob2Object.apply(blob)); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * This implementation invokes {@link CommonSwiftClient#putObject} |
| 201 | * |
| 202 | * @param container |
| 203 | * container name |
| 204 | * @param blob |
| 205 | * object |
| 206 | */ |
| 207 | @Override |
| 208 | public String putBlob(String container, Blob blob, PutOptions options) { |
| 209 | // TODO implement options |
| 210 | return putBlob(container, blob); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * This implementation invokes {@link CommonSwiftClient#removeObject} |
| 215 | * |
| 216 | * @param container |
| 217 | * container name |
| 218 | * @param key |
| 219 | * file name |
| 220 | */ |
| 221 | @Override |
| 222 | public void removeBlob(String container, String key) { |
| 223 | sync.removeObject(container, key); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected boolean deleteAndVerifyContainerGone(String container) { |
| 228 | sync.deleteContainerIfEmpty(container); |
| 229 | return !sync.containerExists(container); |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public boolean createContainerInLocation(Location location, String container, CreateContainerOptions options) { |
| 234 | if (options.isPublicRead()) |
| 235 | throw new UnsupportedOperationException("publicRead"); |
| 236 | return createContainerInLocation(location, container); |
| 237 | } |
| 238 | } |