michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: *
michael@0: * HttpRequestExecutor relies on {@link HttpProcessor} to generate mandatory
michael@0: * protocol headers for all outgoing messages and apply common, cross-cutting
michael@0: * message transformations to all incoming and outgoing messages. Application
michael@0: * specific processing can be implemented outside HttpRequestExecutor once the
michael@0: * request has been executed and a response has been received.
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *
michael@0: * This method also handles the expect-continue handshake if necessary.
michael@0: * If it does not have to handle an expect-continue handshake, it will
michael@0: * not use the connection for reading or anything else that depends on
michael@0: * data coming in over the connection.
michael@0: *
michael@0: * @param request the request to send, already
michael@0: * {@link #preProcess preprocessed}
michael@0: * @param conn the connection over which to send the request,
michael@0: * already established
michael@0: * @param context the context for sending the request
michael@0: *
michael@0: * @return a terminal response received as part of an expect-continue
michael@0: * handshake, or
michael@0: * null
if the expect-continue handshake is not used
michael@0: *
michael@0: * @throws IOException in case of an I/O error.
michael@0: * @throws HttpException in case of HTTP protocol violation or a processing
michael@0: * problem.
michael@0: */
michael@0: protected HttpResponse doSendRequest(
michael@0: final HttpRequest request,
michael@0: final HttpClientConnection conn,
michael@0: final HttpContext context)
michael@0: throws IOException, HttpException {
michael@0: if (request == null) {
michael@0: throw new IllegalArgumentException("HTTP request may not be null");
michael@0: }
michael@0: if (conn == null) {
michael@0: throw new IllegalArgumentException("HTTP connection may not be null");
michael@0: }
michael@0: if (context == null) {
michael@0: throw new IllegalArgumentException("HTTP context may not be null");
michael@0: }
michael@0:
michael@0: HttpResponse response = null;
michael@0:
michael@0: context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
michael@0: context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.FALSE);
michael@0:
michael@0: conn.sendRequestHeader(request);
michael@0: if (request instanceof HttpEntityEnclosingRequest) {
michael@0: // Check for expect-continue handshake. We have to flush the
michael@0: // headers and wait for an 100-continue response to handle it.
michael@0: // If we get a different response, we must not send the entity.
michael@0: boolean sendentity = true;
michael@0: final ProtocolVersion ver =
michael@0: request.getRequestLine().getProtocolVersion();
michael@0: if (((HttpEntityEnclosingRequest) request).expectContinue() &&
michael@0: !ver.lessEquals(HttpVersion.HTTP_1_0)) {
michael@0:
michael@0: conn.flush();
michael@0: // As suggested by RFC 2616 section 8.2.3, we don't wait for a
michael@0: // 100-continue response forever. On timeout, send the entity.
michael@0: int tms = request.getParams().getIntParameter(
michael@0: CoreProtocolPNames.WAIT_FOR_CONTINUE, 2000);
michael@0:
michael@0: if (conn.isResponseAvailable(tms)) {
michael@0: response = conn.receiveResponseHeader();
michael@0: if (canResponseHaveBody(request, response)) {
michael@0: conn.receiveResponseEntity(response);
michael@0: }
michael@0: int status = response.getStatusLine().getStatusCode();
michael@0: if (status < 200) {
michael@0: if (status != HttpStatus.SC_CONTINUE) {
michael@0: throw new ProtocolException(
michael@0: "Unexpected response: " + response.getStatusLine());
michael@0: }
michael@0: // discard 100-continue
michael@0: response = null;
michael@0: } else {
michael@0: sendentity = false;
michael@0: }
michael@0: }
michael@0: }
michael@0: if (sendentity) {
michael@0: conn.sendRequestEntity((HttpEntityEnclosingRequest) request);
michael@0: }
michael@0: }
michael@0: conn.flush();
michael@0: context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
michael@0: return response;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Waits for and receives a response.
michael@0: * This method will automatically ignore intermediate responses
michael@0: * with status code 1xx.
michael@0: *
michael@0: * @param request the request for which to obtain the response
michael@0: * @param conn the connection over which the request was sent
michael@0: * @param context the context for receiving the response
michael@0: *
michael@0: * @return the terminal response, not yet post-processed
michael@0: *
michael@0: * @throws IOException in case of an I/O error.
michael@0: * @throws HttpException in case of HTTP protocol violation or a processing
michael@0: * problem.
michael@0: */
michael@0: protected HttpResponse doReceiveResponse(
michael@0: final HttpRequest request,
michael@0: final HttpClientConnection conn,
michael@0: final HttpContext context)
michael@0: throws HttpException, IOException {
michael@0: if (request == null) {
michael@0: throw new IllegalArgumentException("HTTP request may not be null");
michael@0: }
michael@0: if (conn == null) {
michael@0: throw new IllegalArgumentException("HTTP connection may not be null");
michael@0: }
michael@0: if (context == null) {
michael@0: throw new IllegalArgumentException("HTTP context may not be null");
michael@0: }
michael@0:
michael@0: HttpResponse response = null;
michael@0: int statuscode = 0;
michael@0:
michael@0: while (response == null || statuscode < HttpStatus.SC_OK) {
michael@0:
michael@0: response = conn.receiveResponseHeader();
michael@0: if (canResponseHaveBody(request, response)) {
michael@0: conn.receiveResponseEntity(response);
michael@0: }
michael@0: statuscode = response.getStatusLine().getStatusCode();
michael@0:
michael@0: } // while intermediate response
michael@0:
michael@0: return response;
michael@0:
michael@0: }
michael@0:
michael@0: /**
michael@0: * Post-processes the given response using the given protocol processor and
michael@0: * completes the process of request execution.
michael@0: *
michael@0: * This method does not read the response entity, if any. michael@0: * The connection over which content of the response entity is being michael@0: * streamed from cannot be reused until {@link HttpEntity#consumeContent()} michael@0: * has been invoked. michael@0: * michael@0: * @param response the response object to post-process michael@0: * @param processor the processor to use michael@0: * @param context the context for post-processing the response michael@0: * michael@0: * @throws IOException in case of an I/O error. michael@0: * @throws HttpException in case of HTTP protocol violation or a processing michael@0: * problem. michael@0: */ michael@0: public void postProcess( michael@0: final HttpResponse response, michael@0: final HttpProcessor processor, michael@0: final HttpContext context) michael@0: throws HttpException, IOException { michael@0: if (response == null) { michael@0: throw new IllegalArgumentException("HTTP response may not be null"); michael@0: } michael@0: if (processor == null) { michael@0: throw new IllegalArgumentException("HTTP processor may not be null"); michael@0: } michael@0: if (context == null) { michael@0: throw new IllegalArgumentException("HTTP context may not be null"); michael@0: } michael@0: context.setAttribute(ExecutionContext.HTTP_RESPONSE, response); michael@0: processor.process(response, context); michael@0: } michael@0: michael@0: } // class HttpRequestExecutor