Example usage for javax.mail Folder exists

List of usage examples for javax.mail Folder exists

Introduction

In this page you can find the example usage for javax.mail Folder exists.

Prototype

public abstract boolean exists() throws MessagingException;

Source Link

Document

Tests if this folder physically exists on the Store.

Usage

From source file:com.cubusmail.server.mail.imap.IMAPMailbox.java

public IMailFolder createFolder(String parentFolderId, String folderName) throws MailFolderException {

    try {// w  ww  .  j a va 2 s  . c  o  m

        String newFolderName = null;
        if (!StringUtils.isEmpty(parentFolderId)) {
            newFolderName = parentFolderId + getFolderSeparator() + folderName;
        } else {
            newFolderName = folderName;
        }

        Folder newFolder = this.store.getFolder(newFolderName);
        if (!newFolder.exists()) {
            log.debug("Creating folder... " + newFolderName);
            boolean success = newFolder.create(Folder.HOLDS_MESSAGES);
            if (!success) {
                throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_CREATE, null);
            }
            newFolder.setSubscribed(true);
        } else {
            throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_ALREADY_EXIST, null);
        }
        loadMailFolder();

        return createMailFolder(newFolder);
    } catch (MessagingException ex) {
        throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_CREATE, ex);
    }
}

From source file:com.cubusmail.server.mail.imap.IMAPMailbox.java

public IMailFolder moveFolder(String sourceFolderId, String targetFolderId) throws MailFolderException {

    IMailFolder sourceFolder = null;// ww  w  .  ja v  a 2 s  . co m
    IMailFolder targetFolder = null;

    try {
        String newFolderName;

        sourceFolder = getMailFolderById(sourceFolderId);
        targetFolder = getMailFolderById(targetFolderId);

        if (sourceFolder != null) {
            if (targetFolder == null) {
                newFolderName = sourceFolder.getName();
            } else {
                newFolderName = targetFolder.getId() + getFolderSeparator() + sourceFolder.getName();
            }

            if (sourceFolder.isOpen()) {
                sourceFolder.close(false);
            }
            if (targetFolder != null && targetFolder.isOpen()) {
                targetFolder.close(false);
            }

            Folder newFolder = this.store.getFolder(newFolderName);
            if (!newFolder.exists()) {
                sourceFolder.renameTo(newFolder);
            } else {
                throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_ALREADY_EXIST, null, sourceFolder);
            }
            loadMailFolder();

            return createMailFolder(newFolder);
        }
    } catch (MessagingException e) {
        throw new MailFolderException(IErrorCodes.EXCEPTION_FOLDER_MOVE, e, sourceFolder);
    }

    return null;
}

From source file:org.jasig.portlet.emailpreview.dao.javamail.JavamailAccountDaoImpl.java

