From 284556d9be5cae415c907b74f3d4d3f1c20f9658 Mon Sep 17 00:00:00 2001 From: Sam Berlin Date: Sun, 8 Jun 2008 13:21:20 +0000 Subject: [PATCH] HTTPCLIENT-779: toplevel exception cleanup. HttpClient.execute now only throws an IOException (and specific subtypes of that), and HttpGet/HttpPut and associated classes' String constructor now throw IllegalArgumentException instead of URISyntaxException. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@664505 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/client/ClientProtocolException.java | 30 +++++++++++++++++ .../org/apache/http/client/HttpClient.java | 33 +++++++------------ .../http/client/methods/HttpDelete.java | 8 +++-- .../apache/http/client/methods/HttpGet.java | 8 +++-- .../apache/http/client/methods/HttpHead.java | 8 +++-- .../http/client/methods/HttpOptions.java | 8 +++-- .../apache/http/client/methods/HttpPost.java | 10 +++--- .../apache/http/client/methods/HttpPut.java | 8 +++-- .../apache/http/client/methods/HttpTrace.java | 10 +++--- .../http/impl/client/AbstractHttpClient.java | 15 ++++++--- .../http/client/protocol/TestRedirects.java | 23 +++++++------ .../TestDefaultClientRequestDirector.java | 9 ++--- 12 files changed, 107 insertions(+), 63 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/client/ClientProtocolException.java diff --git a/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java b/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java new file mode 100644 index 000000000..f337654e3 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/client/ClientProtocolException.java @@ -0,0 +1,30 @@ +package org.apache.http.client; + +import java.io.IOException; + +/** + * Signals an error in the HTTP protocol. + */ +public class ClientProtocolException extends IOException { + + private static final long serialVersionUID = -5596590843227115865L; + + public ClientProtocolException() { + super(); + } + + public ClientProtocolException(String s) { + super(s); + } + + public ClientProtocolException(Throwable cause) { + initCause(cause); + } + + public ClientProtocolException(String message, Throwable cause) { + super(message); + initCause(cause); + } + + +} diff --git a/module-client/src/main/java/org/apache/http/client/HttpClient.java b/module-client/src/main/java/org/apache/http/client/HttpClient.java index 0f09e5e41..514a2c7a5 100644 --- a/module-client/src/main/java/org/apache/http/client/HttpClient.java +++ b/module-client/src/main/java/org/apache/http/client/HttpClient.java @@ -36,7 +36,6 @@ import java.io.IOException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; -import org.apache.http.HttpException; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; import org.apache.http.client.methods.HttpUriRequest; @@ -87,13 +86,11 @@ public interface HttpClient { * @param request the request to execute * * @return the response to the request - * - * @throws HttpException in case of a problem - * @throws IOException in case of an IO problem - * or the connection was aborted + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error */ HttpResponse execute(HttpUriRequest request) - throws HttpException, IOException + throws IOException, ClientProtocolException ; @@ -110,13 +107,11 @@ public interface HttpClient { * Whether redirects or authentication challenges will be returned * or handled automatically depends on the implementation and * configuration of this client. - * - * @throws HttpException in case of a problem - * @throws IOException in case of an IO problem - * or the connection was aborted + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error */ HttpResponse execute(HttpUriRequest request, HttpContext context) - throws HttpException, IOException + throws IOException, ClientProtocolException ; @@ -135,13 +130,11 @@ public interface HttpClient { * Whether redirects or authentication challenges will be returned * or handled automatically depends on the implementation and * configuration of this client. - * - * @throws HttpException in case of a problem - * @throws IOException in case of an IO problem - * or the connection was aborted + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error */ HttpResponse execute(HttpHost target, HttpRequest request) - throws HttpException, IOException + throws IOException, ClientProtocolException ; @@ -161,14 +154,12 @@ public interface HttpClient { * Whether redirects or authentication challenges will be returned * or handled automatically depends on the implementation and * configuration of this client. - * - * @throws HttpException in case of a problem - * @throws IOException in case of an IO problem - * or the connection was aborted + * @throws IOException in case of a problem or the connection was aborted + * @throws ClientProtocolException in case of an http protocol error */ HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) - throws HttpException, IOException + throws IOException, ClientProtocolException ; diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpDelete.java b/module-client/src/main/java/org/apache/http/client/methods/HttpDelete.java index 4a4c3ef20..2dd1f2fef 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpDelete.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpDelete.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP DELETE method @@ -61,9 +60,12 @@ public class HttpDelete extends HttpRequestBase { setURI(uri); } - public HttpDelete(final String uri) throws URISyntaxException { + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpDelete(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpGet.java b/module-client/src/main/java/org/apache/http/client/methods/HttpGet.java index 4b9474e5f..cd1d989ce 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpGet.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpGet.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP GET method. @@ -68,9 +67,12 @@ public class HttpGet extends HttpRequestBase { setURI(uri); } - public HttpGet(final String uri) throws URISyntaxException { + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpGet(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpHead.java b/module-client/src/main/java/org/apache/http/client/methods/HttpHead.java index ab9eccfcf..3dbd3b927 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpHead.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpHead.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP HEAD method. @@ -68,9 +67,12 @@ public class HttpHead extends HttpRequestBase { setURI(uri); } - public HttpHead(final String uri) throws URISyntaxException { + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpHead(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java b/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java index 1fba84bd1..0c64376a4 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; import java.util.HashSet; import java.util.Set; @@ -73,9 +72,12 @@ public class HttpOptions extends HttpRequestBase { setURI(uri); } - public HttpOptions(final String uri) throws URISyntaxException { + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpOptions(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpPost.java b/module-client/src/main/java/org/apache/http/client/methods/HttpPost.java index 1bcef62de..7d0aca390 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpPost.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpPost.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP POST method. @@ -71,10 +70,13 @@ public class HttpPost extends HttpEntityEnclosingRequestBase { super(); setURI(uri); } - - public HttpPost(final String uri) throws URISyntaxException { + + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpPost(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpPut.java b/module-client/src/main/java/org/apache/http/client/methods/HttpPut.java index bd12c495a..56959e9d5 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpPut.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpPut.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP PUT method. @@ -64,9 +63,12 @@ public class HttpPut extends HttpEntityEnclosingRequestBase { setURI(uri); } - public HttpPut(final String uri) throws URISyntaxException { + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpPut(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpTrace.java b/module-client/src/main/java/org/apache/http/client/methods/HttpTrace.java index 7dfa52c2e..5802d916e 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpTrace.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpTrace.java @@ -32,7 +32,6 @@ package org.apache.http.client.methods; import java.net.URI; -import java.net.URISyntaxException; /** * HTTP TRACE method. @@ -66,10 +65,13 @@ public class HttpTrace extends HttpRequestBase { super(); setURI(uri); } - - public HttpTrace(final String uri) throws URISyntaxException { + + /** + * @throws IllegalArgumentException if the uri is invalid. + */ + public HttpTrace(final String uri) { super(); - setURI(new URI(uri)); + setURI(URI.create(uri)); } @Override diff --git a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java index c86356bc8..fa35257c7 100644 --- a/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java +++ b/module-client/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java @@ -43,6 +43,7 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; import org.apache.http.auth.AuthSchemeRegistry; import org.apache.http.client.AuthenticationHandler; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientRequestDirector; import org.apache.http.client.CookieStore; import org.apache.http.client.CredentialsProvider; @@ -434,7 +435,7 @@ public abstract class AbstractHttpClient implements HttpClient { // non-javadoc, see interface HttpClient public final HttpResponse execute(HttpUriRequest request) - throws HttpException, IOException { + throws IOException, ClientProtocolException { return execute(request, null); } @@ -451,7 +452,7 @@ public abstract class AbstractHttpClient implements HttpClient { */ public final HttpResponse execute(HttpUriRequest request, HttpContext context) - throws HttpException, IOException { + throws IOException, ClientProtocolException { if (request == null) { throw new IllegalArgumentException @@ -476,7 +477,7 @@ public abstract class AbstractHttpClient implements HttpClient { // non-javadoc, see interface HttpClient public final HttpResponse execute(HttpHost target, HttpRequest request) - throws HttpException, IOException { + throws IOException, ClientProtocolException { return execute(target, request, null); } @@ -485,7 +486,7 @@ public abstract class AbstractHttpClient implements HttpClient { // non-javadoc, see interface HttpClient public final HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) - throws HttpException, IOException { + throws IOException, ClientProtocolException { if (request == null) { throw new IllegalArgumentException @@ -535,7 +536,11 @@ public abstract class AbstractHttpClient implements HttpClient { determineParams(request)); } - return director.execute(target, request, execContext); + try { + return director.execute(target, request, execContext); + } catch(HttpException httpException) { + throw new ClientProtocolException(httpException); + } } // execute diff --git a/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java b/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java index 921f8e201..ff0e61772 100644 --- a/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java +++ b/module-client/src/test/java/org/apache/http/client/protocol/TestRedirects.java @@ -43,6 +43,7 @@ import org.apache.http.HttpStatus; import org.apache.http.ProtocolException; import org.apache.http.ProtocolVersion; import org.apache.http.client.CircularRedirectException; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CookieStore; import org.apache.http.client.RedirectException; import org.apache.http.client.methods.HttpGet; @@ -426,9 +427,9 @@ public class TestRedirects extends ServerTestBase { try { client.execute(getServerHttp(), httpget); - fail("RedirectException exception should have been thrown"); - } catch (RedirectException e) { - // expected + fail("ClientProtocolException exception should have been thrown"); + } catch (ClientProtocolException e) { + assertTrue(e.getCause() instanceof RedirectException); } } @@ -442,9 +443,9 @@ public class TestRedirects extends ServerTestBase { try { client.execute(getServerHttp(), httpget); - fail("CircularRedirectException exception should have been thrown"); - } catch (CircularRedirectException e) { - // expected + fail("ClientProtocolException exception should have been thrown"); + } catch (ClientProtocolException e) { + assertTrue(e.getCause() instanceof CircularRedirectException); } } @@ -542,8 +543,9 @@ public class TestRedirects extends ServerTestBase { try { client.execute(getServerHttp(), httpget); - fail("ProtocolException exception should have been thrown"); - } catch (ProtocolException e) { + fail("ClientProtocolException exception should have been thrown"); + } catch (ClientProtocolException e) { + assertTrue(e.getCause() instanceof ProtocolException); // expected } } @@ -575,8 +577,9 @@ public class TestRedirects extends ServerTestBase { try { client.execute(getServerHttp(), httpget); - fail("ProtocolException should have been thrown"); - } catch (ProtocolException e) { + fail("ClientProtocolException should have been thrown"); + } catch (ClientProtocolException e) { + assertTrue(e.getCause() instanceof ProtocolException); // expected } } 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 5dd685f34..4d87d3689 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 @@ -31,7 +31,6 @@ package org.apache.http.impl.client; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.ConnectException; -import java.net.URISyntaxException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -49,6 +48,7 @@ import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.ProtocolVersion; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.NonRepeatableRequestException; import org.apache.http.client.methods.AbortableHttpRequest; @@ -554,7 +554,7 @@ public class TestDefaultClientRequestDirector extends ServerTestBase { private static class CustomGet extends HttpGet { private final CountDownLatch releaseTriggerLatch; - public CustomGet(String uri, CountDownLatch releaseTriggerLatch) throws URISyntaxException { + public CustomGet(String uri, CountDownLatch releaseTriggerLatch) { super(uri); this.releaseTriggerLatch = releaseTriggerLatch; } @@ -726,8 +726,9 @@ public class TestDefaultClientRequestDirector extends ServerTestBase { try { client.execute(getServerHttp(), httppost, context); - fail("NonRepeatableEntityException should have been thrown"); - } catch (NonRepeatableRequestException ex) { + fail("ClientProtocolException should have been thrown"); + } catch (ClientProtocolException ex) { + assertTrue(ex.getCause() instanceof NonRepeatableRequestException); // expected } }