private void createMailboxIfNotExist(MailboxSession session, MailboxPath path) throws MailboxException { if (!mailboxManager.mailboxExists(path, session)) { try { mailboxManager.createMailbox(path, session); } catch (MailboxExistsException e) { LOGGER.info("Mailbox {} have been created concurrently", path); } } }
private void createMailbox(MailboxPath mailboxPath, MailboxSession session) { try { Optional<MailboxId> mailboxId = mailboxManager.createMailbox(mailboxPath, session); if (mailboxId.isPresent()) { subscriptionManager.subscribe(session, mailboxPath.getName()); } LOGGER.info("Provisioning {}. {} created.", mailboxPath, mailboxId); } catch (MailboxExistsException e) { LOGGER.info("Mailbox {} have been created concurrently", mailboxPath); } catch (MailboxException e) { throw new RuntimeException(e); } }
private void provisionInbox(ImapSession session, MailboxManager mailboxManager, MailboxSession mailboxSession) throws MailboxException { final MailboxPath inboxPath = PathConverter.forSession(session).buildFullPath(MailboxConstants.INBOX); if (mailboxManager.mailboxExists(inboxPath, mailboxSession)) { LOGGER.debug("INBOX exists. No need to create it."); } else { try { Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inboxPath, mailboxSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId); } catch (MailboxExistsException e) { LOGGER.warn("Mailbox INBOX created by concurrent call. Safe to ignore this exception."); } } }
public void createMailboxes() throws MailboxException { mailboxManager.createMailbox(inbox, session); mailboxManager.createMailbox(subFolder, session); messageManager = mailboxManager.getMailbox(inbox, session); }
public MailboxId createMailbox(MailboxPath mailboxPath) { MailboxSession mailboxSession = null; try { mailboxSession = mailboxManager.createSystemSession(mailboxPath.getUser()); mailboxManager.startProcessingRequest(mailboxSession); return mailboxManager.createMailbox(mailboxPath, mailboxSession) .orElseThrow(() -> new MailboxException("mailbox name is probably empty")); } catch (MailboxException e) { throw new RuntimeException(e); } finally { closeSession(mailboxSession); } }
private static void createMailbox(MailboxManager mailboxManager, MailboxSession mailboxSession, MailboxPath mailboxPath) { try { mailboxManager.createMailbox(mailboxPath, mailboxSession); MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession); IntStream.range(0, MESSAGE_PER_MAILBOX_COUNT) .forEach(i -> appendMessage(messageManager, mailboxSession)); } catch (MailboxException e) { throw new RuntimeException(e); } }
@Test public void testConcurrentAccessToFilterShouldNotThrow() throws Exception { doNothing().when(subscriptionManager).subscribe(eq(session), anyString()); when(mailboxManager.createMailbox(any(MailboxPath.class), eq(session))).thenReturn(Optional.of(TestId.of(18L))); when(mailboxManager.mailboxExists(any(MailboxPath.class), eq(session))).thenReturn(false); when(mailboxManager.createSystemSession(USERNAME)).thenReturn(session); ConcurrentTestRunner .builder() .operation((threadNumber, step) -> sut.createMailboxesIfNeeded(session)) .threadCount(2) .runSuccessfullyWithin(Duration.ofMinutes(1)); } }
@Test public void createMailboxShouldReturnRightId() throws Exception { session = mailboxManager.createSystemSession(USER_1); mailboxManager.startProcessingRequest(session); MailboxPath mailboxPath = MailboxPath.forUser(USER_1, "name.subfolder"); Optional<MailboxId> mailboxId = mailboxManager.createMailbox(mailboxPath, session); MessageManager retrievedMailbox = mailboxManager.getMailbox(mailboxPath, session); assertThat(mailboxId.isPresent()).isTrue(); assertThat(mailboxId.get()).isEqualTo(retrievedMailbox.getId()); }
@Test public void hasChildrenShouldReturnTrueWhenHasAChild() throws Exception { MailboxPath parentMailboxPath = MailboxPath.forUser(user, "inbox"); mailboxManager.createMailbox(parentMailboxPath, mailboxSession); MailboxId parentId = mailboxManager.getMailbox(parentMailboxPath, mailboxSession).getId(); MailboxPath mailboxPath = MailboxPath.forUser(user, "inbox.myBox"); mailboxManager.createMailbox(mailboxPath, mailboxSession); assertThat(sut.hasChildren(parentId, mailboxSession)).isTrue(); } }
@Override public void createMailbox(MailboxPath mailboxPath) throws Exception { MailboxManager mailboxManager = getMailboxManager(); MailboxSession mailboxSession = mailboxManager.createSystemSession(mailboxPath.getUser()); mailboxManager.startProcessingRequest(mailboxSession); mailboxManager.createMailbox(mailboxPath, mailboxSession); mailboxManager.logout(mailboxSession, true); mailboxManager.endProcessingRequest(mailboxSession); }
@Override protected void doProcess(CreateRequest request, ImapSession session, String tag, ImapCommand command, Responder responder) { final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(request.getMailboxName()); try { final MailboxManager mailboxManager = getMailboxManager(); mailboxManager.createMailbox(mailboxPath, ImapSessionUtils.getMailboxSession(session)); unsolicitedResponses(session, responder, false); okComplete(command, tag, responder); } catch (MailboxExistsException e) { LOGGER.debug("Create failed for mailbox {} as it already exists", mailboxPath, e); no(command, tag, responder, HumanReadableText.MAILBOX_EXISTS); } catch (TooLongMailboxNameException e) { LOGGER.debug("The mailbox name length is over limit: {}", mailboxPath.getName(), e); taggedBad(command, tag, responder, HumanReadableText.FAILURE_MAILBOX_NAME); } catch (MailboxException e) { LOGGER.error("Create failed for mailbox {}", mailboxPath, e); no(command, tag, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING); } }
/** * @see * org.apache.james.imap.processor.AbstractMailboxProcessor#doProcess(org.apache.james.imap.api.message.request.ImapRequest, * org.apache.james.imap.api.process.ImapSession, java.lang.String, * org.apache.james.imap.api.ImapCommand, * org.apache.james.imap.api.process.ImapProcessor.Responder) */ protected void doProcess(CreateRequest request, ImapSession session, String tag, ImapCommand command, Responder responder) { final MailboxPath mailboxPath = buildFullPath(session, request.getMailboxName()); try { final MailboxManager mailboxManager = getMailboxManager(); mailboxManager.createMailbox(mailboxPath, ImapSessionUtils.getMailboxSession(session)); unsolicitedResponses(session, responder, false); okComplete(command, tag, responder); } catch (MailboxExistsException e) { if (session.getLog().isDebugEnabled()) { session.getLog().debug("Create failed for mailbox " + mailboxPath + " as it already exists", e); } no(command, tag, responder, HumanReadableText.MAILBOX_EXISTS); } catch (MailboxException e) { if (session.getLog().isInfoEnabled()) { session.getLog().info("Create failed for mailbox " + mailboxPath, e); } no(command, tag, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING); } } }
@Test public void hasChildrenShouldReturnFalseWhenNoChild() throws Exception { MailboxPath mailboxPath = MailboxPath.forUser(user, "myBox"); mailboxManager.createMailbox(mailboxPath, mailboxSession); MailboxId mailboxId = mailboxManager.getMailbox(mailboxPath, mailboxSession).getId(); assertThat(sut.hasChildren(mailboxId, mailboxSession)).isFalse(); }
@Override public Mailbox createMailbox(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException { mailboxManager.createMailbox(mailboxPath, mailboxSession); MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession); return new SimpleMailbox(mailboxPath, UID_VALIDITY, messageManager.getId()); }
@Override protected Mailbox auth(POP3Session session, String username, String password) throws Exception { MailboxSession mSession = null; try { mSession = manager.login(session.getUser(), password); manager.startProcessingRequest(mSession); MailboxPath inbox = MailboxPath.inbox(mSession); // check if the mailbox exists, if not create it if (!manager.mailboxExists(inbox, mSession)) { Optional<MailboxId> mailboxId = manager.createMailbox(inbox, mSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId); } MessageManager mailbox = manager.getMailbox(MailboxPath.inbox(mSession), mSession); return new MailboxAdapter(manager, mailbox, mSession); } catch (BadCredentialsException e) { return null; } catch (MailboxException e) { throw new IOException("Unable to access mailbox for user " + session.getUser(), e); } finally { if (mSession != null) { manager.endProcessingRequest(mSession); } } }
@Override protected void doProcess(RenameRequest request, ImapSession session, String tag, ImapCommand command, Responder responder) { PathConverter pathConverter = PathConverter.forSession(session); MailboxPath existingPath = pathConverter.buildFullPath(request.getExistingName()); MailboxPath newPath = pathConverter.buildFullPath(request.getNewName()); try { final MailboxManager mailboxManager = getMailboxManager(); MailboxSession mailboxsession = ImapSessionUtils.getMailboxSession(session); mailboxManager.renameMailbox(existingPath, newPath, mailboxsession); if (existingPath.getName().equalsIgnoreCase(ImapConstants.INBOX_NAME) && !mailboxManager.mailboxExists(existingPath, mailboxsession)) { mailboxManager.createMailbox(existingPath, mailboxsession); } okComplete(command, tag, responder); unsolicitedResponses(session, responder, false); } catch (MailboxExistsException e) { LOGGER.debug("Rename from {} to {} failed because the target mailbox exists", existingPath, newPath, e); no(command, tag, responder, HumanReadableText.FAILURE_MAILBOX_EXISTS); } catch (MailboxNotFoundException e) { LOGGER.debug("Rename from {} to {} failed because the source mailbox doesn't exist", existingPath, newPath, e); no(command, tag, responder, HumanReadableText.MAILBOX_NOT_FOUND); } catch (TooLongMailboxNameException e) { LOGGER.debug("The mailbox name length is over limit: {}", newPath.getName(), e); taggedBad(command, tag, responder, HumanReadableText.FAILURE_MAILBOX_NAME); } catch (MailboxException e) { LOGGER.error("Rename from {} to {} failed", existingPath, newPath, e); no(command, tag, responder, HumanReadableText.GENERIC_FAILURE_DURING_PROCESSING); } }
mailboxManager.createMailbox(existingPath, mailboxsession);
@Test public void creatingConcurrentlyMailboxesWithSameParentShouldNotFail() throws Exception { MailboxSession session = mailboxManager.createSystemSession(USER_1); String mailboxName = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z"; ConcurrentTestRunner.builder() .operation((a, b) -> mailboxManager.createMailbox(MailboxPath.forUser(USER_1, mailboxName + a), session)) .threadCount(10) .runSuccessfullyWithin(Duration.ofMinutes(1)); }
@Test void appendShouldAddMessageToDesiredMailboxWhenMailboxExists() throws Exception { MailboxPath mailboxPath = MailboxPath.forUser(USER, FOLDER); mailboxManager.createMailbox(mailboxPath, session); testee.append(mimeMessage, USER, FOLDER); MessageResultIterator messages = mailboxManager.getMailbox(mailboxPath, session) .getMessages(MessageRange.all(), new FetchGroupImpl(MessageResult.FetchGroup.FULL_CONTENT), session); assertThat(messages).hasSize(1); }
Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId);