EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.blobstore.util]

COVERAGE SUMMARY FOR SOURCE FILE [BlobStoreUtils.java]

nameclass, %method, %block, %line, %
BlobStoreUtils.java100% (1/1)54%  (7/13)50%  (156/313)47%  (27/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BlobStoreUtils100% (1/1)54%  (7/13)50%  (156/313)47%  (27/58)
BlobStoreUtils (): void 0%   (0/1)0%   (0/3)0%   (0/1)
cleanRequest (HttpRequest): HttpRequest 0%   (0/1)0%   (0/40)0%   (0/7)
containerNotFoundToNullOrPropagate (Exception): Object 0%   (0/1)0%   (0/4)0%   (0/1)
getContentAsStringOrNullAndClose (Blob): String 0%   (0/1)0%   (0/40)0%   (0/8)
keyNotFoundToNullOrPropagate (Exception): Object 0%   (0/1)0%   (0/4)0%   (0/1)
newBlob (BlobStore, StorageMetadata): Blob 0%   (0/1)0%   (0/52)0%   (0/9)
getNameFor (GeneratedHttpRequest): String 100% (1/1)79%  (53/67)64%  (7/11)
<static initializer> 100% (1/1)100% (20/20)100% (4/4)
createParentIfNeededAsync (AsyncBlobStore, String, Blob): ListenableFuture 100% (1/1)100% (25/25)100% (6/6)
getSupportedProviders (): Iterable 100% (1/1)100% (3/3)100% (1/1)
parseContainerFromPath (String): String 100% (1/1)100% (19/19)100% (4/4)
parseDirectoryFromPath (String): String 100% (1/1)100% (10/10)100% (1/1)
parsePrefixFromPath (String): String 100% (1/1)100% (26/26)100% (4/4)

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 */
19package org.jclouds.blobstore.util;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22 
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27 
28import org.jclouds.blobstore.AsyncBlobStore;
29import org.jclouds.blobstore.BlobStore;
30import org.jclouds.blobstore.BlobStoreContextBuilder;
31import org.jclouds.blobstore.ContainerNotFoundException;
32import org.jclouds.blobstore.KeyNotFoundException;
33import org.jclouds.blobstore.domain.Blob;
34import org.jclouds.blobstore.domain.BlobMetadata;
35import org.jclouds.blobstore.domain.StorageMetadata;
36import org.jclouds.blobstore.functions.BlobName;
37import org.jclouds.functions.ExceptionToValueOrPropagate;
38import org.jclouds.http.HttpRequest;
39import org.jclouds.http.HttpRequestFilter;
40import org.jclouds.http.HttpUtils;
41import org.jclouds.rest.Providers;
42import org.jclouds.rest.internal.GeneratedHttpRequest;
43import org.jclouds.util.Strings2;
44 
45import com.google.common.collect.ImmutableMultimap;
46import com.google.common.util.concurrent.Futures;
47import com.google.common.util.concurrent.ListenableFuture;
48 
49/**
50 * 
51 * @author Adrian Cole
52 */
53public 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}

[all classes][org.jclouds.blobstore.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov