Record Class RestServiceResponse

java.lang.Object
java.lang.Record
com.bonitasoft.processbuilder.records.RestServiceResponse
Record Components:
statusCode - The HTTP status code
headers - Response headers
body - The response body as a string
contentType - The content type of the response
executionTimeMs - Time taken to execute the request in milliseconds
errorMessage - Error message if the request failed (null if successful)
url - The URL that was called

public record RestServiceResponse(int statusCode, Map<String,String> headers, String body, RestContentType contentType, long executionTimeMs, String errorMessage, String url) extends Record
Represents a REST service response.

This record contains all the information from a REST API response, including the status code, headers, body, and execution metadata.

Since:
1.0
Author:
Bonitasoft
  • Constructor Details

  • Method Details

    • success

      public static RestServiceResponse success(int statusCode, Map<String,String> headers, String body, RestContentType contentType, long executionTimeMs, String url)
      Creates a successful response.
      Parameters:
      statusCode - The HTTP status code
      headers - Response headers
      body - Response body
      contentType - Content type of the response
      executionTimeMs - Execution time
      url - The URL called
      Returns:
      A successful response
    • error

      public static RestServiceResponse error(String errorMessage, long executionTimeMs, String url)
      Creates an error response.
      Parameters:
      errorMessage - The error message
      executionTimeMs - Execution time
      url - The URL that was called
      Returns:
      An error response with status code -1
    • fromException

      public static RestServiceResponse fromException(Exception exception, long executionTimeMs, String url)
      Creates an error response from an exception.
      Parameters:
      exception - The exception that occurred
      executionTimeMs - Execution time
      url - The URL that was called
      Returns:
      An error response
    • isSuccessful

      public boolean isSuccessful()
      Checks if the response indicates success (2xx status code).
      Returns:
      true if status code is between 200 and 299
    • isClientError

      public boolean isClientError()
      Checks if the response indicates a client error (4xx status code).
      Returns:
      true if status code is between 400 and 499
    • isServerError

      public boolean isServerError()
      Checks if the response indicates a server error (5xx status code).
      Returns:
      true if status code is between 500 and 599
    • isError

      public boolean isError()
      Checks if there was an error (network error or non-success HTTP status).
      Returns:
      true if there was an error
    • isRedirect

      public boolean isRedirect()
      Checks if the response was a redirect (3xx status code).
      Returns:
      true if status code is between 300 and 399
    • bodyAsJson

      public Optional<com.fasterxml.jackson.databind.JsonNode> bodyAsJson()
      Parses the response body as JSON.
      Returns:
      Optional containing the JsonNode, or empty if parsing fails
    • bodyAs

      public <T> Optional<T> bodyAs(Class<T> clazz)
      Parses the response body as a specific type.
      Type Parameters:
      T - The type to parse to
      Parameters:
      clazz - The class of the type
      Returns:
      Optional containing the parsed object, or empty if parsing fails
    • getJsonField

      public Optional<String> getJsonField(String fieldName)
      Gets a specific field from the JSON body.
      Parameters:
      fieldName - The field name to extract
      Returns:
      Optional containing the field value as string, or empty if not found
    • hasJsonBody

      public boolean hasJsonBody()
      Checks if the response body contains JSON content.
      Returns:
      true if the content type is JSON and body is not empty
    • getHeader

      public Optional<String> getHeader(String headerName)
      Gets a specific header value (case-insensitive).
      Parameters:
      headerName - The header name
      Returns:
      Optional containing the header value, or empty if not found
    • getLocation

      public Optional<String> getLocation()
      Gets the Location header (for redirects).
      Returns:
      Optional containing the Location header value
    • toSummary

      public String toSummary()
      Returns a summary of the response for logging.
      Returns:
      A summary string
    • withBody

      public RestServiceResponse withBody(String newBody)
      Creates a copy of this response with a different body.
      Parameters:
      newBody - The new body
      Returns:
      A new response with the updated body
    • withError

      public RestServiceResponse withError(String newErrorMessage)
      Creates a copy of this response with a different error message.
      Parameters:
      newErrorMessage - The new error message
      Returns:
      A new response with the updated error message
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • statusCode

      public int statusCode()
      Returns the value of the statusCode record component.
      Returns:
      the value of the statusCode record component
    • headers

      public Map<String,String> headers()
      Returns the value of the headers record component.
      Returns:
      the value of the headers record component
    • body

      public String body()
      Returns the value of the body record component.
      Returns:
      the value of the body record component
    • contentType

      public RestContentType contentType()
      Returns the value of the contentType record component.
      Returns:
      the value of the contentType record component
    • executionTimeMs

      public long executionTimeMs()
      Returns the value of the executionTimeMs record component.
      Returns:
      the value of the executionTimeMs record component
    • errorMessage

      public String errorMessage()
      Returns the value of the errorMessage record component.
      Returns:
      the value of the errorMessage record component
    • url

      public String url()
      Returns the value of the url record component.
      Returns:
      the value of the url record component