HttpRequestExecutorpublic class HttpRequestExecutor extends Object Sends HTTP requests and receives the responses.
Takes care of request preprocessing and response postprocessing
by the respective interceptors. |
Constructors Summary |
---|
public HttpRequestExecutor()Create a new request executor.
super();
|
Methods Summary |
---|
protected boolean | canResponseHaveBody(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response)Decide whether a response comes with an entity.
The implementation in this class is based on RFC 2616.
Unknown methods and response codes are supposed to
indicate responses with an entity.
Derived executors can override this method to handle
methods and response codes not specified in RFC 2616.
if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
return false;
}
int status = response.getStatusLine().getStatusCode();
return status >= HttpStatus.SC_OK
&& status != HttpStatus.SC_NO_CONTENT
&& status != HttpStatus.SC_NOT_MODIFIED
&& status != HttpStatus.SC_RESET_CONTENT;
| protected org.apache.http.HttpResponse | doReceiveResponse(org.apache.http.HttpRequest request, org.apache.http.HttpClientConnection conn, org.apache.http.protocol.HttpContext context)Wait for and receive a response.
This method will automatically ignore intermediate responses
with status code 1xx.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (conn == null) {
throw new IllegalArgumentException("HTTP connection may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
HttpResponse response = null;
int statuscode = 0;
while (response == null || statuscode < HttpStatus.SC_OK) {
response = conn.receiveResponseHeader();
if (canResponseHaveBody(request, response)) {
conn.receiveResponseEntity(response);
}
statuscode = response.getStatusLine().getStatusCode();
} // while intermediate response
return response;
| protected org.apache.http.HttpResponse | doSendRequest(org.apache.http.HttpRequest request, org.apache.http.HttpClientConnection conn, org.apache.http.protocol.HttpContext context)Send a request over a connection.
This method also handles the expect-continue handshake if necessary.
If it does not have to handle an expect-continue handshake, it will
not use the connection for reading or anything else that depends on
data coming in over the connection.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (conn == null) {
throw new IllegalArgumentException("HTTP connection may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
HttpResponse response = null;
context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.FALSE);
conn.sendRequestHeader(request);
if (request instanceof HttpEntityEnclosingRequest) {
// Check for expect-continue handshake. We have to flush the
// headers and wait for an 100-continue response to handle it.
// If we get a different response, we must not send the entity.
boolean sendentity = true;
final ProtocolVersion ver =
request.getRequestLine().getProtocolVersion();
if (((HttpEntityEnclosingRequest) request).expectContinue() &&
!ver.lessEquals(HttpVersion.HTTP_1_0)) {
conn.flush();
// As suggested by RFC 2616 section 8.2.3, we don't wait for a
// 100-continue response forever. On timeout, send the entity.
int tms = request.getParams().getIntParameter(
CoreProtocolPNames.WAIT_FOR_CONTINUE, 2000);
if (conn.isResponseAvailable(tms)) {
response = conn.receiveResponseHeader();
if (canResponseHaveBody(request, response)) {
conn.receiveResponseEntity(response);
}
int status = response.getStatusLine().getStatusCode();
if (status < 200) {
if (status != HttpStatus.SC_CONTINUE) {
throw new ProtocolException(
"Unexpected response: " + response.getStatusLine());
}
// discard 100-continue
response = null;
} else {
sendentity = false;
}
}
}
if (sendentity) {
conn.sendRequestEntity((HttpEntityEnclosingRequest) request);
}
}
conn.flush();
context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
return response;
| public org.apache.http.HttpResponse | execute(org.apache.http.HttpRequest request, org.apache.http.HttpClientConnection conn, org.apache.http.protocol.HttpContext context)Synchronously send a request and obtain the response.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (conn == null) {
throw new IllegalArgumentException("Client connection may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
try {
HttpResponse response = doSendRequest(request, conn, context);
if (response == null) {
response = doReceiveResponse(request, conn, context);
}
return response;
} catch (IOException ex) {
conn.close();
throw ex;
} catch (HttpException ex) {
conn.close();
throw ex;
} catch (RuntimeException ex) {
conn.close();
throw ex;
}
| public void | postProcess(org.apache.http.HttpResponse response, org.apache.http.protocol.HttpProcessor processor, org.apache.http.protocol.HttpContext context)Finish a response.
This includes post-processing of the response object.
It does not read the response entity, if any.
It does not allow for immediate re-use of the
connection over which the response is coming in.
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
if (processor == null) {
throw new IllegalArgumentException("HTTP processor may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
processor.process(response, context);
| public void | preProcess(org.apache.http.HttpRequest request, org.apache.http.protocol.HttpProcessor processor, org.apache.http.protocol.HttpContext context)Prepare a request for sending.
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (processor == null) {
throw new IllegalArgumentException("HTTP processor may not be null");
}
if (context == null) {
throw new IllegalArgumentException("HTTP context may not be null");
}
processor.process(request, context);
|
|