Class ResponseHandler<T>

java.lang.Object
org.stellar.sdk.requests.ResponseHandler<T>
Type Parameters:
T - the expected response type

public class ResponseHandler<T> extends Object
Handles HTTP responses and converts them into typed Java objects.

Successful responses (2xx) are deserialized into the target type T using Gson. Error responses are translated into the appropriate SDK exception:

  • Constructor Summary

    Constructors
    Constructor
    Description
    ResponseHandler(com.google.gson.reflect.TypeToken<T> type)
    "Generics on a type are typically erased at runtime, except when the type is compiled with the generic parameter bound.
  • Method Summary

    Modifier and Type
    Method
    Description
    handleResponse(okhttp3.Response response)
    Handles the HTTP response and converts it to the appropriate object or throws exceptions based on the response status.
    handleResponse(okhttp3.Response response, boolean submitTransactionAsync)
    Handles the HTTP response and converts it to the appropriate object or throws exceptions based on the response status.
    handleResponse(okhttp3.Response response, String content)
    Handles the HTTP response with pre-read body content and converts it to the appropriate object or throws exceptions based on the response status.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResponseHandler

      public ResponseHandler(com.google.gson.reflect.TypeToken<T> type)
      "Generics on a type are typically erased at runtime, except when the type is compiled with the generic parameter bound. In that case, the compiler inserts the generic type information into the compiled class. In other cases, that is not possible." More info: http://stackoverflow.com/a/14506181
      Parameters:
      type - the type token representing the response class to deserialize into
  • Method Details

    • handleResponse

      public T handleResponse(okhttp3.Response response)
      Handles the HTTP response and converts it to the appropriate object or throws exceptions based on the response status.
      Parameters:
      response - The HTTP response to handle
      Returns:
      The parsed object of type T
      Throws:
      TooManyRequestsException - If the response code is 429 (Too Many Requests)
      RequestTimeoutException - If the response code is 504 (Gateway Timeout)
      UnexpectedException - If the response body is empty or there's an unexpected error reading the response
      BadRequestException - If the response code is in the 4xx range (except 429)
      BadResponseException - If the response code is in the 5xx range (except 504)
      UnknownResponseException - If the response code is not in the 2xx, 4xx, or 5xx range
    • handleResponse

      public T handleResponse(okhttp3.Response response, String content)
      Handles the HTTP response with pre-read body content and converts it to the appropriate object or throws exceptions based on the response status.

      This method is useful when the caller needs to limit the response body size before processing, for example to prevent denial-of-service attacks.

      Note: This method does NOT close the response. The caller is responsible for closing the response, typically using try-with-resources on the response object.

      Parameters:
      response - The HTTP response to handle (used for status code and headers)
      content - The pre-read response body content
      Returns:
      The parsed object of type T
      Throws:
      TooManyRequestsException - If the response code is 429 (Too Many Requests)
      RequestTimeoutException - If the response code is 504 (Gateway Timeout)
      BadRequestException - If the response code is in the 4xx range (except 429)
      BadResponseException - If the response code is in the 5xx range (except 504)
      UnknownResponseException - If the response code is not in the 2xx, 4xx, or 5xx range
    • handleResponse

      public T handleResponse(okhttp3.Response response, boolean submitTransactionAsync)
      Handles the HTTP response and converts it to the appropriate object or throws exceptions based on the response status.
      Parameters:
      response - The HTTP response to handle
      submitTransactionAsync - Only set it to true when calling Server.submitTransactionXdrAsync(String).
      Returns:
      The parsed object of type T
      Throws:
      TooManyRequestsException - If the response code is 429 (Too Many Requests)
      RequestTimeoutException - If the response code is 504 (Gateway Timeout)
      UnexpectedException - If the response body is empty or there's an unexpected error reading the response
      BadRequestException - If the response code is in the 4xx range (except 429)
      BadResponseException - If the response code is in the 5xx range (except 504)
      UnknownResponseException - If the response code is not in the 2xx, 4xx, or 5xx range