Class WebResponse

java.lang.Object
io.fluxcapacitor.javaclient.common.Message
io.fluxcapacitor.javaclient.web.WebResponse
All Implemented Interfaces:
HasMetadata, HasMessage

public class WebResponse extends Message
Represents a response to a WebRequest in the Flux platform.

WebResponse extends Message and includes:

  • status – HTTP status code (e.g. 200, 404)
  • headers – HTTP headers
  • cookies – parsed from Set-Cookie
  • contentType – inferred from headers

The response payload may be encoded and compressed (e.g. gzip) based on metadata. It supports transformation, enrichment, and construction via a Builder.

See Also:
  • Field Details

  • Constructor Details

    • WebResponse

      public WebResponse(Message m)
      Constructs a new WebResponse instance using the provided Message object.
      Parameters:
      m - the Message object from which the payload, metadata, message ID, and timestamp are extracted to initialize the WebResponse.
  • Method Details

    • ok

      public static WebResponse ok(Object payload, Map<String,String> headers)
    • ok

      public static WebResponse ok(org.springframework.util.function.ThrowingSupplier<? extends InputStream> inputStreamSupplier, Map<String,String> headers)
    • partial

      public static WebResponse partial(org.springframework.util.function.ThrowingSupplier<? extends InputStream> inputStreamSupplier, Map<String,String> headers)
    • notModified

      public static WebResponse notModified(Map<String,String> headers)
    • notFound

      public static WebResponse notFound(String reason)
    • serialize

      public SerializedMessage serialize(Serializer serializer)
      Serializes the response using the content type if applicable.
      Overrides:
      serialize in class Message
    • asMetadata

      public static Metadata asMetadata(int statusCode, Map<String,List<String>> headers)
      Constructs a Metadata object containing the provided status code and headers.
      Parameters:
      statusCode - the HTTP status code to be included in the metadata
      headers - a map of HTTP headers where each key is a header name and the corresponding value is a list of header values
      Returns:
      a Metadata object containing the status code and headers
    • withMetadata

      public WebResponse withMetadata(Metadata metadata)
    • addMetadata

      public WebResponse addMetadata(Metadata metadata)
      Description copied from class: Message
      Returns a new message with the combined metadata.
      Overrides:
      addMetadata in class Message
    • addMetadata

      public WebResponse addMetadata(String key, Object value)
      Description copied from class: Message
      Adds a single metadata entry.
      Overrides:
      addMetadata in class Message
    • addMetadata

      public WebResponse addMetadata(Object... keyValues)
      Description copied from class: Message
      Adds multiple metadata entries.
      Overrides:
      addMetadata in class Message
    • addMetadata

      public WebResponse addMetadata(Map<String,?> values)
      Description copied from class: Message
      Adds metadata from a given map.
      Overrides:
      addMetadata in class Message
    • addUser

      public WebResponse addUser(User user)
      Description copied from class: Message
      Attaches a user object to the metadata using the configured UserProvider.
      Overrides:
      addUser in class Message
    • withPayload

      public WebResponse withPayload(Object payload)
      Description copied from class: Message
      Returns a new message instance with the provided payload and existing metadata, ID, and timestamp.
      Overrides:
      withPayload in class Message
    • withMessageId

      public WebResponse withMessageId(String messageId)
    • withTimestamp

      public WebResponse withTimestamp(Instant timestamp)
    • toBuilder

      public WebResponse.Builder toBuilder()
      Converts this WebResponse instance into a builder, which can be used to create a modified copy of the instance.
      Returns:
      a new WebResponse.Builder initialized with the properties of the current WebResponse instance.
    • builder

      public static WebResponse.Builder builder()
      Creates a new WebResponse.Builder instance for constructing WebResponse objects.
      Returns:
      a new instance of the WebResponse.Builder.
    • getHeaders

      public static Map<String,List<String>> getHeaders(Metadata metadata)
      Retrieves a case-insensitive map of headers from the provided Metadata object.
      Parameters:
      metadata - the Metadata object from which headers should be extracted
      Returns:
      a map of headers where the keys are header names and the values are lists of header values; if no headers are found, returns an empty case-insensitive map
    • getStatusCode

      public static Integer getStatusCode(Metadata metadata)
      Retrieves the status code from the provided metadata.
      Parameters:
      metadata - the metadata containing information associated with the response
      Returns:
      the status code as an Integer if present; otherwise, null
    • getPayload

      public <R> R getPayload()
      Retrieves the decoded payload from the response and casts it to the specified type.
      Specified by:
      getPayload in interface HasMessage
      Overrides:
      getPayload in class Message
      Type Parameters:
      R - The type to which the payload will be cast.
      Returns:
      The decoded payload of the response, cast to the specified type.
    • getPayloadAs

      public <R> R getPayloadAs(Type type)
      Retrieves the payload and converts it to the specified type.
      Type Parameters:
      R - the desired type of the returned payload
      Parameters:
      type - the target type to which the payload should be converted
      Returns:
      the payload converted to the specified type R
    • getHeaders

      public List<String> getHeaders(String name)
      Retrieves the list of header values associated with the given header name. If no headers are found for the provided name, an empty list is returned.
      Parameters:
      name - the name of the header to retrieve
      Returns:
      a list of header values associated with the specified name, or an empty list if none are found
    • retainHeaders

      public WebResponse retainHeaders(String... headerNames)
      Retains only the specified headers from the current WebResponse. Any headers not listed in the provided header names will be removed.
      Parameters:
      headerNames - the names of headers to be retained in the WebResponse
      Returns:
      a new WebResponse instance containing only the specified headers
    • getHeader

      public String getHeader(String name)
      Retrieves the value of the first occurrence of the specified header name.
      Parameters:
      name - the name of the HTTP header to retrieve
      Returns:
      the first value associated with the specified header name, or null if the header is not present
    • getCookies

      public List<HttpCookie> getCookies()
      Retrieves a list of cookies from the response's "Set-Cookie" headers. The cookies are parsed from the headers using the WebUtils.parseResponseCookieHeader method.
      Returns:
      a list of HttpCookie objects parsed from the "Set-Cookie" headers; an empty list if there are no cookies or the headers are absent
    • getContentType

      public String getContentType()
      Retrieves the value of the "Content-Type" header from the HTTP response. If the header is not present, returns null.
      Returns:
      the value of the "Content-Type" header, or null if the header is absent
    • getHeaders

      @NonNull public @NonNull Map<String,List<String>> getHeaders()
      Returns all HTTP headers associated with the current response.
    • getStatus

      public Integer getStatus()
      Returns the HTTP status code associated with the current response.