Class RequestHttpPart

java.lang.Object
org.apache.juneau.rest.httppart.RequestHttpPart
Direct Known Subclasses:
RequestFormParam, RequestHeader, RequestPathParam, RequestQueryParam

public class RequestHttpPart extends Object
Represents a single HTTP part on an HTTP request. Parent of the following classes:
See Also:
  • Constructor Details

  • Method Details

    • schema

      Specifies the part schema for this part.

      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 part.

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

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

      public RequestHttpPart def(String def)
      Sets a default value for this part.
      Parameters:
      def - The default value.
      Returns:
      This object.
    • getValue

      public String getValue()
      Returns the value of this part.
      Returns:
      The value of this part.
    • isPresent

      public boolean isPresent()
      Returns true if this part exists on the request.

      This is a shortened form for calling asString().isPresent().

      Returns:
      true if this part exists on the request.
    • get

      public String get()
      If a value is present, returns the value, otherwise throws NoSuchElementException.

      This is a shortened form for calling asString().get().

      Returns:
      The value if present.
    • orElse

      public String orElse(String other)
      Return the value if present, otherwise return other.

      This is a shortened form for calling asString().orElse(other).

      Parameters:
      other - The value to be returned if there is no value present, may be null.
      Returns:
      The value, if present, otherwise other.
    • asString

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

      public <T> Optional<T> as(Type type, Type... args) throws BasicHttpException
      Converts this part to the specified POJO type using the request HttpPartParser and optional schema.

      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 Optional.empty() if the part is not present.
      Throws:
      BasicHttpException - If value could not be parsed.
    • as

      public <T> Optional<T> as(Class<T> type) throws BasicHttpException
      Converts this part to the specified POJO type using the request HttpPartParser and optional schema.

      If the specified type is an HTTP part type (extends from Header/NameValuePair), then looks for one of the following constructors:

      • public T(String name, String value);

      If it doesn't find one of those constructors, then it parses it into the specified type using the part parser.

      Type Parameters:
      T - The type to convert to.
      Parameters:
      type - The type to convert to.
      Returns:
      The converted type, or Optional.empty() if the part is not present.
      Throws:
      BasicHttpException - If value could not be parsed.
    • as

      public <T> Optional<T> as(ClassMeta<T> type) throws BasicHttpException
      Converts this part to the specified POJO type using the request HttpPartParser and optional schema.

      If the specified type is an HTTP part type (extends from Header/NameValuePair), then looks for one of the following constructors:

      • public T(String name, String value);

      If it doesn't find one of those constructors, then it parses it into the specified type using the part parser.

      Type Parameters:
      T - The type to convert to.
      Parameters:
      type - The type to convert to.
      Returns:
      The converted type, or Optional.empty() if the part is not present.
      Throws:
      BasicHttpException - If value could not be parsed.
    • asMatcher

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

      Matcher matcher = request .getHeader("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.
      Throws:
      BasicHttpException - If a connection error occurred.
    • asMatcher

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

      Matcher matcher = request .getHeader("Content-Type") .asMatcher("application/(.*)"); if (matcher.matches()) { String mediaType = matcher.group(1); }

      Parameters:
      regex - The regular expression pattern to match.
      Returns:
      The matcher.
      Throws:
      BasicHttpException - If a connection error occurred.
    • asMatcher

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

      Matcher matcher = request .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.
      Throws:
      BasicHttpException - If a connection error occurred.
    • asInteger

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

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

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

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

      Returns the value of this parameter as a list from a comma-delimited string.
      Returns:
      The value of this parameter as a list from a comma-delimited string, or Optional.empty() if the parameter was not present.
    • asCsvArrayPart

      Returns the value of this parameter as a BasicCsvArrayPart.
      Returns:
      The value of this parameter as a BasicCsvArrayPart, never null.
    • asDatePart

      Returns the value of this parameter as a BasicDatePart.
      Returns:
      The value of this parameter as a BasicDatePart, never null.
    • asIntegerPart

      Returns the value of this parameter as a BasicIntegerPart.
      Returns:
      The value of this parameter as a BasicIntegerPart, never null.
    • asBooleanPart

      Returns the value of this parameter as a BasicBooleanPart.
      Returns:
      The value of this parameter as a BasicBooleanPart, never null.
    • asLongPart

      Returns the value of this parameter as a BasicLongPart.
      Returns:
      The value of this parameter as a BasicLongPart, never null.
    • asStringPart

      Returns the value of this parameter as a BasicStringPart.
      Returns:
      The value of this parameter as a BasicStringPart, never null.
    • asUriPart

      Returns the value of this parameter as a BasicUriPart.
      Returns:
      The value of this parameter as a BasicUriPart, never null.
    • assertString

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

      request .getQueryParam("foo") .assertString().contains("bar");

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

      String foo = request .getQueryParam("foo") .assertString().contains("bar") .asString().get();

      Returns:
      A new fluent assertion object.
    • assertInteger

      Provides the ability to perform fluent-style assertions on an integer parameter.
      Examples:

      request .getQueryParam("age") .assertInteger().isGreaterThan(1);

      Returns:
      A new fluent assertion object.
    • assertLong

      Provides the ability to perform fluent-style assertions on a long parameter.
      Examples:

      request .getQueryParam("length") .assertLong().isLessThan(100000);

      Returns:
      A new fluent assertion object.
    • assertDate

      Provides the ability to perform fluent-style assertions on a date parameter.
      Examples:

      request .getQueryParam("time") .assertDate().isAfterNow();

      Returns:
      A new fluent assertion object.
    • assertCsvArray

      Provides the ability to perform fluent-style assertions on comma-separated string parameters.
      Examples:

      request .getQueryParam("allow") .assertCsvArray().contains("GET");

      Returns:
      A new fluent assertion object.
    • getRequest

      protected RestRequest getRequest()
      Returns the request that created this part.
      Returns:
      The request that created this part.
    • getName

      public String getName()
      Gets the name of this part.
      Returns:
      The name of this part, never null.
    • toString

      public String toString()
      Overrides:
      toString in class Object