Class PartList

java.lang.Object
org.apache.juneau.http.part.PartList
All Implemented Interfaces:
Iterable<HttpPart>, HttpBody

public final class PartList extends Object implements HttpBody, Iterable<HttpPart>
An ordered, immutable list of HttpPart instances that also implements HttpBody for URL-encoded form submission.

Use this class to send a application/x-www-form-urlencoded request body, or to represent a collection of query parameters, form fields, or path variables.

Create instances via the static factory methods:

// From parts PartList parts = PartList.of( HttpPartBean.of("username", "alice"), HttpPartBean.of("password", "secret") ); // From alternating name/value strings PartList parts2 = PartList.ofPairs("username", "alice", "password", "secret");

See Also:
Since:
9.2.1
  • Method Details

    • empty

      public static PartList empty()
      Creates an empty PartList.
      Returns:
      A new instance. Never null.
    • of

      public static PartList of(HttpPart... parts)
      Creates a PartList from the given parts.
      Parameters:
      parts - The parts. Must not be null.
      Returns:
      A new instance. Never null.
    • of

      public static PartList of(List<HttpPart> parts)
      Creates a PartList from the given list of parts.
      Parameters:
      parts - The parts. Must not be null.
      Returns:
      A new instance. Never null.
    • ofPairs

      public static PartList ofPairs(String... pairs)
      Creates a PartList from alternating name/value string pairs.

      The number of arguments must be even.

      Parameters:
      pairs - Alternating name/value strings. Must not be null. Length must be even.
      Returns:
      A new instance. Never null.
      Throws:
      IllegalArgumentException - If the number of arguments is odd.
    • getParts

      public List<HttpPart> getParts()
      Returns the parts in this list.
      Returns:
      An unmodifiable list of parts. Never null.
    • getFirst

      public HttpPart getFirst(String name)
      Returns the first part with the given name (case-sensitive), or null if absent.
      Parameters:
      name - The part name. Must not be null.
      Returns:
      The first matching part, or null if absent.
    • size

      public int size()
      Returns the number of parts in this list.
      Returns:
      The number of parts.
    • isEmpty

      public boolean isEmpty()
      Returns true if this list is empty.
      Returns:
      true if empty.
    • iterator

      Specified by:
      iterator in interface Iterable<HttpPart>
    • getContentType

      Description copied from interface: HttpBody
      Returns the MIME content type of this body, e.g. "application/json; charset=UTF-8".
      Specified by:
      getContentType in interface HttpBody
      Returns:
      The content type, or null if unknown.
    • getContentLength

      public long getContentLength()
      Description copied from interface: HttpBody
      Returns the content length in bytes, or -1 if unknown.

      Implementations that buffer content may return the exact length; streaming implementations should return -1 to cause the transport to use chunked transfer encoding.

      Specified by:
      getContentLength in interface HttpBody
      Returns:
      The content length, or -1 if unknown.
    • isRepeatable

      public boolean isRepeatable()
      Description copied from interface: HttpBody
      Returns true if this body can be written more than once.

      Streaming bodies backed by a one-shot InputStream should return false. Buffered bodies (byte arrays, strings, files) should return true.

      Specified by:
      isRepeatable in interface HttpBody
      Returns:
      true if repeatable.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Description copied from interface: HttpBody
      Writes the body content to the given output stream.

      Implementations must not close out.

      Specified by:
      writeTo in interface HttpBody
      Parameters:
      out - The stream to write to. Never null.
      Throws:
      IOException - If an I/O error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object