Class EncryptedGridFsRepositoryImpl

All Implemented Interfaces:
GridFsRepository, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

@Repository @ConditionalOnExpression("${application.service.vault.encryption.enabled}.equals(true) and \'${application.service.vault.storage.impl}\'.equals(\'gridfs\')") public class EncryptedGridFsRepositoryImpl extends GridFsRepositoryImpl
GridFS repository that transparently encrypts content before it is persisted and decrypts it when retrieved. The bean is loaded only when application.service.vault.encryption.enabled is true and application.service.vault.storage.impl equals "gridfs".
  • Constructor Details

    • EncryptedGridFsRepositoryImpl

      public EncryptedGridFsRepositoryImpl(org.springframework.data.mongodb.gridfs.GridFsTemplate gridFsTemplate, org.springframework.data.mongodb.gridfs.GridFsOperations gridFsOperations, org.springframework.data.mongodb.core.MongoTemplate mongoTemplate, CryptoService cryptoService)
      Constructs an EncryptedGridFsRepositoryImpl with the required dependencies.
      Parameters:
      gridFsTemplate - the GridFsTemplate for GridFS operations
      gridFsOperations - the GridFsOperations for GridFS operations
      mongoTemplate - the MongoTemplate for MongoDB operations
      cryptoService - the CryptoService used for encryption and decryption
  • Method Details

    • saveFile

      public void saveFile(InputStream inputStream, String fileName, String contentType, Map<String,String> metadata)
      Saves a file to GridFS after encrypting its content. Marks the file metadata as encrypted.
      Specified by:
      saveFile in interface GridFsRepository
      Overrides:
      saveFile in class GridFsRepositoryImpl
      Parameters:
      inputStream - the input stream of the file content
      fileName - the name of the file
      contentType - the MIME type of the file
      metadata - additional metadata to associate with the file
    • getFileContent

      public InputStream getFileContent(com.mongodb.client.gridfs.model.GridFSFile file)
      Retrieves the content of a file from GridFS. If the file is encrypted, decrypts the content before returning.
      Overrides:
      getFileContent in class GridFsRepositoryImpl
      Parameters:
      file - the GridFSFile to retrieve content from
      Returns:
      an InputStream of the file content, decrypted if necessary
    • computeHash

      public String computeHash(String uuid, String algorithm)
      Computes the hash of a file's content using the specified algorithm. If the file is encrypted, computes the hash on the decrypted content.
      Specified by:
      computeHash in interface GridFsRepository
      Overrides:
      computeHash in class GridFsRepositoryImpl
      Parameters:
      uuid - the unique identifier of the file
      algorithm - the hash algorithm to use (e.g., SHA-256)
      Returns:
      the computed hash as a hexadecimal string