The {@link oaj.http.header} package contains various convenience classes for creating
standard HTTP components using static imports.
- {@link oaj.http.HttpHeaders} - Utility class for standard HTTP headers.
- {@link oaj.http.HttpParts} - Utility class for standard HTTP parts.
- {@link oaj.http.HttpEntities} - Utility class for standard HTTP entities.
- {@link oaj.http.HttpResources} - Utility class for standard HTTP resources.
- {@link oaj.http.HttpResponses} - Utility class for standard HTTP resources.
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.
| 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}:
- {@link oaj.http.HttpHeaders#basicHeader(String,Object) basicHeader}
- {@link oaj.http.HttpHeaders#booleanHeader(String,String) booleanHeader}
- {@link oaj.http.HttpHeaders#csvHeader(String,String) csvHeader}
- {@link oaj.http.HttpHeaders#dateHeader(String,String) dateHeader}
- {@link oaj.http.HttpHeaders#entityTagsHeader(String,String) entityTagsHeader}
- {@link oaj.http.HttpHeaders#entityTagHeader(String,String) entityTagHeader}
- {@link oaj.http.HttpHeaders#integerHeader(String,String) integerHeader}
- {@link oaj.http.HttpHeaders#longHeader(String,String) longHeader}
- {@link oaj.http.HttpHeaders#mediaRangesHeader(String,String) mediaRangesHeader}
- {@link oaj.http.HttpHeaders#mediaTypeHeader(String,String) mediaTypeHeader}
- {@link oaj.http.HttpHeaders#stringHeader(String,String) stringHeader}
- {@link oaj.http.HttpHeaders#serializedHeader(String,Object) serializedHeader}
- {@link oaj.http.HttpHeaders#stringRangesHeader(String,String) stringRangesHeader}
- {@link oaj.http.HttpHeaders#uriHeader(String,String) uriHeader}
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:
- {@link oaj.http.HttpHeaders#header(Class,String,Object) header(Class,String,Object)}
- {@link oaj.http.HttpHeaders#header(Class,Object) header(Class,String,Object)}
| import static org.apache.juneau.http.HttpHeaders.*;
|
| ContentType contentType = header(ContentType.class, "text/xml");
Lists of headers can be produced with the following methods:
- {@link oaj.http.HttpHeaders#headerList() headerList()}
- {@link oaj.http.HttpHeaders#headerList(Header...) headerList(Header...)}
- {@link oaj.http.HttpHeaders#headerList(List) headerList(List<Header>)}
- {@link oaj.http.HttpHeaders#headerList(String...) headerList(String...)}
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.
| 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}:
- {@link oaj.http.HttpParts#basicPart(String,Object) basicPart}
- {@link oaj.http.HttpParts#booleanPart(String,Boolean) booleanPart}
- {@link oaj.http.HttpParts#csvArrayPart(String,String...) csvArrayPart}
- {@link oaj.http.HttpParts#datePart(String,ZonedDateTime) datePart}
- {@link oaj.http.HttpParts#integerPart(String,Integer) integerPart}
- {@link oaj.http.HttpParts#longPart(String,Long) longPart}
- {@link oaj.http.HttpParts#serializedPart(String,Object) serializedPart}
- {@link oaj.http.HttpParts#stringPart(String,String) stringPart}
- {@link oaj.http.HttpParts#uriPart(String,URI) uriPart}
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:
- {@link oaj.http.HttpParts#partList() partList()}
- {@link oaj.http.HttpParts#partList(List) partList(List<NameValuePair>)}
- {@link oaj.http.HttpParts#partList(NameValuePair...) partList(NameValuePair...)}
- {@link oaj.http.HttpParts#partList(String...) partList(String...)}
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:
-
Caching.
-
Fluent setters.
-
Fluent assertions.
-
Externally-supplied/dynamic content.
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:
- {@link oaj.http.HttpEntities#byteArrayEntity(byte[]) byteArrayEntity}
- {@link oaj.http.HttpEntities#fileEntity(File) fileEntity}
- {@link oaj.http.HttpEntities#readerEntity(Reader) readerEntity}
- {@link oaj.http.HttpEntities#serializedEntity(Object, Serializer) serializedEntity}
- {@link oaj.http.HttpEntities#streamEntity(InputStream) streamEntity}
- {@link oaj.http.HttpEntities#stringEntity(String) stringEntity}
HTTP entities are automatically supported in both the server and client REST APIs for requests and responses.
| 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.
- {@link oaj.http.HttpResources#byteArrayResource(byte[]) byteArrayResource}
- {@link oaj.http.HttpResources#fileResource(File) fileResource}
- {@link oaj.http.HttpResources#readerResource(Reader) readerResource}
- {@link oaj.http.HttpResources#streamResource(InputStream) streamResource}
- {@link oaj.http.HttpResources#stringResource(String) stringResource}
The most common location where resources are used are as returned types of REST operation methods described later.
| 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:
- {@link oaj.http.HttpResponses#_continue() _continue}
- {@link oaj.http.HttpResponses#accepted() accepted}
- {@link oaj.http.HttpResponses#alreadyReported() alreadyReported}
- {@link oaj.http.HttpResponses#badRequest() badRequest}
- {@link oaj.http.HttpResponses#conflict() conflict}
- {@link oaj.http.HttpResponses#created() created}
- {@link oaj.http.HttpResponses#earlyHints() earlyHints}
- {@link oaj.http.HttpResponses#expectationFailed() expectationFailed}
- {@link oaj.http.HttpResponses#failedDependency() failedDependency}
- {@link oaj.http.HttpResponses#forbidden() forbidden}
- {@link oaj.http.HttpResponses#found(String) found}
- {@link oaj.http.HttpResponses#gone() gone}
- {@link oaj.http.HttpResponses#httpVersionNotSupported() httpVersionNotSupported}
- {@link oaj.http.HttpResponses#imUsed() imUsed}
- {@link oaj.http.HttpResponses#insufficientStorage() insufficientStorage}
- {@link oaj.http.HttpResponses#internalServerError() internalServerError}
- {@link oaj.http.HttpResponses#lengthRequired() lengthRequired}
- {@link oaj.http.HttpResponses#locked() locked}
- {@link oaj.http.HttpResponses#loopDetected() loopDetected}
- {@link oaj.http.HttpResponses#methodNotAllowed() methodNotAllowed}
- {@link oaj.http.HttpResponses#misdirectedRequest() misdirectedRequest}
- {@link oaj.http.HttpResponses#movedPermanently(String) movedPermanently}
- {@link oaj.http.HttpResponses#multipleChoices() multipleChoices}
- {@link oaj.http.HttpResponses#multiStatus() multiStatus}
- {@link oaj.http.HttpResponses#networkAuthenticationRequired() networkAuthenticationRequired}
- {@link oaj.http.HttpResponses#noContent() noContent}
- {@link oaj.http.HttpResponses#nonAuthoritiveInformation() nonAuthoritiveInformation}
- {@link oaj.http.HttpResponses#notAcceptable() notAcceptable}
- {@link oaj.http.HttpResponses#notExtended() notExtended}
- {@link oaj.http.HttpResponses#notFound() notFound}
- {@link oaj.http.HttpResponses#notImplemented() notImplemented}
- {@link oaj.http.HttpResponses#notModified() notModified}
- {@link oaj.http.HttpResponses#ok() ok}
- {@link oaj.http.HttpResponses#partialContent() partialContent}
- {@link oaj.http.HttpResponses#payloadTooLarge() payloadTooLarge}
- {@link oaj.http.HttpResponses#permanentRedirect(String) permanentRedirect}
- {@link oaj.http.HttpResponses#preconditionFailed() preconditionFailed}
- {@link oaj.http.HttpResponses#preconditionRequired() preconditionRequired}
- {@link oaj.http.HttpResponses#processing() processing}
- {@link oaj.http.HttpResponses#rangeNotSatisfiable() rangeNotSatisfiable}
- {@link oaj.http.HttpResponses#requestHeaderFieldsTooLarge() requestHeaderFieldsTooLarge}
- {@link oaj.http.HttpResponses#resetContent() resetContent}
- {@link oaj.http.HttpResponses#seeOther(String) seeOther}
- {@link oaj.http.HttpResponses#serviceUnavailable() serviceUnavailable}
- {@link oaj.http.HttpResponses#switchingProtocols() switchingProtocols}
- {@link oaj.http.HttpResponses#temporaryRedirect(String) temporaryRedirect}
- {@link oaj.http.HttpResponses#tooManyRequests() tooManyRequests}
- {@link oaj.http.HttpResponses#unauthorized() unauthorized}
- {@link oaj.http.HttpResponses#unavailableForLegalReasons() unavailableForLegalReasons}
- {@link oaj.http.HttpResponses#unprocessableEntity() unprocessableEntity}
- {@link oaj.http.HttpResponses#unsupportedMediaType() unsupportedMediaType}
- {@link oaj.http.HttpResponses#upgradeRequired() upgradeRequired}
- {@link oaj.http.HttpResponses#uriTooLong() uriTooLong}
- {@link oaj.http.HttpResponses#useProxy() useProxy}
- {@link oaj.http.HttpResponses#variantAlsoNegotiates() variantAlsoNegotiates}
The most common location where these responses are used are in REST operation methods described later.
| 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");
| }