| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.blobstore.util; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | import org.jclouds.blobstore.AsyncBlobStore; |
| 29 | import org.jclouds.blobstore.BlobStore; |
| 30 | import org.jclouds.blobstore.BlobStoreContextBuilder; |
| 31 | import org.jclouds.blobstore.ContainerNotFoundException; |
| 32 | import org.jclouds.blobstore.KeyNotFoundException; |
| 33 | import org.jclouds.blobstore.domain.Blob; |
| 34 | import org.jclouds.blobstore.domain.BlobMetadata; |
| 35 | import org.jclouds.blobstore.domain.StorageMetadata; |
| 36 | import org.jclouds.blobstore.functions.BlobName; |
| 37 | import org.jclouds.functions.ExceptionToValueOrPropagate; |
| 38 | import org.jclouds.http.HttpRequest; |
| 39 | import org.jclouds.http.HttpRequestFilter; |
| 40 | import org.jclouds.http.HttpUtils; |
| 41 | import org.jclouds.rest.Providers; |
| 42 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 43 | import org.jclouds.util.Strings2; |
| 44 | |
| 45 | import com.google.common.collect.ImmutableMultimap; |
| 46 | import com.google.common.util.concurrent.Futures; |
| 47 | import com.google.common.util.concurrent.ListenableFuture; |
| 48 | |
| 49 | /** |
| 50 | * |
| 51 | * @author Adrian Cole |
| 52 | */ |
| 53 | public class BlobStoreUtils { |
| 54 | public static <T> HttpRequest cleanRequest(HttpRequest returnVal) { |
| 55 | checkNotNull(returnVal, "http request"); |
| 56 | for (HttpRequestFilter filter : returnVal.getFilters()) |
| 57 | returnVal = filter.filter(returnVal); |
| 58 | HttpRequest toReturn = new HttpRequest(returnVal.getMethod(), returnVal.getEndpoint(), |
| 59 | ImmutableMultimap.copyOf(returnVal.getHeaders())); |
| 60 | if (returnVal.getPayload() != null) |
| 61 | toReturn.setPayload(returnVal.getPayload()); |
| 62 | return toReturn; |
| 63 | } |
| 64 | |
| 65 | public static final ExceptionToValueOrPropagate<KeyNotFoundException, ?> keyNotFoundToNullOrPropagate = new ExceptionToValueOrPropagate<KeyNotFoundException, Object>( |
| 66 | KeyNotFoundException.class, null); |
| 67 | |
| 68 | public static final ExceptionToValueOrPropagate<ContainerNotFoundException, ?> containerNotFoundToNullOrPropagate = new ExceptionToValueOrPropagate<ContainerNotFoundException, Object>( |
| 69 | ContainerNotFoundException.class, null); |
| 70 | |
| 71 | @SuppressWarnings("unchecked") |
| 72 | public static <T> T keyNotFoundToNullOrPropagate(Exception e) { |
| 73 | return (T) keyNotFoundToNullOrPropagate.apply(e); |
| 74 | } |
| 75 | |
| 76 | @SuppressWarnings("unchecked") |
| 77 | public static <T> T containerNotFoundToNullOrPropagate(Exception e) { |
| 78 | return (T) containerNotFoundToNullOrPropagate.apply(e); |
| 79 | } |
| 80 | |
| 81 | public static Blob newBlob(BlobStore blobStore, StorageMetadata blobMeta) { |
| 82 | Blob blob = checkNotNull(blobStore, "blobStore").blobBuilder(checkNotNull(blobMeta, "blobMeta").getName()) |
| 83 | .userMetadata(blobMeta.getUserMetadata()).build(); |
| 84 | if (blobMeta instanceof BlobMetadata) { |
| 85 | HttpUtils.copy(((BlobMetadata) blobMeta).getContentMetadata(), blob.getMetadata().getContentMetadata()); |
| 86 | } |
| 87 | blob.getMetadata().setETag(blobMeta.getETag()); |
| 88 | blob.getMetadata().setId(blobMeta.getProviderId()); |
| 89 | blob.getMetadata().setLastModified(blobMeta.getLastModified()); |
| 90 | blob.getMetadata().setLocation(blobMeta.getLocation()); |
| 91 | blob.getMetadata().setUri(blobMeta.getUri()); |
| 92 | return blob; |
| 93 | } |
| 94 | |
| 95 | public static String parseContainerFromPath(String path) { |
| 96 | String container = checkNotNull(path, "path"); |
| 97 | if (path.indexOf('/') != -1) |
| 98 | container = path.substring(0, path.indexOf('/')); |
| 99 | return container; |
| 100 | } |
| 101 | |
| 102 | public static String parsePrefixFromPath(String path) { |
| 103 | String prefix = null; |
| 104 | if (checkNotNull(path, "path").indexOf('/') != -1) |
| 105 | prefix = path.substring(path.indexOf('/') + 1); |
| 106 | return "".equals(prefix) ? null : prefix; |
| 107 | } |
| 108 | |
| 109 | public static String parseDirectoryFromPath(String path) { |
| 110 | return checkNotNull(path, "path").substring(0, path.lastIndexOf('/')); |
| 111 | } |
| 112 | |
| 113 | private static Pattern keyFromContainer = Pattern.compile("/?[^/]+/(.*)"); |
| 114 | |
| 115 | public static String getNameFor(GeneratedHttpRequest<?> request) { |
| 116 | checkNotNull(request, "request"); |
| 117 | // assume first params are container and key |
| 118 | if (request.getArgs().size() >= 2 && request.getArgs().get(0) instanceof String |
| 119 | && request.getArgs().get(1) instanceof String) { |
| 120 | return request.getArgs().get(1).toString(); |
| 121 | } else if (request.getArgs().size() >= 1 && request.getArgs().get(0) instanceof String) { |
| 122 | Matcher matcher = keyFromContainer.matcher(request.getArgs().get(0).toString()); |
| 123 | if (matcher.find()) |
| 124 | return matcher.group(1); |
| 125 | } |
| 126 | String objectKey = request.getEndpoint().getPath(); |
| 127 | if (objectKey.startsWith("/")) { |
| 128 | // Trim initial slash from object key name. |
| 129 | objectKey = objectKey.substring(1); |
| 130 | } |
| 131 | return objectKey; |
| 132 | } |
| 133 | |
| 134 | public static String getContentAsStringOrNullAndClose(Blob blob) throws IOException { |
| 135 | checkNotNull(blob, "blob"); |
| 136 | checkNotNull(blob.getPayload(), "blob.payload"); |
| 137 | if (blob.getPayload().getInput() == null) |
| 138 | return null; |
| 139 | Object o = blob.getPayload().getInput(); |
| 140 | if (o instanceof InputStream) { |
| 141 | return Strings2.toStringAndClose((InputStream) o); |
| 142 | } else { |
| 143 | throw new IllegalArgumentException("Object type not supported: " + o.getClass().getName()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | private static final BlobName blobName = new BlobName(); |
| 148 | |
| 149 | public static ListenableFuture<Void> createParentIfNeededAsync(AsyncBlobStore asyncBlobStore, String container, |
| 150 | Blob blob) { |
| 151 | checkNotNull(asyncBlobStore, "asyncBlobStore"); |
| 152 | checkNotNull(container, "container"); |
| 153 | |
| 154 | String name = blobName.apply(blob); |
| 155 | if (name.indexOf('/') > 0) { |
| 156 | return asyncBlobStore.createDirectory(container, parseDirectoryFromPath(name)); |
| 157 | } else { |
| 158 | return Futures.immediateFuture(null); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public static Iterable<String> getSupportedProviders() { |
| 163 | return Providers.getSupportedProvidersOfType(BlobStoreContextBuilder.class); |
| 164 | } |
| 165 | } |