Class UploadService

java.lang.Object
org.exoplatform.upload.UploadService

public class UploadService extends Object
  • Field Details

    • UPLOAD_TOKEN_MULTIPART_ALLOWANCE

      public static final long UPLOAD_TOKEN_MULTIPART_ALLOWANCE
      Bytes 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_SECONDS
      Validity of an upload token when the service configuration sets none.
      See Also:
    • UPLOAD_TOKEN_INVALID

      public static final String UPLOAD_TOKEN_INVALID
      Message of a refused, expired, already used or mismatched upload token.
      See Also:
    • UPLOAD_ID_ALREADY_IN_USE

      public static final String UPLOAD_ID_ALREADY_IN_USE
      Message of an upload id that is already registered.
      See Also:
    • UPLOAD_RESOURCES_STACK

      public static final String 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, see resolveUploadLocation(InitParams, String)) and upload.token.validity.seconds.
      Parameters:
      pinfo - the portal container, whose name separates the staging folders of several containers
      params - the service parameters, may be null
      Throws:
      Exception - when a numeric parameter is not a number
  • Method Details

    • register

      public void register(MimeTypeUploadPlugin plugin)
    • createUploadResource

      public void createUploadResource(jakarta.servlet.http.HttpServletRequest request) throws org.apache.commons.fileupload2.core.FileUploadException
      Create UploadResource for HttpServletRequest
      Parameters:
      request - the webapp's HttpServletRequest
      Throws:
      org.apache.commons.fileupload2.core.FileUploadException
    • createUploadResource

      public void createUploadResource(UploadResource uploadResource)
      Registers a resource built server side, as given: no UploadFileValidator runs, 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 upload the request carries under the given id and reads its content. An id registered by an authenticated request belongs to the user who registered it, request.getRemoteUser(): the same user may register it again from any session (a retry of a failed upload), another user is refused, and so is any request on an id that is registered without an owner (a server-side registration, or an anonymous request's). The decision and the registration are one atomic step of the registry.
      Parameters:
      uploadId - the client-chosen upload id
      request - the upload request
      Throws:
      org.apache.commons.fileupload2.core.FileUploadException - when the id is malformed, is held by another user or is a registered owner-less id
    • createUploadToken

      public String 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. 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 owner
      uploadId - 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 a Content-Length within the token's size plus UPLOAD_TOKEN_MULTIPART_ALLOWANCE, and hold a file no larger than the token's size. The file goes through the same parsing and UploadFileValidators 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 an ObjectAlreadyExistsException. Any refusal after registration removes this upload's resource only.
      Parameters:
      token - the token value, as issued by createUploadToken(String, String, long)
      uploadId - the upload id the request names
      request - 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 id
      org.exoplatform.commons.ObjectAlreadyExistsException - when the upload id is already registered
      org.apache.commons.fileupload2.core.FileUploadSizeException - when the request has no Content-Length (actual size -1) or exceeds the token's size
      org.apache.commons.fileupload2.core.FileUploadException - when the request is not one multipart file, is incomplete, or a validator refuses the file
    • createUploadResource

      public void createUploadResource(String uploadId, String encoding, String contentType, double contentLength, InputStream inputStream) throws Exception
      Throws:
      Exception
    • getUploadResource

      public UploadResource getUploadResource(String uploadId)
      Get UploadResource by uploadId. Answers any caller, whoever uploaded the resource: a consumer acting for a user prefers getUploadResource(String, String).
      Parameters:
      uploadId - uploadId of UploadResource
      Returns:
      org.exoplatform.upload.UploadResource of uploadId
    • getUploadResource

      public UploadResource getUploadResource(String uploadId, String username)
      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 in UploadResource.UPLOADING_STATUS, without its store location: the parser marks it uploaded before its file is written and validated, and the copy cannot be removed through removeUploadResource(String, String).
      Parameters:
      uploadId - upload id
      username - 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 public void removeUpload(String uploadId)
      Deprecated.
      Parameters:
      uploadId -
    • removeUploadResource

      public void removeUploadResource(String uploadId)
      Remove the UploadResource and its temporary file that associated with given uploadId.
      If uploadId is null or UploadResource is null, do nothing. Removes for any caller, whoever uploaded the resource: a consumer acting for a user prefers removeUploadResource(String, String).
      Parameters:
      uploadId - uploadId of UploadResource will be removed
    • removeUploadResource

      public void removeUploadResource(String uploadId, jakarta.servlet.http.HttpServletRequest request) throws org.apache.commons.fileupload2.core.FileUploadException
      Removes the resource as the request's user: an id held by another user is refused, an id the user holds or an owner-less id is removed, with its temporary file. The decision and the removal are one atomic step of the registry.
      Parameters:
      uploadId - the upload id, null for nothing to remove
      request - the delete or abort request
      Throws:
      org.apache.commons.fileupload2.core.FileUploadException - when the id is held by another user
    • removeUploadResource

      public boolean removeUploadResource(String uploadId, String username)
      Removes the resource registered under the upload id, and its temporary file, only when the given user owns it.
      Parameters:
      uploadId - upload id
      username - 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

      public void addUploadLimit(String uploadId, Integer limitMB)
      Registry upload limit size for uploadLimitsMB_. If limitMB is null, defaultUploadLimitMB_ will be registried
      Parameters:
      uploadId -
      limitMB - upload limit size
    • addUploadLimit

      public void addUploadLimit(String uploadId, Integer limit, UploadService.UploadUnit unit)
    • removeUploadLimit

      public void removeUploadLimit(String uploadId)
    • getUploadLimits

      public Map<String, UploadService.UploadLimit> 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

      public String correctFileName(String fileName)
    • isLimited

      public boolean isLimited(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.
      Parameters:
      upResource - the resource being uploaded
      contentLength - the request's length in bytes
      Returns:
      true when the length exceeds the limit
    • getLimitForResource

      public UploadService.UploadLimit 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.
      Parameters:
      upResource - the resource
      Returns:
      the applicable limit