From a25cb3c6c03ef4bc98c08c8f02658a82425977a1 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 6 Jun 2008 18:13:18 +0000 Subject: [PATCH] HttpClient will throw an exception if an attempt is made to retry a request with a non-repeatable request entity. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@664066 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 4 ++ .../client/NonRepeatableEntityException.java | 63 +++++++++++++++++++ .../apache/http/client/RedirectException.java | 2 +- .../client/DefaultClientRequestDirector.java | 13 ++++ .../TestDefaultClientRequestDirector.java | 26 ++++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 module-client/src/main/java/org/apache/http/client/NonRepeatableEntityException.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index f91fc5104..51cf542e9 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,10 @@ Changes since 4.0 Alpha 4 ------------------- +* HttpClient will throw an exception if an attempt is made to retry + a request with a non-repeatable request entity. + Contributed by Oleg Kalnichevski + * Fixed request re-generation logic when retrying a failed request. Auto-generated headers will no accumulate. Contributed by Oleg Kalnichevski diff --git a/module-client/src/main/java/org/apache/http/client/NonRepeatableEntityException.java b/module-client/src/main/java/org/apache/http/client/NonRepeatableEntityException.java new file mode 100644 index 000000000..9b09bd9d0 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/client/NonRepeatableEntityException.java @@ -0,0 +1,63 @@ +/* + * $HeadURL:$ + * $Revision:$ + * $Date:$ + * + * ==================================================================== + * + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.client; + +import org.apache.http.ProtocolException; + +/** + * Signals failure to retry the request due to non-repeatable request + * entity. + * + * @author Oleg Kalnichevski + * + * @since 4.0 + */ +public class NonRepeatableEntityException extends ProtocolException { + + private static final long serialVersionUID = 82685265288806048L; + + /** + * Creates a new NonRepeatableEntityException with a null detail message. + */ + public NonRepeatableEntityException() { + super(); + } + + /** + * Creates a new NonRepeatableEntityException with the specified detail message. + * + * @param message The exception detail message + */ + public NonRepeatableEntityException(String message) { + super(message); + } + +} diff --git a/module-client/src/main/java/org/apache/http/client/RedirectException.java b/module-client/src/main/java/org/apache/http/client/RedirectException.java index 590606643..7771b226d 100644 --- a/module-client/src/main/java/org/apache/http/client/RedirectException.java +++ b/module-client/src/main/java/org/apache/http/client/RedirectException.java @@ -35,7 +35,7 @@ import org.apache.http.ProtocolException; /** * Signals violation of HTTP specification caused by an invalid redirect * - * @author Oleg Kalnichevski + * @author Oleg Kalnichevski * * @since 4.0 */ diff --git a/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java b/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java index 2b86d0c09..6389d4dc7 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java +++ b/module-client/src/main/java/org/apache/http/impl/client/DefaultClientRequestDirector.java @@ -61,6 +61,7 @@ import org.apache.http.client.AuthenticationHandler; 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.RedirectException; import org.apache.http.client.RedirectHandler; import org.apache.http.client.UserTokenHandler; @@ -384,6 +385,18 @@ public class DefaultClientRequestDirector boolean retrying = true; while (retrying) { 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"); + } + } + } + try { if (LOG.isDebugEnabled()) { LOG.debug("Attempt " + execCount + " to execute request"); diff --git a/module-client/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java b/module-client/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java index 7be0b05fa..066d2e774 100644 --- a/module-client/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java +++ b/module-client/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java @@ -28,6 +28,7 @@ package org.apache.http.impl.client; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.ConnectException; import java.net.URISyntaxException; @@ -49,8 +50,10 @@ import org.apache.http.HttpResponse; 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.methods.AbortableHttpRequest; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.client.params.ClientPNames; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.ClientConnectionRequest; @@ -61,6 +64,7 @@ import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.entity.InputStreamEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.conn.ClientConnAdapterMockup; import org.apache.http.impl.conn.SingleClientConnManager; @@ -706,4 +710,26 @@ public class TestDefaultClientRequestDirector extends ServerTestBase { assertEquals(1, myheaders.length); } + public void testNonRepatableEntity() throws Exception { + int port = this.localServer.getServicePort(); + this.localServer.register("*", new SimpleService()); + + FaultyHttpClient client = new FaultyHttpClient(); + HttpContext context = new BasicHttpContext(); + + String s = "http://localhost:" + port; + HttpPost httppost = new HttpPost(s); + httppost.setEntity(new InputStreamEntity( + new ByteArrayInputStream( + new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ), + -1)); + + try { + client.execute(getServerHttp(), httppost, context); + fail("NonRepeatableEntityException should have been thrown"); + } catch (NonRepeatableEntityException ex) { + // expected + } + } + }