Class ProxyStorageProvider

java.lang.Object
com.persequor.saga.modules.dynamicfiles.proxy.ProxyStorageProvider
All Implemented Interfaces:
IAssignableStorageProvider<ProxyStorageContext>, IProxyStorageMarker, IStorageProvider<ProxyStorageContext>

@Singleton public class ProxyStorageProvider extends Object implements IAssignableStorageProvider<ProxyStorageContext>, IProxyStorageMarker
  • Constructor Details

  • Method Details

    • assign

      public void assign(@Nonnull ProxyStorageContext assignableContext, @Nonnull UUID uuid, @Nonnull String path) throws PathAlreadyBoundException, InvalidPathException
      Description copied from interface: IAssignableStorageProvider
      Assigns a path to a UUID.

      This is useful for when a link needs to be human-readable or similar.

      When assigning a path to a UUID using this method the storage provider is expected to respect the desired path, and if it is not possible to store the file at the desired path then an exception should be thrown.

      The path specified by the user will generally be affixed to a base path that is determined by the storage provider.

      If the path provided does not contain a file extension then the storage provider may add a file extension

      If the path provided contains any directory traversal characters (e.g. "..") then the storage provider should throw an InvalidPathException.

      If the path provided is already bound to a file then the storage provider should throw a PathAlreadyBoundException.

      If the path provided is invalid for any other reason then the storage provider should throw an InvalidPathException.

      Specified by:
      assign in interface IAssignableStorageProvider<ProxyStorageContext>
      Parameters:
      assignableContext - The storage context in which the file should be stored, containing any configuration information
      uuid - The unique identifier of the file to assign a path to
      path - The path to assign to the file, or null to remove the path
      Throws:
      PathAlreadyBoundException - if the desired path is already bound to a file
      InvalidPathException - if the path is invalid for other reasons
    • unassign

      public boolean unassign(@Nonnull ProxyStorageContext assignableContext, @Nonnull UUID uuid)
      Description copied from interface: IAssignableStorageProvider
      Unassigns a path from a UUID.

      When unassigning a path from a UUID using this method the storage provider is expected to remove the path from the UUID, but it is not expected to delete the file.

      If the path was not assigned then this method should return false.

      Specified by:
      unassign in interface IAssignableStorageProvider<ProxyStorageContext>
      Parameters:
      assignableContext - The storage context in which the file should be stored, containing any configuration information
      uuid - The unique identifier of the file to unassign a path from
      Returns:
      true if the path was unassigned, false if the path was not assigned or could not be unassigned
    • hasPathAssigned

      public boolean hasPathAssigned(@Nonnull ProxyStorageContext assignableContext, @Nonnull UUID uuid)
      Description copied from interface: IAssignableStorageProvider
      Checks if a path is assigned to a UUID.

      While this is specifically available for assignable storage providers, it should still return true if an automatic path is assigned by the storage provider for a given UUID.

      Specified by:
      hasPathAssigned in interface IAssignableStorageProvider<ProxyStorageContext>
      Parameters:
      assignableContext - The storage context in which the file should be stored, containing any configuration information
      uuid - The unique identifier of the file to check
      Returns:
      true if the path is assigned, false otherwise
    • pathExists

      public boolean pathExists(@Nonnull ProxyStorageContext assignableContext, @Nonnull String path)
      Description copied from interface: IAssignableStorageProvider
      Checks if a path is already assigned to a UUID.

      Only the partial path is checked, not the full path.

      Specified by:
      pathExists in interface IAssignableStorageProvider<ProxyStorageContext>
      Parameters:
      assignableContext - The storage context in which the file should be stored, containing any configuration information
      path - The path to check
      Returns:
      true if the path exists, false otherwise
    • store

      public void store(ProxyStorageContext context, InputStream fileStream, UUID fileId, @Nullable String desiredPath, String fileName) throws PathAlreadyBoundException
      Description copied from interface: IStorageProvider
      Stores a file in the storage system.

      Each file is stored in the context of a specific storage context. This allows the storage provider to store the file in a way that is appropriate for the context.

      Files do not have names, only identifiers. As such it is up to the storage provider to determine how to store the file and how to retrieve it later, including how to map the identifier to a file name.

      The file system also takes a desired path to store the file in the storage system. This path is merely a suggestion and the storage provider may choose to ignore it.

      Specified by:
      store in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file should be stored, containing any configuration information
      fileStream - The stream from which the file can be read
      fileId - A unique identifier for the file. If a file with this identifier already exists then it will be overwritten
      Throws:
      PathAlreadyBoundException - if the desired path is already bound to a file
    • retrieve

      public InputStream retrieve(ProxyStorageContext context, UUID fileId) throws NoSuchFileException
      Description copied from interface: IStorageProvider
      Retrieves a file from the storage system.

      If no file with the given identifier exists then an exception is thrown.

      Specified by:
      retrieve in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file is stored, containing any configuration information
      fileId - The unique identifier of the file to retrieve
      Returns:
      A stream from which the file can be read
      Throws:
      NoSuchFileException - if the file does not exist
    • delete

      public void delete(ProxyStorageContext context, UUID fileId) throws NoSuchFileException
      Description copied from interface: IStorageProvider
      Deletes a file from the storage system.

      If no file with the given identifier exists then an exception is thrown.

      Specified by:
      delete in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file is stored, containing any configuration information
      fileId - The unique identifier of the file to delete
      Throws:
      NoSuchFileException - if the file does not exist
    • exists

      public boolean exists(ProxyStorageContext context, UUID fileId)
      Description copied from interface: IStorageProvider
      Checks if a file exists in the storage system.
      Specified by:
      exists in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file is stored, containing any configuration information
      fileId - The unique identifier of the file to check
      Returns:
      true if the file exists, false otherwise
    • list

      public Stream<UUID> list(ProxyStorageContext context)
      Description copied from interface: IStorageProvider
      Lists all files in the storage system.
      Specified by:
      list in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the files are stored, containing any configuration information
      Returns:
      A stream of unique identifiers for all files in the storage system
    • getPath

      public String getPath(ProxyStorageContext context, UUID fileId)
      Description copied from interface: IStorageProvider
      Gets publicly accessible path of a file in the storage system.

      E.g. if this is a file stored in an S3 bucket then this would return the URL of the file. If this is a file stored in a local file system then this would return the path of the file relative to the root of the web server.

      This should return null if the file does not have a path.

      Specified by:
      getPath in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file is stored, containing any configuration information
      fileId - The unique identifier of the file to get the path of
      Returns:
      The path of the file, or null if the file does not have a path
    • getPaths

      public Map<UUID,String> getPaths(ProxyStorageContext context, List<UUID> ids)
      Description copied from interface: IStorageProvider
      Gets publicly accessible paths of multiple files in the storage system.

      E.g. if this is a file stored in an S3 bucket then this would return the URL of the file. If this is a file stored in a local file system then this would return the path of the file relative to the root of the web server.

      This is meant to be used when multiple files need to be retrieved at once, so that the storage provider can optimise the retrieval of the paths.

      Specified by:
      getPaths in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the files are stored, containing any configuration information
      ids - The unique identifiers of the files to get the paths of
      Returns:
      A map of file identifiers to their paths
    • getContentType

      public String getContentType(ProxyStorageContext context, UUID uuid)
      Description copied from interface: IStorageProvider
      Gets the content type of a file in the storage system.

      This should return null if the file does not have a content type or if the content type is unknown.

      The type should be a MIME type, e.g. "application/pdf" or "image/jpeg".

      Specified by:
      getContentType in interface IStorageProvider<ProxyStorageContext>
      Parameters:
      context - The storage context in which the file is stored, containing any configuration information
      uuid - The unique identifier of the file to get the content type of
      Returns:
      The content type of the file, or null if the file does not have a content type