Issue 69: corrected retry/error guice bindings

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1463 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-06-26 12:34:28 +00:00
parent 383b9bf2c6
commit 44d4c37a90
8 changed files with 74 additions and 41 deletions

View File

@ -24,15 +24,16 @@
package org.jclouds.http;
/**
* // TODO: Adrian: Document this!
* Responsible for setting an exception on the command relevant to the unrecoverable error in the
* HttpResponse.
*
* @author Adrian Cole
*/
public interface HttpErrorHandler {
public static final HttpErrorHandler NOOP = new HttpErrorHandler() {
public void handleError(HttpFutureCommand<?> command, HttpResponse response) {
}
};
public static final HttpErrorHandler NOOP = new HttpErrorHandler() {
public void handleError(HttpFutureCommand<?> command, HttpResponse response) {
}
};
void handleError(HttpFutureCommand<?> command, HttpResponse response);
void handleError(HttpFutureCommand<?> command, HttpResponse response);
}

View File

@ -40,20 +40,26 @@ import com.google.inject.Inject;
*/
public class DelegatingErrorHandler implements HttpErrorHandler {
@VisibleForTesting
@Inject(optional=true)
@Redirection
@Inject(optional = true)
@VisibleForTesting
HttpErrorHandler redirectionHandler = new CloseContentAndSetExceptionErrorHandler();
HttpErrorHandler redirectionHandler;
@VisibleForTesting
@Inject(optional=true)
@ClientError
@Inject(optional = true)
@VisibleForTesting
HttpErrorHandler clientErrorHandler = new CloseContentAndSetExceptionErrorHandler();
HttpErrorHandler clientErrorHandler;
@ServerError
@Inject(optional = true)
@VisibleForTesting
HttpErrorHandler serverErrorHandler = new CloseContentAndSetExceptionErrorHandler();
@Inject(optional=true)
@ServerError
HttpErrorHandler serverErrorHandler;
public DelegatingErrorHandler() {
this.redirectionHandler = new CloseContentAndSetExceptionErrorHandler();
this.clientErrorHandler = redirectionHandler;
this.serverErrorHandler = redirectionHandler;
}
public void handleError(HttpFutureCommand<?> command, org.jclouds.http.HttpResponse response) {
int statusCode = response.getStatusCode();

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;
/**
@ -39,17 +40,26 @@ import com.google.inject.Inject;
*/
public class DelegatingRetryHandler implements HttpRetryHandler {
@VisibleForTesting
@Inject(optional = true)
@Redirection
@Inject(optional = true)
private HttpRetryHandler redirectionRetryHandler = new RedirectionRetryHandler(5);
HttpRetryHandler redirectionRetryHandler;
@VisibleForTesting
@Inject(optional = true)
@ClientError
@Inject(optional = true)
private HttpRetryHandler clientErrorRetryHandler = new CannotRetryHandler();
HttpRetryHandler clientErrorRetryHandler;
@ServerError
@VisibleForTesting
@Inject(optional = true)
private HttpRetryHandler serverErrorRetryHandler = new BackoffLimitedRetryHandler(5);
@ServerError
HttpRetryHandler serverErrorRetryHandler;
public DelegatingRetryHandler() {
this.redirectionRetryHandler = new RedirectionRetryHandler(5);
this.clientErrorRetryHandler = new CannotRetryHandler();
this.serverErrorRetryHandler = new BackoffLimitedRetryHandler(5);
}
public boolean shouldRetryRequest(HttpFutureCommand<?> command,
org.jclouds.http.HttpResponse response) {

View File

@ -29,13 +29,11 @@ import java.util.List;
import javax.annotation.Resource;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.logging.Logger;
@ -44,17 +42,20 @@ import com.google.inject.Inject;
public abstract class BaseHttpFutureCommandClient<Q> implements HttpFutureCommandClient {
private final DelegatingRetryHandler retryHandler;
private final DelegatingErrorHandler errorHandler;
@Resource
protected Logger logger = Logger.NULL;
@Inject(optional = true)
protected List<HttpRequestFilter> requestFilters = Collections.emptyList();
@Inject(optional = true)
private HttpRetryHandler retryHandler = new DelegatingRetryHandler();
@Inject(optional = true)
private HttpErrorHandler errorHandler = new DelegatingErrorHandler();
protected BaseHttpFutureCommandClient(DelegatingRetryHandler retryHandler,
DelegatingErrorHandler errorHandler) {
this.retryHandler = retryHandler;
this.errorHandler = errorHandler;
}
public void submit(HttpFutureCommand<?> command) {
HttpRequest request = command.getRequest();

View File

@ -37,6 +37,10 @@ import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import com.google.inject.Inject;
/**
* Basic implementation of a {@link HttpFutureCommandClient}.
@ -45,6 +49,12 @@ import org.jclouds.http.HttpResponse;
*/
public class JavaUrlHttpFutureCommandClient extends BaseHttpFutureCommandClient<HttpURLConnection> {
@Inject
public JavaUrlHttpFutureCommandClient(DelegatingRetryHandler retryHandler,
DelegatingErrorHandler errorHandler) {
super(retryHandler, errorHandler);
}
@Override
protected HttpResponse invoke(HttpURLConnection connection) throws IOException {
HttpResponse response = new HttpResponse();

View File

@ -30,7 +30,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;
@ -41,6 +40,8 @@ import org.jclouds.http.HttpFutureCommandClient;
import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.internal.BaseHttpFutureCommandClient;
import com.google.appengine.api.urlfetch.FetchOptions;
@ -61,7 +62,9 @@ public class URLFetchServiceClient extends BaseHttpFutureCommandClient<HTTPReque
private final URLFetchService urlFetchService;
@Inject
public URLFetchServiceClient(URLFetchService urlFetchService) throws MalformedURLException {
public URLFetchServiceClient(URLFetchService urlFetchService,
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler) {
super(retryHandler, errorHandler);
this.urlFetchService = urlFetchService;
}

View File

@ -43,6 +43,8 @@ import org.jclouds.http.HttpHeaders;
import org.jclouds.http.HttpMethod;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
@ -64,7 +66,9 @@ public class URLFetchServiceClientTest {
@BeforeTest
void setupClient() throws MalformedURLException {
endPoint = URI.create("http://localhost:80");
client = new URLFetchServiceClient(createNiceMock(URLFetchService.class));
client = new URLFetchServiceClient(createNiceMock(URLFetchService.class),
createNiceMock(DelegatingRetryHandler.class),
createNiceMock(DelegatingErrorHandler.class));
}
@Test

View File

@ -35,10 +35,8 @@ import org.apache.http.HttpResponse;
import org.apache.http.nio.entity.ConsumingNHttpEntity;
import org.apache.http.nio.protocol.NHttpRequestExecutionHandler;
import org.apache.http.protocol.HttpContext;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpFutureCommand;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.httpnio.util.HttpNioUtils;
@ -53,27 +51,27 @@ import com.google.inject.Inject;
*/
public class HttpNioFutureCommandExecutionHandler implements NHttpRequestExecutionHandler {
private final ExecutorService executor;
@Resource
protected Logger logger = Logger.NULL;
private final ConsumingNHttpEntityFactory entityFactory;
private final DelegatingRetryHandler retryHandler;
private final DelegatingErrorHandler errorHandler;
/**
* inputOnly: nothing is taken from this queue.
*/
private final BlockingQueue<HttpFutureCommand<?>> resubmitQueue;
@Inject(optional = true)
private HttpRetryHandler retryHandler = new DelegatingRetryHandler();
@Inject(optional = true)
private HttpErrorHandler errorHandler = new DelegatingErrorHandler();
@Resource
protected Logger logger = Logger.NULL;
@Inject
public HttpNioFutureCommandExecutionHandler(ConsumingNHttpEntityFactory entityFactory,
ExecutorService executor, BlockingQueue<HttpFutureCommand<?>> resubmitQueue) {
ExecutorService executor, BlockingQueue<HttpFutureCommand<?>> resubmitQueue,
DelegatingRetryHandler retryHandler, DelegatingErrorHandler errorHandler) {
this.executor = executor;
this.entityFactory = entityFactory;
this.resubmitQueue = resubmitQueue;
this.retryHandler = retryHandler;
this.errorHandler = errorHandler;
}
public interface ConsumingNHttpEntityFactory {