private EmailQuota getQuota(Folder folder) {
    if (!(folder instanceof IMAPFolder)) {
        return null;
    }/*from w  ww. j  a va2s .c  o  m*/
    try {
        // Make sure the account is activated and contains messages
        if (folder.exists() && folder.getMessageCount() > 0) {
            Quota[] quotas = ((IMAPFolder) folder).getQuota();

            for (Quota quota : quotas) {
                for (Quota.Resource resource : quota.resources) {
                    if (resource.name.equals("STORAGE")) {
                        return new EmailQuota(resource.limit, resource.usage);
                    }
                }
            }
        }
    } catch (MessagingException e) {
        log.error("Failed to connect or get quota for mail user ");
    }
    return null;
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

private Folder testAttachmentsGuts(final ImapMailReceiver receiver) throws MessagingException, IOException {
    Store store = mock(Store.class);
    Folder folder = mock(Folder.class);
    given(folder.exists()).willReturn(true);
    given(folder.isOpen()).willReturn(true);

    Message message = new MimeMessage(null, new ClassPathResource("test.mail").getInputStream());
    given(folder.search((SearchTerm) Mockito.any())).willReturn(new Message[] { message });
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    DirectFieldAccessor df = new DirectFieldAccessor(receiver);
    df.setPropertyValue("store", store);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();//from   w  w w .j  a v  a  2  s . c  om

    return folder;
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

public void deleteFolder(long folderId) throws PortalException {
    try {//from   w  w  w  .  j  av a  2 s  .  c  o m
        Folder jxFolder = getFolder(folderId);

        if (!jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_DOES_NOT_EXIST);
        }

        jxFolder.delete(true);

        if (jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_DELETE_FAILED);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    }
}

From source file:org.springframework.integration.mail.ImapMailReceiverTests.java

@Test // see INT-1801
public void testImapLifecycleForRaceCondition() throws Exception {

    for (int i = 0; i < 1000; i++) {
        final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
        Store store = mock(Store.class);
        Folder folder = mock(Folder.class);
        given(folder.exists()).willReturn(true);
        given(folder.isOpen()).willReturn(true);
        given(folder.search((SearchTerm) Mockito.any())).willReturn(new Message[] {});
        given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
        given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));

        DirectFieldAccessor df = new DirectFieldAccessor(receiver);
        df.setPropertyValue("store", store);
        receiver.setBeanFactory(mock(BeanFactory.class));
        receiver.afterPropertiesSet();/*from   ww  w  .j av  a2s.  co m*/

        new Thread(() -> {
            try {
                receiver.receive();
            } catch (javax.mail.MessagingException e) {
                if (e.getCause() instanceof NullPointerException) {
                    failed.getAndIncrement();
                }
            }

        }).start();

        new Thread(() -> {
            try {
                receiver.destroy();
            } catch (Exception ignore) {
                // ignore
            }
        }).start();
    }
    assertEquals(0, failed.get());
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

public String[] addFolder(String displayName) throws MailException {
    try {//from   w  w w.  ja va  2  s .c  o m
        String fullName = displayName;

        if (Validator.isNotNull(_account.getFolderPrefix())) {
            Store store = _imapConnection.getStore(true);

            Folder jxFolder = store.getDefaultFolder();

            fullName = _account.getFolderPrefix() + jxFolder.getSeparator() + displayName;
        }

        Folder jxFolder = getFolder(fullName);

        if (jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_ALREADY_EXISTS);
        } else {
            if (jxFolder.create(Folder.HOLDS_MESSAGES)) {
                return new String[] { jxFolder.getFullName(), jxFolder.getName() };
            }

            throw new MailException(MailException.FOLDER_CREATE_FAILED);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    }
}

From source file:com.sonicle.webtop.mail.MailAccount.java

public boolean checkFolder(String foldername) throws MessagingException {
    Folder folder = store.getFolder(foldername);
    return folder.exists();
}

From source file:com.sonicle.webtop.mail.MailAccount.java

public Folder checkCreateFolder(String foldername) throws MessagingException {
    Folder folder = store.getFolder(foldername);
    if (!folder.exists()) {
        folder.create(Folder.HOLDS_MESSAGES | Folder.HOLDS_FOLDERS);
    }//from   w  ww .  j av  a 2s  .co m
    return folder;
}

From source file:com.liferay.mail.imap.IMAPAccessor.java

public String[] renameFolder(long folderId, String newDisplayName) throws PortalException {

    try {//from ww w.j  a v  a  2 s .c o  m
        Folder jxFolder = getFolder(folderId);

        String newFullName = StringPool.BLANK;

        if (Validator.isNotNull(_account.getFolderPrefix())) {
            Store store = _imapConnection.getStore(true);

            Folder defaultJxFolder = store.getDefaultFolder();

            newFullName = _account.getFolderPrefix() + defaultJxFolder.getSeparator() + newDisplayName;
        }

        if (!jxFolder.exists()) {
            throw new MailException(MailException.FOLDER_DOES_NOT_EXIST);
        }

        if (jxFolder.renameTo(getFolder(newFullName))) {
            return new String[] { jxFolder.getFullName(), jxFolder.getName() };
        } else {
            throw new MailException(MailException.FOLDER_RENAME_FAILED);
        }
    } catch (MessagingException me) {
        throw new MailException(me);
    }
}