From 383b9bf2c698c21ac1642169181243e94f996e56 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Wed, 24 Jun 2009 22:38:37 +0000 Subject: [PATCH] cleaning up Handler binding git-svn-id: http://jclouds.googlecode.com/svn/trunk@1462 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../org/jclouds/aws/s3/S3ContextFactory.java | 11 +-- .../jclouds/aws/s3/commands/GetObject.java | 8 +-- .../jclouds/aws/s3/commands/ListBucket.java | 8 +-- .../aws/s3/config/LiveS3ConnectionModule.java | 7 +- .../aws/s3/config/S3ContextModuleTest.java | 25 ++----- .../config/SunCloudS3ConnectionModule.java | 10 ++- .../http/handlers/DelegatingErrorHandler.java | 28 ++++++-- .../handlers/DelegatingErrorHandlerTest.java | 70 +++++++++++++++++++ 8 files changed, 120 insertions(+), 47 deletions(-) create mode 100644 core/src/test/java/org/jclouds/http/handlers/DelegatingErrorHandlerTest.java diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/S3ContextFactory.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/S3ContextFactory.java index cc93d2c76f..9d1cbd7a24 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/S3ContextFactory.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/S3ContextFactory.java @@ -240,14 +240,15 @@ public class S3ContextFactory { addHttpModuleIfNeededAndNotPresent(modules); - return Guice.createInjector(new AbstractModule() { + modules.add(new AbstractModule() { @Override protected void configure() { Names.bindProperties(binder(), checkNotNull(properties, "properties")); - for (Module module : modules) - install(module); } - }, new S3ContextModule()); + }); + modules.add(new S3ContextModule()); + + return Guice.createInjector(modules); } @VisibleForTesting @@ -285,7 +286,7 @@ public class S3ContextFactory { } })) { - modules.add(new LiveS3ConnectionModule()); + modules.add(0, new LiveS3ConnectionModule()); } } diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java index a711cbbc27..d2ca550c28 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java @@ -30,11 +30,11 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.jclouds.aws.AWSResponseException; import org.jclouds.aws.s3.commands.callables.ParseObjectFromHeadersAndHttpContent; import org.jclouds.aws.s3.commands.options.GetObjectOptions; import org.jclouds.aws.s3.domain.S3Object; import org.jclouds.http.HttpMethod; +import org.jclouds.http.HttpResponseException; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; @@ -85,9 +85,9 @@ public class GetObject extends S3FutureCommand { @VisibleForTesting S3Object attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof AWSResponseException) { - AWSResponseException responseException = (AWSResponseException) e.getCause(); - if ("NoSuchKey".equals(responseException.getError().getCode())) { + if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) e.getCause(); + if (responseException.getResponse().getStatusCode() == 404) { return S3Object.NOT_FOUND; } } diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java index 77b562425a..d8cf63ac3c 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java @@ -28,11 +28,11 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.jclouds.aws.AWSResponseException; import org.jclouds.aws.s3.commands.options.ListBucketOptions; import org.jclouds.aws.s3.domain.S3Bucket; import org.jclouds.aws.s3.xml.ListBucketHandler; import org.jclouds.http.HttpMethod; +import org.jclouds.http.HttpResponseException; import org.jclouds.http.commands.callables.xml.ParseSax; import com.google.common.annotations.VisibleForTesting; @@ -80,9 +80,9 @@ public class ListBucket extends S3FutureCommand { */ @VisibleForTesting S3Bucket attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof AWSResponseException) { - AWSResponseException responseException = (AWSResponseException) e.getCause(); - if ("NoSuchBucket".equals(responseException.getError().getCode())) { + if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) e.getCause(); + if (responseException.getResponse().getStatusCode() == 404) { return S3Bucket.NOT_FOUND; } } diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/config/LiveS3ConnectionModule.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/config/LiveS3ConnectionModule.java index 97076c5511..0d5eae6169 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/config/LiveS3ConnectionModule.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/config/LiveS3ConnectionModule.java @@ -37,10 +37,8 @@ import org.jclouds.aws.s3.internal.LiveS3Connection; import org.jclouds.http.HttpConstants; import org.jclouds.http.HttpErrorHandler; import org.jclouds.http.HttpRequestFilter; -import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.annotation.ClientError; import org.jclouds.http.annotation.ServerError; -import org.jclouds.http.handlers.BackoffLimitedRetryHandler; import org.jclouds.logging.Logger; import com.google.inject.AbstractModule; @@ -74,13 +72,12 @@ public class LiveS3ConnectionModule extends AbstractModule { protected void configure() { bind(S3Connection.class).to(LiveS3Connection.class).in(Scopes.SINGLETON); - bind(HttpRetryHandler.class).to(BackoffLimitedRetryHandler.class).in(Scopes.SINGLETON); - bindErrorHandler(); + bindErrorHandlers(); requestInjection(this); logger.info("S3 Context = %1$s://%2$s:%3$s", (isSecure ? "https" : "http"), address, port); } - protected void bindErrorHandler() { + protected void bindErrorHandlers() { bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to( ParseAWSErrorFromXmlContent.class).in(Scopes.SINGLETON); bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( diff --git a/aws/s3/core/src/test/java/org/jclouds/aws/s3/config/S3ContextModuleTest.java b/aws/s3/core/src/test/java/org/jclouds/aws/s3/config/S3ContextModuleTest.java index 964c3d337d..74c829df8f 100644 --- a/aws/s3/core/src/test/java/org/jclouds/aws/s3/config/S3ContextModuleTest.java +++ b/aws/s3/core/src/test/java/org/jclouds/aws/s3/config/S3ContextModuleTest.java @@ -28,14 +28,11 @@ import static org.testng.Assert.assertEquals; import org.jclouds.aws.s3.handlers.ParseAWSErrorFromXmlContent; import org.jclouds.aws.s3.reference.S3Constants; import org.jclouds.aws.s3.xml.config.S3ParserModule; -import org.jclouds.http.HttpErrorHandler; -import org.jclouds.http.annotation.ClientError; -import org.jclouds.http.annotation.ServerError; import org.jclouds.http.config.JavaUrlHttpFutureCommandClientModule; +import org.jclouds.http.handlers.DelegatingErrorHandler; import org.testng.annotations.Test; import com.google.inject.Guice; -import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Names; @@ -69,28 +66,16 @@ public class S3ContextModuleTest { }, new JavaUrlHttpFutureCommandClientModule()); } - private static class ServerErrorHandlerTest { - @Inject - @ServerError - HttpErrorHandler errorHandler; - } - @Test void testServerErrorHandler() { - ServerErrorHandlerTest error = createInjector().getInstance(ServerErrorHandlerTest.class); - assertEquals(error.errorHandler.getClass(), ParseAWSErrorFromXmlContent.class); - } - - private static class ClientErrorHandlerTest { - @Inject - @ClientError - HttpErrorHandler errorHandler; + DelegatingErrorHandler handler = createInjector().getInstance(DelegatingErrorHandler.class); + assertEquals(handler.getServerErrorHandler().getClass(), ParseAWSErrorFromXmlContent.class); } @Test void testClientErrorHandler() { - ClientErrorHandlerTest handler = createInjector().getInstance(ClientErrorHandlerTest.class); - assertEquals(handler.errorHandler.getClass(), ParseAWSErrorFromXmlContent.class); + DelegatingErrorHandler handler = createInjector().getInstance(DelegatingErrorHandler.class); + assertEquals(handler.getClientErrorHandler().getClass(), ParseAWSErrorFromXmlContent.class); } } \ No newline at end of file diff --git a/aws/s3/extensions/suncloud/src/main/java/org/jclouds/aws/s3/suncloud/config/SunCloudS3ConnectionModule.java b/aws/s3/extensions/suncloud/src/main/java/org/jclouds/aws/s3/suncloud/config/SunCloudS3ConnectionModule.java index 037fc3fc17..7d4145387c 100644 --- a/aws/s3/extensions/suncloud/src/main/java/org/jclouds/aws/s3/suncloud/config/SunCloudS3ConnectionModule.java +++ b/aws/s3/extensions/suncloud/src/main/java/org/jclouds/aws/s3/suncloud/config/SunCloudS3ConnectionModule.java @@ -27,6 +27,8 @@ import org.jclouds.aws.s3.config.LiveS3ConnectionModule; import org.jclouds.aws.s3.config.S3ConnectionModule; import org.jclouds.aws.s3.suncloud.handlers.ParseSunCloudS3ErrorFromXmlContent; import org.jclouds.http.HttpErrorHandler; +import org.jclouds.http.annotation.ClientError; +import org.jclouds.http.annotation.ServerError; import com.google.inject.Scopes; @@ -38,9 +40,11 @@ import com.google.inject.Scopes; @S3ConnectionModule public class SunCloudS3ConnectionModule extends LiveS3ConnectionModule { - protected void bindErrorHandler() { - bind(HttpErrorHandler.class).to(ParseSunCloudS3ErrorFromXmlContent.class) - .in(Scopes.SINGLETON); + protected void bindErrorHandlers() { + bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to( + ParseSunCloudS3ErrorFromXmlContent.class).in(Scopes.SINGLETON); + bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( + ParseSunCloudS3ErrorFromXmlContent.class).in(Scopes.SINGLETON); } } \ No newline at end of file diff --git a/core/src/main/java/org/jclouds/http/handlers/DelegatingErrorHandler.java b/core/src/main/java/org/jclouds/http/handlers/DelegatingErrorHandler.java index e98ab79c8c..2e96e03f7e 100644 --- a/core/src/main/java/org/jclouds/http/handlers/DelegatingErrorHandler.java +++ b/core/src/main/java/org/jclouds/http/handlers/DelegatingErrorHandler.java @@ -29,6 +29,7 @@ import org.jclouds.http.annotation.ClientError; import org.jclouds.http.annotation.Redirection; import org.jclouds.http.annotation.ServerError; +import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; /** @@ -41,24 +42,39 @@ public class DelegatingErrorHandler implements HttpErrorHandler { @Redirection @Inject(optional = true) - private HttpErrorHandler redirectionHandler = new CloseContentAndSetExceptionErrorHandler(); + @VisibleForTesting + HttpErrorHandler redirectionHandler = new CloseContentAndSetExceptionErrorHandler(); @ClientError @Inject(optional = true) - private HttpErrorHandler clientErrorHandler = new CloseContentAndSetExceptionErrorHandler(); + @VisibleForTesting + HttpErrorHandler clientErrorHandler = new CloseContentAndSetExceptionErrorHandler(); @ServerError @Inject(optional = true) - private HttpErrorHandler serverErrorHandler = new CloseContentAndSetExceptionErrorHandler(); + @VisibleForTesting + HttpErrorHandler serverErrorHandler = new CloseContentAndSetExceptionErrorHandler(); public void handleError(HttpFutureCommand command, org.jclouds.http.HttpResponse response) { int statusCode = response.getStatusCode(); if (statusCode >= 300 && statusCode < 400) { - redirectionHandler.handleError(command, response); + getRedirectionHandler().handleError(command, response); } else if (statusCode >= 400 && statusCode < 500) { - clientErrorHandler.handleError(command, response); + getClientErrorHandler().handleError(command, response); } else if (statusCode >= 500) { - serverErrorHandler.handleError(command, response); + getServerErrorHandler().handleError(command, response); } } + + public HttpErrorHandler getRedirectionHandler() { + return redirectionHandler; + } + + public HttpErrorHandler getClientErrorHandler() { + return clientErrorHandler; + } + + public HttpErrorHandler getServerErrorHandler() { + return serverErrorHandler; + } } diff --git a/core/src/test/java/org/jclouds/http/handlers/DelegatingErrorHandlerTest.java b/core/src/test/java/org/jclouds/http/handlers/DelegatingErrorHandlerTest.java new file mode 100644 index 0000000000..5220580de6 --- /dev/null +++ b/core/src/test/java/org/jclouds/http/handlers/DelegatingErrorHandlerTest.java @@ -0,0 +1,70 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.http.handlers; + +import static org.testng.Assert.assertEquals; + +import org.jclouds.http.HttpErrorHandler; +import org.jclouds.http.annotation.ClientError; +import org.testng.annotations.Test; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "core.DelegatingErrorHandlerTest") +public class DelegatingErrorHandlerTest { + + public void testDefaultInjection() { + DelegatingErrorHandler handler = Guice.createInjector().getInstance( + DelegatingErrorHandler.class); + assertEquals(handler.getClientErrorHandler().getClass(), + CloseContentAndSetExceptionErrorHandler.class); + assertEquals(handler.getServerErrorHandler().getClass(), + CloseContentAndSetExceptionErrorHandler.class); + assertEquals(handler.getRedirectionHandler().getClass(), + CloseContentAndSetExceptionErrorHandler.class); + } + + public void testClientHandlerInjection() { + DelegatingErrorHandler handler = Guice.createInjector(new AbstractModule() { + + @Override + protected void configure() { + bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to( + DelegatingErrorHandler.class); + } + + }).getInstance(DelegatingErrorHandler.class); + assertEquals(handler.getClientErrorHandler().getClass(), DelegatingErrorHandler.class); + assertEquals(handler.getServerErrorHandler().getClass(), + CloseContentAndSetExceptionErrorHandler.class); + assertEquals(handler.getRedirectionHandler().getClass(), + CloseContentAndSetExceptionErrorHandler.class); + } + +}