Issue 262: name of the blob didn't parse correctly; fixed

This commit is contained in:
Adrian Cole 2010-05-24 00:13:17 -07:00
parent 103098d354
commit 7c4307ba56
4 changed files with 65 additions and 9 deletions

View File

@ -28,6 +28,7 @@ import javax.inject.Provider;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import org.jclouds.blobstore.domain.MutableBlobMetadata; import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.util.internal.BlobStoreUtilsImpl;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.encryption.EncryptionService; import org.jclouds.encryption.EncryptionService;
import org.jclouds.http.HttpException; import org.jclouds.http.HttpException;
@ -60,7 +61,7 @@ public class ParseSystemAndUserMetadataFromHeaders implements
} }
public MutableBlobMetadata apply(HttpResponse from) { public MutableBlobMetadata apply(HttpResponse from) {
String objectKey = getKeyFor(from); String objectKey = BlobStoreUtilsImpl.getKeyFor(request, from);
MutableBlobMetadata to = metadataFactory.get(); MutableBlobMetadata to = metadataFactory.get();
to.setName(objectKey); to.setName(objectKey);
setContentTypeOrThrowException(from, to); setContentTypeOrThrowException(from, to);
@ -117,14 +118,6 @@ public class ParseSystemAndUserMetadataFromHeaders implements
} }
} }
protected String getKeyFor(HttpResponse from) {
String objectKey = request.getEndpoint().getPath();
if (objectKey.startsWith("/")) {
// Trim initial slash from object key name.
objectKey = objectKey.substring(1);
}
return objectKey;
}
@VisibleForTesting @VisibleForTesting
void setContentTypeOrThrowException(HttpResponse from, MutableBlobMetadata metadata) void setContentTypeOrThrowException(HttpResponse from, MutableBlobMetadata metadata)

View File

@ -22,6 +22,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -37,6 +39,8 @@ import org.jclouds.blobstore.strategy.DeleteDirectoryStrategy;
import org.jclouds.blobstore.strategy.GetDirectoryStrategy; import org.jclouds.blobstore.strategy.GetDirectoryStrategy;
import org.jclouds.blobstore.strategy.MkdirStrategy; import org.jclouds.blobstore.strategy.MkdirStrategy;
import org.jclouds.blobstore.util.BlobStoreUtils; import org.jclouds.blobstore.util.BlobStoreUtils;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.util.Utils; import org.jclouds.util.Utils;
/** /**
@ -131,6 +135,28 @@ public class BlobStoreUtilsImpl implements BlobStoreUtils {
return "".equals(prefix) ? null : prefix; return "".equals(prefix) ? null : prefix;
} }
private static Pattern keyFromContainer = Pattern.compile("/?[^/]+/(.*)");
public static String getKeyFor(GeneratedHttpRequest<?> request, HttpResponse from) {
checkNotNull(request, "request");
checkNotNull(from, "from");
// assume first params are container and key
if (request.getArgs().length >= 2 && request.getArgs()[0] instanceof String
&& request.getArgs()[1] instanceof String) {
return request.getArgs()[1].toString();
} else if (request.getArgs().length >= 1 && request.getArgs()[0] instanceof String) {
Matcher matcher = keyFromContainer.matcher(request.getArgs()[0].toString());
if (matcher.find())
return matcher.group(1);
}
String objectKey = request.getEndpoint().getPath();
if (objectKey.startsWith("/")) {
// Trim initial slash from object key name.
objectKey = objectKey.substring(1);
}
return objectKey;
}
public static String getContentAsStringOrNullAndClose(Blob blob) throws IOException { public static String getContentAsStringOrNullAndClose(Blob blob) throws IOException {
checkNotNull(blob, "blob"); checkNotNull(blob, "blob");
if (blob.getContent() == null) if (blob.getContent() == null)

View File

@ -62,6 +62,7 @@ public class ParseBlobMetadataFromHeadersTest {
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class); GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes(); expect(request.getEndpoint()).andReturn(URI.create("http://localhost/key")).anyTimes();
expect(request.getArgs()).andReturn(new Object[] { "container/key" }).anyTimes();
replay(request); replay(request);
parser.setContext(request); parser.setContext(request);
} }

View File

@ -18,9 +18,16 @@
*/ */
package org.jclouds.blobstore.util; package org.jclouds.blobstore.util;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.jclouds.blobstore.util.internal.BlobStoreUtilsImpl; import org.jclouds.blobstore.util.internal.BlobStoreUtilsImpl;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
@ -31,6 +38,35 @@ import org.testng.annotations.Test;
@Test(groups = "unit", testName = "blobstore.BlobStoreUtilsTest") @Test(groups = "unit", testName = "blobstore.BlobStoreUtilsTest")
public class BlobStoreUtilsTest { public class BlobStoreUtilsTest {
public void testGetKeyForAzureS3AndRackspace() {
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
HttpResponse from = createMock(HttpResponse.class);
expect(request.getEndpoint()).andReturn(
URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore0/five"));
expect(request.getArgs()).andReturn(new Object[] { "adriancole-blobstore0", "five" }).atLeastOnce();
replay(request);
replay(from);
assertEquals(BlobStoreUtilsImpl.getKeyFor(request, from), "five");
}
public void testGetKeyForAtmos() {
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
HttpResponse from = createMock(HttpResponse.class);
expect(request.getEndpoint()).andReturn(
URI.create("https://storage4.clouddrive.com/v1/MossoCloudFS_dc1f419c-5059-4c87-a389-3f2e33a77b22/adriancole-blobstore0/four"));
expect(request.getArgs()).andReturn(new Object[] { "adriancole-blobstore0/four" }).atLeastOnce();
replay(request);
replay(from);
assertEquals(BlobStoreUtilsImpl.getKeyFor(request, from), "four");
}
public void testGetContainer() { public void testGetContainer() {
String container = BlobStoreUtilsImpl.parseContainerFromPath("foo"); String container = BlobStoreUtilsImpl.parseContainerFromPath("foo");
assertEquals(container, "foo"); assertEquals(container, "foo");