Better code for handling of non-repeatable requests

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@664326 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2008-06-07 11:48:27 +00:00
parent a25cb3c6c0
commit df92be5902
5 changed files with 39 additions and 24 deletions

View File

@ -1,7 +1,7 @@
/*
* $HeadURL:$
* $Revision:$
* $Date:$
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
@ -40,14 +40,14 @@
*
* @since 4.0
*/
public class NonRepeatableEntityException extends ProtocolException {
public class NonRepeatableRequestException extends ProtocolException {
private static final long serialVersionUID = 82685265288806048L;
/**
* Creates a new NonRepeatableEntityException with a <tt>null</tt> detail message.
*/
public NonRepeatableEntityException() {
public NonRepeatableRequestException() {
super();
}
@ -56,7 +56,7 @@ public NonRepeatableEntityException() {
*
* @param message The exception detail message
*/
public NonRepeatableEntityException(String message) {
public NonRepeatableRequestException(String message) {
super(message);
}

View File

@ -61,7 +61,7 @@
import org.apache.http.client.ClientRequestDirector;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.NonRepeatableEntityException;
import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.RedirectException;
import org.apache.http.client.RedirectHandler;
import org.apache.http.client.UserTokenHandler;
@ -384,17 +384,13 @@ public HttpResponse execute(HttpHost target, HttpRequest request,
boolean retrying = true;
while (retrying) {
// Increment total exec count (with redirects)
execCount++;
if (execCount > 1) {
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity =
((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null && !entity.isRepeatable()) {
throw new NonRepeatableEntityException(
"Cannot retry the request");
}
}
// Increment exec count for this particular request
wrapper.incrementExecCount();
if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) {
throw new NonRepeatableRequestException("Cannot retry request " +
"with a non-repeatable request entity");
}
try {

View File

@ -74,5 +74,10 @@ public boolean expectContinue() {
Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
}
@Override
public boolean isRepeatable() {
return this.entity == null || this.entity.isRepeatable();
}
}

View File

@ -64,6 +64,7 @@ class RequestWrapper extends AbstractHttpMessage implements HttpUriRequest {
private URI uri;
private String method;
private ProtocolVersion version;
private int execCount;
public RequestWrapper(final HttpRequest request) throws ProtocolException {
super();
@ -88,8 +89,15 @@ public RequestWrapper(final HttpRequest request) throws ProtocolException {
this.method = requestLine.getMethod();
this.version = request.getProtocolVersion();
}
this.execCount = 0;
}
public void resetHeaders() {
// Make a copy of original headers
this.headergroup.clear();
setHeaders(this.original.getAllHeaders());
}
public String getMethod() {
return this.method;
}
@ -147,10 +155,16 @@ public HttpRequest getOriginal() {
return this.original;
}
public void resetHeaders() {
// Make a copy of original headers
this.headergroup.clear();
setHeaders(this.original.getAllHeaders());
public boolean isRepeatable() {
return true;
}
public int getExecCount() {
return this.execCount;
}
public void incrementExecCount() {
this.execCount++;
}
}

View File

@ -50,7 +50,7 @@
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.NonRepeatableEntityException;
import org.apache.http.client.NonRepeatableRequestException;
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
@ -727,7 +727,7 @@ public void testNonRepatableEntity() throws Exception {
try {
client.execute(getServerHttp(), httppost, context);
fail("NonRepeatableEntityException should have been thrown");
} catch (NonRepeatableEntityException ex) {
} catch (NonRepeatableRequestException ex) {
// expected
}
}