- All Implemented Interfaces:
HeadersAccumulator<MutableRequest>
,TaggableRequest.Builder
,HttpRequest.Builder
HttpRequest
that supports tags
, relative URIs &
setting arbitrary objects as request bodies. This class implements HttpRequest.Builder
for setting request fields. Querying a field before it's been set will return its default value.
Invoking toImmutableRequest()
will return an immutable copy that is independent of this
instance.
MutableRequest
accepts an arbitrary object as the request body, referred to as the
payload. The payload is resolved into a BodyPublisher
only when one is requested
. Resolution is done by this request's AdapterCodec
. Sending the request through a Methanol
client
with an AdapterCodec
sets the request's AdapterCodec
automatically if one is not
already present. Note that a request with an AdapterCodec
overrides the client's
AdapterCodec
both for encoding the request body and decoding the response body.
Additionally, this class allows setting a URI
without a host or a scheme or not
setting a URI
at all. This is for the case when the request is used with a Methanol
client that has a base URL, against which this request's URL is resolved.
MutableRequest
also adds some convenience when the HttpRequest
is used
immediately after creation:
client.send(
MutableRequest
.GET("https://www.google.com/search?q=java")
.header("Accept", "text/html"),
BodyHandlers.ofString());
It is recommended, however, to use toImmutableRequest()
if the request is stored
somewhere before it's sent in order to prevent accidental mutation, especially when the request
is sent asynchronously.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.mizosoft.methanol.TaggableRequest
TaggableRequest.Builder
Nested classes/interfaces inherited from class java.net.http.HttpRequest
HttpRequest.BodyPublisher, HttpRequest.BodyPublishers
-
Method Summary
Modifier and TypeMethodDescriptionReturns theAdapterCodec
to be used for resolving this request's payload.adapterCodec
(AdapterCodec adapterCodec) Set's theAdapterCodec
to be used for resolving this request's payload.apply
(Consumer<? super MutableRequest> consumer) Calls the given consumer against this request.build()
Returns an immutable copy of this request.cacheControl
(CacheControl cacheControl) Sets theCache-Control
header to the given value.copy()
Returns a copy of this request that is independent of this instance.static MutableRequest
copyOf
(HttpRequest other) Returns a newMutableRequest
that is a copy of the given request.static MutableRequest
create()
Returns a newMutableRequest
.static MutableRequest
Returns a newMutableRequest
with the given URI and a default GET method.static MutableRequest
Returns a newMutableRequest
with the given URI and a default GET method.DELETE()
static MutableRequest
Returns a newMutableRequest
with the given URI and a DELETE method.static MutableRequest
Returns a newMutableRequest
with the given URI and a DELETE method.boolean
expectContinue
(boolean enable) GET()
static MutableRequest
Returns a newMutableRequest
with the given URI and a GET method.static MutableRequest
Returns a newMutableRequest
with the given URI and a GET method.HEAD()
static MutableRequest
Returns a newMutableRequest
with the given URI and a HEAD method.static MutableRequest
Returns a newMutableRequest
with the given URI and a HEAD method.Adds the given header name-value pair.headers()
Adds each of the given header name-value pairs.headers
(HttpHeaders headers) Adds each of the givenHttpHeaders
.<T> MutableRequest
Adds the given value to this request'sBodyAdapter.Hints
.hints()
Returns this request'sBodyAdapter.Hints
.hints
(Consumer<BodyAdapter.Hints.Builder> hintsMutator) Modifies this request'sBodyAdapter.Hints
by mutating aBodyAdapter.Hints.Builder
.method()
Sets the request method and sets the payload to the given value.method
(String method, HttpRequest.BodyPublisher bodyPublisher) <T> MutableRequest
Sets the request method and sets the payload to the given value with an explicitly specified type.Optional
<com.github.mizosoft.methanol.MimeBody> mimeBody()
Sets the request method to PATCH and sets the payload to the given value.static MutableRequest
Returns a newMutableRequest
with the given URI and a PATCH method.static MutableRequest
PATCH
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a PATCH method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a PATCH method.PATCH
(HttpRequest.BodyPublisher bodyPublisher) Sets the request method to PATCH and sets the body publisher to the given value.static MutableRequest
Returns a newMutableRequest
with the given URI and a PATCH method.static MutableRequest
PATCH
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a PATCH method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a PATCH method.<T> MutableRequest
Sets the request method to PATCH and sets the payload to the given value with an explicitly specified type.Sets the request method to POST and sets the payload to the given value.static MutableRequest
Returns a newMutableRequest
with the given URI and a POST method.static MutableRequest
POST
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a POST method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a POST method.POST
(HttpRequest.BodyPublisher bodyPublisher) static MutableRequest
Returns a newMutableRequest
with the given URI and a POST method.static MutableRequest
POST
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a POST method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a POST method.<T> MutableRequest
Sets the request method to POST and sets the payload to the given value with an explicitly specified type.Sets the request method to PUT and sets the payload to the given value.static MutableRequest
Returns a newMutableRequest
with the given URI and a PUT method.static MutableRequest
PUT
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a PUT method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a PUT method.PUT
(HttpRequest.BodyPublisher bodyPublisher) static MutableRequest
Returns a newMutableRequest
with the given URI and a PUT method.static MutableRequest
PUT
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the given URI and a PUT method.static <T> MutableRequest
Returns a newMutableRequest
with the given URI and a PUT method.<T> MutableRequest
Sets the request method to PUT and sets the payload to the given value with an explicitly specified type.removeHeader
(String name) Removes any header associated with the given name.Removes all headers added so far.removeHeadersIf
(BiPredicate<String, String> filter) Removes all headers matched by the given predicate.Removes the tag associated with the given type.Removes the tag associated with the given type.Sets the header represented by the given name to the given value, overwriting the previous value (if any).Sets the header represented by the given name to the given values, overwriting the previous value (if any).setHeaderIfAbsent
(String name, String value) Sets the header represented by the given name to the given value, only if there was no header with the given name.setHeaderIfAbsent
(String name, List<String> values) Sets the header represented by the given name to the given values, only if there was no header with the given name.<T> MutableRequest
Adds a tag mapped to the given type.<T> MutableRequest
Adds a tag mapped to the given type.Adds a tag mapped to the given object's runtime type.timeout()
Returns an immutable copy of this request.toString()
uri()
Sets this request'sURI
.Sets this request'sURI
.version()
version
(HttpClient.Version version) Methods inherited from class com.github.mizosoft.methanol.TaggableRequest
from, tag, tag, tagOf, tagOf
Methods inherited from class java.net.http.HttpRequest
equals, hashCode, newBuilder, newBuilder, newBuilder
-
Method Details
-
uri
Sets this request'sURI
. Can be relative or without a host or a scheme.- Throws:
IllegalArgumentException
- if the uri's syntax is invalid
-
cacheControl
Sets theCache-Control
header to the given value. -
apply
Calls the given consumer against this request. -
tag
Description copied from interface:TaggableRequest.Builder
Adds a tag mapped to the given object's runtime type.- Specified by:
tag
in interfaceTaggableRequest.Builder
-
tag
Description copied from interface:TaggableRequest.Builder
Adds a tag mapped to the given type.- Specified by:
tag
in interfaceTaggableRequest.Builder
-
tag
Description copied from interface:TaggableRequest.Builder
Adds a tag mapped to the given type.- Specified by:
tag
in interfaceTaggableRequest.Builder
-
removeTag
Description copied from interface:TaggableRequest.Builder
Removes the tag associated with the given type.- Specified by:
removeTag
in interfaceTaggableRequest.Builder
-
removeTag
Description copied from interface:TaggableRequest.Builder
Removes the tag associated with the given type.- Specified by:
removeTag
in interfaceTaggableRequest.Builder
-
hints
Modifies this request'sBodyAdapter.Hints
by mutating aBodyAdapter.Hints.Builder
. -
hint
Adds the given value to this request'sBodyAdapter.Hints
. -
bodyPublisher
- Specified by:
bodyPublisher
in classHttpRequest
-
adapterCodec
Set's theAdapterCodec
to be used for resolving this request's payload. -
adapterCodec
Returns theAdapterCodec
to be used for resolving this request's payload. -
method
- Specified by:
method
in classHttpRequest
-
timeout
- Specified by:
timeout
in classHttpRequest
-
expectContinue
public boolean expectContinue()- Specified by:
expectContinue
in classHttpRequest
-
uri
An empty
URI
(without a scheme, path or a host) is returned if noURI
was previously set.- Specified by:
uri
in classHttpRequest
-
version
- Specified by:
version
in classHttpRequest
-
headers
- Specified by:
headers
in classHttpRequest
-
hints
Description copied from class:TaggableRequest
Returns this request'sBodyAdapter.Hints
.- Specified by:
hints
in classTaggableRequest
-
uri
Sets this request'sURI
. Can be relative or without a host or a scheme.- Specified by:
uri
in interfaceHttpRequest.Builder
- Specified by:
uri
in interfaceTaggableRequest.Builder
-
expectContinue
- Specified by:
expectContinue
in interfaceHttpRequest.Builder
- Specified by:
expectContinue
in interfaceTaggableRequest.Builder
-
version
- Specified by:
version
in interfaceHttpRequest.Builder
- Specified by:
version
in interfaceTaggableRequest.Builder
-
header
Description copied from interface:HeadersAccumulator
Adds the given header name-value pair.- Specified by:
header
in interfaceHeadersAccumulator<MutableRequest>
- Specified by:
header
in interfaceHttpRequest.Builder
- Specified by:
header
in interfaceTaggableRequest.Builder
-
headers
Description copied from interface:HeadersAccumulator
Adds each of the given header name-value pairs. The pairs must be appended to each other in the given array, where each name is followed by a corresponding value.- Specified by:
headers
in interfaceHeadersAccumulator<MutableRequest>
- Specified by:
headers
in interfaceHttpRequest.Builder
- Specified by:
headers
in interfaceTaggableRequest.Builder
-
headers
Adds each of the givenHttpHeaders
.- Specified by:
headers
in interfaceHeadersAccumulator<MutableRequest>
-
setHeader
Description copied from interface:HeadersAccumulator
Sets the header represented by the given name to the given value, overwriting the previous value (if any).- Specified by:
setHeader
in interfaceHeadersAccumulator<MutableRequest>
- Specified by:
setHeader
in interfaceHttpRequest.Builder
- Specified by:
setHeader
in interfaceTaggableRequest.Builder
-
setHeader
Description copied from interface:HeadersAccumulator
Sets the header represented by the given name to the given values, overwriting the previous value (if any).- Specified by:
setHeader
in interfaceHeadersAccumulator<MutableRequest>
-
setHeaderIfAbsent
Description copied from interface:HeadersAccumulator
Sets the header represented by the given name to the given value, only if there was no header with the given name.- Specified by:
setHeaderIfAbsent
in interfaceHeadersAccumulator<MutableRequest>
-
setHeaderIfAbsent
Description copied from interface:HeadersAccumulator
Sets the header represented by the given name to the given values, only if there was no header with the given name.- Specified by:
setHeaderIfAbsent
in interfaceHeadersAccumulator<MutableRequest>
-
removeHeaders
Removes all headers added so far.- Specified by:
removeHeaders
in interfaceHeadersAccumulator<MutableRequest>
-
removeHeader
Removes any header associated with the given name.- Specified by:
removeHeader
in interfaceHeadersAccumulator<MutableRequest>
-
removeHeadersIf
Removes all headers matched by the given predicate.- Specified by:
removeHeadersIf
in interfaceHeadersAccumulator<MutableRequest>
-
timeout
- Specified by:
timeout
in interfaceHttpRequest.Builder
- Specified by:
timeout
in interfaceTaggableRequest.Builder
-
GET
- Specified by:
GET
in interfaceHttpRequest.Builder
- Specified by:
GET
in interfaceTaggableRequest.Builder
-
HEAD
- Specified by:
HEAD
in interfaceHttpRequest.Builder
-
POST
- Specified by:
POST
in interfaceHttpRequest.Builder
- Specified by:
POST
in interfaceTaggableRequest.Builder
-
POST
Sets the request method to POST and sets the payload to the given value. The media type defines the format used for resolving the payload into aBodyPublisher
. -
POST
@CanIgnoreReturnValue public <T> MutableRequest POST(T payload, TypeRef<T> typeRef, MediaType mediaType) Sets the request method to POST and sets the payload to the given value with an explicitly specified type. The media type defines the format used for resolving the payload into aBodyPublisher
. -
PUT
- Specified by:
PUT
in interfaceHttpRequest.Builder
- Specified by:
PUT
in interfaceTaggableRequest.Builder
-
PUT
Sets the request method to PUT and sets the payload to the given value. The media type defines the format used for resolving the payload into aBodyPublisher
. -
PUT
@CanIgnoreReturnValue public <T> MutableRequest PUT(T payload, TypeRef<T> typeRef, MediaType mediaType) Sets the request method to PUT and sets the payload to the given value with an explicitly specified type. The media type defines the format used for resolving the payload into aBodyPublisher
. -
PATCH
Sets the request method to PATCH and sets the body publisher to the given value. -
PATCH
Sets the request method to PATCH and sets the payload to the given value. The media type defines the format used for resolving the payload into aBodyPublisher
. -
PATCH
@CanIgnoreReturnValue public <T> MutableRequest PATCH(T payload, TypeRef<T> typeRef, MediaType mediaType) Sets the request method to PATCH and sets the payload to the given value with an explicitly specified type. The media type defines the format used for resolving the payload into aBodyPublisher
. -
DELETE
- Specified by:
DELETE
in interfaceHttpRequest.Builder
- Specified by:
DELETE
in interfaceTaggableRequest.Builder
-
method
@CanIgnoreReturnValue public MutableRequest method(String method, HttpRequest.BodyPublisher bodyPublisher) - Specified by:
method
in interfaceHttpRequest.Builder
- Specified by:
method
in interfaceTaggableRequest.Builder
-
method
@CanIgnoreReturnValue public MutableRequest method(String method, Object payload, MediaType mediaType) Sets the request method and sets the payload to the given value. The media type defines the format used for resolving this payload into aBodyPublisher
. -
method
@CanIgnoreReturnValue public <T> MutableRequest method(String method, T payload, TypeRef<T> typeRef, MediaType mediaType) Sets the request method and sets the payload to the given value with an explicitly specified type. The media type defines the format used for resolving this payload into aBodyPublisher
. -
build
Returns an immutable copy of this request. Prefer usingtoImmutableRequest()
.- Specified by:
build
in interfaceHttpRequest.Builder
- Specified by:
build
in interfaceTaggableRequest.Builder
-
toImmutableRequest
Returns an immutable copy of this request. -
copy
Returns a copy of this request that is independent of this instance.- Specified by:
copy
in interfaceHttpRequest.Builder
- Specified by:
copy
in interfaceTaggableRequest.Builder
-
toString
-
mimeBody
-
copyOf
Returns a newMutableRequest
that is a copy of the given request. -
create
Returns a newMutableRequest
. -
create
Returns a newMutableRequest
with the given URI and a default GET method. -
create
Returns a newMutableRequest
with the given URI and a default GET method. -
GET
Returns a newMutableRequest
with the given URI and a GET method. -
GET
Returns a newMutableRequest
with the given URI and a GET method. -
HEAD
Returns a newMutableRequest
with the given URI and a HEAD method. -
HEAD
Returns a newMutableRequest
with the given URI and a HEAD method. -
DELETE
Returns a newMutableRequest
with the given URI and a DELETE method. -
DELETE
Returns a newMutableRequest
with the given URI and a DELETE method. -
POST
Returns a newMutableRequest
with the given URI and a POST method. -
POST
Returns a newMutableRequest
with the given URI and a POST method. -
POST
Returns a newMutableRequest
with the given URI and a POST method. -
POST
Returns a newMutableRequest
with the given URI and a POST method. -
POST
public static <T> MutableRequest POST(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequest
with the given URI and a POST method. -
POST
Returns a newMutableRequest
with the given URI and a POST method. -
PUT
Returns a newMutableRequest
with the given URI and a PUT method. -
PUT
Returns a newMutableRequest
with the given URI and a PUT method. -
PUT
Returns a newMutableRequest
with the given URI and a PUT method. -
PUT
Returns a newMutableRequest
with the given URI and a PUT method. -
PUT
public static <T> MutableRequest PUT(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequest
with the given URI and a PUT method. -
PUT
Returns a newMutableRequest
with the given URI and a PUT method. -
PATCH
Returns a newMutableRequest
with the given URI and a PATCH method. -
PATCH
Returns a newMutableRequest
with the given URI and a PATCH method. -
PATCH
Returns a newMutableRequest
with the given URI and a PATCH method. -
PATCH
Returns a newMutableRequest
with the given URI and a PATCH method. -
PATCH
public static <T> MutableRequest PATCH(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequest
with the given URI and a PATCH method. -
PATCH
Returns a newMutableRequest
with the given URI and a PATCH method.
-