## org.mortbay.jetty.client
Class HttpExchange

    java.lang.Object
      org.mortbay.jetty.client.HttpExchange

Direct Known Subclasses:
  CachedExchange, PropfindExchange, WebdavSupportedExchange

    public class HttpExchange

extends java.lang.Object

An HTTP client API that encapsulates Exchange with a HTTP server. This object encapsulates:

- The HTTP server. (see `#setAddress(InetSocketAddress)` or `setURL(String)`)
- The HTTP request method, URI and HTTP version (see `setMethod(String)`, `setURI(String)`, and `setVersion(int)`
- The Request headers (see `addRequestHeader(String, String)` or `setRequestHeader(String, String)`)
- The Request content (see `setRequestContent(Buffer)` or `setRequestContentSource(InputStream)`)
- The status of the exchange (see `getStatus()`)
- Callbacks to handle state changes (see the onXxx methods such as `onRequestComplete()` or `onResponseComplete()`)
- The ability to intercept callbacks (see `setEventListener(HttpEventListener)`

The HttpExchange class is intended to be used by a developer wishing to have close asynchronous interaction with the the exchange. Typically a developer will extend the HttpExchange class with a derived class that implements some or all of the onXxx callbacks. There are also some predefined HttpExchange subtypes that can be used as a basis (see `ContentExchange` and `HttpExchange.CachedExchange`.

Typically the HttpExchange is passed to a the `HttpClient.send(HttpExchange)` method, which in turn selects a `HttpDestination` and calls it's HttpDestination#send(HttpExchange), which then creates or selects a {@link HttpConnection} and calls its {@link HttpConnection#send(HttpExchange). A developer may wish to directly call send on the destination or connection if they wish to bypass some handling provided (eg Cookie handling in the HttpDestination).

In some circumstances, the HttpClient or HttpDestination may wish to retry a HttpExchange (eg. failed pipeline request, authentication retry or redirection). In such cases, the HttpClient and/or HttpDestination may insert their own HttpExchangeListener to intercept and filter the call backs intended for the HttpExchange.

Author:
  gregw, Guillaume Nodet

Nested Class Summary
`static class` `HttpExchange.CachedExchange` Deprecated. use `CachedExchange`
`static class` `HttpExchange.ContentExchange` Deprecated. use `ContentExchange`

Field Summary
`static int` `STATUS_COMPLETED`
`static int` `STATUS_EXCEPTED`
`static int` `STATUS_EXPIRED`
`static int` `STATUS_PARSING_CONTENT`
`static int` `STATUS_PARSING_HEADERS`
`static int` `STATUS_SENDING_REQUEST`
`static int` `STATUS_START`
`static int` `STATUS_WAITING_FOR_COMMIT`
`static int` `STATUS_WAITING_FOR_CONNECTION`
`static int` `STATUS_WAITING_FOR_RESPONSE`

Constructor Summary
`HttpExchange()`

Method Summary
`void` `addRequestHeader(Buffer name, Buffer value)`
`void` `addRequestHeader(java.lang.String name, java.lang.String value)`
`void` `cancel()` Cancel this exchange Currently this implementation does nothing.
`boolean` `configureListeners()` true of the exchange should have listeners configured for it by the destination false if this is being managed elsewhere
`Address` `getAddress()`
`HttpEventListener` `getEventListener()`
`java.lang.String` `getMethod()`
`Buffer` `getRequestContent()`
`Buffer` `getRequestContentChunk()`
`java.io.InputStream` `getRequestContentSource()`
`HttpFields` `getRequestFields()`
`boolean` `getRetryStatus()`
`Buffer` `getScheme()`
`int` `getStatus()`
`long` `getTimeout()`
`java.lang.String` `getURI()`
`int` `getVersion()`
`boolean` `isDone(int status)`
`protected void` `onConnectionFailed(java.lang.Throwable ex)` Called when an exception was thrown during an attempt to open a connection
`protected void` `onException(java.lang.Throwable ex)` Called when any other exception occurs during handling for the exchange
`protected void` `onExpire()` Called when no response has been received within the timeout.
`protected void` `onRequestCommitted()` Called when the request headers has been sent
`protected void` `onRequestComplete()` Called when the request and it's body have been sent.
`protected void` `onResponseComplete()` Called when the entire response has been received
`protected void` `onResponseContent(Buffer content)` Called for each chunk of the response content received.
`protected void` `onResponseHeader(Buffer name, Buffer value)` Called for each response header received
`protected void` `onResponseHeaderComplete()` Called when the response header has been completely received.
`protected void` `onResponseStatus(Buffer version, int status, Buffer reason)` Called when a response status line has been received.
`protected void` `onRetry()` Called when the request is retried (due to failures or authentication).
`void` `reset()`
`void` `setAddress(Address address)`
`void` `setConfigureListeners(boolean autoConfigure)`
`void` `setEventListener(HttpEventListener listener)`
`void` `setMethod(java.lang.String method)`
`void` `setRequestContent(Buffer requestContent)`
`void` `setRequestContentSource(java.io.InputStream in)`
`void` `setRequestContentType(java.lang.String value)`
`void` `setRequestHeader(Buffer name, Buffer value)`
`void` `setRequestHeader(java.lang.String name, java.lang.String value)`
`void` `setRetryStatus(boolean retryStatus)`
`void` `setScheme(Buffer scheme)`
`void` `setTimeout(long timeout)`
`void` `setURI(java.lang.String uri)`
`void` `setURL(java.lang.String url)`
`void` `setVersion(int version)`
`void` `setVersion(java.lang.String version)`
`java.lang.String` `toString()`
`int` `waitForDone()` Wait until the exchange is "done".
`void` `waitForStatus(int status)` Deprecated.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### STATUS_START

    public static final int STATUS_START

See Also:
  Constant Field Values

### STATUS_WAITING_FOR_CONNECTION

    public static final int STATUS_WAITING_FOR_CONNECTION

See Also:
  Constant Field Values

### STATUS_WAITING_FOR_COMMIT

    public static final int STATUS_WAITING_FOR_COMMIT

See Also:
  Constant Field Values

### STATUS_SENDING_REQUEST

    public static final int STATUS_SENDING_REQUEST

See Also:
  Constant Field Values

### STATUS_WAITING_FOR_RESPONSE

    public static final int STATUS_WAITING_FOR_RESPONSE

See Also:
  Constant Field Values

### STATUS_PARSING_HEADERS

    public static final int STATUS_PARSING_HEADERS

See Also:
  Constant Field Values

### STATUS_PARSING_CONTENT

    public static final int STATUS_PARSING_CONTENT

See Also:
  Constant Field Values

### STATUS_COMPLETED

    public static final int STATUS_COMPLETED

See Also:
  Constant Field Values

### STATUS_EXPIRED

    public static final int STATUS_EXPIRED

See Also:
  Constant Field Values

### STATUS_EXCEPTED

    public static final int STATUS_EXCEPTED

See Also:
  Constant Field Values

Constructor Detail

### HttpExchange

    public HttpExchange()

Method Detail

### getStatus

    public int getStatus()

### waitForStatus

    public void waitForStatus(int status)
                       throws java.lang.InterruptedException

  Deprecated.

  Throws:
    `java.lang.InterruptedException`

### waitForDone

    public int waitForDone()
                    throws java.lang.InterruptedException

  Wait until the exchange is "done". Done is defined as when a final state has been passed to the HttpExchange via the associated onXxx call. Note that an exchange can transit a final state when being used as part of a dialog (eg `SecurityListener`. Done status is thus defined as:

         done == onConnectionFailed
              || onException
              || onExpire
              || onRequestComplete && onResponseComplete

  Returns:
  Throws:
    `java.lang.InterruptedException`

### reset

    public void reset()

### isDone

    public boolean isDone(int status)

### getEventListener

    public HttpEventListener getEventListener()

### setEventListener

    public void setEventListener(HttpEventListener listener)

### setURL

    public void setURL(java.lang.String url)

  Parameters:
    `url` - Including protocol, host and port

### setAddress

    public void setAddress(Address address)

  Parameters:
    `address` -

### getAddress

    public Address getAddress()

  Returns:

### setScheme

    public void setScheme(Buffer scheme)

  Parameters:
    `scheme` -

### getScheme

    public Buffer getScheme()

  Returns:

### setVersion

    public void setVersion(int version)

  Parameters:
    `version` - as integer, 9, 10 or 11 for 0.9, 1.0 or 1.1

### setVersion

    public void setVersion(java.lang.String version)

### getVersion

    public int getVersion()

  Returns:

### setMethod

    public void setMethod(java.lang.String method)

  Parameters:
    `method` -

### getMethod

    public java.lang.String getMethod()

  Returns:

### getURI

    public java.lang.String getURI()

  Returns:

### setURI

    public void setURI(java.lang.String uri)

  Parameters:
    `uri` -

### addRequestHeader

    public void addRequestHeader(java.lang.String name,
                                 java.lang.String value)

  Parameters:
    `name` -
    `value` -

### addRequestHeader

    public void addRequestHeader(Buffer name,
                                 Buffer value)

  Parameters:
    `name` -
    `value` -

### setRequestHeader

    public void setRequestHeader(java.lang.String name,
                                 java.lang.String value)

  Parameters:
    `name` -
    `value` -

### setRequestHeader

    public void setRequestHeader(Buffer name,
                                 Buffer value)

  Parameters:
    `name` -
    `value` -

### setRequestContentType

    public void setRequestContentType(java.lang.String value)

  Parameters:
    `value` -

### getRequestFields

    public HttpFields getRequestFields()

  Returns:

### setRequestContent

    public void setRequestContent(Buffer requestContent)

  Parameters:
    `requestContent` -

### setRequestContentSource

    public void setRequestContentSource(java.io.InputStream in)

  Parameters:
    `in` -

### getRequestContentSource

    public java.io.InputStream getRequestContentSource()

### getRequestContentChunk

    public Buffer getRequestContentChunk()
                                  throws java.io.IOException

  Throws:
    `java.io.IOException`

### getRequestContent

    public Buffer getRequestContent()

### getRetryStatus

    public boolean getRetryStatus()

### setRetryStatus

    public void setRetryStatus(boolean retryStatus)

### getTimeout

    public long getTimeout()

### setTimeout

    public void setTimeout(long timeout)

### cancel

    public void cancel()

  Cancel this exchange Currently this implementation does nothing.

### toString

    public java.lang.String toString()

  Overrides:
    `toString` in class `java.lang.Object`

### onRequestCommitted

    protected void onRequestCommitted()
                               throws java.io.IOException

  Called when the request headers has been sent

  Throws:
    `java.io.IOException`

### onRequestComplete

    protected void onRequestComplete()
                              throws java.io.IOException

  Called when the request and it's body have been sent.

  Throws:
    `java.io.IOException`

### onResponseStatus

    protected void onResponseStatus(Buffer version,
                                    int status,
                                    Buffer reason)
                             throws java.io.IOException

  Called when a response status line has been received.

  Parameters:
    `version` - HTTP version
    `status` - HTTP status code
    `reason` - HTTP status code reason string
  Throws:
    `java.io.IOException`

### onResponseHeader

    protected void onResponseHeader(Buffer name,
                                    Buffer value)
                             throws java.io.IOException

  Called for each response header received

  Parameters:
    `name` - header name
    `value` - header value
  Throws:
    `java.io.IOException`

### onResponseHeaderComplete

    protected void onResponseHeaderComplete()
                                     throws java.io.IOException

  Called when the response header has been completely received.

  Throws:
    `java.io.IOException`

### onResponseContent

    protected void onResponseContent(Buffer content)
                              throws java.io.IOException

  Called for each chunk of the response content received.

  Parameters:
    `content` -
  Throws:
    `java.io.IOException`

### onResponseComplete

    protected void onResponseComplete()
                               throws java.io.IOException

  Called when the entire response has been received

  Throws:
    `java.io.IOException`

### onConnectionFailed

    protected void onConnectionFailed(java.lang.Throwable ex)

  Called when an exception was thrown during an attempt to open a connection

  Parameters:
    `ex` -

### onException

    protected void onException(java.lang.Throwable ex)

  Called when any other exception occurs during handling for the exchange

  Parameters:
    `ex` -

### onExpire

    protected void onExpire()

  Called when no response has been received within the timeout.

### onRetry

    protected void onRetry()
                    throws java.io.IOException

  Called when the request is retried (due to failures or authentication). Implementations may need to reset any consumable content that needs to be sent.

  Throws:
    `java.io.IOException`

### configureListeners

    public boolean configureListeners()

  true of the exchange should have listeners configured for it by the destination false if this is being managed elsewhere

  Returns:

### setConfigureListeners

    public void setConfigureListeners(boolean autoConfigure)

Copyright © 1995-2010 Mort Bay Consulting. All Rights Reserved.