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

View File

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

View File

@ -75,4 +75,9 @@ public boolean expectContinue() {
return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue()); 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 URI uri;
private String method; private String method;
private ProtocolVersion version; private ProtocolVersion version;
private int execCount;
public RequestWrapper(final HttpRequest request) throws ProtocolException { public RequestWrapper(final HttpRequest request) throws ProtocolException {
super(); super();
@ -88,6 +89,13 @@ public RequestWrapper(final HttpRequest request) throws ProtocolException {
this.method = requestLine.getMethod(); this.method = requestLine.getMethod();
this.version = request.getProtocolVersion(); 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() { public String getMethod() {
@ -147,10 +155,16 @@ public HttpRequest getOriginal() {
return this.original; return this.original;
} }
public void resetHeaders() { public boolean isRepeatable() {
// Make a copy of original headers return true;
this.headergroup.clear(); }
setHeaders(this.original.getAllHeaders());
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.HttpStatus;
import org.apache.http.ProtocolVersion; import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpRequestRetryHandler; 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.AbortableHttpRequest;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -727,7 +727,7 @@ public void testNonRepatableEntity() throws Exception {
try { try {
client.execute(getServerHttp(), httppost, context); client.execute(getServerHttp(), httppost, context);
fail("NonRepeatableEntityException should have been thrown"); fail("NonRepeatableEntityException should have been thrown");
} catch (NonRepeatableEntityException ex) { } catch (NonRepeatableRequestException ex) {
// expected // expected
} }
} }