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
This commit is contained in:
Oleg Kalnichevski 2008-06-06 18:13:18 +00:00
parent c4fd46d1f8
commit a25cb3c6c0
5 changed files with 107 additions and 1 deletions

View File

@ -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 <olegk at apache.org>
* Fixed request re-generation logic when retrying a failed request.
Auto-generated headers will no accumulate.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -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
* <http://www.apache.org/>.
*
*/
package org.apache.http.client;
import org.apache.http.ProtocolException;
/**
* Signals failure to retry the request due to non-repeatable request
* entity.
*
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/
public class NonRepeatableEntityException extends ProtocolException {
private static final long serialVersionUID = 82685265288806048L;
/**
* Creates a new NonRepeatableEntityException with a <tt>null</tt> 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);
}
}

View File

@ -35,7 +35,7 @@ import org.apache.http.ProtocolException;
/**
* Signals violation of HTTP specification caused by an invalid redirect
*
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
* @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 4.0
*/

View File

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

View File

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