Class UploadService
java.lang.Object
org.exoplatform.upload.UploadService
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enum -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longValidity of an upload token when the service configuration sets none.static final StringMessage of an upload id that is already registered.static final Stringstatic final StringMessage of a refused, expired, already used or mismatched upload token.static final longBytes a token upload's request body may carry above the file size the token declares: the multipart boundaries and part headers. -
Constructor Summary
ConstructorsConstructorDescriptionUploadService(org.exoplatform.container.xml.PortalContainerInfo pinfo, org.exoplatform.container.xml.InitParams params) Builds the service from its parameters:upload.limit.size(default limit in MB, 0 or absent for unlimited),upload.location(staging root, seeresolveUploadLocation(InitParams, String)) andupload.token.validity.seconds. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddUploadLimit(String uploadId, Integer limitMB) Registry upload limit size for uploadLimitsMB_.voidaddUploadLimit(String uploadId, Integer limit, UploadService.UploadUnit unit) voidcleanUp(jakarta.servlet.http.HttpSession session) Clean up temporary files that are uploaded in the Session but not removed yetcorrectFileName(String fileName) voidcreateUploadResource(jakarta.servlet.http.HttpServletRequest request) Create UploadResource for HttpServletRequestvoidcreateUploadResource(String uploadId, jakarta.servlet.http.HttpServletRequest request) Registers the file of a multipart request under the given upload id, owned by the request's authenticated user (HttpServletRequest.getRemoteUser(), null for an anonymous request).voidcreateUploadResource(String uploadId, String encoding, String contentType, double contentLength, InputStream inputStream) Deprecated.voidcreateUploadResource(UploadResource uploadResource) Registers a resource built server side, as given: noUploadFileValidatorruns, and the resource's owner is whatever the caller set on it (UploadResource.setOwner(String)).createUploadResourceWithToken(String token, String uploadId, jakarta.servlet.http.HttpServletRequest request) Registers the file of a multipart request authorized by a one-time upload token instead of a user session.createUploadToken(String username, String uploadId, long maxSize) Issues a one-time token that lets a request without a user session upload one file through the upload handler, as the given user.getLimitForResource(UploadResource upResource) Returns the limit that applies to the resource's upload: its own limit when one was set, else the service's default.Get all upload limit sizes.getUploadResource(String uploadId) Get UploadResource by uploadId.getUploadResource(String uploadId, String username) Returns the resource registered under the upload id only when the given user owns it.booleanisLimited(UploadResource upResource, double contentLength) Tells whether a request of the given length exceeds the limit of the resource's upload: its own limit when one was set, else the service's default; a limit of 0 means unlimited.voidregister(MimeTypeUploadPlugin plugin) voidremoveUpload(String uploadId) Deprecated.useremoveUploadResource(String)insteadvoidremoveUploadLimit(String uploadId) voidremoveUploadResource(String uploadId) Remove the UploadResource and its temporary file that associated with givenuploadId.booleanremoveUploadResource(String uploadId, String username) Removes the resource registered under the upload id, and its temporary file, only when the given user owns it.
-
Field Details
-
UPLOAD_TOKEN_MULTIPART_ALLOWANCE
public static final long UPLOAD_TOKEN_MULTIPART_ALLOWANCEBytes a token upload's request body may carry above the file size the token declares: the multipart boundaries and part headers.- See Also:
-
DEFAULT_UPLOAD_TOKEN_VALIDITY_SECONDS
public static final long DEFAULT_UPLOAD_TOKEN_VALIDITY_SECONDSValidity of an upload token when the service configuration sets none.- See Also:
-
UPLOAD_TOKEN_INVALID
Message of a refused, expired, already used or mismatched upload token.- See Also:
-
UPLOAD_ID_ALREADY_IN_USE
Message of an upload id that is already registered.- See Also:
-
UPLOAD_RESOURCES_STACK
- See Also:
-
-
Constructor Details
-
UploadService
public UploadService(org.exoplatform.container.xml.PortalContainerInfo pinfo, org.exoplatform.container.xml.InitParams params) throws Exception Builds the service from its parameters:upload.limit.size(default limit in MB, 0 or absent for unlimited),upload.location(staging root, seeresolveUploadLocation(InitParams, String)) andupload.token.validity.seconds.- Parameters:
pinfo- the portal container, whose name separates the staging folders of several containersparams- the service parameters, may be null- Throws:
Exception- when a numeric parameter is not a number
-
-
Method Details
-
register
-
createUploadResource
public void createUploadResource(jakarta.servlet.http.HttpServletRequest request) throws org.apache.commons.fileupload2.core.FileUploadException Create UploadResource for HttpServletRequest- Parameters:
request- the webapp'sHttpServletRequest- Throws:
org.apache.commons.fileupload2.core.FileUploadException
-
createUploadResource
Registers a resource built server side, as given: noUploadFileValidatorruns, and the resource's owner is whatever the caller set on it (UploadResource.setOwner(String)).- Parameters:
uploadResource- the resource to register under its upload id
-
createUploadResource
public void createUploadResource(String uploadId, jakarta.servlet.http.HttpServletRequest request) throws org.apache.commons.fileupload2.core.FileUploadException Registers the file of a multipart request under the given upload id, owned by the request's authenticated user (HttpServletRequest.getRemoteUser(), null for an anonymous request).- Parameters:
uploadId- upload idrequest- the request carrying one multipart file- Throws:
org.apache.commons.fileupload2.core.FileUploadException- when the id is invalid, the request carries other than one file, or a validator refuses the file
-
createUploadToken
Issues a one-time token that lets a request without a user session upload one file through the upload handler, as the given user. The token is returned once and never stored: the service keeps its SHA-256 digest with the user, the upload id, the maximum size and the expiry. It is consumed by its first presentation, whatever the outcome. The token's size replaces the platform default upload limit for its upload id, so the caller caps it.- Parameters:
username- user the upload is made for, recorded as the resource's owneruploadId- the only upload id the token can register; the caller generates it unguessable (a random UUID)maxSize- maximum size of the uploaded file, in bytes- Returns:
- the token value, to send as
Authorization: Bearer <token> - Throws:
IllegalArgumentException- when the username is blank, the upload id is invalid or already registered, or the size is not positive
-
createUploadResourceWithToken
public UploadResource createUploadResourceWithToken(String token, String uploadId, jakarta.servlet.http.HttpServletRequest request) throws IllegalAccessException, org.exoplatform.commons.ObjectAlreadyExistsException, org.apache.commons.fileupload2.core.FileUploadException Registers the file of a multipart request authorized by a one-time upload token instead of a user session. The token is consumed first, whatever the outcome; the request must then name the token's upload id, carry aContent-Lengthwithin the token's size plusUPLOAD_TOKEN_MULTIPART_ALLOWANCE, and hold a file no larger than the token's size. The file goes through the same parsing andUploadFileValidators as a browser upload, is owned by the token's user, and is tied to the request's HTTP session for cleanup. The upload id is reserved atomically: of two concurrent token uploads of one id, exactly one registers it and the other gets anObjectAlreadyExistsException. Any refusal after registration removes this upload's resource only.- Parameters:
token- the token value, as issued bycreateUploadToken(String, String, long)uploadId- the upload id the request namesrequest- the request carrying one multipart file- Returns:
- the registered resource, in
UploadResource.UPLOADED_STATUS - Throws:
IllegalAccessException- when the token is unknown, expired, already used, or issued for another upload idorg.exoplatform.commons.ObjectAlreadyExistsException- when the upload id is already registeredorg.apache.commons.fileupload2.core.FileUploadSizeException- when the request has noContent-Length(actual size -1) or exceeds the token's sizeorg.apache.commons.fileupload2.core.FileUploadException- when the request is not one multipart file, is incomplete, or a validator refuses the file
-
createUploadResource
-
getUploadResource
Get UploadResource by uploadId. Answers any caller, whoever uploaded the resource: a consumer acting for a user prefersgetUploadResource(String, String).- Parameters:
uploadId- uploadId of UploadResource- Returns:
- org.exoplatform.upload.UploadResource of uploadId
-
getUploadResource
Returns the resource registered under the upload id only when the given user owns it. A token upload still being written or validated is returned as a detached copy inUploadResource.UPLOADING_STATUS, without its store location: the parser marks it uploaded before its file is written and validated, and the copy cannot be removed throughremoveUploadResource(String, String).- Parameters:
uploadId- upload idusername- the user the caller acts for- Returns:
- the resource, or null when the id is unknown, the resource has no owner, or another user owns it
-
cleanUp
public void cleanUp(jakarta.servlet.http.HttpSession session) Clean up temporary files that are uploaded in the Session but not removed yet- Parameters:
session-
-
removeUpload
Deprecated.useremoveUploadResource(String)instead- Parameters:
uploadId-
-
removeUploadResource
Remove the UploadResource and its temporary file that associated with givenuploadId.
IfuploadIdis null or UploadResource is null, do nothing. Removes for any caller, whoever uploaded the resource: a consumer acting for a user prefersremoveUploadResource(String, String).- Parameters:
uploadId- uploadId of UploadResource will be removed
-
removeUploadResource
Removes the resource registered under the upload id, and its temporary file, only when the given user owns it.- Parameters:
uploadId- upload idusername- the user the caller acts for- Returns:
- true when the resource was removed; false when the id is unknown, the resource has no owner, or another user owns it
-
addUploadLimit
-
addUploadLimit
-
removeUploadLimit
-
getUploadLimits
Get all upload limit sizes. The map is synchronized: a caller iterating over it holds its monitor (synchronized (map)).- Returns:
- all upload limit sizes
-
correctFileName
-
isLimited
Tells whether a request of the given length exceeds the limit of the resource's upload: its own limit when one was set, else the service's default; a limit of 0 means unlimited.- Parameters:
upResource- the resource being uploadedcontentLength- the request's length in bytes- Returns:
- true when the length exceeds the limit
-
getLimitForResource
Returns the limit that applies to the resource's upload: its own limit when one was set, else the service's default.- Parameters:
upResource- the resource- Returns:
- the applicable limit
-
createUploadResource(String, jakarta.servlet.http.HttpServletRequest)instead