java.lang.Object
org.miaixz.bus.http.plugin.httpv.CoverHttp<C>
- Type Parameters:
C- The concrete subclass type, for method chaining.
- All Implemented Interfaces:
Cancelable
- Direct Known Subclasses:
CoverCall.Client,CoverHttp.Async,CoverHttp.Sync
An abstract base class for building HTTP requests, providing a fluent interface. It supports synchronous,
asynchronous, and WebSocket requests.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA builder for asynchronous HTTP requests.static classA builder for synchronous HTTP requests. -
Field Summary
FieldsModifier and TypeFieldDescriptionThe core HTTP client instance.booleanIf true, the next callback will be executed on an I/O thread.booleanIf true, exceptions will not be thrown but will be available in the result object.booleanIf true, all preprocessors (both serial and parallel) will be skipped for this request.booleanIf true, serial preprocessors will be skipped for this request. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBodyPara(String name, Object value) Adds a parameter to be included in the request body (e.g., for form submissions).addBodyPara(Map<String, ?> params) Adds multiple body parameters.addFilePara(String name, File file) Adds a file parameter for a multipart request.addFilePara(String name, String filePath) Adds a file parameter for a multipart request.addFilePara(String name, String type, byte[] content) Adds a file parameter from a byte array for a multipart request.addFilePara(String name, String type, String fileName, byte[] content) Adds a file parameter from a byte array for a multipart request.Adds a request header.Adds multiple request headers.addPathPara(String name, Object value) Adds a path parameter to be replaced in the URL (e.g., for /api/user/{id}).addPathPara(Map<String, ?> params) Adds multiple path parameters.addUrlPara(String name, Object value) Adds a URL parameter to be appended to the query string.addUrlPara(Map<String, ?> params) Adds multiple URL parameters.protected voidassertNotConflict(boolean bodyCantUsed) Asserts that there are no conflicting body parameter settings.Binds an arbitrary object to this request.Sets the request body type, such as "form", "json", "xml", "protobuf", etc.booleancancel()Cancels the request.Sets the character encoding for this request.Extracts the character set from an HTTP response.Gets the body type of this request task.getBound()Gets the object bound to this request.Gets the headers of this request task.getTag()Gets the tag of this request task.getUrl()Gets the URL of this request task.booleanChecks if this request's tag matches (contains) the given tag.nextOnIO()Specifies that the next callback should be executed on an I/O thread.nothrow()Configures the request to not throw exceptions on failure.protected NewCallprepareCall(String method) Prepares the Httpd Call object for execution.protected RequestprepareRequest(String method) Prepares the Httpd Request object.protected voidregisteTagTask(Cancelable canceler) Registers this task with the tag manager.protected voidRemoves this task from the tag manager.setBodyPara(Object body) Sets the request body directly.setOnProcess(Callback<Progress> onProcess) Sets a callback to monitor the progress of the request body upload.setRange(long rangeStart) Sets the Range header to resume a download.setRange(long rangeStart, long rangeEnd) Sets the Range header to download a specific chunk of a file.Specifies that this request should skip all preprocessors (both serial and parallel).Specifies that this request should skip any serial preprocessors.stepBytes(long stepBytes) Sets the step size in bytes for progress callbacks.stepRate(double stepRate) Sets the step rate for progress callbacks.Adds a tag to the request.protected booleantimeoutAwait(CountDownLatch latch) Awaits on a CountDownLatch with a timeout.protected CoverResultCreates a result object for a timeout event.Converts an IOException into a CoverResult.State.
-
Field Details
-
httpv
The core HTTP client instance. -
nothrow
public boolean nothrowIf true, exceptions will not be thrown but will be available in the result object. -
nextOnIO
public boolean nextOnIOIf true, the next callback will be executed on an I/O thread. -
skipPreproc
public boolean skipPreprocIf true, all preprocessors (both serial and parallel) will be skipped for this request. -
skipSerialPreproc
public boolean skipSerialPreprocIf true, serial preprocessors will be skipped for this request.
-
-
Constructor Details
-
CoverHttp
Constructs a new CoverHttp request builder.- Parameters:
httpv- The core Httpv client instance.url- The base URL or URL path for the request.
-
-
Method Details
-
getUrl
Gets the URL of this request task.- Returns:
- The URL string.
-
getTag
Gets the tag of this request task.- Returns:
- The tag string.
-
getBodyType
Gets the body type of this request task.- Returns:
- The body type string (e.g., "json", "form").
-
isTagged
Checks if this request's tag matches (contains) the given tag.- Parameters:
tag- The tag to check against.- Returns:
- True if the request is tagged with the specified tag, false otherwise.
-
getHeaders
Gets the headers of this request task.- Returns:
- A map of headers.
-
getBound
Gets the object bound to this request.- Returns:
- The bound object.
-
nothrow
Configures the request to not throw exceptions on failure. Instead, the exception will be available in the `CoverResult` object.- Returns:
- This instance for chaining.
-
skipPreproc
Specifies that this request should skip all preprocessors (both serial and parallel).- Returns:
- This instance for chaining.
-
skipSerialPreproc
Specifies that this request should skip any serial preprocessors.- Returns:
- This instance for chaining.
-
tag
Adds a tag to the request. Multiple tags can be added and will be concatenated with dots.- Parameters:
tag- The tag to add.- Returns:
- This instance for chaining.
-
charset
Sets the character encoding for this request.- Parameters:
charset- The character set.- Returns:
- This instance for chaining.
-
bodyType
Sets the request body type, such as "form", "json", "xml", "protobuf", etc. This determines which message converter will be used for serialization.- Parameters:
type- The body type.- Returns:
- This instance for chaining.
-
nextOnIO
Specifies that the next callback should be executed on an I/O thread.- Returns:
- This instance for chaining.
-
bind
Binds an arbitrary object to this request.- Parameters:
object- The object to bind.- Returns:
- This instance for chaining.
-
addHeader
Adds a request header.- Parameters:
name- The header name.value- The header value.- Returns:
- This instance for chaining.
-
addHeader
Adds multiple request headers.- Parameters:
headers- A map of headers to add.- Returns:
- This instance for chaining.
-
setRange
Sets the Range header to resume a download.- Parameters:
rangeStart- The byte offset to start receiving data from (inclusive).- Returns:
- This instance for chaining.
-
setRange
Sets the Range header to download a specific chunk of a file.- Parameters:
rangeStart- The starting byte offset (inclusive).rangeEnd- The ending byte offset (inclusive).- Returns:
- This instance for chaining.
-
setOnProcess
Sets a callback to monitor the progress of the request body upload.- Parameters:
onProcess- The progress callback function.- Returns:
- This instance for chaining.
-
stepBytes
Sets the step size in bytes for progress callbacks. The callback will be triggered approximately every `stepBytes` bytes are transferred. Defaults to 8KB.- Parameters:
stepBytes- The step size in bytes.- Returns:
- This instance for chaining.
-
stepRate
Sets the step rate for progress callbacks. The callback will be triggered at percentage increments defined by this rate (e.g., 0.01 for every 1%).- Parameters:
stepRate- The step rate (between 0.0 and 1.0).- Returns:
- This instance for chaining.
-
addPathPara
Adds a path parameter to be replaced in the URL (e.g., for /api/user/{id}).- Parameters:
name- The parameter name (without braces).value- The parameter value.- Returns:
- This instance for chaining.
-
addPathPara
Adds multiple path parameters.- Parameters:
params- A map of path parameters.- Returns:
- This instance for chaining.
-
addUrlPara
Adds a URL parameter to be appended to the query string.- Parameters:
name- The parameter name.value- The parameter value.- Returns:
- This instance for chaining.
-
addUrlPara
Adds multiple URL parameters.- Parameters:
params- A map of URL parameters.- Returns:
- This instance for chaining.
-
addBodyPara
Adds a parameter to be included in the request body (e.g., for form submissions).- Parameters:
name- The parameter name.value- The parameter value.- Returns:
- This instance for chaining.
-
addBodyPara
Adds multiple body parameters.- Parameters:
params- A map of body parameters.- Returns:
- This instance for chaining.
-
setBodyPara
Sets the request body directly. The body can be a byte array, a string, or a Java object that will be serialized by a configured message converter.- Parameters:
body- The request body.- Returns:
- This instance for chaining.
-
addFilePara
Adds a file parameter for a multipart request.- Parameters:
name- The parameter name.filePath- The path to the file.- Returns:
- This instance for chaining.
-
addFilePara
Adds a file parameter for a multipart request.- Parameters:
name- The parameter name.file- The file object.- Returns:
- This instance for chaining.
-
addFilePara
Adds a file parameter from a byte array for a multipart request.- Parameters:
name- The parameter name.type- The file type/extension (e.g., "png", "jpg").content- The file content as a byte array.- Returns:
- This instance for chaining.
-
addFilePara
Adds a file parameter from a byte array for a multipart request.- Parameters:
name- The parameter name.type- The file type/extension (e.g., "png", "jpg").fileName- The name of the file.content- The file content as a byte array.- Returns:
- This instance for chaining.
-
cancel
public boolean cancel()Cancels the request.- Specified by:
cancelin interfaceCancelable- Returns:
- True if the request was successfully canceled, false otherwise.
-
registeTagTask
Registers this task with the tag manager.- Parameters:
canceler- The object that can cancel this task.
-
removeTagTask
protected void removeTagTask()Removes this task from the tag manager. -
prepareCall
Prepares the Httpd Call object for execution.- Parameters:
method- The HTTP method (e.g., "GET", "POST").- Returns:
- A new Call instance.
-
prepareRequest
Prepares the Httpd Request object.- Parameters:
method- The HTTP method.- Returns:
- A new Request instance.
-
toState
Converts an IOException into a CoverResult.State.- Parameters:
e- The IOException.- Returns:
- The corresponding state.
-
assertNotConflict
protected void assertNotConflict(boolean bodyCantUsed) Asserts that there are no conflicting body parameter settings.- Parameters:
bodyCantUsed- True if the HTTP method does not permit a request body.
-
timeoutAwait
Awaits on a CountDownLatch with a timeout.- Parameters:
latch- The CountDownLatch to wait on.- Returns:
- False if a timeout occurred, true otherwise.
-
timeoutResult
Creates a result object for a timeout event.- Returns:
- A timeout CoverResult.
-
charset
Extracts the character set from an HTTP response.- Parameters:
response- The HTTP response.- Returns:
- The character set, or the default if not specified.
-