From 306334fad772868a926dd66701626d86d5071c17 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 26 Jan 2015 10:41:30 +0000 Subject: [PATCH] HTTPCLIENT-1601, HTTPCLIENT-1603: clarifications in tutorial regarding exception handling and request recovery (automatic re-execution) git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1654758 13f79535-47bb-0310-9956-ffa450edef68 --- src/docbkx/fundamentals.xml | 125 ++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/src/docbkx/fundamentals.xml b/src/docbkx/fundamentals.xml index 7fd52d2bc..2d8eda29c 100644 --- a/src/docbkx/fundamentals.xml +++ b/src/docbkx/fundamentals.xml @@ -657,14 +657,71 @@ try { } ]]> +
+ HTTP protocol interceptors + The HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP + protocol. Usually protocol interceptors are expected to act upon one specific header or + a group of related headers of the incoming message, or populate the outgoing message with + one specific header or a group of related headers. Protocol interceptors can also + manipulate content entities enclosed with messages - transparent content compression / + decompression being a good example. Usually this is accomplished by using the + 'Decorator' pattern where a wrapper entity class is used to decorate the original + entity. Several protocol interceptors can be combined to form one logical unit. + Protocol interceptors can collaborate by sharing information - such as a processing + state - through the HTTP execution context. Protocol interceptors can use HTTP context + to store a processing state for one request or several consecutive requests. + Usually the order in which interceptors are executed should not matter as long as they + do not depend on a particular state of the execution context. If protocol interceptors + have interdependencies and therefore must be executed in a particular order, they should + be added to the protocol processor in the same sequence as their expected execution + order. + Protocol interceptors must be implemented as thread-safe. Similarly to servlets, + protocol interceptors should not use instance variables unless access to those variables + is synchronized. + This is an example of how local context can be used to persist a processing state + between consecutive requests: + +
Exception handling - HttpClient can throw two types of exceptions: + HTTP protocol processors can throw two types of exceptions: java.io.IOException in case of an I/O failure such as socket timeout or an socket reset and HttpException that signals an HTTP failure such as a violation of the HTTP protocol. Usually I/O errors are considered non-fatal and recoverable, whereas HTTP protocol errors are considered fatal - and cannot be automatically recovered from. + and cannot be automatically recovered from. Please note that HttpClient + implementations re-throw HttpExceptions + as ClientProtocolException, which is a subclass + of java.io.IOException. This enables the users + of HttpClient to handle both I/O errors and protocol + violations from a single catch clause.
HTTP transport safety It is important to understand that the HTTP protocol is not well suited to all @@ -699,9 +756,10 @@ try { Please note that this problem is not specific to HttpClient. Browser based applications are subject to exactly the same issues related to HTTP methods non-idempotency. - HttpClient assumes non-entity enclosing methods such as GET and - HEAD to be idempotent and entity enclosing methods such as - POST and PUT to be not. + By default HttpClient assumes only non-entity enclosing methods such as + GET and HEAD to be idempotent and entity + enclosing methods such as POST and PUT to be + not for compatibility reasons.
Automatic exception recovery @@ -773,6 +831,11 @@ CloseableHttpClient httpclient = HttpClients.custom() .setRetryHandler(myRetryHandler) .build(); ]]> + Please note that one can use StandardHttpRequestRetryHandler + instead of the one used by default in order to treat those request methods defined + as idempotent by RFC-2616 as safe to retry automatically: GET, + HEAD, PUT, DELETE, + OPTIONS, and TRACE.
@@ -787,58 +850,6 @@ CloseableHttpClient httpclient = HttpClients.custom() - even if currently blocked in an I/O operation - is guaranteed to unblock by throwing a InterruptedIOException
-
- HTTP protocol interceptors - The HTTP protocol interceptor is a routine that implements a specific aspect of the HTTP - protocol. Usually protocol interceptors are expected to act upon one specific header or - a group of related headers of the incoming message, or populate the outgoing message with - one specific header or a group of related headers. Protocol interceptors can also - manipulate content entities enclosed with messages - transparent content compression / - decompression being a good example. Usually this is accomplished by using the - 'Decorator' pattern where a wrapper entity class is used to decorate the original - entity. Several protocol interceptors can be combined to form one logical unit. - Protocol interceptors can collaborate by sharing information - such as a processing - state - through the HTTP execution context. Protocol interceptors can use HTTP context - to store a processing state for one request or several consecutive requests. - Usually the order in which interceptors are executed should not matter as long as they - do not depend on a particular state of the execution context. If protocol interceptors - have interdependencies and therefore must be executed in a particular order, they should - be added to the protocol processor in the same sequence as their expected execution - order. - Protocol interceptors must be implemented as thread-safe. Similarly to servlets, - protocol interceptors should not use instance variables unless access to those variables - is synchronized. - This is an example of how local context can be used to persist a processing state - between consecutive requests: - -
Redirect handling HttpClient handles all types of redirects automatically, except those explicitly