Class HttpPostRequestEncoder
- java.lang.Object
-
- io.netty.handler.codec.http.multipart.HttpPostRequestEncoder
-
- All Implemented Interfaces:
ChunkedInput<HttpContent>
public class HttpPostRequestEncoder extends java.lang.Object implements 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 Classes Modifier and Type Class Description static classHttpPostRequestEncoder.EncoderModeDifferent modes to use to encode form data.static classHttpPostRequestEncoder.ErrorDataEncoderExceptionException when an error occurs while encoding
-
Constructor Summary
Constructors Constructor Description HttpPostRequestEncoder(HttpRequest request, boolean multipart)HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart)HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart, java.nio.charset.Charset charset, HttpPostRequestEncoder.EncoderMode encoderMode)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddBodyAttribute(java.lang.String name, java.lang.String value)Add a simple attribute in the body as Name=ValuevoidaddBodyFileUpload(java.lang.String name, java.io.File file, java.lang.String contentType, boolean isText)Add a file as a FileUploadvoidaddBodyFileUpload(java.lang.String name, java.lang.String filename, java.io.File file, java.lang.String contentType, boolean isText)Add a file as a FileUploadvoidaddBodyFileUploads(java.lang.String name, java.io.File[] file, java.lang.String[] contentType, boolean[] isText)Add a series of Files associated with one File parametervoidaddBodyHttpData(InterfaceHttpData data)Add the InterfaceHttpData to the Body listvoidcleanFiles()Clean all HttpDatas (on Disk) for the current request.voidclose()Releases the resources associated with the input.HttpRequestfinalizeRequest()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.java.util.List<InterfaceHttpData>getBodyListAttributes()This getMethod returns a List of all InterfaceHttpData from body part.booleanisChunked()booleanisEndOfInput()Returntrueif and only if there is no data left in the stream and the stream has reached at its end.booleanisMultipart()True if this request is a Multipart requestlonglength()Returns the length of the input.longprogress()Returns current transfer progress.HttpContentreadChunk(ByteBufAllocator allocator)Returns the next available HttpChunk.HttpContentreadChunk(ChannelHandlerContext ctx)Deprecated.voidsetBodyHttpDatas(java.util.List<InterfaceHttpData> datas)Set the Body HttpDatas list
-
-
-
Constructor Detail
-
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:
java.lang.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:
java.lang.NullPointerException- for request and factoryHttpPostRequestEncoder.ErrorDataEncoderException- if the request is a TRACE
-
HttpPostRequestEncoder
public HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart, java.nio.charset.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.EncoderModefor the details.- Throws:
java.lang.NullPointerException- for request or charset or factoryHttpPostRequestEncoder.ErrorDataEncoderException- if the request is a TRACE
-
-
Method Detail
-
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
-
getBodyListAttributes
public java.util.List<InterfaceHttpData> getBodyListAttributes()
This getMethod returns a List of all InterfaceHttpData from body part.- Returns:
- the list of InterfaceHttpData from Body part
-
setBodyHttpDatas
public void setBodyHttpDatas(java.util.List<InterfaceHttpData> datas) throws HttpPostRequestEncoder.ErrorDataEncoderException
Set the Body HttpDatas list- Throws:
java.lang.NullPointerException- for datasHttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error or if the finalize were already done
-
addBodyAttribute
public void addBodyAttribute(java.lang.String name, java.lang.String value) throws HttpPostRequestEncoder.ErrorDataEncoderExceptionAdd a simple attribute in the body as Name=Value- Parameters:
name- name of the parametervalue- the value of the parameter- Throws:
java.lang.NullPointerException- for nameHttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error or if the finalize were already done
-
addBodyFileUpload
public void addBodyFileUpload(java.lang.String name, java.io.File file, java.lang.String contentType, boolean isText) throws HttpPostRequestEncoder.ErrorDataEncoderExceptionAdd 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:
java.lang.NullPointerException- for name and fileHttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error or if the finalize were already done
-
addBodyFileUpload
public void addBodyFileUpload(java.lang.String name, java.lang.String filename, java.io.File file, java.lang.String contentType, boolean isText) throws HttpPostRequestEncoder.ErrorDataEncoderExceptionAdd 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)filename- the filename to use for this File part, empty String will be ignored by the encodercontentType- the associated contentType for the FileisText- True if this file should be transmitted in Text format (else binary)- Throws:
java.lang.NullPointerException- for name and fileHttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error or if the finalize were already done
-
addBodyFileUploads
public void addBodyFileUploads(java.lang.String name, java.io.File[] file, java.lang.String[] contentType, boolean[] isText) throws HttpPostRequestEncoder.ErrorDataEncoderExceptionAdd 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:
java.lang.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:
java.lang.NullPointerException- for dataHttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error or if the finalize were already done
-
finalizeRequest
public HttpRequest finalizeRequest() throws HttpPostRequestEncoder.ErrorDataEncoderException
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
-
close
public void close() throws java.lang.ExceptionDescription copied from interface:ChunkedInputReleases the resources associated with the input.- Specified by:
closein interfaceChunkedInput<HttpContent>- Throws:
java.lang.Exception
-
readChunk
@Deprecated public HttpContent readChunk(ChannelHandlerContext ctx) throws java.lang.Exception
Deprecated.- Specified by:
readChunkin interfaceChunkedInput<HttpContent>- Parameters:
ctx- The context which provides aByteBufAllocatorif buffer allocation is necessary.- Returns:
- the fetched chunk.
nullif there is no data left in the stream. Please note thatnulldoes not necessarily mean that the stream has reached at its end. In a slow stream, the next chunk might be unavailable just momentarily. - Throws:
java.lang.Exception
-
readChunk
public HttpContent readChunk(ByteBufAllocator allocator) throws java.lang.Exception
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:
readChunkin interfaceChunkedInput<HttpContent>- Parameters:
allocator-ByteBufAllocatorif buffer allocation is necessary.- Returns:
- the next available HttpChunk
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in errorjava.lang.Exception
-
isEndOfInput
public boolean isEndOfInput() throws java.lang.ExceptionDescription copied from interface:ChunkedInputReturntrueif and only if there is no data left in the stream and the stream has reached at its end.- Specified by:
isEndOfInputin interfaceChunkedInput<HttpContent>- Throws:
java.lang.Exception
-
length
public long length()
Description copied from interface:ChunkedInputReturns the length of the input.- Specified by:
lengthin 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:ChunkedInputReturns current transfer progress.- Specified by:
progressin interfaceChunkedInput<HttpContent>
-
-