/** * Return a {@link MailboxPath} which represent the INBOX of the given * session * * @param session * @return inbox */ public static MailboxPath inbox(MailboxSession session) { return MailboxPath.forUser(session.getUser().asString(), MailboxConstants.INBOX); }
/** * Create a {@link MailboxPath} in the prive namespace of the specified user */ public static MailboxPath forUser(String username, String mailboxName) { return new MailboxPath(MailboxConstants.USER_NAMESPACE, username, mailboxName); }
public SimpleMailbox(MailboxPath path, long uidValidity, MailboxId mailboxId) { this.id = mailboxId; this.namespace = path.getNamespace(); this.user = path.getUser(); this.name = path.getName(); this.uidValidity = uidValidity; }
public Builder userAndNamespaceFrom(MailboxPath base) { Preconditions.checkState(!this.namespace.isPresent()); Preconditions.checkState(!this.username.isPresent()); this.namespace = Optional.ofNullable(base.getNamespace()); this.username = Optional.ofNullable(base.getUser()); return this; }
private Stream<MailboxPath> listAncestors(MailboxSession mailboxSession, MailboxPath mailboxPath) { return mailboxPath.getHierarchyLevels(mailboxSession.getPathDelimiter()) .stream() .filter(hierarchyMailboxPath -> !hierarchyMailboxPath.equals(mailboxPath)); }
@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 buildShouldConstructMailboxPathWhenPrivateUserMailboxes() throws Exception { MailboxPath expected = MailboxPath.forUser("user", ""); MailboxQuery actual = MailboxQuery.builder() .username("user") .privateNamespace() .build(); assertThat(actual.getNamespace()).contains(expected.getNamespace()); assertThat(actual.getUser()).contains(expected.getUser()); assertThat(actual.getMailboxNameExpression()).isEqualTo(Wildcard.INSTANCE); }
@Override public List<Mailbox> findMailboxWithPathLike(MailboxPath path) { List<Mailbox> mailboxesV2 = toMailboxes(path, mailboxPathV2DAO.listUserMailboxes(path.getNamespace(), path.getUser())); List<Mailbox> mailboxesV1 = toMailboxes(path, mailboxPathDAO.listUserMailboxes(path.getNamespace(), path.getUser())); List<Mailbox> mailboxesV1NotInV2 = mailboxesV1.stream() .filter(mailboxV1 -> mailboxesV2.stream() .map(Mailbox::generateAssociatedPath) .noneMatch(mailboxV2path -> mailboxV2path.equals(mailboxV1.generateAssociatedPath()))) .collect(Guavate.toImmutableList()); return ImmutableList.<Mailbox>builder() .addAll(mailboxesV2) .addAll(mailboxesV1NotInV2) .build(); }
@Test public void notTrueScriptShouldWork() throws Exception { prepareTestUsingScript("org/apache/james/transport/mailets/delivery/notTrue.script"); FakeMail mail = createMail(); testee.service(mail); assertThat(mail.getAttribute(MailStore.DELIVERY_PATH_PREFIX + LOCAL_PART)).isEqualTo(expressMailboxNameWithSlash(NOT_SELECTED_MAILBOX.getName())); }
@Test public void getHierarchyLevelsShouldBeOrdered() { assertThat(MailboxPath.forUser("user", "inbox.folder.subfolder") .getHierarchyLevels('.')) .containsExactly( MailboxPath.forUser("user", "inbox"), MailboxPath.forUser("user", "inbox.folder"), MailboxPath.forUser("user", "inbox.folder.subfolder")); }
private static void provisionUser(MailboxManager mailboxManager, String user) throws MailboxException { MailboxSession mailboxSession = mailboxManager.createSystemSession(user); mailboxManager.startProcessingRequest(mailboxSession); createMailbox(mailboxManager, mailboxSession, MailboxPath.inbox(mailboxSession)); IntStream.range(0, SUB_MAILBOXES_COUNT) .mapToObj(i -> MailboxConstants.INBOX + ".SUB_FOLDER_" + i) .peek(name -> createMailbox(mailboxManager, mailboxSession, MailboxPath.forUser(user, name))) .forEach(name -> createSubSubMailboxes(mailboxManager, mailboxSession, name)); mailboxManager.endProcessingRequest(mailboxSession); mailboxManager.logout(mailboxSession, true); }
private boolean isAddedToOutboxEvent(String messageId, Event event) { if (!(event instanceof EventFactory.AddedImpl)) { return false; } EventFactory.AddedImpl added = (EventFactory.AddedImpl) event; return added.getMailboxPath().equals(MailboxPath.forUser(USERNAME, DefaultMailboxes.OUTBOX)) && added.getUids().size() == 1 && added.getMetaData(added.getUids().get(0)).getMessageId().serialize().equals(messageId); }
private void updateMailbox(Mailbox mailbox, MailboxUpdateRequest updateRequest, MailboxSession mailboxSession) throws MailboxException { MailboxPath originMailboxPath = mailboxManager.getMailbox(mailbox.getId(), mailboxSession).getMailboxPath(); MailboxPath destinationMailboxPath = computeNewMailboxPath(mailbox, originMailboxPath, updateRequest, mailboxSession); if (updateRequest.getSharedWith().isPresent()) { mailboxManager.setRights(originMailboxPath, updateRequest.getSharedWith() .get() .removeEntriesFor(Username.forMailboxPath(originMailboxPath)) .toMailboxAcl(), mailboxSession); } if (!originMailboxPath.equals(destinationMailboxPath)) { mailboxManager.renameMailbox(originMailboxPath, destinationMailboxPath, mailboxSession); subscriptionManager.unsubscribe(mailboxSession, originMailboxPath.getName()); subscriptionManager.subscribe(mailboxSession, destinationMailboxPath.getName()); } }
throws MailboxException { LOGGER.debug("createMailbox {}", mailboxPath); if (mailboxPath.getName().isEmpty()) { LOGGER.warn("Ignoring mailbox with empty name"); } else { MailboxPath sanitizedMailboxPath = mailboxPath.sanitize(mailboxSession.getPathDelimiter()); if (isMailboxNameTooLong(mailboxPath)) { throw new TooLongMailboxNameException("Mailbox name exceed maximum size of " + MAX_MAILBOX_NAME_LENGTH + " characters"); throw new MailboxExistsException(sanitizedMailboxPath.asString()); for (MailboxPath mailbox : sanitizedMailboxPath.getHierarchyLevels(getDelimiter())) { locker.executeWithLock(mailboxSession, mailbox, (LockAwareExecution<Void>) () -> { if (!mailboxExists(mailbox, mailboxSession)) {
@Override public boolean isNoop() { return newPath.equals(path); }
private MailboxNamespace getNamespace(MailboxPath mailboxPath, boolean isOwner) { if (isOwner) { return MailboxNamespace.personal(); } return MailboxNamespace.delegated(mailboxPath.getUser()); }
@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 isInboxShouldReturnTrueWhenINBOX() { MailboxPath mailboxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, "user", DefaultMailboxes.INBOX); assertThat(mailboxPath.isInbox()).isTrue(); }
@Override public String toString() { return asString(); }