private boolean mailboxDoesntExist(MailboxPath mailboxPath, MailboxSession session) { try { return !mailboxManager.mailboxExists(mailboxPath, session); } catch (MailboxException e) { throw new RuntimeException(e); } }
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 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."); } } }
@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)); } }
if (!mailboxManager.mailboxExists(path, session)) { mailboxManager.createMailbox(path, session);
@Test public void processShouldRespondNoOnUnExistingTargetMailbox() throws Exception { CopyRequest copyRequest = new CopyRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(4, 6)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(false); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class), any(StatusResponse.ResponseCode.class))).thenReturn(noResponse); testee.process(copyRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@Test public void processShouldRespondNoOnMailboxException() throws Exception { CopyRequest copyRequest = new CopyRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(4, 6)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenThrow(new MailboxException()); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class))).thenReturn(noResponse); testee.process(copyRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@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); } } }
@Test public void processShouldRespondNoOnUnExistingTargetMailbox() throws Exception { MoveRequest moveRequest = new MoveRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(5, 6), new IdRange(1,3)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(false); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class), any(StatusResponse.ResponseCode.class))).thenReturn(noResponse); testee.process(moveRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@Test public void processShouldRespondNoOnMailboxException() throws Exception { MoveRequest moveRequest = new MoveRequest(ImapCommand.anyStateCommand("Name"), new IdRange[] {new IdRange(5, 6), new IdRange(1,3)}, ImapConstants.INBOX_NAME, true, TAG); MailboxSession.User user = mock(MailboxSession.User.class); when(user.getUserName()).thenReturn("username"); when(mockMailboxSession.getUser()).thenReturn(user); when(mockMailboxSession.getSessionId()).thenReturn(42L); when(mockImapSession.getState()).thenReturn(ImapSessionState.SELECTED); when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mockMailboxSession); MailboxPath inbox = MailboxPath.inbox(mockMailboxSession); MailboxPath selected = new MailboxPath(inbox, "selected"); SelectedMailbox selectedMailbox = mock(SelectedMailbox.class); when(selectedMailbox.getLastUid()).thenReturn(Optional.of(MessageUid.of(8))); when(selectedMailbox.existsCount()).thenReturn(8L); when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenThrow(new MailboxException()); StatusResponse noResponse = mock(StatusResponse.class); when(mockStatusResponseFactory.taggedNo(any(String.class), any(ImapCommand.class), any(HumanReadableText.class))).thenReturn(noResponse); testee.process(moveRequest, mockResponder, mockImapSession); verify(mockMailboxManager).startProcessingRequest(mockMailboxSession); verify(mockMailboxManager).endProcessingRequest(mockMailboxSession); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockResponder).respond(noResponse); verifyNoMoreInteractions(mockMailboxManager, mockResponder, mockNextProcessor); }
@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.renameMailbox(existingPath, newPath, mailboxsession); if (existingPath.getName().equalsIgnoreCase(ImapConstants.INBOX_NAME) && mailboxManager.mailboxExists(existingPath, mailboxsession) == false) { mailboxManager.createMailbox(existingPath, mailboxsession);
when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(true); MessageManager targetMessageManager = mock(MessageManager.class); when(mockMailboxManager.getMailbox(inbox, mockMailboxSession)).thenReturn(targetMessageManager); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockMailboxManager).getMailbox(inbox, mockMailboxSession); verify(mockMailboxManager).moveMessages(MessageRange.range(MessageUid.of(5), MessageUid.of(6)), selected, inbox, mockMailboxSession);
when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(true); MessageManager targetMessageManager = mock(MessageManager.class); when(mockMailboxManager.getMailbox(inbox, mockMailboxSession)).thenReturn(targetMessageManager); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockMailboxManager).getMailbox(inbox, mockMailboxSession); verify(mockMailboxManager).copyMessages(MessageRange.range(MessageUid.of(4), MessageUid.of(6)), selected, inbox, mockMailboxSession);
when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(true); MessageManager targetMessageManager = mock(MessageManager.class); when(mockMailboxManager.getMailbox(inbox, mockMailboxSession)).thenReturn(targetMessageManager); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockMailboxManager).getMailbox(inbox, mockMailboxSession); verify(mockMailboxManager).moveMessages(MessageRange.range(MessageUid.of(4), MessageUid.of(6)), selected, inbox, mockMailboxSession);
when(selectedMailbox.getPath()).thenReturn(selected); when(mockImapSession.getSelected()).thenReturn(selectedMailbox); when(mockMailboxManager.mailboxExists(inbox, mockMailboxSession)).thenReturn(true); MessageManager targetMessageManager = mock(MessageManager.class); when(mockMailboxManager.getMailbox(inbox, mockMailboxSession)).thenReturn(targetMessageManager); verify(mockMailboxManager).mailboxExists(inbox, mockMailboxSession); verify(mockMailboxManager).getMailbox(inbox, mockMailboxSession); verify(mockMailboxManager).copyMessages(MessageRange.range(MessageUid.of(5), MessageUid.of(6)), selected, inbox, mockMailboxSession);
if (mailboxManager.mailboxExists(inbox, mailboxSession) == false) { mailboxManager.createMailbox(inbox, mailboxSession);
if (mailboxManager.mailboxExists(mailboxPath, mSession) == false) { mailboxManager.createMailbox(mailboxPath, mSession);
final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session); final MailboxManager mailboxManager = getMailboxManager(); final boolean mailboxExists = mailboxManager.mailboxExists(targetMailbox, mailboxSession);
if (!mailboxManager.mailboxExists(inbox, mailboxSession)) { Optional<MailboxId> mailboxId = mailboxManager.createMailbox(inbox, mailboxSession); LOGGER.info("Provisioning INBOX. {} created.", mailboxId);