@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); } }
SetError.builder() .type(SetError.Type.ERROR) .description(e.getMessage()) .build());
private boolean isTargetingOutbox(MailboxSession mailboxSession, Set<MailboxId> targetMailboxIds) throws MailboxException { Set<MailboxId> outboxes = listMailboxIdsForRole(mailboxSession, Role.OUTBOX); if (outboxes.isEmpty()) { throw new MailboxNotFoundException("At least one outbox should be accessible"); } return targetMailboxIds.stream().anyMatch(outboxes::contains); }
@Override public Mailbox findMailboxById(MailboxId id) throws MailboxException { CassandraId mailboxId = (CassandraId) id; return retrieveMailbox(mailboxId) .join() .orElseThrow(() -> new MailboxNotFoundException(id)); }
@Override public Mailbox findMailboxByPath(MailboxPath path) throws MailboxException { return mailboxPathV2DAO.retrieveId(path) .thenCompose(cassandraIdOptional -> cassandraIdOptional .map(CassandraIdAndPath::getCassandraId) .map(this::retrieveMailbox) .orElseGet(Throwing.supplier(() -> fromPreviousTable(path)))) .join() .orElseThrow(() -> new MailboxNotFoundException(path)); }
private void assertRightsOnMailboxes(Collection<MailboxId> mailboxIds, MailboxSession mailboxSession, Right... rights) throws MailboxNotFoundException { Optional<MailboxId> mailboxForbidden = mailboxIds.stream() .filter(hasRightsOnMailbox(mailboxSession, rights).negate()) .findFirst(); if (mailboxForbidden.isPresent()) { LOGGER.info("Mailbox with Id {} does not belong to {}", mailboxForbidden.get(), mailboxSession.getUser().getUserName()); throw new MailboxNotFoundException(mailboxForbidden.get()); } } }
@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); }
@Override public MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailboxRow = mapper.findMailboxByPath(mailboxPath); if (mailboxRow == null) { LOGGER.info("Mailbox '{}' not found.", mailboxPath); throw new MailboxNotFoundException(mailboxPath); } else { LOGGER.debug("Loaded mailbox {}", mailboxPath); return createMessageManager(mailboxRow, session); } }
@Override public Mailbox findMailboxById(MailboxId id) throws MailboxException, MailboxNotFoundException { JPAId mailboxId = (JPAId)id; try { return getEntityManager().createNamedQuery("findMailboxById", JPAMailbox.class) .setParameter("idParam", mailboxId.getRawId()) .getSingleResult(); } catch (NoResultException e) { throw new MailboxNotFoundException(mailboxId); } catch (PersistenceException e) { throw new MailboxException("Search of mailbox " + mailboxId.serialize() + " failed", e); } }
private void applyMessageMoveNoMailboxChecks(MailboxSession mailboxSession, List<MailboxMessage> currentMailboxMessages, MessageMoves messageMoves) throws MailboxException { MailboxMessage mailboxMessage = currentMailboxMessages.stream().findAny().orElseThrow(() -> new MailboxNotFoundException("can't load message")); validateQuota(messageMoves, mailboxSession, mailboxMessage); addMessageToMailboxes(mailboxMessage, messageMoves.addedMailboxIds(), mailboxSession); removeMessageFromMailboxes(mailboxMessage, messageMoves.removedMailboxIds(), mailboxSession); dispatcher.moved(mailboxSession, messageMoves, ImmutableMap.of(mailboxMessage.getUid(), mailboxMessage)); }
private Mailbox getMailbox(MailboxId mailboxId, MailboxSession mailboxSession) throws MailboxNotFoundException { return mailboxFactory.builder() .id(mailboxId) .session(mailboxSession) .build() .orElseThrow(() -> new MailboxNotFoundException(mailboxId)); }
@Override public Mailbox findMailboxByPath(MailboxPath mailboxPath) throws MailboxException, MailboxNotFoundException { try { if (mailboxPath.getUser() == null) { return getEntityManager().createNamedQuery("findMailboxByName", JPAMailbox.class) .setParameter("nameParam", mailboxPath.getName()) .setParameter("namespaceParam", mailboxPath.getNamespace()) .getSingleResult(); } else { return getEntityManager().createNamedQuery("findMailboxByNameWithUser", JPAMailbox.class) .setParameter("nameParam", mailboxPath.getName()) .setParameter("namespaceParam", mailboxPath.getNamespace()) .setParameter("userParam", mailboxPath.getUser()) .getSingleResult(); } } catch (NoResultException e) { throw new MailboxNotFoundException(mailboxPath); } catch (PersistenceException e) { throw new MailboxException("Search of mailbox " + mailboxPath + " failed", e); } }
private void assertIsOwner(MailboxSession mailboxSession, MailboxPath mailboxPath) throws MailboxNotFoundException { if (!mailboxPath.belongsTo(mailboxSession)) { LOGGER.info("Mailbox {} does not belong to {}", mailboxPath.asString(), mailboxSession.getUser().getUserName()); throw new MailboxNotFoundException(mailboxPath.asString()); } }
@Test public void testInexistentMailboxName() throws Exception { when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) .thenThrow(new MailboxNotFoundException("")); subject.doProcess(listRightsRequest, responder, imapSession); verify(responder, times(1)).respond(argumentCaptor.capture()); verifyNoMoreInteractions(responder); assertThat(argumentCaptor.getAllValues()) .hasSize(1); assertThat(argumentCaptor.getAllValues().get(0)) .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); }
@Override public MessageManager getMailbox(MailboxId mailboxId, MailboxSession session) throws MailboxException { MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailboxRow = mapper.findMailboxById(mailboxId); if (mailboxRow == null) { LOGGER.info("Mailbox '{}' not found.", mailboxId.serialize()); throw new MailboxNotFoundException(mailboxId); } if (! assertUserHasAccessTo(mailboxRow, session)) { LOGGER.info("Mailbox '{}' does not belong to user '{}' but to '{}'", mailboxId.serialize(), session.getUser(), mailboxRow.getUser()); throw new MailboxNotFoundException(mailboxId); } LOGGER.debug("Loaded mailbox {}", mailboxId.serialize()); return createMessageManager(mailboxRow, session); }
@Test public void testInexistentMailboxName() throws Exception { when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) .thenThrow(new MailboxNotFoundException("")); subject.doProcess(replaceAclRequest, responder, imapSession); verify(responder, times(1)).respond(argumentCaptor.capture()); verifyNoMoreInteractions(responder); assertThat(argumentCaptor.getAllValues()) .hasSize(1); assertThat(argumentCaptor.getAllValues().get(0)) .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); }
@Test public void testInexistentMailboxName() throws Exception { when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) .thenThrow(new MailboxNotFoundException("")); subject.doProcess(getACLRequest, responder, imapSession); verify(responder, times(1)).respond(argumentCaptor.capture()); verifyNoMoreInteractions(responder); assertThat(argumentCaptor.getAllValues()) .hasSize(1); assertThat(argumentCaptor.getAllValues().get(0)) .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); }
@Test public void testNonExistentMailboxName() throws Exception { when(mailboxManager.getMailbox(any(MailboxPath.class), any(MailboxSession.class))) .thenThrow(new MailboxNotFoundException("")); subject.doProcess(deleteACLRequest, responder, imapSession); verify(responder, times(1)).respond(argumentCaptor.capture()); verifyNoMoreInteractions(responder); assertThat(argumentCaptor.getAllValues()) .hasSize(1); assertThat(argumentCaptor.getAllValues().get(0)) .matches(StatusResponseTypeMatcher.NO_RESPONSE_MATCHER::matches); }
@Test public void validateIsUserOwnerOfMailboxesShouldThrowWhenMailboxIdDoesntExist() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenThrow(new MailboxNotFoundException(mailboxId)); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)); }
@Override public void deleteMailbox(final MailboxPath mailboxPath, final MailboxSession session) throws MailboxException { LOGGER.info("deleteMailbox {}", mailboxPath); assertIsOwner(session, mailboxPath); MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(session); MessageMapper messageMapper = mailboxSessionMapperFactory.getMessageMapper(session); mailboxMapper.execute((Mapper.Transaction<Mailbox>) () -> { Mailbox mailbox = mailboxMapper.findMailboxByPath(mailboxPath); if (mailbox == null) { throw new MailboxNotFoundException(mailboxPath); } QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(mailboxPath); long messageCount = messageMapper.countMessagesInMailbox(mailbox); long totalSize = Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, -1)) .mapToLong(Message::getFullContentOctets) .sum(); // We need to create a copy of the mailbox as maybe we can not refer to the real // mailbox once we remove it SimpleMailbox m = new SimpleMailbox(mailbox); mailboxMapper.delete(mailbox); dispatcher.mailboxDeleted(session, mailbox, quotaRoot, QuotaCount.count(messageCount), QuotaSize.size(totalSize)); return m; }); }