handled 307 redirects and added tests

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1471 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-27 17:19:01 +00:00
parent 8da58cc24d
commit 07c36ec8cb
3 changed files with 82 additions and 24 deletions

View File

@ -23,6 +23,8 @@
*/
package org.jclouds.aws.s3.filters;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
@ -37,6 +39,7 @@ import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.name.Named;
@ -157,10 +160,14 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
toSign.append(valueOrEmpty(request.getHeaders().get(header))).append("\n");
}
private static void appendBucketName(HttpRequest request, StringBuilder toSign) {
String hostHeader = request.getHeaders().get(HttpHeaders.HOST).iterator().next();
if (hostHeader.endsWith(".s3.amazonaws.com"))
toSign.append("/").append(hostHeader.substring(0, hostHeader.length() - 17));
@VisibleForTesting
static void appendBucketName(HttpRequest request, StringBuilder toSign) {
String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST);
if (hostHeader == null)
hostHeader = checkNotNull(request.getEndPoint().getHost(),
"request.getEndPoint().getHost()");
if (hostHeader.endsWith(".amazonaws.com") && !hostHeader.equals("s3.amazonaws.com"))
toSign.append("/").append(hostHeader.substring(0, hostHeader.lastIndexOf(".s3")));
}
private static void appendUriPath(HttpRequest request, StringBuilder toSign) {

View File

@ -55,7 +55,7 @@ public class AWSRedirectionRetryHandler extends RedirectionRetryHandler {
}
public boolean shouldRetryRequest(HttpFutureCommand<?> command, HttpResponse response) {
if (response.getStatusCode() == 301) {
if (response.getStatusCode() == 301 || response.getStatusCode() == 307) {
byte[] content = S3Utils.closeConnectionButKeepContentStream(response);
if (command.getRequest().getMethod() == HttpMethod.HEAD) {
command.getRequest().setMethod(HttpMethod.GET);
@ -67,9 +67,22 @@ public class AWSRedirectionRetryHandler extends RedirectionRetryHandler {
new String(content));
String host = error.getDetails().get(S3Constants.ENDPOINT);
if (host != null) {
if (host.equals(command.getRequest().getEndPoint().getHost())) {
// must be an amazon error related to
// http://developer.amazonwebservices.com/connect/thread.jspa?messageID=72287&#72287
try {
command.incrementFailureCount();
Thread.sleep(100);
return true;
} catch (InterruptedException e) {
command.setException(e);
return false;
}
} else {
URI endPoint = command.getRequest().getEndPoint();
endPoint = Utils.replaceHostInEndPoint(endPoint, host);
command.getRequest().setEndPoint(endPoint);
}
return true;
} else {
return false;

View File

@ -23,8 +23,15 @@
*/
package org.jclouds.aws.s3.filters;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.aws.util.DateService;
import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
@ -34,21 +41,38 @@ import com.google.inject.name.Names;
@Test(groups = "unit", testName = "s3.RequestAuthorizeSignatureTest")
public class RequestAuthorizeSignatureTest {
RequestAuthorizeSignature filter = null;
@Test
void testAppendBucketNameHostHeader() {
URI host = URI.create("http://s3.amazonaws.com:80");
HttpRequest request = new HttpRequest(host, HttpMethod.GET, "/");
request.getHeaders().put(HttpHeaders.HOST, "adriancole.s3int5.s3.amazonaws.com");
StringBuilder builder = new StringBuilder();
RequestAuthorizeSignature.appendBucketName(request, builder);
assertEquals(builder.toString(), "/adriancole.s3int5");
}
@Test
void testAppendBucketNameHostHeaderService() {
URI host = URI.create("http://s3.amazonaws.com:80");
HttpRequest request = new HttpRequest(host, HttpMethod.GET, "/");
request.getHeaders().put(HttpHeaders.HOST, "s3.amazonaws.com");
StringBuilder builder = new StringBuilder();
RequestAuthorizeSignature.appendBucketName(request, builder);
assertEquals(builder.toString(), "");
}
@Test
void testAppendBucketNameURIHost() {
URI host = URI.create("http://adriancole.s3int5.s3-external-3.amazonaws.com:80");
HttpRequest request = new HttpRequest(host, HttpMethod.GET, "/");
StringBuilder builder = new StringBuilder();
RequestAuthorizeSignature.appendBucketName(request, builder);
assertEquals(builder.toString(), "/adriancole.s3int5");
}
@Test
void testUpdatesOnlyOncePerSecond() throws NoSuchMethodException, InterruptedException {
filter = Guice.createInjector(new AbstractModule() {
protected void configure() {
bindConstant().annotatedWith(Names.named(S3Constants.PROPERTY_AWS_ACCESSKEYID)).to(
"foo");
bindConstant().annotatedWith(Names.named(S3Constants.PROPERTY_AWS_SECRETACCESSKEY)).to(
"bar");
bind(DateService.class);
}
}).getInstance(RequestAuthorizeSignature.class);
RequestAuthorizeSignature filter = createFilter();
// filter.createNewStamp();
String timeStamp = filter.timestampAsHeaderString();
// replay(filter);
@ -60,4 +84,18 @@ public class RequestAuthorizeSignatureTest {
// verify(filter);
}
private RequestAuthorizeSignature createFilter() {
return Guice.createInjector(new AbstractModule() {
protected void configure() {
bindConstant().annotatedWith(Names.named(S3Constants.PROPERTY_AWS_ACCESSKEYID)).to(
"foo");
bindConstant().annotatedWith(Names.named(S3Constants.PROPERTY_AWS_SECRETACCESSKEY)).to(
"bar");
bind(DateService.class);
}
}).getInstance(RequestAuthorizeSignature.class);
}
}