java.lang.Object
java.net.http.HttpRequest
com.github.mizosoft.methanol.TaggableRequest
com.github.mizosoft.methanol.MutableRequest
- All Implemented Interfaces:
TaggableRequest.Builder
,HttpRequest.Builder
A mutable
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, referred to as payloads, as the request
body. The payload is resolved into a BodyPublisher
only when one is requested
. Resolution is done by this request's AdapterCodec
.
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, with 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 givenURI
.static MutableRequest
Returns a newMutableRequest
with the givenURI
.DELETE()
boolean
expectContinue
(boolean enable) GET()
static MutableRequest
Returns a newMutableRequest
with the givenURI
and a GET method.static MutableRequest
Returns a newMutableRequest
with the givenURI
and a GET method.headers()
headers
(HttpHeaders headers) Adds each of the givenHttpHeaders
.method()
Sets the request method and sets the payload to the given value.method
(String method, HttpRequest.BodyPublisher bodyPublisher) Sets the request method to PATCH and sets the payload to the given value.static MutableRequest
Returns a newMutableRequest
with the givenURI
and a PATCH method.static MutableRequest
PATCH
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
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 givenURI
and a PATCH method.static MutableRequest
PATCH
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
and a PATCH method.static MutableRequest
Returns a newMutableRequest
with the givenURI
and a POST method.static MutableRequest
POST
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
and a POST method.POST
(HttpRequest.BodyPublisher bodyPublisher) static MutableRequest
Returns a newMutableRequest
with the givenURI
and a POST method.static MutableRequest
POST
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
and a POST method.Sets the request method to PUT and sets the payload to the given value.static MutableRequest
Returns a newMutableRequest
with the givenURI
and a PUT method.static MutableRequest
PUT
(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
and a PUT method.PUT
(HttpRequest.BodyPublisher bodyPublisher) static MutableRequest
Returns a newMutableRequest
with the givenURI
and a PUT method.static MutableRequest
PUT
(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequest
with the givenURI
and a PUT method.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.<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
Methods inherited from class java.net.http.HttpRequest
equals, hashCode, newBuilder, newBuilder, newBuilder
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.net.http.HttpRequest.Builder
HEAD
-
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
-
removeHeaders
Removes all headers added so far. -
removeHeader
Removes any header associated with the given name. -
removeHeadersIf
Removes all headers matched by the given predicate. -
headers
Adds each of the givenHttpHeaders
. -
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
-
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
-
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
- Specified by:
header
in interfaceHttpRequest.Builder
- Specified by:
header
in interfaceTaggableRequest.Builder
-
headers
- Specified by:
headers
in interfaceHttpRequest.Builder
- Specified by:
headers
in interfaceTaggableRequest.Builder
-
timeout
- Specified by:
timeout
in interfaceHttpRequest.Builder
- Specified by:
timeout
in interfaceTaggableRequest.Builder
-
setHeader
- Specified by:
setHeader
in interfaceHttpRequest.Builder
- Specified by:
setHeader
in interfaceTaggableRequest.Builder
-
GET
- Specified by:
GET
in interfaceHttpRequest.Builder
- Specified by:
GET
in interfaceTaggableRequest.Builder
-
POST
- Specified by:
POST
in interfaceHttpRequest.Builder
- Specified by:
POST
in interfaceTaggableRequest.Builder
-
POST
-
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. -
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. -
DELETE
- Specified by:
DELETE
in interfaceHttpRequest.Builder
- Specified by:
DELETE
in interfaceTaggableRequest.Builder
-
method
- Specified by:
method
in interfaceHttpRequest.Builder
- Specified by:
method
in interfaceTaggableRequest.Builder
-
method
Sets the request method and sets the payload to the given value. The media type defines the format used for resolving this payload. -
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
-
copyOf
Returns a newMutableRequest
that is a copy of the given request. -
create
Returns a newMutableRequest
. -
create
Returns a newMutableRequest
with the givenURI
. -
create
Returns a newMutableRequest
with the givenURI
. -
GET
Returns a newMutableRequest
with the givenURI
and a GET method. -
GET
Returns a newMutableRequest
with the givenURI
and a GET method. -
POST
Returns a newMutableRequest
with the givenURI
and a POST method. -
POST
Returns a newMutableRequest
with the givenURI
and a POST method. -
POST
Returns a newMutableRequest
with the givenURI
and a POST method. -
POST
Returns a newMutableRequest
with the givenURI
and a POST method. -
PUT
Returns a newMutableRequest
with the givenURI
and a PUT method. -
PUT
Returns a newMutableRequest
with the givenURI
and a PUT method. -
PUT
Returns a newMutableRequest
with the givenURI
and a PUT method. -
PUT
Returns a newMutableRequest
with the givenURI
and a PUT method. -
PATCH
Returns a newMutableRequest
with the givenURI
and a PATCH method. -
PATCH
Returns a newMutableRequest
with the givenURI
and a PATCH method. -
PATCH
Returns a newMutableRequest
with the givenURI
and a PATCH method. -
PATCH
Returns a newMutableRequest
with the givenURI
and a PATCH method.
-