Class HttpPostMultipartRequestDecoder
- java.lang.Object
-
- org.jboss.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder
-
- All Implemented Interfaces:
InterfaceHttpPostRequestDecoder
public class HttpPostMultipartRequestDecoder extends java.lang.Object implements InterfaceHttpPostRequestDecoder
This decoder will decode Body and can handle POST BODY in multipart form.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<InterfaceHttpData>bodyListHttpDataHttpDatas from Bodyprivate intbodyListHttpDataRankBody HttpDatas current positionprivate java.util.Map<java.lang.String,java.util.List<InterfaceHttpData>>bodyMapHttpDataHttpDatas as Map from Bodyprivate java.nio.charset.CharsetcharsetDefault charset to useprivate AttributecurrentAttributeThe current Attribute that is currently in decode processprivate java.util.Map<java.lang.String,Attribute>currentFieldAttributesUsed in Multipartprivate FileUploadcurrentFileUploadThe current FileUpload that is currently in decode processprivate HttpPostRequestDecoder.MultiPartStatuscurrentStatusCurrent statusprivate HttpDataFactoryfactoryFactory used to create InterfaceHttpDataprivate booleanisLastChunkDoes the last chunk already receivedprivate intmaxBufferedBytesThe maximum number of accumulated bytes when decoding a fieldprivate intmaxFieldsThe maximum number of fields allows by the formprivate java.lang.StringmultipartDataBoundaryIf multipart, this is the boundary for the flobal multipartprivate java.lang.StringmultipartMixedBoundaryIf multipart, there could be internal multiparts (mixed) to the global multipart.private HttpRequestrequestRequest to decodeprivate ChannelBufferundecodedChunkThe current channelBuffer
-
Constructor Summary
Constructors Constructor Description HttpPostMultipartRequestDecoder(HttpRequest request)HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request)HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request, java.nio.charset.Charset charset)HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request, java.nio.charset.Charset charset, int maxFields, int maxBufferedBytes)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddHttpData(InterfaceHttpData data)Utility function to add a new decoded datavoidcleanFiles()Clean all HttpDatas (on Disk) for the current request.private voidcleanMixedAttributes()Remove all Attributes that should be cleaned between two FileUpload in Mixed modeprivate static java.lang.StringcleanString(java.lang.String field)Clean the String from any unallowed characterprivate InterfaceHttpDatadecodeMultipart(HttpPostRequestDecoder.MultiPartStatus state)Decode a multipart request by pieces
NOTSTARTED PREAMBLE (
(HEADERDELIMITER DISPOSITION (FIELD | FILEUPLOAD))*
(HEADERDELIMITER DISPOSITION MIXEDPREAMBLE
(MIXEDDELIMITER MIXEDDISPOSITION MIXEDFILEUPLOAD)+
MIXEDCLOSEDELIMITER)*
CLOSEDELIMITER)+ EPILOGUE
Inspired from HttpMessageDecoderprivate InterfaceHttpDatafindMultipartDelimiter(java.lang.String delimiter, HttpPostRequestDecoder.MultiPartStatus dispositionStatus, HttpPostRequestDecoder.MultiPartStatus closeDelimiterStatus)Find the next Multipart Delimiterprivate InterfaceHttpDatafindMultipartDisposition()Find the next DispositionInterfaceHttpDatagetBodyHttpData(java.lang.String name)This method returns the first InterfaceHttpData with the given name from body.
If chunked, all chunks must have been offered using offer() method.java.util.List<InterfaceHttpData>getBodyHttpDatas()This method returns a List of all HttpDatas from body.
If chunked, all chunks must have been offered using offer() method.java.util.List<InterfaceHttpData>getBodyHttpDatas(java.lang.String name)This method returns a List of all HttpDatas with the given name from body.
If chunked, all chunks must have been offered using offer() method.private InterfaceHttpDatagetFileUpload(java.lang.String delimiter)Get the FileUpload (new one or current one)booleanhasNext()True if at current status, there is an available decoded InterfaceHttpData from the Body.booleanisMultipart()True if this request is a Multipart requestprivate voidloadFieldMultipart(java.lang.String delimiter)Load the field value from a Multipart requestprivate voidloadFieldMultipartStandard(java.lang.String delimiter)Load the field value from a Multipart requestInterfaceHttpDatanext()Returns the next available InterfaceHttpData or null if, at the time it is called, there is no more available InterfaceHttpData.voidoffer(HttpChunk chunk)Initialized the internals from a new chunkprivate voidparseBody()This method will parse as much as possible data and fill the list and mapprivate voidparseBodyMultipart()Parse the Body for multipartprivate java.lang.StringreadDelimiter(java.lang.String delimiter)Read one line up to --delimiter or --delimiter-- and if existing the CRLF or LF.private java.lang.StringreadDelimiterStandard(java.lang.String delimiter)Read one line up to --delimiter or --delimiter-- and if existing the CRLF or LF Read one line up to --delimiter or --delimiter-- and if existing the CRLF or LF.private voidreadFileUploadByteMultipart(java.lang.String delimiter)Read a FileUpload data as Byte (Binary) and add the bytes directly to the FileUpload.private voidreadFileUploadByteMultipartStandard(java.lang.String delimiter)Read a FileUpload data as Byte (Binary) and add the bytes directly to the FileUpload.private java.lang.StringreadLine()Read one line up to the CRLF or LFprivate java.lang.StringreadLineStandard()Read one line up to the CRLF or LFvoidremoveHttpDataFromClean(InterfaceHttpData data)Remove the given FileUpload from the list of FileUploads to cleanprivate voidsetMultipart(java.lang.String contentType)Set from the request ContentType the multipartDataBoundary and the possible charset.(package private) voidskipControlCharacters()Skip control Characters(package private) voidskipControlCharactersStandard()private booleanskipOneLine()Skip one empty lineprivate static java.lang.String[]splitMultipartHeader(java.lang.String sb)Split one header in Multipartprivate static java.lang.String[]splitMultipartHeaderValues(java.lang.String svalue)Split one header value in Multipart.
-
-
-
Field Detail
-
factory
private final HttpDataFactory factory
Factory used to create InterfaceHttpData
-
request
private final HttpRequest request
Request to decode
-
maxFields
private final int maxFields
The maximum number of fields allows by the form
-
maxBufferedBytes
private final int maxBufferedBytes
The maximum number of accumulated bytes when decoding a field
-
charset
private java.nio.charset.Charset charset
Default charset to use
-
isLastChunk
private boolean isLastChunk
Does the last chunk already received
-
bodyListHttpData
private final java.util.List<InterfaceHttpData> bodyListHttpData
HttpDatas from Body
-
bodyMapHttpData
private final java.util.Map<java.lang.String,java.util.List<InterfaceHttpData>> bodyMapHttpData
HttpDatas as Map from Body
-
undecodedChunk
private ChannelBuffer undecodedChunk
The current channelBuffer
-
bodyListHttpDataRank
private int bodyListHttpDataRank
Body HttpDatas current position
-
multipartDataBoundary
private java.lang.String multipartDataBoundary
If multipart, this is the boundary for the flobal multipart
-
multipartMixedBoundary
private java.lang.String multipartMixedBoundary
If multipart, there could be internal multiparts (mixed) to the global multipart. Only one level is allowed.
-
currentStatus
private HttpPostRequestDecoder.MultiPartStatus currentStatus
Current status
-
currentFieldAttributes
private java.util.Map<java.lang.String,Attribute> currentFieldAttributes
Used in Multipart
-
currentFileUpload
private FileUpload currentFileUpload
The current FileUpload that is currently in decode process
-
currentAttribute
private Attribute currentAttribute
The current Attribute that is currently in decode process
-
-
Constructor Detail
-
HttpPostMultipartRequestDecoder
public HttpPostMultipartRequestDecoder(HttpRequest request) throws HttpPostRequestDecoder.ErrorDataDecoderException
- Parameters:
request- the request to decode- Throws:
java.lang.NullPointerException- for requestHttpPostRequestDecoder.ErrorDataDecoderException- if the default charset was wrong when decoding or other errors
-
HttpPostMultipartRequestDecoder
public HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request) throws HttpPostRequestDecoder.ErrorDataDecoderException
- Parameters:
factory- the factory used to create InterfaceHttpDatarequest- the request to decode- Throws:
java.lang.NullPointerException- for request or factoryHttpPostRequestDecoder.ErrorDataDecoderException- if the default charset was wrong when decoding or other errors
-
HttpPostMultipartRequestDecoder
public HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request, java.nio.charset.Charset charset) throws HttpPostRequestDecoder.ErrorDataDecoderException
- Parameters:
factory- the factory used to create InterfaceHttpDatarequest- the request to decodecharset- the charset to use as default- Throws:
java.lang.NullPointerException- for request or charset or factoryHttpPostRequestDecoder.ErrorDataDecoderException- if the default charset was wrong when decoding or other errors
-
HttpPostMultipartRequestDecoder
public HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request, java.nio.charset.Charset charset, int maxFields, int maxBufferedBytes) throws HttpPostRequestDecoder.ErrorDataDecoderException
- Parameters:
factory- the factory used to create InterfaceHttpDatarequest- the request to decodecharset- the charset to use as defaultmaxFields- the maximum number of fields the form can have,-1to disablemaxBufferedBytes- the maximum number of bytes the decoder can buffer when decoding a field,-1to disable- Throws:
java.lang.NullPointerException- for request or charset or factoryHttpPostRequestDecoder.ErrorDataDecoderException- if the default charset was wrong when decoding or other errors
-
-
Method Detail
-
setMultipart
private void setMultipart(java.lang.String contentType) throws HttpPostRequestDecoder.ErrorDataDecoderExceptionSet from the request ContentType the multipartDataBoundary and the possible charset.
-
isMultipart
public boolean isMultipart()
Description copied from interface:InterfaceHttpPostRequestDecoderTrue if this request is a Multipart request- Specified by:
isMultipartin interfaceInterfaceHttpPostRequestDecoder- Returns:
- True if this request is a Multipart request
-
getBodyHttpDatas
public java.util.List<InterfaceHttpData> getBodyHttpDatas() throws HttpPostRequestDecoder.NotEnoughDataDecoderException
Description copied from interface:InterfaceHttpPostRequestDecoderThis method returns a List of all HttpDatas from body.
If chunked, all chunks must have been offered using offer() method. If not, NotEnoughDataDecoderException will be raised.- Specified by:
getBodyHttpDatasin interfaceInterfaceHttpPostRequestDecoder- Returns:
- the list of HttpDatas from Body part for POST method
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks
-
getBodyHttpDatas
public java.util.List<InterfaceHttpData> getBodyHttpDatas(java.lang.String name) throws HttpPostRequestDecoder.NotEnoughDataDecoderException
Description copied from interface:InterfaceHttpPostRequestDecoderThis method returns a List of all HttpDatas with the given name from body.
If chunked, all chunks must have been offered using offer() method. If not, NotEnoughDataDecoderException will be raised.- Specified by:
getBodyHttpDatasin interfaceInterfaceHttpPostRequestDecoder- Returns:
- All Body HttpDatas with the given name (ignore case)
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- need more chunks
-
getBodyHttpData
public InterfaceHttpData getBodyHttpData(java.lang.String name) throws HttpPostRequestDecoder.NotEnoughDataDecoderException
Description copied from interface:InterfaceHttpPostRequestDecoderThis method returns the first InterfaceHttpData with the given name from body.
If chunked, all chunks must have been offered using offer() method. If not, NotEnoughDataDecoderException will be raised.- Specified by:
getBodyHttpDatain interfaceInterfaceHttpPostRequestDecoder- Returns:
- The first Body InterfaceHttpData with the given name (ignore case)
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- need more chunks
-
offer
public void offer(HttpChunk chunk) throws HttpPostRequestDecoder.ErrorDataDecoderException
Description copied from interface:InterfaceHttpPostRequestDecoderInitialized the internals from a new chunk- Specified by:
offerin interfaceInterfaceHttpPostRequestDecoder- Parameters:
chunk- the new received chunk- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException- if there is a problem with the charset decoding or other errors
-
hasNext
public boolean hasNext() throws HttpPostRequestDecoder.EndOfDataDecoderExceptionDescription copied from interface:InterfaceHttpPostRequestDecoderTrue if at current status, there is an available decoded InterfaceHttpData from the Body. This method works for chunked and not chunked request.- Specified by:
hasNextin interfaceInterfaceHttpPostRequestDecoder- Returns:
- True if at current status, there is a decoded InterfaceHttpData
- Throws:
HttpPostRequestDecoder.EndOfDataDecoderException- No more data will be available
-
next
public InterfaceHttpData next() throws HttpPostRequestDecoder.EndOfDataDecoderException
Description copied from interface:InterfaceHttpPostRequestDecoderReturns the next available InterfaceHttpData or null if, at the time it is called, there is no more available InterfaceHttpData. A subsequent call to offer(httpChunk) could enable more data.- Specified by:
nextin interfaceInterfaceHttpPostRequestDecoder- Returns:
- the next available InterfaceHttpData or null if none
- Throws:
HttpPostRequestDecoder.EndOfDataDecoderException- No more data will be available
-
parseBody
private void parseBody() throws HttpPostRequestDecoder.ErrorDataDecoderExceptionThis method will parse as much as possible data and fill the list and map- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException- if there is a problem with the charset decoding or other errors
-
addHttpData
private void addHttpData(InterfaceHttpData data) throws HttpPostRequestDecoder.ErrorDataDecoderException
Utility function to add a new decoded data
-
parseBodyMultipart
private void parseBodyMultipart() throws HttpPostRequestDecoder.ErrorDataDecoderExceptionParse the Body for multipart- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException- if there is a problem with the charset decoding or other errors
-
decodeMultipart
private InterfaceHttpData decodeMultipart(HttpPostRequestDecoder.MultiPartStatus state) throws HttpPostRequestDecoder.ErrorDataDecoderException
Decode a multipart request by pieces
NOTSTARTED PREAMBLE (
(HEADERDELIMITER DISPOSITION (FIELD | FILEUPLOAD))*
(HEADERDELIMITER DISPOSITION MIXEDPREAMBLE
(MIXEDDELIMITER MIXEDDISPOSITION MIXEDFILEUPLOAD)+
MIXEDCLOSEDELIMITER)*
CLOSEDELIMITER)+ EPILOGUE
Inspired from HttpMessageDecoder- Returns:
- the next decoded InterfaceHttpData or null if none until now.
- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException- if an error occurs
-
skipControlCharacters
void skipControlCharacters() throws HttpPostRequestDecoder.NotEnoughDataDecoderExceptionSkip control Characters
-
skipControlCharactersStandard
void skipControlCharactersStandard()
-
findMultipartDelimiter
private InterfaceHttpData findMultipartDelimiter(java.lang.String delimiter, HttpPostRequestDecoder.MultiPartStatus dispositionStatus, HttpPostRequestDecoder.MultiPartStatus closeDelimiterStatus) throws HttpPostRequestDecoder.ErrorDataDecoderException
Find the next Multipart Delimiter- Parameters:
delimiter- delimiter to finddispositionStatus- the next status if the delimiter is a startcloseDelimiterStatus- the next status if the delimiter is a close delimiter- Returns:
- the next InterfaceHttpData if any
- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException
-
findMultipartDisposition
private InterfaceHttpData findMultipartDisposition() throws HttpPostRequestDecoder.ErrorDataDecoderException
Find the next Disposition- Returns:
- the next InterfaceHttpData if any
- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException
-
getFileUpload
private InterfaceHttpData getFileUpload(java.lang.String delimiter) throws HttpPostRequestDecoder.ErrorDataDecoderException
Get the FileUpload (new one or current one)- Parameters:
delimiter- the delimiter to use- Returns:
- the InterfaceHttpData if any
- Throws:
HttpPostRequestDecoder.ErrorDataDecoderException
-
cleanFiles
public void cleanFiles()
Description copied from interface:InterfaceHttpPostRequestDecoderClean all HttpDatas (on Disk) for the current request.- Specified by:
cleanFilesin interfaceInterfaceHttpPostRequestDecoder
-
removeHttpDataFromClean
public void removeHttpDataFromClean(InterfaceHttpData data)
Description copied from interface:InterfaceHttpPostRequestDecoderRemove the given FileUpload from the list of FileUploads to clean- Specified by:
removeHttpDataFromCleanin interfaceInterfaceHttpPostRequestDecoder
-
cleanMixedAttributes
private void cleanMixedAttributes()
Remove all Attributes that should be cleaned between two FileUpload in Mixed mode
-
readLineStandard
private java.lang.String readLineStandard() throws HttpPostRequestDecoder.NotEnoughDataDecoderExceptionRead one line up to the CRLF or LF- Returns:
- the String from one line
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks and reset the readerInder to the previous value
-
readLine
private java.lang.String readLine() throws HttpPostRequestDecoder.NotEnoughDataDecoderExceptionRead one line up to the CRLF or LF- Returns:
- the String from one line
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks and reset the readerInder to the previous value
-
readDelimiterStandard
private java.lang.String readDelimiterStandard(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderExceptionRead one line up to --delimiter or --delimiter-- and if existing the CRLF or LF Read one line up to --delimiter or --delimiter-- and if existing the CRLF or LF. Note that CRLF or LF are mandatory for opening delimiter (--delimiter) but not for closing delimiter (--delimiter--) since some clients does not include CRLF in this case.- Parameters:
delimiter- of the form --string, such that '--' is already included- Returns:
- the String from one line as the delimiter searched (opening or closing)
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks and reset the readerInder to the previous value
-
readDelimiter
private java.lang.String readDelimiter(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderExceptionRead one line up to --delimiter or --delimiter-- and if existing the CRLF or LF. Note that CRLF or LF are mandatory for opening delimiter (--delimiter) but not for closing delimiter (--delimiter--) since some clients does not include CRLF in this case.- Parameters:
delimiter- of the form --string, such that '--' is already included- Returns:
- the String from one line as the delimiter searched (opening or closing)
- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks and reset the readerInder to the previous value
-
readFileUploadByteMultipartStandard
private void readFileUploadByteMultipartStandard(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderException, HttpPostRequestDecoder.ErrorDataDecoderExceptionRead a FileUpload data as Byte (Binary) and add the bytes directly to the FileUpload. If the delimiter is found, the FileUpload is completed.- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks but do not reset the readerInder since some values will be already added to the FileOutputHttpPostRequestDecoder.ErrorDataDecoderException- write IO error occurs with the FileUpload
-
readFileUploadByteMultipart
private void readFileUploadByteMultipart(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderException, HttpPostRequestDecoder.ErrorDataDecoderExceptionRead a FileUpload data as Byte (Binary) and add the bytes directly to the FileUpload. If the delimiter is found, the FileUpload is completed.- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunks but do not reset the readerInder since some values will be already added to the FileOutputHttpPostRequestDecoder.ErrorDataDecoderException- write IO error occurs with the FileUpload
-
loadFieldMultipartStandard
private void loadFieldMultipartStandard(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderException, HttpPostRequestDecoder.ErrorDataDecoderExceptionLoad the field value from a Multipart request- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunksHttpPostRequestDecoder.ErrorDataDecoderException
-
loadFieldMultipart
private void loadFieldMultipart(java.lang.String delimiter) throws HttpPostRequestDecoder.NotEnoughDataDecoderException, HttpPostRequestDecoder.ErrorDataDecoderExceptionLoad the field value from a Multipart request- Throws:
HttpPostRequestDecoder.NotEnoughDataDecoderException- Need more chunksHttpPostRequestDecoder.ErrorDataDecoderException
-
cleanString
private static java.lang.String cleanString(java.lang.String field)
Clean the String from any unallowed character- Returns:
- the cleaned String
-
skipOneLine
private boolean skipOneLine()
Skip one empty line- Returns:
- True if one empty line was skipped
-
splitMultipartHeader
private static java.lang.String[] splitMultipartHeader(java.lang.String sb)
Split one header in Multipart- Returns:
- an array of String where rank 0 is the name of the header, follows by several values that were separated by ';' or ','
-
splitMultipartHeaderValues
private static java.lang.String[] splitMultipartHeaderValues(java.lang.String svalue)
Split one header value in Multipart.- Returns:
- an array of
Stringwhere values that were separated by ';' or ','
-
-