Package org.saidone.repository
Class EncryptedGridFsRepositoryImpl
java.lang.Object
org.saidone.component.BaseComponent
org.saidone.repository.GridFsRepositoryImpl
org.saidone.repository.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 Summary
ConstructorsConstructorDescriptionEncryptedGridFsRepositoryImpl
(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. -
Method Summary
Modifier and TypeMethodDescriptioncomputeHash
(String uuid, String algorithm) Computes the hash of a file's content using the specified algorithm.getFileContent
(com.mongodb.client.gridfs.model.GridFSFile file) Retrieves the content of a file from GridFS.void
Saves a file to GridFS after encrypting its content.Methods inherited from class org.saidone.repository.GridFsRepositoryImpl
computeHash, deleteFileById, findFileById, init, updateFileMetadata
Methods inherited from class org.saidone.component.BaseComponent
setApplicationContext, shutDown, stop
-
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 operationsgridFsOperations
- the GridFsOperations for GridFS operationsmongoTemplate
- the MongoTemplate for MongoDB operationscryptoService
- 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 interfaceGridFsRepository
- Overrides:
saveFile
in classGridFsRepositoryImpl
- Parameters:
inputStream
- the input stream of the file contentfileName
- the name of the filecontentType
- the MIME type of the filemetadata
- additional metadata to associate with the file
-
getFileContent
Retrieves the content of a file from GridFS. If the file is encrypted, decrypts the content before returning.- Overrides:
getFileContent
in classGridFsRepositoryImpl
- Parameters:
file
- the GridFSFile to retrieve content from- Returns:
- an InputStream of the file content, decrypted if necessary
-
computeHash
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 interfaceGridFsRepository
- Overrides:
computeHash
in classGridFsRepositoryImpl
- Parameters:
uuid
- the unique identifier of the filealgorithm
- the hash algorithm to use (e.g., SHA-256)- Returns:
- the computed hash as a hexadecimal string
-