Class ResponseHeader

java.lang.Object
org.apache.juneau.http.header.BasicHeader
org.apache.juneau.rest.client.ResponseHeader
All Implemented Interfaces:
Serializable, Cloneable, Header, NameValuePair

public class ResponseHeader extends BasicHeader
Represents a single header on an HTTP response.

An extension of an HttpClient Header that provides various support for converting the header to POJOs and other convenience methods.

See Also:
See Also:
  • Constructor Details

  • Method Details

    • schema

      Specifies the part schema for this header.

      Used by schema-based part parsers such as OpenApiParser.

      Parameters:
      value - The part schema.
      Returns:
      This object.
    • parser

      Specifies the part parser to use for this header.

      If not specified, uses the part parser defined on the client by calling RestClient.Builder.partParser(Class).

      Parameters:
      value - The new part parser to use for this header.
      If null, SimplePartParser.DEFAULT will be used.
      Returns:
      This object.
    • asInteger

      Returns the value of this header as an integer.
      Returns:
      The value of this header as an integer, or Optional.empty() if the header was not present.
    • asBoolean

      Returns the value of this header as a boolean.
      Returns:
      The value of this header as a boolean, or Optional.empty() if the header was not present.
    • asLong

      public Optional<Long> asLong()
      Returns the value of this header as a long.
      Returns:
      The value of this header as a long, or Optional.empty() if the header was not present.
    • asDate

      Returns the value of this header as a date.
      Returns:
      The value of this header as a date, or Optional.empty() if the header was not present.
    • asCsvArray

      public Optional<String[]> asCsvArray()
      Returns the value of this header as a list from a comma-delimited string.
      Returns:
      The value of this header as a list from a comma-delimited string, or Optional.empty() if the header was not present.
    • asHeader

      public <T extends BasicHeader> T asHeader(Class<T> c)
      Returns the value of this header as a BasicHeader.
      Type Parameters:
      T - The subclass of BasicHeader to instantiate.
      Parameters:
      c - The subclass of BasicHeader to instantiate.
      Returns:
      The value of this header as a string, never null.
    • asCsvHeader

      Returns the value of this header as a CSV array header.
      Returns:
      The value of this header as a CSV array header, never null.
    • asDateHeader

      Returns the value of this header as a date header.
      Returns:
      The value of this header as a date header, never null.
    • asEntityTagsHeader

      Returns the value of this header as an entity validator array header.
      Returns:
      The value of this header as an entity validator array header, never null.
    • asEntityTagHeader

      Returns the value of this header as an entity validator header.
      Returns:
      The value of this header as an entity validator array, never null.
    • asIntegerHeader

      Returns the value of this header as an integer header.
      Returns:
      The value of this header as an integer header, never null.
    • asBooleanHeader

      Returns the value of this header as an boolean header.
      Returns:
      The value of this header as an boolean header, never null.
    • asLongHeader

      Returns the value of this header as a long header.
      Returns:
      The value of this header as a long header, never null.
    • asStringRangesHeader

      Returns the value of this header as a range array header.
      Returns:
      The value of this header as a range array header, never null.
    • asStringHeader

      Returns the value of this header as a string header.
      Returns:
      The value of this header as a string header, never null.
    • asUriHeader

      Returns the value of this header as a URI header.
      Returns:
      The value of this header as a URI header, never null.
    • asString

      public RestResponse asString(Value<String> value)
      Same as BasicHeader.asString() but sets the value in a mutable for fluent calls.
      Parameters:
      value - The mutable to set the header value in.
      Returns:
      This object.
    • as

      public <T> Optional<T> as(Type type, Type... args)
      Converts this header to the specified type.

      See Complex Data Types for information on defining complex generic types of Maps and Collections.

      Type Parameters:
      T - The type to convert to.
      Parameters:
      type - The type to convert to.
      args - The type parameters.
      Returns:
      The converted type, or null if header is not present.
    • as

      public <T> RestResponse as(Value<T> value, Type type, Type... args)
      Same as as(Type,Type...) but sets the value in a mutable for fluent calls.

      See Complex Data Types for information on defining complex generic types of Maps and Collections.

      Type Parameters:
      T - The type to convert to.
      Parameters:
      value - The mutable to set the parsed header value in.
      type - The type to convert to.
      args - The type parameters.
      Returns:
      This object.
    • as

      public <T> Optional<T> as(Class<T> type)
      Converts this header to the specified type.
      Type Parameters:
      T - The type to convert to.
      Parameters:
      type - The type to convert to.
      Returns:
      The converted type, or null if header is not present.
    • as

      public <T> RestResponse as(Value<T> value, Class<T> type)
      Same as as(Class) but sets the value in a mutable for fluent calls.
      Type Parameters:
      T - The type to convert to.
      Parameters:
      value - The mutable to set the parsed header value in.
      type - The type to convert to.
      Returns:
      This object.
    • as

      public <T> Optional<T> as(ClassMeta<T> type)
      Converts this header to the specified type.
      Type Parameters:
      T - The type to convert to.
      Parameters:
      type - The type to convert to.
      Returns:
      The converted type, or null if header is not present.
    • as

      public <T> RestResponse as(Value<T> value, ClassMeta<T> type)
      Same as as(ClassMeta) but sets the value in a mutable for fluent calls.
      Type Parameters:
      T - The type to convert to.
      Parameters:
      value - The mutable to set the parsed header value in.
      type - The type to convert to.
      Returns:
      This object.
    • asMatcher

      public Matcher asMatcher(Pattern pattern)
      Matches the specified pattern against this header value.
      Example:

      // Parse header using a regular expression. Matcher matcher = client .get(URI) .run() .getResponseHeader("Content-Type").asMatcher(Pattern.compile("application/(.*)")); if (matcher.matches()) { String mediaType = matcher.group(1); }

      Parameters:
      pattern - The regular expression pattern to match.
      Returns:
      The matcher.
    • asMatcher

      public Matcher asMatcher(String regex)
      Matches the specified pattern against this header value.
      Example:

      // Parse header using a regular expression. Matcher matcher = client .get(URI) .run() .getHeader("Content-Type").asMatcher("application/(.*)"); if (matcher.matches()) { String mediaType = matcher.group(1); }

      Parameters:
      regex - The regular expression pattern to match.
      Returns:
      The matcher.
    • asMatcher

      public Matcher asMatcher(String regex, int flags)
      Matches the specified pattern against this header value.
      Example:

      // Parse header using a regular expression. Matcher matcher = client .get(URI) .run() .getHeader("Content-Type").asMatcher("application/(.*)", CASE_INSENSITIVE); if (matcher.matches()) { String mediaType = matcher.group(1); }

      Parameters:
      regex - The regular expression pattern to match.
      flags - Pattern match flags. See Pattern.compile(String, int).
      Returns:
      The matcher.
    • assertValue

      Provides the ability to perform fluent-style assertions on this response header.
      Examples:

      // Validates the content type header is provided. client .get(URI) .run() .getHeader("Content-Type").assertValue().exists(); // Validates the content type is JSON. client .get(URI) .run() .getHeader("Content-Type").assertValue().equals("application/json"); // Validates the content type is JSON using test predicate. client .get(URI) .run() .getHeader("Content-Type").assertValue().is(x -> x.equals("application/json")); // Validates the content type is JSON by just checking for substring. client .get(URI) .run() .getHeader("Content-Type").assertValue().contains("json"); // Validates the content type is JSON using regular expression. client .get(URI) .run() .getHeader("Content-Type").assertValue().isPattern(".*json.*"); // Validates the content type is JSON using case-insensitive regular expression. client .get(URI) .run() .getHeader("Content-Type").assertValue().isPattern(".*json.*", CASE_INSENSITIVE);

      The assertion test returns the original response object allowing you to chain multiple requests like so:

      // Validates the header and converts it to a bean. MediaType mediaType = client .get(URI) .run() .getHeader("Content-Type").assertValue().isNotEmpty() .getHeader("Content-Type").assertValue().isPattern(".*json.*") .getHeader("Content-Type").as(MediaType.class);

      Returns:
      A new fluent assertion object.
    • assertString

      Shortcut for calling assertValue().asString().
      Returns:
      A new fluent assertion.
    • assertInteger

      Shortcut for calling assertValue().asInteger().
      Returns:
      A new fluent assertion.
    • assertLong

      Shortcut for calling assertValue().asLong().
      Returns:
      A new fluent assertion.
    • assertZonedDateTime

      Shortcut for calling assertValue().asZonedDateTime().
      Returns:
      A new fluent assertion.
    • response

      Returns the response that created this object.
      Returns:
      The response that created this object.
    • getElements

      Parses the value.
      Specified by:
      getElements in interface Header
      Overrides:
      getElements in class BasicHeader
      Returns:
      An array of HeaderElement entries, may be empty, but is never null.
      Throws:
      ParseException - In case of a parsing error.