Class HttpPostRequestEncoder
- java.lang.Object
-
- org.jboss.netty.handler.codec.http.multipart.HttpPostRequestEncoder
-
- All Implemented Interfaces:
ChunkedInput
public class HttpPostRequestEncoder extends java.lang.Object implements ChunkedInput
This encoder will help to encode Request for a FORM as POST.
-
-
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
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<InterfaceHttpData>bodyListDatasInterfaceHttpData for Body (without encoding)private java.nio.charset.CharsetcharsetDefault charset to useprivate ChannelBuffercurrentBufferThe ChannelBuffer currently used by the encoderprivate InterfaceHttpDatacurrentDataThe current InterfaceHttpData to encode (used if more chunks are available)private FileUploadcurrentFileUploadThe current FileUpload that is currently in encode processprivate booleanduringMixedModeWhile adding a FileUpload, is the multipart currently in Mixed Modeprivate HttpPostRequestEncoder.EncoderModeencoderModeprivate HttpDataFactoryfactoryFactory used to create InterfaceHttpDataprivate longglobalBodySizeGlobal Body sizeprivate booleanheaderFinalizedTo check if the header has been finalizedprivate booleanisChunkedChunked false by defaultprivate booleanisKeyIf not multipart, does the currentBuffer stands for the Key or for the Valueprivate booleanisLastChunkDoes the last non empty chunk already encoded so that next chunk will be empty (last chunk)private booleanisLastChunkSentLast chunk already sentprivate booleanisMultipartDoes this request is a Multipart requestprivate java.util.ListIterator<InterfaceHttpData>iteratorIterator to be used when encoding will be called chunk after chunk(package private) java.lang.StringmultipartDataBoundaryIf multipart, this is the boundary for the flobal multipart(package private) java.util.List<InterfaceHttpData>multipartHttpDatasThe final Multipart List of InterfaceHttpData including encoding(package private) java.lang.StringmultipartMixedBoundaryIf multipart, there could be internal multiparts (mixed) to the global multipart.private static java.util.Map<java.util.regex.Pattern,java.lang.String>percentEncodingsprivate HttpRequestrequestRequest to encode
-
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(HttpDataFactory factory, HttpRequest request, boolean multipart, java.nio.charset.Charset charset, HttpPostRequestEncoder.EncoderMode encoderMode)
-
Method Summary
All Methods Static Methods Instance Methods Concrete 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 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 stream.private java.lang.StringencodeAttribute(java.lang.String s, java.nio.charset.Charset charset)Encode one attributeprivate HttpChunkencodeNextChunkMultipart(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 HttpChunkencodeNextChunkUrlEncoded(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 ChannelBufferfillChannelBuffer()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 method returns a List of all InterfaceHttpData from body part.private static java.lang.StringgetNewMultipartDelimiter()booleanhasNextChunk()Returnstrueif and only if there is any data left in the stream.private voidinitDataMultipart()Init the delimiter for Global Part (Data).private voidinitMixedMultipart()Init the delimiter for Mixed Part (Mixed).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 requestHttpChunknextChunk()Returns the next available HttpChunk.voidsetBodyHttpDatas(java.util.List<InterfaceHttpData> datas)Set the Body HttpDatas list
-
-
-
Field Detail
-
percentEncodings
private static final java.util.Map<java.util.regex.Pattern,java.lang.String> percentEncodings
-
factory
private final HttpDataFactory factory
Factory used to create InterfaceHttpData
-
request
private final HttpRequest request
Request to encode
-
charset
private final java.nio.charset.Charset charset
Default charset to use
-
isChunked
private boolean isChunked
Chunked false by default
-
bodyListDatas
private final java.util.List<InterfaceHttpData> bodyListDatas
InterfaceHttpData for Body (without encoding)
-
multipartHttpDatas
final java.util.List<InterfaceHttpData> multipartHttpDatas
The final Multipart List of InterfaceHttpData including encoding
-
isMultipart
private final boolean isMultipart
Does this request is a Multipart request
-
multipartDataBoundary
java.lang.String multipartDataBoundary
If multipart, this is the boundary for the flobal multipart
-
multipartMixedBoundary
java.lang.String multipartMixedBoundary
If multipart, there could be internal multiparts (mixed) to the global multipart. Only one level is allowed.
-
headerFinalized
private boolean headerFinalized
To check if the header has been finalized
-
encoderMode
private final HttpPostRequestEncoder.EncoderMode encoderMode
-
isLastChunk
private boolean isLastChunk
Does the last non empty chunk already encoded so that next chunk will be empty (last chunk)
-
isLastChunkSent
private boolean isLastChunkSent
Last chunk already sent
-
currentFileUpload
private FileUpload currentFileUpload
The current FileUpload that is currently in encode process
-
duringMixedMode
private boolean duringMixedMode
While adding a FileUpload, is the multipart currently in Mixed Mode
-
globalBodySize
private long globalBodySize
Global Body size
-
iterator
private java.util.ListIterator<InterfaceHttpData> iterator
Iterator to be used when encoding will be called chunk after chunk
-
currentBuffer
private ChannelBuffer currentBuffer
The ChannelBuffer currently used by the encoder
-
currentData
private InterfaceHttpData currentData
The current InterfaceHttpData to encode (used if more chunks are available)
-
isKey
private boolean isKey
If not multipart, does the currentBuffer stands for the Key or for the Value
-
-
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 not a POST
-
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 not a POST
-
HttpPostRequestEncoder
public HttpPostRequestEncoder(HttpDataFactory factory, HttpRequest request, boolean multipart, java.nio.charset.Charset charset) 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 default- Throws:
java.lang.NullPointerException- for request or charset or factoryHttpPostRequestEncoder.ErrorDataEncoderException- if the request is not a POST
-
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 default + @param encoderMode 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 not a POST
-
-
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
-
initDataMultipart
private void initDataMultipart()
Init the delimiter for Global Part (Data).
-
initMixedMultipart
private void initMixedMultipart()
Init the delimiter for Mixed Part (Mixed).
-
getNewMultipartDelimiter
private static java.lang.String getNewMultipartDelimiter()
- Returns:
- a newly generated Delimiter (either for DATA or MIXED)
-
getBodyListAttributes
public java.util.List<InterfaceHttpData> getBodyListAttributes()
This method 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
-
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.NullPointerException- 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
-
encodeAttribute
private java.lang.String encodeAttribute(java.lang.String s, java.nio.charset.Charset charset) throws HttpPostRequestEncoder.ErrorDataEncoderExceptionEncode one attribute- Returns:
- the encoded attribute
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error
-
fillChannelBuffer
private ChannelBuffer fillChannelBuffer()
- Returns:
- the next ChannelBuffer to send as a HttpChunk and modifying currentBuffer accordingly
-
encodeNextChunkMultipart
private HttpChunk 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 HttpChunk 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
public void close() throws java.lang.ExceptionDescription copied from interface:ChunkedInputReleases the resources associated with the stream.- Specified by:
closein interfaceChunkedInput- Throws:
java.lang.Exception
-
nextChunk
public HttpChunk nextChunk() throws HttpPostRequestEncoder.ErrorDataEncoderException
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 method.- Specified by:
nextChunkin interfaceChunkedInput- Returns:
- the next available HttpChunk
- Throws:
HttpPostRequestEncoder.ErrorDataEncoderException- if the encoding is in error
-
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- Throws:
java.lang.Exception
-
hasNextChunk
public boolean hasNextChunk() throws java.lang.ExceptionDescription copied from interface:ChunkedInputReturnstrueif and only if there is any data left in the stream. Please note thatfalsedoes not necessarily mean that the stream has reached at its end. In a slow stream, the next chunk might be unavailable just momentarily.- Specified by:
hasNextChunkin interfaceChunkedInput- Throws:
java.lang.Exception
-
-