public class MultipartParser extends java.lang.Object implements RequestParser
multipart/form-data
in an HttpServletRequest
.
The created file(s) is(are) stored in the temporary upload directory
(UWSFileManager.getTmpDirectory()
. This directory is supposed to be
emptied regularly in case it is forgotten at any moment by the service
implementation to delete unused request files.
The size of the full request body as well as the size of a single uploaded file are both limited. To get and/or change these limits, use the functions:
getMaxFileSize()
and
setMaxFileSize(long)
getMaxRequestSize()
and
setMaxRequestSize(long)
By default, the limit for an uploaded file is -1 (i.e. unlimited) and the limit for the whole request (i.e. all uploaded files together + HTTP header) is -1 (i.e. unlimited}).
By default, this RequestParser
overwrite parameter occurrences in
the map: that's to say if a parameter is provided several times, only the
last value will be kept. This behaviour can be changed by overwriting the
function consumeParameter(String, Object, Map)
of this class.
Modifier and Type | Field and Description |
---|---|
boolean |
allowUpload
Indicates whether this parser should allow inline files or not.
|
static int |
DEFAULT_FILE_SIZE_LIMIT
Default maximum allowed size for a single uploaded file:
-1 (i.e.
|
static int |
DEFAULT_SIZE_LIMIT
Default maximum allowed size for an HTTP request content:
-1 (i.e.
|
static java.lang.String |
EXPECTED_CONTENT_TYPE
HTTP content-type for HTTP request formated in multipart.
|
protected UWSFileManager |
fileManager
File manager to use to create
UploadFile instances. |
protected org.apache.commons.fileupload.servlet.ServletFileUpload |
fileUpload
Tool to parse Multipart HTTP request and fetch files when necessary.
|
protected static int |
SIZE_BEFORE_DISK_STORAGE
Size threshold (in bytes) for an individual file before being stored on
disk.
|
static int |
SIZE_LIMIT
Deprecated.
Since 4.4 ; barely used and never worked (with COS).
|
Modifier | Constructor and Description |
---|---|
|
MultipartParser()
Build a
MultipartParser forbidding uploads (i.e. |
protected |
MultipartParser(boolean uploadEnabled,
UWSFileManager fileManager)
Build a
MultipartParser . |
protected |
MultipartParser(boolean uploadEnabled,
UWSFileManager fileManager,
long maxFileSize,
long maxRequestSize)
Build a
MultipartParser . |
|
MultipartParser(UWSFileManager fileManager)
Build a
MultipartParser allowing uploads (i.e. |
Modifier and Type | Method and Description |
---|---|
protected void |
consumeParameter(java.lang.String name,
java.lang.Object value,
java.util.Map<java.lang.String,java.lang.Object> allParams)
Consume the specified parameter: add it inside the given map.
|
protected java.io.File |
getFileFromParam(javax.servlet.http.HttpServletRequest request,
java.lang.String parentFile,
java.lang.String inputFileName)
Return the path of a non-existing file inside the given directory and
whose the name is built using the given file name and the HTTP request
ID.
|
long |
getMaxFileSize()
Get the maximum size (in bytes) of a single uploaded file.
|
long |
getMaxRequestSize()
Get the maximum size (in bytes) of a whole multipart request.
|
static boolean |
isMultipartContent(javax.servlet.http.HttpServletRequest request)
Utility method that determines whether the content of the given request
is a multipart/form-data.
|
java.util.Map<java.lang.String,java.lang.Object> |
parse(javax.servlet.http.HttpServletRequest request)
Extract parameters from the given HTTP request.
|
void |
setMaxFileSize(long maxFileSize)
Set the maximum size (in bytes) of a single uploaded file.
|
void |
setMaxRequestSize(long maxRequestSize)
Set the maximum size (in bytes) of a whole multipart request.
|
public static final java.lang.String EXPECTED_CONTENT_TYPE
public static final int DEFAULT_FILE_SIZE_LIMIT
public static final int DEFAULT_SIZE_LIMIT
protected static final int SIZE_BEFORE_DISK_STORAGE
Note: By default, set to 10 kiB.
@Deprecated public static int SIZE_LIMIT
Note 1:
The default value is DEFAULT_SIZE_LIMIT
(= -1 MiB).
Note 2:
This limit is expressed in bytes and can not be negative.
Its smallest possible value is 0. If the set value is though negative,
it will be ignored and DEFAULT_SIZE_LIMIT
will be used instead.
public final boolean allowUpload
protected final UWSFileManager fileManager
UploadFile
instances.
It is required by this new object to execute open, move and delete
operations whenever it could be asked.protected final org.apache.commons.fileupload.servlet.ServletFileUpload fileUpload
public MultipartParser()
MultipartParser
forbidding uploads (i.e. inline files).
With this parser, when an upload (i.e. submitted inline files) is
detected, an exception is thrown by parse(HttpServletRequest)
which cancels immediately the request.
public MultipartParser(UWSFileManager fileManager)
MultipartParser
allowing uploads (i.e. inline files).fileManager
- The file manager to use in order to store any
eventual upload. MUST NOT be NULLprotected MultipartParser(boolean uploadEnabled, UWSFileManager fileManager)
MultipartParser
.
If the first parameter is false, then when an upload
(i.e. submitted inline files) is detected, an exception is thrown
by parse(HttpServletRequest)
which cancels immediately the
request.
uploadEnabled
- true to allow uploads (i.e. inline files),
false otherwise. If false, the two
other parameters are useless.fileManager
- The file manager to use in order to store any
eventual upload. MUST NOT be NULLprotected MultipartParser(boolean uploadEnabled, UWSFileManager fileManager, long maxFileSize, long maxRequestSize)
MultipartParser
.
If the first parameter is false, then when an upload
(i.e. submitted inline files) is detected, an exception is thrown
by parse(HttpServletRequest)
which cancels immediately the
request.
uploadEnabled
- true to allow uploads (i.e. inline files),
false otherwise. If false, the two
other parameters are useless.fileManager
- The file manager to use in order to store any
eventual upload. MUST NOT be NULLmaxFileSize
- Maximum size of a single upload file (in bytes).
A negative value for "unlimited".maxRequestSize
- Maximum size of a whole multipart request (in
bytes).
A negative value for "unlimited".public final long getMaxFileSize()
public void setMaxFileSize(long maxFileSize)
maxFileSize
- New maximum upload file size (in bytes).
If -1
, then there will be no limit.public final long getMaxRequestSize()
public void setMaxRequestSize(long maxRequestSize)
maxRequestSize
- New maximum multipart request size (in bytes).
If -1
, then there will be no limit.public final java.util.Map<java.lang.String,java.lang.Object> parse(javax.servlet.http.HttpServletRequest request) throws UWSException
RequestParser
Extract parameters from the given HTTP request.
These parameters can be fetched from ServletRequest.getParameterMap()
or directly from the full request content. In this last case, a parsing is necessary ;
hence this function.
In case a parameter is provided several times with the same time and the same case,
the request parser can choose to keep only the last occurrence or all occurrences.
If all occurrences are kept, this function MUST return an array of Object
s
(in which types may be mixed), otherwise a map value MUST be an elementary object.
Note: A parameter item can be a simple value (e.g. String, integer, ...) or a more complex object (e.g. File, InputStream, ...).
IMPORTANT:
This function MUST NOT be used to check the parameters' value.
It only aims to parse the given request in order to extract its embedded parameters.
Consequently, if this function throws an exception, it could be only because the request
can not be read, and not because a parameter format or value is incorrect.
Parameter checks should be done in UWSParameters
and more particularly by
an InputParamController
.
parse
in interface RequestParser
request
- An HTTP request.Object
s (in which types can be mixed).UWSException
- If any error provides this function to read the parameters.protected java.io.File getFileFromParam(javax.servlet.http.HttpServletRequest request, java.lang.String parentFile, java.lang.String inputFileName)
request
- The received HTTP request.parentFile
- The directory in which the file should be created.inputFileName
- The file name provided by the user.protected void consumeParameter(java.lang.String name, java.lang.Object value, java.util.Map<java.lang.String,java.lang.Object> allParams)
By default, this function is just putting the given value inside the map. So, if the parameter already exists in the map, its old value will be overwritten by the given one.
Note: If the old value was a file, it will be deleted from the file system before its replacement in the map.
name
- Name of the parameter to consume.value
- Its value.allParams
- The list of all parameters read until now.public static final boolean isMultipartContent(javax.servlet.http.HttpServletRequest request)
Important: This function just test the content-type of the request. The HTTP method (e.g. GET, POST, ...) is not tested.
request
- The servlet request to be evaluated. Must be non-null.true
if the request is multipart,
false
otherwise.