{title:'Helper Classes', created:'9.0.0'}

The {@link oaj.http.header} package contains various convenience classes for creating standard HTTP components using static imports.

HttpHeaders

The {@link oaj.http.HttpHeaders} class contains many convenience static methods and fields for working with standard HTTP request and response headers and header lists.

Example:

| import static org.apache.juneau.http.HttpHeaders.*; | | HeaderList headers = | headerList( // Arbitrary list of headers | | CONTENTTYPE_TEXT_XML, // Static constants | | contentType("text/xml") // Predefined headers | | contentType(() -> "text/xml") // Predefined headers with supplied values | | stringHeader("Content-Type", "text/xml") // Freeform headers | | stringHeader("Content-Type", () -> "text/xml") // Freeform headers with supplied values | );

This class is vast in scope and covers all request and response headers defined in RFC2616.

In addition to the predefined headers, various methods are provided for free-form headers. Each accepts either static values or values from {@link java.util.function.Supplier Suppliers}:

The {@link oaj.http.HttpHeaders#serializedHeader(String,Object) serializedHeader} methods allows for headers serialized using schema-based serializers such as the OpenAPI serializer.

Static methods are also provided for instantiating {@link oaj.http.annotation.Header}-annotated or other HttpComponent-defined header classes:

Example:

| import static org.apache.juneau.http.HttpHeaders.*; | | ContentType contentType = header(ContentType.class, "text/xml");

Lists of headers can be produced with the following methods:

The capabilities of the {@link oaj.http.header.HeaderList} class is described later.

HttpParts

The {@link oaj.http.HttpParts} class contains convenience static methods for generating query/form-data/path parts and part lists.

Example:

| import static org.apache.juneau.http.HttpParts.*; | | PartList formData = | partList( // Arbitrary list of parts | stringPart("Name", "Bill") // Freeform part | integerPart("Age", () -> calculateAge()) // Freeform part with supplied value | );

The following methods are provided for creating parts. Each accepts either static values or values from {@link java.util.function.Supplier Suppliers}:

The {@link oaj.http.HttpParts#serializedPart(String,Object) serializedPart} methods allows for parts serialized using schema-based serializers such as the OpenAPI serializer.

Lists of parts can be produced with the following methods:

The capabilities of the {@link oaj.http.part.PartList} class is described later.

HttpEntities

The {@link oaj.http.HttpEntities} class contains convenience static methods for generating HTTP message entities. Returned objects extend from {@code org.apache.http.HttpEntity} but provides the following additional features:

The following methods are provided for creating entities. Each accepts either static values or values from {@link java.util.function.Supplier Suppliers} and returns builders:

HTTP entities are automatically supported in both the server and client REST APIs for requests and responses.

Example:

| import static org.apache.juneau.http.HttpResources.*; | | @RestDelete(path="/{id}") | public HttpEntity helloWold(...) { | return stringEntity("Hello!").contentType("text/plain"); | }

HttpResources

The {@link oaj.http.HttpResources} class contains convenience static methods for generating HTTP message resources. Returned objects extend from {@link oaj.http.resource.HttpResource} which extends from {@link org.apache.http.HttpEntity} but with additional arbitrary headers.

The following methods are provided for creating entities. Each accepts either static values or values from {@link java.util.function.Supplier Suppliers} and are in the form of builders.

The most common location where resources are used are as returned types of REST operation methods described later.

Example:

| import static org.apache.juneau.http.HttpResources.*; | | @RestDelete(path="/{id}") | public HttpResource helloWold(...) { | return stringResource("Hello!").contentType("text/plain").header("Cache-Control", "none"); | }

HttpResponses

The {@link oaj.http.HttpResponses} class contains convenience static methods for standard HTTP responses. Returned objects extend from {@code org.apache.http.HttpResponse} and are in the form of builders.

The following methods are provided for creating entities:

The most common location where these responses are used are in REST operation methods described later.

Example:

| import static org.apache.juneau.http.HttpResponses.*; | import static org.apache.juneau.http.HttpHeaders.*; | | @RestDelete(path="/{id}") | public Ok doDelete(...) throws Unauthorized { | if (/* user not authorized*/) | throw unauthorized(); | return ok().content("Delete was successful"); | }