Class GitHubConnectorResponse

java.lang.Object
org.kohsuke.github.connector.GitHubConnectorResponse
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
GitHubConnectorResponse.ByteArrayResponse

public abstract class GitHubConnectorResponse extends Object implements Closeable
Response information supplied when a response is received and before the body is processed.

During a request to GitHub, GitHubConnector.send(GitHubConnectorRequest) returns a GitHubConnectorResponse. This is processed to create a GitHubResponse.

Instances of this class are closed once the response is done being processed. This means that bodyStream() will not be readable after a call is completed. statusCode(), allHeaders(), and request() will still be readable but it is recommended that consumers copy any information they need rather than retaining a reference to GitHubConnectorResponse.

Author:
Liam Newman
  • Constructor Details

    • GitHubConnectorResponse

      protected GitHubConnectorResponse(@Nonnull GitHubConnectorRequest request, int statusCode, @Nonnull Map<String,List<String>> headers)
      GitHubConnectorResponse constructor
      Parameters:
      request - the request
      statusCode - the status code
      headers - the headers
  • Method Details

    • header

      @CheckForNull public String header(String name)
      Gets the value of a header field for this response.
      Parameters:
      name - the name of the header field.
      Returns:
      the value of the header field, or null if the header isn't set.
    • bodyStream

      @Nonnull public InputStream bodyStream() throws IOException
      The response body as an InputStream. When isBodyStreamRereadable is false, bodyStream() can only be called once and the returned stream should be assumed to be read-once and not resetable. This is the default behavior for HTTP_OK responses and significantly reduces memory usage. When isBodyStreamRereadable is true, bodyStream() can be called be called multiple times. The full stream data is read into a byte array during the first call. Each call returns a new stream backed by the same byte array. This uses more memory, but is required to enable rereading the body stream during trace logging, debugging, and error responses.
      Returns:
      the response body
      Throws:
      IOException - if response stream is null or an I/O Exception occurs.
    • rawBodyStream

      @CheckForNull protected abstract InputStream rawBodyStream() throws IOException
      Get the raw implementation specific body stream for this response. This method will only be called once to completion. If an exception is thrown by this method, it may be called multiple times. The stream returned from this method will be closed when the response is closed or sooner. Inheriting classes do not need to close it.
      Returns:
      the stream for the raw response
      Throws:
      IOException - if an I/O Exception occurs.
    • request

      @Nonnull public GitHubConnectorRequest request()
      Gets the GitHubConnector for this response.
      Returns:
      the GitHubConnector for this response.
    • statusCode

      public int statusCode()
      The status code for this response.
      Returns:
      the status code for this response.
    • allHeaders

      @Nonnull public Map<String,List<String>> allHeaders()
      The headers for this response.
      Returns:
      the headers for this response.
    • isBodyStreamRereadable

      public boolean isBodyStreamRereadable()
      The body stream rereadable state. Body stream defaults to read once for HTTP_OK responses (to reduce memory usage). For non-HTTP_OK responses, body stream is switched to rereadable (in-memory byte array) for error processing. Calling setBodyStreamRereadable() will force isBodyStreamRereadable to be true for this response regardless of statusCode value.
      Returns:
      true when body stream is rereadable.
    • setBodyStreamRereadable

      public void setBodyStreamRereadable()
      Force body stream to rereadable regardless of status code. Calling setBodyStreamRereadable() will force isBodyStreamRereadable to be true for this response regardless of statusCode value. This is required to support body value logging during low-level tracing but should be avoided in general since it consumes significantly more memory. Will throw runtime exception if a non-rereadable body stream has already been returned from bodyStream().
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • wrapStream

      protected InputStream wrapStream(InputStream stream) throws IOException
      Handles wrapping the body stream if indicated by the "Content-Encoding" header.
      Parameters:
      stream - the stream to possibly wrap
      Returns:
      an input stream potentially wrapped to decode gzip input
      Throws:
      IOException - if an I/O Exception occurs.
    • parseInt

      public final int parseInt(String name) throws NumberFormatException
      Parse a header value as a signed decimal integer.
      Parameters:
      name - the header field to parse
      Returns:
      integer value of the header field
      Throws:
      NumberFormatException - if the header is missing or does not contain a parsable integer.