{title:'HTTP Parts', created:'9.0.0'}

The {@link oaj.http.part} package contains implementations of org.apache.http.NameValuePair to be used for query/form-data/path parts and part lists.

PartList

The {@link oaj.http.part.PartList} class is a list of HTTP parts (form-data, query-parameters, path-parameters).

Example

| PartList parts = PartList | .create() | .append(MyPart.of("foo")) | .append("Bar", ()->getDynamicValueFromSomewhere());

Convenience creators are provided for creating lists with minimal code:

| PartList parts = PartList.of(BasicIntegerPart.of("foo", 1));

Static methods are provided on {@link oaj.http.HttpParts} to further simplify creation of part lists.

| import static org.apache.juneau.http.HttpParts.*; | | PartList parts = partList(integerPart("foo", 1), booleanPart("bar", false));

The builder class supports setting default part values (i.e. add a part to the list if it isn't otherwise in the list). Note that this is different from simply setting a value twice as using default values will not overwrite existing parts.
The following example notes the distinction:

| parts = PartList | .create() | .set("Foo", "bar") | .set("Foo", "baz"); | assertObject(parts).isString("foo=baz"); | | parts = PartList | .create() | .set("Foo", "bar") | .setDefault("Foo", "baz"); | assertObject(parts).isString("foo=bar");

Various methods are provided for iterating over the parts in this list to avoid array copies.

In general, try to use these over the {@link oaj.http.part.PartList#getAll() getAll()} / {@link oaj.http.part.PartList#getAll(String) getAll(String)} methods that require array copies.

Similar to the way multiple headers can be collapsed into a single value, the {@link oaj.http.part.PartList#get(String) get(String)} method is special in that it will collapse multiple parts with the same name into a single comma-delimited list.

The {@link oaj.http.part.PartList#get(Class) get(Class)} and {@link oaj.http.part.PartList#get(String,Class) get(String,Class)} methods are provided for working with {@link oaj.http.annotation.FormData} / {@link oaj.http.annotation.Query} / {@link oaj.http.annotation.Path}-annotated beans.

Example

| MyQueryBean foo = parts.get(MyQueryBean.class);

A {@link oaj.svl.VarResolver} can be associated with this builder to create part values with embedded variables that are resolved at runtime.

Example

| // Create a part list with dynamically-resolving values pulled from a system property. | | System.setProperty("foo", "bar"); | | PartList parts = PartList | .create() | .resolving() | .append("X1", "$S{foo}") | .append("X2", ()->"$S{foo}"); | | assertObject(parts).isString("X1=bar&X2=bar");

The {@link oaj.http.part.PartList} object can be extended to defined pre-packaged lists of parts which can be used in various annotations throughout the framework.

Example

| // A predefined list of parts. | public class MyPartList extends PartList { | public MyPartList() { | super(BasicIntegerPart.of("foo",1), BasicBooleanPart.of("bar",false)); | } | }