You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/08/10 22:11:04 UTC
svn commit: r231308 - in
/jakarta/httpclient/trunk/http-common/src/java/org/apache/http:
executor/HttpRequestExecutor.java interceptor/AbstractHttpProcessor.java
Author: olegk
Date: Wed Aug 10 13:10:55 2005
New Revision: 231308
URL: http://svn.apache.org/viewcvs?rev=231308&view=rev
Log:
Common HTTP processing logic factored out into an abstract class
Added:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/interceptor/AbstractHttpProcessor.java
- copied, changed from r231007, jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
Modified:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java?rev=231308&r1=231307&r2=231308&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java Wed Aug 10 13:10:55 2005
@@ -30,9 +30,6 @@
package org.apache.http.executor;
import java.io.IOException;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpContext;
@@ -40,9 +37,8 @@
import org.apache.http.HttpMutableRequest;
import org.apache.http.HttpMutableResponse;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.interceptor.AbstractHttpProcessor;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
@@ -55,17 +51,13 @@
*
* @since 4.0
*/
-public class HttpRequestExecutor {
+public class HttpRequestExecutor extends AbstractHttpProcessor {
- private final HttpContext localContext;
-
private HttpParams params = null;
private HttpRequestRetryHandler retryhandler = null;
- private Set interceptors = null;
public HttpRequestExecutor(final HttpContext parentContext) {
- super();
- this.localContext = new HttpExecutionContext(parentContext);
+ super(new HttpExecutionContext(parentContext));
}
public HttpRequestExecutor() {
@@ -80,107 +72,6 @@
this.params = params;
}
- private void addInterceptor(final Object obj) {
- if (obj == null) {
- return;
- }
- if (this.interceptors == null) {
- this.interceptors = new HashSet();
- }
- this.interceptors.add(obj);
- }
-
- public void removeInterceptor(final Object obj) {
- if (obj == null) {
- return;
- }
- if (this.interceptors == null) {
- return;
- }
- this.interceptors.remove(obj);
- if (this.interceptors.isEmpty()) {
- this.interceptors = null;
- }
- }
-
- public void addRequestInterceptor(final HttpRequestInterceptor interceptor) {
- addInterceptor(interceptor);
- }
-
- public void addResponseInterceptor(final HttpResponseInterceptor interceptor) {
- addInterceptor(interceptor);
- }
-
- public void removeRequestInterceptor(final HttpRequestInterceptor interceptor) {
- removeInterceptor(interceptor);
- }
-
- public void removeResponseInterceptor(final HttpResponseInterceptor interceptor) {
- removeInterceptor(interceptor);
- }
-
- public void removeInterceptors(final Class clazz) {
- if (clazz == null) {
- return;
- }
- if (this.interceptors == null) {
- return;
- }
- for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
- if (clazz.isInstance(i.next())) {
- i.remove();
- }
- }
- }
-
- public void setInterceptors(final Set interceptors) {
- if (interceptors == null) {
- return;
- }
- if (this.interceptors != null) {
- this.interceptors.clear();
- this.interceptors.addAll(interceptors);
- } else {
- this.interceptors = new HashSet(interceptors);
- }
- }
-
- public HttpRequestRetryHandler getRetryHandler() {
- return this.retryhandler;
- }
-
- public void setRetryHandler(final HttpRequestRetryHandler retryhandler) {
- this.retryhandler = retryhandler;
- }
-
- private void preprocessRequest(final HttpMutableRequest request)
- throws IOException, HttpException {
- if (this.interceptors == null) {
- return;
- }
- for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
- Object obj = i.next();
- if (obj instanceof HttpRequestInterceptor) {
- HttpRequestInterceptor interceptor = (HttpRequestInterceptor)obj;
- interceptor.process(request, this.localContext);
- }
- }
- }
-
- private void postprocessResponse(final HttpMutableResponse response)
- throws IOException, HttpException {
- if (this.interceptors == null) {
- return;
- }
- for (Iterator i = this.interceptors.iterator(); i.hasNext(); ) {
- Object obj = i.next();
- if (obj instanceof HttpResponseInterceptor) {
- HttpResponseInterceptor interceptor = (HttpResponseInterceptor)obj;
- interceptor.process(response, this.localContext);
- }
- }
- }
-
public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn)
throws IOException, HttpException {
@@ -190,10 +81,10 @@
if (conn == null) {
throw new IllegalArgumentException("Client connection may not be null");
}
-
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
+ HttpContext localContext = getContext();
+ localContext.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
+ localContext.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
+ localContext.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
conn.getTargetHost());
// Link own parameters as defaults
@@ -218,10 +109,10 @@
conn.open(this.params);
// TODO: Implement secure tunnelling
}
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
+ localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
new Boolean(false));
response = conn.sendRequest(request);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
+ localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
new Boolean(true));
// Request may be terminated prematurely, if the expect-continue
// protocol is used
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/interceptor/AbstractHttpProcessor.java (from r231007, jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/interceptor/AbstractHttpProcessor.java?p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/interceptor/AbstractHttpProcessor.java&p1=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java&r1=231007&r2=231308&rev=231308&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/executor/HttpRequestExecutor.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/interceptor/AbstractHttpProcessor.java Wed Aug 10 13:10:55 2005
@@ -27,24 +27,19 @@
*
*/
-package org.apache.http.executor;
+package org.apache.http.interceptor;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
-import org.apache.http.HttpClientConnection;
import org.apache.http.HttpContext;
import org.apache.http.HttpException;
import org.apache.http.HttpMutableRequest;
import org.apache.http.HttpMutableResponse;
-import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
-import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
-import org.apache.http.params.HttpConnectionParams;
-import org.apache.http.params.HttpParams;
/**
* <p>
@@ -55,29 +50,15 @@
*
* @since 4.0
*/
-public class HttpRequestExecutor {
+public abstract class AbstractHttpProcessor {
private final HttpContext localContext;
- private HttpParams params = null;
- private HttpRequestRetryHandler retryhandler = null;
private Set interceptors = null;
- public HttpRequestExecutor(final HttpContext parentContext) {
+ public AbstractHttpProcessor(final HttpContext localContext) {
super();
- this.localContext = new HttpExecutionContext(parentContext);
- }
-
- public HttpRequestExecutor() {
- this(null);
- }
-
- public HttpParams getParams() {
- return this.params;
- }
-
- public void setParams(final HttpParams params) {
- this.params = params;
+ this.localContext = localContext;
}
private void addInterceptor(final Object obj) {
@@ -145,15 +126,7 @@
}
}
- public HttpRequestRetryHandler getRetryHandler() {
- return this.retryhandler;
- }
-
- public void setRetryHandler(final HttpRequestRetryHandler retryhandler) {
- this.retryhandler = retryhandler;
- }
-
- private void preprocessRequest(final HttpMutableRequest request)
+ protected void preprocessRequest(final HttpMutableRequest request)
throws IOException, HttpException {
if (this.interceptors == null) {
return;
@@ -167,7 +140,7 @@
}
}
- private void postprocessResponse(final HttpMutableResponse response)
+ protected void postprocessResponse(final HttpMutableResponse response)
throws IOException, HttpException {
if (this.interceptors == null) {
return;
@@ -181,78 +154,8 @@
}
}
- public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn)
- throws IOException, HttpException {
-
- if (request == null) {
- throw new IllegalArgumentException("HTTP request may not be null");
- }
- if (conn == null) {
- throw new IllegalArgumentException("Client connection may not be null");
- }
-
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQUEST, request);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_CONNECTION, conn);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST,
- conn.getTargetHost());
-
- // Link own parameters as defaults
- request.getParams().setDefaults(this.params);
-
- if (request instanceof HttpMutableRequest) {
- preprocessRequest((HttpMutableRequest)request);
- }
-
- HttpResponse response = null;
-
- // loop until the method is successfully processed, the retryHandler
- // returns false or a non-recoverable exception is thrown
- for (int execCount = 0; ; execCount++) {
- try {
- if (HttpConnectionParams.isStaleCheckingEnabled(this.params)) {
- if (conn.isOpen() && conn.isStale()) {
- conn.close();
- }
- }
- if (!conn.isOpen()) {
- conn.open(this.params);
- // TODO: Implement secure tunnelling
- }
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
- new Boolean(false));
- response = conn.sendRequest(request);
- this.localContext.setAttribute(HttpExecutionContext.HTTP_REQ_SENT,
- new Boolean(true));
- // Request may be terminated prematurely, if the expect-continue
- // protocol is used
- if (response == null) {
- // No error response so far.
- response = conn.receiveResponse(request);
- }
- break;
- } catch (IOException ex) {
- conn.close();
- if (this.retryhandler == null) {
- throw ex;
- }
- if (!this.retryhandler.retryRequest(ex, execCount, null)) {
- throw ex;
- }
- } catch (HttpException ex) {
- conn.close();
- throw ex;
- } catch (RuntimeException ex) {
- conn.close();
- throw ex;
- }
- }
-
- // Link own parameters as defaults
- response.getParams().setDefaults(this.params);
-
- if (response instanceof HttpMutableResponse) {
- postprocessResponse((HttpMutableResponse)response);
- }
- return response;
+ protected HttpContext getContext() {
+ return this.localContext;
}
+
}