Class HttpPostRequestEncoder
java.lang.Object
io.netty.handler.codec.http.multipart.HttpPostRequestEncoder
- All Implemented Interfaces:
ChunkedInput<HttpContent>
This encoder will help to encode Request for a FORM as POST.
According to RFC 7231, POST, PUT and OPTIONS allow to have a body. This encoder will support widely all methods except TRACE since the RFC notes for GET, DELETE, HEAD and CONNECT: (replaces XXX by one of these methods)
"A payload within a XXX request message has no defined semantics; sending a payload body on a XXX request might cause some existing implementations to reject the request."
On the contrary, for TRACE method, RFC says:
"A client MUST NOT send a message body in a TRACE request."
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Different modes to use to encode form data.static class
Exception when an error occurs while encodingprivate static final class
private static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List
<InterfaceHttpData> InterfaceHttpData for Body (without encoding)private final Charset
Default charset to useprivate ByteBuf
The ByteBuf currently used by the encoderprivate InterfaceHttpData
The current InterfaceHttpData to encode (used if more chunks are available)private FileUpload
The current FileUpload that is currently in encode processprivate boolean
While adding a FileUpload, is the multipart currently in Mixed Modeprivate final HttpPostRequestEncoder.EncoderMode
private final HttpDataFactory
Factory used to create InterfaceHttpDataprivate long
Global Body sizeprivate long
Global Transfer progressprivate boolean
To check if the header has been finalizedprivate boolean
Chunked false by defaultprivate boolean
If not multipart, does the currentBuffer stands for the Key or for the Valueprivate boolean
Does the last non empty chunk already encoded so that next chunk will be empty (last chunk)private boolean
Last chunk already sentprivate final boolean
Does this request is a Multipart requestprivate ListIterator
<InterfaceHttpData> Iterator to be used when encoding will be called chunk after chunk(package private) String
If multipart, this is the boundary for the flobal multipart(package private) final List
<InterfaceHttpData> The final Multipart List of InterfaceHttpData including encoding(package private) String
If multipart, there could be internal multiparts (mixed) to the global multipart.private static final Map.Entry[]
private final HttpRequest
Request to encode -
Constructor Summary
ConstructorsConstructorDescriptionHttpPostRequestEncoder
(HttpRequest request, boolean multipart) HttpPostRequestEncoder
(HttpDataFactory factory, HttpRequest request, boolean multipart) HttpPostRequestEncoder
(HttpDataFactory factory, HttpRequest request, boolean multipart, Charset charset, HttpPostRequestEncoder.EncoderMode encoderMode) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBodyAttribute
(String name, String value) Add a simple attribute in the body as Name=Valuevoid
addBodyFileUpload
(String name, File file, String contentType, boolean isText) Add a file as a FileUploadvoid
addBodyFileUpload
(String name, String filename, File file, String contentType, boolean isText) Add a file as a FileUploadvoid
addBodyFileUploads
(String name, File[] file, String[] contentType, boolean[] isText) Add a series of Files associated with one File parametervoid
Add the InterfaceHttpData to the Body listprivate int
void
Clean all HttpDatas (on Disk) for the current request.void
close()
Releases the resources associated with the input.private String
encodeAttribute
(String s, Charset charset) Encode one attributeprivate HttpContent
encodeNextChunkMultipart
(int sizeleft) From the current context (currentBuffer and currentData), returns the next HttpChunk (if possible) trying to get sizeleft bytes more into the currentBuffer.private HttpContent
encodeNextChunkUrlEncoded
(int sizeleft) From the current context (currentBuffer and currentData), returns the next HttpChunk (if possible) trying to get sizeleft bytes more into the currentBuffer.private ByteBuf
Finalize the request by preparing the Header in the request and returns the request ready to be sent.
Once finalized, no data must be added.
If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote server.This getMethod returns a List of all InterfaceHttpData from body part.private static String
private void
Init the delimiter for Global Part (Data).private void
Init the delimiter for Mixed Part (Mixed).boolean
boolean
Returntrue
if and only if there is no data left in the stream and the stream has reached at its end.boolean
True if this request is a Multipart requestprivate HttpContent
long
length()
Returns the length of the input.private HttpContent
Returns the next available HttpChunk.long
progress()
Returns current transfer progress.readChunk
(ByteBufAllocator allocator) Returns the next available HttpChunk.Deprecated.void
setBodyHttpDatas
(List<InterfaceHttpData> datas) Set the Body HttpDatas list
-
Field Details
-
percentEncodings
-
factory
Factory used to create InterfaceHttpData -
request
Request to encode -
charset
Default charset to use -
isChunked
private boolean isChunkedChunked false by default -
bodyListDatas
InterfaceHttpData for Body (without encoding) -
multipartHttpDatas
The final Multipart List of InterfaceHttpData including encoding -
isMultipart
private final boolean isMultipartDoes this request is a Multipart request -
multipartDataBoundary
String multipartDataBoundaryIf multipart, this is the boundary for the flobal multipart -
multipartMixedBoundary
String multipartMixedBoundaryIf multipart, there could be internal multiparts (mixed) to the global multipart. Only one level is allowed. -
headerFinalized
private boolean headerFinalizedTo check if the header has been finalized -
encoderMode
-
isLastChunk
private boolean isLastChunkDoes the last non empty chunk already encoded so that next chunk will be empty (last chunk) -
isLastChunkSent
private boolean isLastChunkSentLast chunk already sent -
currentFileUpload
The current FileUpload that is currently in encode process -
duringMixedMode
private boolean duringMixedModeWhile adding a FileUpload, is the multipart currently in Mixed Mode -
globalBodySize
private long globalBodySizeGlobal Body size -
globalProgress
private long globalProgressGlobal Transfer progress -
iterator
Iterator to be used when encoding will be called chunk after chunk -
currentBuffer
The ByteBuf currently used by the encoder -
currentData
The current InterfaceHttpData to encode (used if more chunks are available) -
isKey
private boolean isKeyIf not multipart, does the currentBuffer stands for the Key or for the Value
-
-
Constructor Details
-
HttpPostRequestEncoder
public HttpPostRequestEncoder(HttpRequest request, boolean multipart) throws HttpPostRequestEncoder.ErrorDataEncoderException - Parameters:
request
- the request to encodemultipart
- True if the FORM is a ENCTYPE="multipart/form-data"- Throws:
NullPointerException
- for requestHttpPostRequestEncoder.ErrorDataEncoderException
- if the request is a TRACE
-
HttpPostRequestEncoder
public HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart) throws HttpPostRequestEncoder.ErrorDataEncoderException - Parameters:
factory
- the factory used to create InterfaceHttpDatarequest
- the request to encodemultipart
- True if the FORM is a ENCTYPE="multipart/form-data"- Throws:
NullPointerException
- for request and factoryHttpPostRequestEncoder.ErrorDataEncoderException
- if the request is a TRACE
-
HttpPostRequestEncoder
public HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart, Charset charset, HttpPostRequestEncoder.EncoderMode encoderMode) throws HttpPostRequestEncoder.ErrorDataEncoderException - Parameters:
factory
- the factory used to create InterfaceHttpDatarequest
- the request to encodemultipart
- True if the FORM is a ENCTYPE="multipart/form-data"charset
- the charset to use as defaultencoderMode
- the mode for the encoder to use. SeeHttpPostRequestEncoder.EncoderMode
for the details.- Throws:
NullPointerException
- for request or charset or factoryHttpPostRequestEncoder.ErrorDataEncoderException
- if the request is a TRACE
-
-
Method Details
-
cleanFiles
public void cleanFiles()Clean all HttpDatas (on Disk) for the current request. -
isMultipart
public boolean isMultipart()True if this request is a Multipart request- Returns:
- True if this request is a Multipart request
-
initDataMultipart
private void initDataMultipart()Init the delimiter for Global Part (Data). -
initMixedMultipart
private void initMixedMultipart()Init the delimiter for Mixed Part (Mixed). -
getNewMultipartDelimiter
- Returns:
- a newly generated Delimiter (either for DATA or MIXED)
-
getBodyListAttributes
This getMethod returns a List of all InterfaceHttpData from body part.- Returns:
- the list of InterfaceHttpData from Body part
-
setBodyHttpDatas
public void setBodyHttpDatas(List<InterfaceHttpData> datas) throws HttpPostRequestEncoder.ErrorDataEncoderException Set the Body HttpDatas list- Throws:
NullPointerException
- for datasHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
addBodyAttribute
public void addBodyAttribute(String name, String value) throws HttpPostRequestEncoder.ErrorDataEncoderException Add a simple attribute in the body as Name=Value- Parameters:
name
- name of the parametervalue
- the value of the parameter- Throws:
NullPointerException
- for nameHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
addBodyFileUpload
public void addBodyFileUpload(String name, File file, String contentType, boolean isText) throws HttpPostRequestEncoder.ErrorDataEncoderException Add a file as a FileUpload- Parameters:
name
- the name of the parameterfile
- the file to be uploaded (if not Multipart mode, only the filename will be included)contentType
- the associated contentType for the FileisText
- True if this file should be transmitted in Text format (else binary)- Throws:
NullPointerException
- for name and fileHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
addBodyFileUpload
public void addBodyFileUpload(String name, String filename, File file, String contentType, boolean isText) throws HttpPostRequestEncoder.ErrorDataEncoderException Add a file as a FileUpload- Parameters:
name
- the name of the parameterfilename
- the filename to use for this File part, empty String will be ignored by the encoderfile
- the file to be uploaded (if not Multipart mode, only the filename will be included)contentType
- the associated contentType for the FileisText
- True if this file should be transmitted in Text format (else binary)- Throws:
NullPointerException
- for name and fileHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
addBodyFileUploads
public void addBodyFileUploads(String name, File[] file, String[] contentType, boolean[] isText) throws HttpPostRequestEncoder.ErrorDataEncoderException Add a series of Files associated with one File parameter- Parameters:
name
- the name of the parameterfile
- the array of filescontentType
- the array of content Types associated with each fileisText
- the array of isText attribute (False meaning binary mode) for each file- Throws:
IllegalArgumentException
- also throws if array have different sizesHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
addBodyHttpData
public void addBodyHttpData(InterfaceHttpData data) throws HttpPostRequestEncoder.ErrorDataEncoderException Add the InterfaceHttpData to the Body list- Throws:
NullPointerException
- for dataHttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
finalizeRequest
Finalize the request by preparing the Header in the request and returns the request ready to be sent.
Once finalized, no data must be added.
If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote server.- Returns:
- the request object (chunked or not according to size of body)
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error or if the finalize were already done
-
isChunked
public boolean isChunked()- Returns:
- True if the request is by Chunk
-
encodeAttribute
private String encodeAttribute(String s, Charset charset) throws HttpPostRequestEncoder.ErrorDataEncoderException Encode one attribute- Returns:
- the encoded attribute
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error
-
fillByteBuf
- Returns:
- the next ByteBuf to send as an HttpChunk and modifying currentBuffer accordingly
-
encodeNextChunkMultipart
private HttpContent encodeNextChunkMultipart(int sizeleft) throws HttpPostRequestEncoder.ErrorDataEncoderException From the current context (currentBuffer and currentData), returns the next HttpChunk (if possible) trying to get sizeleft bytes more into the currentBuffer. This is the Multipart version.- Parameters:
sizeleft
- the number of bytes to try to get from currentData- Returns:
- the next HttpChunk or null if not enough bytes were found
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error
-
encodeNextChunkUrlEncoded
private HttpContent encodeNextChunkUrlEncoded(int sizeleft) throws HttpPostRequestEncoder.ErrorDataEncoderException From the current context (currentBuffer and currentData), returns the next HttpChunk (if possible) trying to get sizeleft bytes more into the currentBuffer. This is the UrlEncoded version.- Parameters:
sizeleft
- the number of bytes to try to get from currentData- Returns:
- the next HttpChunk or null if not enough bytes were found
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error
-
close
Description copied from interface:ChunkedInput
Releases the resources associated with the input.- Specified by:
close
in interfaceChunkedInput<HttpContent>
- Throws:
Exception
-
readChunk
Deprecated.- Specified by:
readChunk
in interfaceChunkedInput<HttpContent>
- Parameters:
ctx
- The context which provides aByteBufAllocator
if buffer allocation is necessary.- Returns:
- the fetched chunk.
null
if there is no data left in the stream. Please note thatnull
does not necessarily mean that the stream has reached at its end. In a slow stream, the next chunk might be unavailable just momentarily. - Throws:
Exception
-
readChunk
Returns the next available HttpChunk. The caller is responsible to test if this chunk is the last one (isLast()), in order to stop calling this getMethod.- Specified by:
readChunk
in interfaceChunkedInput<HttpContent>
- Parameters:
allocator
-ByteBufAllocator
if buffer allocation is necessary.- Returns:
- the next available HttpChunk
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in errorException
-
nextChunk
Returns the next available HttpChunk. The caller is responsible to test if this chunk is the last one (isLast()), in order to stop calling this getMethod.- Returns:
- the next available HttpChunk
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException
- if the encoding is in error
-
calculateRemainingSize
private int calculateRemainingSize() -
lastChunk
-
isEndOfInput
Description copied from interface:ChunkedInput
Returntrue
if and only if there is no data left in the stream and the stream has reached at its end.- Specified by:
isEndOfInput
in interfaceChunkedInput<HttpContent>
- Throws:
Exception
-
length
public long length()Description copied from interface:ChunkedInput
Returns the length of the input.- Specified by:
length
in interfaceChunkedInput<HttpContent>
- Returns:
- the length of the input if the length of the input is known. a negative value if the length of the input is unknown.
-
progress
public long progress()Description copied from interface:ChunkedInput
Returns current transfer progress.- Specified by:
progress
in interfaceChunkedInput<HttpContent>
-