diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraMailboxSessionMapperFactory.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraMailboxSessionMapperFactory.java index 6c8e39c5b3c..17abbdab444 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraMailboxSessionMapperFactory.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/CassandraMailboxSessionMapperFactory.java @@ -132,7 +132,8 @@ public CassandraMailboxSessionMapperFactory(UidProvider uidProvider, ModSeqProvi deletedMessageDAO); this.cassandraMailboxMapper = new CassandraMailboxMapper(mailboxDAO, mailboxPathV3DAO, userMailboxRightsDAO, aclMapper, cassandraConfiguration); this.cassandraSubscriptionMapper = new CassandraSubscriptionMapper(session); - this.cassandraAttachmentMapper = new CassandraAttachmentMapper(attachmentDAOV2, blobStore, new StringBackedAttachmentIdFactory()); + this.cassandraAttachmentMapper = new CassandraAttachmentMapper(attachmentDAOV2, blobStore, + new CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default(new StringBackedAttachmentIdFactory())); this.cassandraMessageMapper = new CassandraMessageMapper( uidProvider, modSeqProvider, diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java index 4fef446aecb..a6542ce3a1d 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraAttachmentMapper.java @@ -54,15 +54,33 @@ public class CassandraAttachmentMapper implements AttachmentMapper { private static final Logger LOGGER = LoggerFactory.getLogger(CassandraAttachmentMapper.class); + public interface AttachmentIdAssignationStrategy { + AttachmentId assign(ParsedAttachment parsedAttachment, MessageId messageId); + + class Default implements AttachmentIdAssignationStrategy { + private final AttachmentIdFactory attachmentIdFactory; + + @Inject + public Default(AttachmentIdFactory attachmentIdFactory) { + this.attachmentIdFactory = attachmentIdFactory; + } + + @Override + public AttachmentId assign(ParsedAttachment parsedAttachment, MessageId messageId) { + return attachmentIdFactory.random(); + } + } + } + private final CassandraAttachmentDAOV2 attachmentDAOV2; private final BlobStore blobStore; - private final AttachmentIdFactory attachmentIdFactory; + private final AttachmentIdAssignationStrategy attachmentIdAssignationStrategy; @Inject - public CassandraAttachmentMapper(CassandraAttachmentDAOV2 attachmentDAOV2, BlobStore blobStore, AttachmentIdFactory attachmentIdFactory) { + public CassandraAttachmentMapper(CassandraAttachmentDAOV2 attachmentDAOV2, BlobStore blobStore, AttachmentIdAssignationStrategy attachmentIdAssignationStrategy) { this.attachmentDAOV2 = attachmentDAOV2; this.blobStore = blobStore; - this.attachmentIdFactory = attachmentIdFactory; + this.attachmentIdAssignationStrategy = attachmentIdAssignationStrategy; } @Override @@ -130,7 +148,7 @@ public Mono> storeAttachmentsReactive(Collection private Mono storeAttachmentAsync(ParsedAttachment parsedAttachment, MessageId ownerMessageId) { try { - AttachmentId attachmentId = attachmentIdFactory.random(); + AttachmentId attachmentId = attachmentIdAssignationStrategy.assign(parsedAttachment, ownerMessageId); ByteSource content = parsedAttachment.getContent(); long size = content.size(); return Mono.from(blobStore.save(blobStore.getDefaultBucketName(), content, LOW_COST)) diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/GuiceUtils.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/GuiceUtils.java index ecbb898d0b3..4e64d8f3a86 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/GuiceUtils.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/utils/GuiceUtils.java @@ -42,6 +42,7 @@ import org.apache.james.mailbox.cassandra.ids.CassandraMessageId; import org.apache.james.mailbox.cassandra.mail.ACLMapper; import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper; +import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentMapper; import org.apache.james.mailbox.cassandra.mail.CassandraModSeqProvider; import org.apache.james.mailbox.cassandra.mail.CassandraUidProvider; import org.apache.james.mailbox.cassandra.mail.eventsourcing.acl.ACLModule; @@ -85,6 +86,7 @@ public static Module commonModules(CqlSession session, CassandraTypesProvider ty binder -> binder.bind(MessageId.Factory.class).toInstance(messageIdFactory), binder -> binder.bind(BatchSizes.class).toInstance(BatchSizes.defaultValues()), binder -> binder.bind(UidProvider.class).to(CassandraUidProvider.class), + binder -> binder.bind(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.class).to(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default.class), binder -> binder.bind(ModSeqProvider.class).to(CassandraModSeqProvider.class), binder -> binder.bind(ACLMapper.class).to(CassandraACLMapper.class), binder -> binder.bind(BlobId.Factory.class).toInstance(new PlainBlobId.Factory()), diff --git a/server/container/guice/cassandra/src/main/java/org/apache/james/modules/mailbox/CassandraMailboxModule.java b/server/container/guice/cassandra/src/main/java/org/apache/james/modules/mailbox/CassandraMailboxModule.java index fd6c2b9f1e0..5a63333ef39 100644 --- a/server/container/guice/cassandra/src/main/java/org/apache/james/modules/mailbox/CassandraMailboxModule.java +++ b/server/container/guice/cassandra/src/main/java/org/apache/james/modules/mailbox/CassandraMailboxModule.java @@ -75,6 +75,7 @@ import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper; import org.apache.james.mailbox.cassandra.mail.CassandraApplicableFlagDAO; import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentDAOV2; +import org.apache.james.mailbox.cassandra.mail.CassandraAttachmentMapper; import org.apache.james.mailbox.cassandra.mail.CassandraDeletedMessageDAO; import org.apache.james.mailbox.cassandra.mail.CassandraFirstUnseenDAO; import org.apache.james.mailbox.cassandra.mail.CassandraMailboxCounterDAO; @@ -218,6 +219,7 @@ protected void configure() { bind(AttachmentContentLoader.class).to(AttachmentManager.class); bind(MailboxCounterCorrector.class).to(CassandraMailboxCounterCorrector.class); bind(MessageParser.class).toInstance(new MessageParserImpl()); + bind(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.class).to(CassandraAttachmentMapper.AttachmentIdAssignationStrategy.Default.class); bind(Limit.class).annotatedWith(Names.named(CassandraEmailChangeRepository.LIMIT_NAME)).toInstance(Limit.of(256)); bind(Limit.class).annotatedWith(Names.named(CassandraMailboxChangeRepository.LIMIT_NAME)).toInstance(Limit.of(256));