/** * Create a Mailbox for the given mailbox path. This will by default return a {@link SimpleMailbox}. * <p/> * If you need to return something more special just override this method * * @param mailboxPath * @param session * @throws MailboxException */ protected org.apache.james.mailbox.store.mail.model.Mailbox doCreateMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { return new SimpleMailbox(mailboxPath, randomUidValidity()); }
public Mailbox retrieveMailbox(MailboxDataTransferObject mailboxDataTransferObject) { SimpleMailbox mailbox = new SimpleMailbox(new MailboxPath(mailboxDataTransferObject.getNamespace(), mailboxDataTransferObject.getUser(), mailboxDataTransferObject.getName()), mailboxDataTransferObject.getUidValidity()); try { mailbox.setACL(MailboxACLJsonConverter.toACL(mailboxDataTransferObject.getSerializedACL())); mailbox.setMailboxId(mailboxIdDeserializer.deserialize(mailboxDataTransferObject.getSerializedMailboxId())); } catch (IOException e) { LOGGER.warn("Failed to deserialize ACL", e); } catch (MailboxIdDeserialisationException e) { LOGGER.warn("Failed to deserialize mailbox ID", e); } return mailbox; }
private CompletableFuture<SimpleMailbox> toMailboxWithAclFuture(SimpleMailbox mailbox) { CassandraId cassandraId = (CassandraId) mailbox.getMailboxId(); return cassandraACLMapper.getACL(cassandraId) .thenApply(acl -> { mailbox.setACL(acl); return mailbox; }); }
@Override protected Mailbox doCreateMailbox(MailboxPath mailboxPath, MailboxSession session) { SimpleMailbox cassandraMailbox = new SimpleMailbox(mailboxPath, randomUidValidity()); cassandraMailbox.setACL(MailboxACL.EMPTY); return cassandraMailbox; }
private CompletableFuture<Optional<SimpleMailbox>> migrate(SimpleMailbox mailbox) { CassandraId mailboxId = (CassandraId) mailbox.getMailboxId(); return mailboxPathV2DAO.save(mailbox.generateAssociatedPath(), mailboxId) .thenCompose(success -> deleteIfSuccess(mailbox, success)) .thenApply(any -> Optional.of(mailbox)); }
@BeforeEach void setUp(CassandraCluster cassandra) { modSeqProvider = new CassandraModSeqProvider(cassandra.getConf()); MailboxPath path = new MailboxPath("gsoc", "ieugen", "Trash"); mailbox = new SimpleMailbox(path, 1234); mailbox.setMailboxId(CASSANDRA_ID); }
@Test void saveWithUpdateShouldBeThreadSafe() throws Exception { SimpleMailbox mailbox = new SimpleMailbox(MAILBOX_PATH, UID_VALIDITY); testee.save(mailbox); mailbox.setName("newName"); ConcurrentTestRunner.builder() .operation((a, b) -> testee.save(mailbox)) .threadCount(THREAD_COUNT) .operationCount(OPERATION_COUNT) .runAcceptingErrorsWithin(Duration.ofMinutes(1)); List<Mailbox> list = testee.list(); assertThat(list).hasSize(1); assertThat(list.get(0)).isEqualToComparingFieldByField(mailbox); } }
private Optional<SimpleMailbox> addMailboxId(CassandraId cassandraId, Optional<SimpleMailbox> mailboxOptional) { mailboxOptional.ifPresent(mailbox -> mailbox.setMailboxId(cassandraId)); return mailboxOptional; }
@Override public boolean equals(Object obj) { if (obj instanceof SimpleMailbox) { SimpleMailbox o = (SimpleMailbox)obj; return Objects.equal(id, o.getMailboxId()); } return false; }
private Optional<SimpleMailbox> addAcl(MailboxACL acl, Optional<SimpleMailbox> mailboxOptional) { mailboxOptional.ifPresent(mailbox -> mailbox.setACL(acl)); return mailboxOptional; }
private CompletionStage<Void> deleteIfSuccess(SimpleMailbox mailbox, boolean success) { if (success) { return mailboxPathDAO.delete(mailbox.generateAssociatedPath()); } LOGGER.info("Concurrent execution lead to data race while migrating {} to 'mailboxPathV2DAO'.", mailbox.generateAssociatedPath()); return CompletableFuture.completedFuture(null); }
@Test void mailboxEventListenersShouldBeTriggeredIfRegistered() { SimpleMailbox simpleMailbox = new SimpleMailbox(MAILBOX_PATH_1, 42); simpleMailbox.setMailboxId(TestId.of(52)); TreeMap<MessageUid, MessageMetaData> uids = new TreeMap<>(); final MailboxListener.MailboxEvent event = new EventFactory().added(mailboxSession, uids, simpleMailbox, EMPTY_MESSAGE_CACHE); registeredDelegatingMailboxListener1.event(event); assertThat(eventCollectorMailbox1.getEvents()).hasSize(1); assertThat(eventCollectorMailbox2.getEvents()).hasSize(1); assertThat(eventCollectorMailbox3.getEvents()).isEmpty(); }
@Override public MailboxId save(Mailbox mailbox) throws MailboxException { Preconditions.checkArgument(mailbox instanceof SimpleMailbox); SimpleMailbox cassandraMailbox = (SimpleMailbox) mailbox; CassandraId cassandraId = retrieveId(cassandraMailbox); cassandraMailbox.setMailboxId(cassandraId); boolean applied = trySave(cassandraMailbox, cassandraId).join(); if (!applied) { throw new MailboxExistsException(mailbox.generateAssociatedPath().asString()); } return cassandraId; }
private CassandraId retrieveId(SimpleMailbox cassandraMailbox) { if (cassandraMailbox.getMailboxId() == null) { return CassandraId.timeBased(); } else { return (CassandraId) cassandraMailbox.getMailboxId(); } }
private CompletableFuture<Boolean> trySave(SimpleMailbox cassandraMailbox, CassandraId cassandraId) { return mailboxPathV2DAO.save(cassandraMailbox.generateAssociatedPath(), cassandraId) .thenCompose(CompletableFutureUtil.composeIfTrue( () -> retrieveMailbox(cassandraId) .thenCompose(optional -> CompletableFuture .allOf(optional .map(storedMailbox -> mailboxPathV2DAO.delete(storedMailbox.generateAssociatedPath())) .orElse(CompletableFuture.completedFuture(null)), mailboxDAO.save(cassandraMailbox))))); }
@Override public synchronized Mailbox findMailboxByPath(MailboxPath path) throws MailboxException { Mailbox result = mailboxesByPath.get(path); if (result == null) { throw new MailboxNotFoundException(path); } else { return new SimpleMailbox(result); } }
@BeforeEach void setUp(CassandraCluster cassandra) { uidProvider = new CassandraUidProvider(cassandra.getConf()); MailboxPath path = new MailboxPath("gsoc", "ieugen", "Trash"); mailbox = new SimpleMailbox(path, 1234); mailbox.setMailboxId(CASSANDRA_ID); }
private SimpleMailbox toMailboxWithId(Row row) { SimpleMailbox mailbox = mailboxFromRow(row); mailbox.setMailboxId(CassandraId.of(row.getUUID(ID))); return mailbox; }
@Override public synchronized Mailbox findMailboxById(MailboxId id) throws MailboxException { InMemoryId mailboxId = (InMemoryId)id; for (Mailbox mailbox: mailboxesByPath.values()) { if (mailbox.getMailboxId().equals(mailboxId)) { return new SimpleMailbox(mailbox); } } throw new MailboxNotFoundException(mailboxId); }
@Test void onceEventListenersShouldBeTriggeredOnceAcrossTheCluster() { SimpleMailbox simpleMailbox = new SimpleMailbox(MAILBOX_PATH_1, 42); simpleMailbox.setMailboxId(TestId.of(52)); TreeMap<MessageUid, MessageMetaData> uids = new TreeMap<>(); final MailboxListener.MailboxEvent event = new EventFactory().added(mailboxSession, uids, simpleMailbox, EMPTY_MESSAGE_CACHE); registeredDelegatingMailboxListener1.event(event); assertThat(eventCollectorOnce1.getEvents()).hasSize(1); assertThat(eventCollectorOnce2.getEvents()).isEmpty(); assertThat(eventCollectorOnce3.getEvents()).isEmpty(); }