public static boolean isMailboxChildOf(Mailbox mailbox, Mailbox potentialParent, MailboxSession mailboxSession) { return mailbox.getNamespace().equals(potentialParent.getNamespace()) && mailbox.getUser().equals(potentialParent.getUser()) && mailbox.getName().startsWith(potentialParent.getName() + mailboxSession.getPathDelimiter()); } }
@Override public boolean hasChildren(Mailbox mailbox, char delimiter) throws MailboxException, MailboxNotFoundException { final String name = mailbox.getName() + delimiter + SQL_WILDCARD_CHAR; final Long numberOfChildMailboxes; if (mailbox.getUser() == null) { numberOfChildMailboxes = (Long) getEntityManager().createNamedQuery("countMailboxesWithNameLike").setParameter("nameParam", name).setParameter("namespaceParam", mailbox.getNamespace()).getSingleResult(); } else { numberOfChildMailboxes = (Long) getEntityManager().createNamedQuery("countMailboxesWithNameLikeWithUser").setParameter("nameParam", name).setParameter("namespaceParam", mailbox.getNamespace()).setParameter("userParam", mailbox.getUser()).getSingleResult(); } return numberOfChildMailboxes != null && numberOfChildMailboxes > 0; }
@Override public MailboxId save(Mailbox mailbox) throws MailboxException { try { if (isPathAlreadyUsedByAnotherMailbox(mailbox)) { throw new MailboxExistsException(mailbox.getName()); } this.lastMailboxName = mailbox.getName(); JPAMailbox persistedMailbox = JPAMailbox.from(mailbox); getEntityManager().persist(persistedMailbox); if (!(mailbox instanceof JPAMailbox)) { mailbox.setMailboxId(persistedMailbox.getMailboxId()); } return mailbox.getMailboxId(); } catch (PersistenceException e) { throw new MailboxException("Save of mailbox " + mailbox.getName() + " failed", e); } }
private boolean mailboxMatchesRegex(Mailbox mailbox, MailboxPath path, String regex) { return Objects.equal(mailbox.getNamespace(), path.getNamespace()) && Objects.equal(mailbox.getUser(), path.getUser()) && mailbox.getName().matches(regex); }
add(session, mailbox, message); } catch (MailboxException e) { LOGGER.error("Unable to index message {} in mailbox {}", message.getUid(), mailbox.getName(), e);
@Override public boolean hasChildren(Mailbox mailbox, char delimiter) { return ImmutableList.of( mailboxPathDAO.listUserMailboxes(mailbox.getNamespace(), mailbox.getUser()), mailboxPathV2DAO.listUserMailboxes(mailbox.getNamespace(), mailbox.getUser())) .stream() .map(CompletableFuture::join) .flatMap(Function.identity()) .anyMatch(idAndPath -> idAndPath.getMailboxPath().getName().startsWith(mailbox.getName() + String.valueOf(delimiter))); }
@Override public MailboxId save(Mailbox mailbox) throws MailboxException { InMemoryId id = (InMemoryId) mailbox.getMailboxId(); if (id == null) { id = InMemoryId.of(mailboxIdGenerator.incrementAndGet()); mailbox.setMailboxId(id); } else { try { Mailbox mailboxWithPreviousName = findMailboxById(id); mailboxesByPath.remove(mailboxWithPreviousName.generateAssociatedPath()); } catch (MailboxNotFoundException e) { // No need to remove the previous mailbox } } Mailbox previousMailbox = mailboxesByPath.putIfAbsent(mailbox.generateAssociatedPath(), mailbox); if (previousMailbox != null) { throw new MailboxExistsException(mailbox.getName()); } return mailbox.getMailboxId(); }
public SimpleMailbox(Mailbox mailbox) { this.id = mailbox.getMailboxId(); this.namespace = mailbox.getNamespace(); this.user = mailbox.getUser(); this.name = mailbox.getName(); this.uidValidity = mailbox.getUidValidity(); this.acl = new MailboxACL(mailbox.getACL().getEntries()); }
@Override public void add(MailboxSession session, Mailbox mailbox, MailboxMessage message) throws MailboxException { try { LOGGER.info("Indexing mailbox {}-{} of user {} on message {}", mailbox.getName(), mailbox.getMailboxId(), session.getUser().getUserName(), message.getUid()); elasticSearchIndexer.index(indexIdFor(mailbox, message.getUid()), messageToElasticSearchJson.convertToJson(message, ImmutableList.of(session.getUser()))); } catch (Exception e) { try { LOGGER.warn("Indexing mailbox {}-{} of user {} on message {} without attachments ", mailbox.getName(), mailbox.getMailboxId().serialize(), session.getUser().getUserName(), message.getUid(), e); elasticSearchIndexer.index(indexIdFor(mailbox, message.getUid()), messageToElasticSearchJson.convertToJsonWithoutAttachment(message, ImmutableList.of(session.getUser()))); } catch (JsonProcessingException e1) { LOGGER.error("Error when indexing mailbox {}-{} of user {} on message {} without its attachment", mailbox.getName(), mailbox.getMailboxId().serialize(), session.getUser().getUserName(), message.getUid(), e1); } } }
public CompletableFuture<Void> save(Mailbox mailbox) { CassandraId cassandraId = (CassandraId) mailbox.getMailboxId(); return executor.executeVoid(insertStatement.bind() .setUUID(ID, cassandraId.asUuid()) .setString(NAME, mailbox.getName()) .setLong(UIDVALIDITY, mailbox.getUidValidity()) .setUDTValue(MAILBOX_BASE, mailboxBaseTupleUtil.createMailboxBaseUDT(mailbox.getNamespace(), mailbox.getUser()))); }
private void doRenameMailbox(MailboxPath from, MailboxPath to, MailboxSession session, MailboxMapper mapper) throws MailboxException { // TODO put this into a serilizable transaction Mailbox mailbox = Optional.ofNullable(mapper.findMailboxByPath(from)) .orElseThrow(() -> new MailboxNotFoundException(from)); mailbox.setNamespace(to.getNamespace()); mailbox.setUser(to.getUser()); mailbox.setName(to.getName()); mapper.save(mailbox); dispatcher.mailboxRenamed(session, from, mailbox); // rename submailboxes MailboxPath children = new MailboxPath(from.getNamespace(), from.getUser(), from.getName() + getDelimiter() + "%"); locker.executeWithLock(session, children, (LockAwareExecution<Void>) () -> { List<Mailbox> subMailboxes = mapper.findMailboxWithPathLike(children); for (Mailbox sub : subMailboxes) { String subOriginalName = sub.getName(); String subNewName = to.getName() + subOriginalName.substring(from.getName().length()); MailboxPath fromPath = new MailboxPath(children, subOriginalName); sub.setName(subNewName); mapper.save(sub); dispatcher.mailboxRenamed(session, fromPath, sub); LOGGER.debug("Rename mailbox sub-mailbox {} to {}", subOriginalName, subNewName); } return null; }, true); }
public MailboxDataTransferObject convertMailboxDataTransferObject(Mailbox mailbox) { return MailboxDataTransferObject.builder() .serializedMailboxId(mailbox.getMailboxId().serialize()) .namespace(mailbox.getNamespace()) .user(mailbox.getUser()) .name(mailbox.getName()) .uidValidity(mailbox.getUidValidity()) .serializedACL(getSerializedACL(mailbox)) .build(); }