cleaning up Handler binding

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1462 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-24 22:38:37 +00:00
parent 502b9e1bac
commit 383b9bf2c6
8 changed files with 120 additions and 47 deletions

View File

@ -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());
}
}

View File

@ -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<S3Object> {
@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;
}
}

View File

@ -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<S3Bucket> {
*/
@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;
}
}

View File

@ -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(

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,70 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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);
}
}