Class HttpComponentsHttpResponse

java.lang.Object
it.unimi.dsi.law.warc.util.AbstractHttpResponse
it.unimi.dsi.law.warc.util.HttpComponentsHttpResponse
All Implemented Interfaces:
HttpResponse, Response

public class HttpComponentsHttpResponse
extends AbstractHttpResponse
An concrete subclass of AbstractHttpResponse that implements missing methods by wrapping an Apache HTTP Components HttpResponse.

A typical use case of this class is storing in a WarcRecord the content of an Apache HTTP Components HttpResponse. The nested class HttpComponentsHttpResponse.HttpResponseHeaderMap can be used in other classes to expose as a standard Java map the header-related methods of an Apache HTTP Components HttpResponse.

To be able to return a MeasurableInputStream, this class caches the result of the underlying Apache HTTP Components HttpResponse. The cache is never shrunk, but clear() will trim it to length zero. You can override contentAsStream() to provide different ways of perform the caching.

  • Field Details

  • Constructor Details

    • HttpComponentsHttpResponse

      public HttpComponentsHttpResponse()
      Creates a new instance.

      Use set(URI, HttpResponse) to wrap an Apache HTTP Components HttpResponse.

    • HttpComponentsHttpResponse

      public HttpComponentsHttpResponse​(URI url, org.apache.http.HttpResponse httpResponse)
      Creates a new instance wrapping a given Apache HTTP Components HttpResponse.
      Parameters:
      url - the URL for httpResponse.
      httpResponse - the response to be wrapped.
  • Method Details

    • set

      public void set​(URI url, org.apache.http.HttpResponse httpResponse)
      Sets the response wrapped by this instance.
      Parameters:
      url - the URL for httpResponse.
      httpResponse - the response to be wrapped.
    • consume

      public void consume() throws IOException
      Invokes EntityUtils.consume(HttpEntity) on the entity returned by the underlying Apache HTTP Components HttpResponse.
      Throws:
      IOException
    • status

      public int status()
      Description copied from interface: HttpResponse
      Returns the response status.
      Returns:
      the status of this response.
    • statusLine

      public org.apache.http.StatusLine statusLine()
      Description copied from interface: HttpResponse
      Returns the response status line.
      Returns:
      the status line of this response.
    • headers

      public Map<String,​String> headers()
      Description copied from interface: HttpResponse
      Returns the headers of this response.

      Warning: as of LAW 3.0, and contrarily to previous behaviour, the map is case-sensitive. Use the predefined names in HttpHeaders or HttpHeaders to avoid typing and casing mistakes.

      Returns:
      the headers of this response.
    • contentAsStream

      public MeasurableInputStream contentAsStream() throws IOException
      Description copied from interface: HttpResponse
      Returns the content of this response as a stream.
      Returns:
      the content of this response as a stream.
      Throws:
      IOException
    • uri

      public URI uri()
      Description copied from interface: Response
      Returns the URI associated with this response.
      Returns:
      the URI associated with this response.
    • fromWarcRecord

      public boolean fromWarcRecord​(WarcRecord record) throws IOException
      Description copied from interface: Response
      Fills this response with the content of a WarcRecord (optional operation).
      Parameters:
      record - the record.
      Returns:
      true iff the record-type of the given record is response.
      Throws:
      IOException
    • clear

      public void clear()
      Clears this HttpComponentsHttpResponse, in particular trimming the content cache.