Class MutableRequest
- 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 TaggableRequest
TaggableRequest.BuilderNested classes/interfaces inherited from class HttpRequest
HttpRequest.BodyPublisher, HttpRequest.BodyPublishers -
Method Summary
Modifier and TypeMethodDescriptionReturns theAdapterCodecto be used for resolving this request's payload.adapterCodec(AdapterCodec adapterCodec) Set's theAdapterCodecto 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-Controlheader to the given value.copy()Returns a copy of this request that is independent of this instance.static MutableRequestcopyOf(HttpRequest other) Returns a newMutableRequestthat is a copy of the given request.static MutableRequestcreate()Returns a newMutableRequest.static MutableRequestReturns a newMutableRequestwith the given URI and a default GET method.static MutableRequestReturns a newMutableRequestwith the given URI and a default GET method.DELETE()static MutableRequestReturns a newMutableRequestwith the given URI and a DELETE method.static MutableRequestReturns a newMutableRequestwith the given URI and a DELETE method.booleanexpectContinue(boolean enable) GET()static MutableRequestReturns a newMutableRequestwith the given URI and a GET method.static MutableRequestReturns a newMutableRequestwith the given URI and a GET method.HEAD()static MutableRequestReturns a newMutableRequestwith the given URI and a HEAD method.static MutableRequestReturns a newMutableRequestwith 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> MutableRequestAdds the given value to this request'sBodyAdapter.Hints.hints()Returns this request'sBodyAdapter.Hints.hints(Consumer<BodyAdapter.Hints.Builder> hintsMutator) Modifies this request'sBodyAdapter.Hintsby mutating aBodyAdapter.Hints.Builder.method()Sets the request method and sets the payload to the given value.method(String method, HttpRequest.BodyPublisher bodyPublisher) <T> MutableRequestSets 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 MutableRequestReturns a newMutableRequestwith the given URI and a PATCH method.static MutableRequestPATCH(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a PATCH method.static <T> MutableRequestReturns a newMutableRequestwith 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 MutableRequestReturns a newMutableRequestwith the given URI and a PATCH method.static MutableRequestPATCH(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a PATCH method.static <T> MutableRequestReturns a newMutableRequestwith the given URI and a PATCH method.<T> MutableRequestSets 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 MutableRequestReturns a newMutableRequestwith the given URI and a POST method.static MutableRequestPOST(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a POST method.static <T> MutableRequestReturns a newMutableRequestwith the given URI and a POST method.POST(HttpRequest.BodyPublisher bodyPublisher) static MutableRequestReturns a newMutableRequestwith the given URI and a POST method.static MutableRequestPOST(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a POST method.static <T> MutableRequestReturns a newMutableRequestwith the given URI and a POST method.<T> MutableRequestSets 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 MutableRequestReturns a newMutableRequestwith the given URI and a PUT method.static MutableRequestPUT(String uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a PUT method.static <T> MutableRequestReturns a newMutableRequestwith the given URI and a PUT method.PUT(HttpRequest.BodyPublisher bodyPublisher) static MutableRequestReturns a newMutableRequestwith the given URI and a PUT method.static MutableRequestPUT(URI uri, HttpRequest.BodyPublisher bodyPublisher) Returns a newMutableRequestwith the given URI and a PUT method.static <T> MutableRequestReturns a newMutableRequestwith the given URI and a PUT method.<T> MutableRequestSets 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> MutableRequestAdds a tag mapped to the given type.<T> MutableRequestAdds 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 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-Controlheader to the given value. -
apply
Calls the given consumer against this request. -
tag
Description copied from interface:TaggableRequest.BuilderAdds a tag mapped to the given object's runtime type.- Specified by:
tagin interfaceTaggableRequest.Builder
-
tag
Description copied from interface:TaggableRequest.BuilderAdds a tag mapped to the given type.- Specified by:
tagin interfaceTaggableRequest.Builder
-
tag
Description copied from interface:TaggableRequest.BuilderAdds a tag mapped to the given type.- Specified by:
tagin interfaceTaggableRequest.Builder
-
removeTag
Description copied from interface:TaggableRequest.BuilderRemoves the tag associated with the given type.- Specified by:
removeTagin interfaceTaggableRequest.Builder
-
removeTag
Description copied from interface:TaggableRequest.BuilderRemoves the tag associated with the given type.- Specified by:
removeTagin interfaceTaggableRequest.Builder
-
hints
Modifies this request'sBodyAdapter.Hintsby mutating aBodyAdapter.Hints.Builder. -
hint
Adds the given value to this request'sBodyAdapter.Hints. -
bodyPublisher
- Specified by:
bodyPublisherin classHttpRequest
-
adapterCodec
Set's theAdapterCodecto be used for resolving this request's payload. -
adapterCodec
Returns theAdapterCodecto be used for resolving this request's payload. -
method
- Specified by:
methodin classHttpRequest
-
timeout
- Specified by:
timeoutin classHttpRequest
-
expectContinue
public boolean expectContinue()- Specified by:
expectContinuein classHttpRequest
-
uri
An empty
URI(without a scheme, path or a host) is returned if noURIwas previously set.- Specified by:
uriin classHttpRequest
-
version
- Specified by:
versionin classHttpRequest
-
headers
- Specified by:
headersin classHttpRequest
-
hints
Description copied from class:TaggableRequestReturns this request'sBodyAdapter.Hints.- Specified by:
hintsin classTaggableRequest
-
uri
Sets this request'sURI. Can be relative or without a host or a scheme.- Specified by:
uriin interfaceHttpRequest.Builder- Specified by:
uriin interfaceTaggableRequest.Builder
-
expectContinue
- Specified by:
expectContinuein interfaceHttpRequest.Builder- Specified by:
expectContinuein interfaceTaggableRequest.Builder
-
version
- Specified by:
versionin interfaceHttpRequest.Builder- Specified by:
versionin interfaceTaggableRequest.Builder
-
header
Description copied from interface:HeadersAccumulatorAdds the given header name-value pair.- Specified by:
headerin interfaceHeadersAccumulator<MutableRequest>- Specified by:
headerin interfaceHttpRequest.Builder- Specified by:
headerin interfaceTaggableRequest.Builder
-
headers
Description copied from interface:HeadersAccumulatorAdds 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:
headersin interfaceHeadersAccumulator<MutableRequest>- Specified by:
headersin interfaceHttpRequest.Builder- Specified by:
headersin interfaceTaggableRequest.Builder
-
headers
Adds each of the givenHttpHeaders.- Specified by:
headersin interfaceHeadersAccumulator<MutableRequest>
-
setHeader
Description copied from interface:HeadersAccumulatorSets the header represented by the given name to the given value, overwriting the previous value (if any).- Specified by:
setHeaderin interfaceHeadersAccumulator<MutableRequest>- Specified by:
setHeaderin interfaceHttpRequest.Builder- Specified by:
setHeaderin interfaceTaggableRequest.Builder
-
setHeader
Description copied from interface:HeadersAccumulatorSets the header represented by the given name to the given values, overwriting the previous value (if any).- Specified by:
setHeaderin interfaceHeadersAccumulator<MutableRequest>
-
setHeaderIfAbsent
Description copied from interface:HeadersAccumulatorSets the header represented by the given name to the given value, only if there was no header with the given name.- Specified by:
setHeaderIfAbsentin interfaceHeadersAccumulator<MutableRequest>
-
setHeaderIfAbsent
Description copied from interface:HeadersAccumulatorSets the header represented by the given name to the given values, only if there was no header with the given name.- Specified by:
setHeaderIfAbsentin interfaceHeadersAccumulator<MutableRequest>
-
removeHeaders
Removes all headers added so far.- Specified by:
removeHeadersin interfaceHeadersAccumulator<MutableRequest>
-
removeHeader
Removes any header associated with the given name.- Specified by:
removeHeaderin interfaceHeadersAccumulator<MutableRequest>
-
removeHeadersIf
Removes all headers matched by the given predicate.- Specified by:
removeHeadersIfin interfaceHeadersAccumulator<MutableRequest>
-
timeout
- Specified by:
timeoutin interfaceHttpRequest.Builder- Specified by:
timeoutin interfaceTaggableRequest.Builder
-
GET
- Specified by:
GETin interfaceHttpRequest.Builder- Specified by:
GETin interfaceTaggableRequest.Builder
-
HEAD
- Specified by:
HEADin interfaceHttpRequest.Builder
-
POST
- Specified by:
POSTin interfaceHttpRequest.Builder- Specified by:
POSTin 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:
PUTin interfaceHttpRequest.Builder- Specified by:
PUTin 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:
DELETEin interfaceHttpRequest.Builder- Specified by:
DELETEin interfaceTaggableRequest.Builder
-
method
@CanIgnoreReturnValue public MutableRequest method(String method, HttpRequest.BodyPublisher bodyPublisher) - Specified by:
methodin interfaceHttpRequest.Builder- Specified by:
methodin 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:
buildin interfaceHttpRequest.Builder- Specified by:
buildin 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:
copyin interfaceHttpRequest.Builder- Specified by:
copyin interfaceTaggableRequest.Builder
-
toString
-
mimeBody
-
copyOf
Returns a newMutableRequestthat is a copy of the given request. -
create
Returns a newMutableRequest. -
create
Returns a newMutableRequestwith the given URI and a default GET method. -
create
Returns a newMutableRequestwith the given URI and a default GET method. -
GET
Returns a newMutableRequestwith the given URI and a GET method. -
GET
Returns a newMutableRequestwith the given URI and a GET method. -
HEAD
Returns a newMutableRequestwith the given URI and a HEAD method. -
HEAD
Returns a newMutableRequestwith the given URI and a HEAD method. -
DELETE
Returns a newMutableRequestwith the given URI and a DELETE method. -
DELETE
Returns a newMutableRequestwith the given URI and a DELETE method. -
POST
Returns a newMutableRequestwith the given URI and a POST method. -
POST
Returns a newMutableRequestwith the given URI and a POST method. -
POST
Returns a newMutableRequestwith the given URI and a POST method. -
POST
Returns a newMutableRequestwith the given URI and a POST method. -
POST
public static <T> MutableRequest POST(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequestwith the given URI and a POST method. -
POST
Returns a newMutableRequestwith the given URI and a POST method. -
PUT
Returns a newMutableRequestwith the given URI and a PUT method. -
PUT
Returns a newMutableRequestwith the given URI and a PUT method. -
PUT
Returns a newMutableRequestwith the given URI and a PUT method. -
PUT
Returns a newMutableRequestwith the given URI and a PUT method. -
PUT
public static <T> MutableRequest PUT(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequestwith the given URI and a PUT method. -
PUT
Returns a newMutableRequestwith the given URI and a PUT method. -
PATCH
Returns a newMutableRequestwith the given URI and a PATCH method. -
PATCH
Returns a newMutableRequestwith the given URI and a PATCH method. -
PATCH
Returns a newMutableRequestwith the given URI and a PATCH method. -
PATCH
Returns a newMutableRequestwith the given URI and a PATCH method. -
PATCH
public static <T> MutableRequest PATCH(String uri, T payload, TypeRef<T> typeRef, MediaType mediaType) Returns a newMutableRequestwith the given URI and a PATCH method. -
PATCH
Returns a newMutableRequestwith the given URI and a PATCH method.
-