Example usage for javax.mail Folder renameTo

List of usage examples for javax.mail Folder renameTo

Introduction

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

Prototype

public abstract boolean renameTo(Folder f) throws MessagingException;

Source Link

Document

Rename this Folder.

Usage

From source file:de.saly.elasticsearch.imap.AbstractIMAPRiverUnitTest.java

protected void renameMailbox(final Properties props, final String folderName, final String user,
        final String password) throws MessagingException {
    final Store store = Session.getInstance(props).getStore();

    store.connect(user, password);//from  w  ww .  j a  v a  2s. co  m
    checkStoreForTestConnection(store);
    final Folder f = store.getFolder(folderName);

    f.renameTo(store.getFolder("renamed_from_" + folderName));

    logger.info("Renamed " + f.getFullName());
    store.close();

}

From source file:br.unicamp.cotuca.dpd.pd12.acinet.vagalmail.servlet.Configuracoes.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("UTF-8");
    request.setCharacterEncoding("UTF-8");

    if (request.getRequestURI().contains("/pasta")) {
        String acao = request.getParameter("acao"), url = request.getParameter("url"),
                novo = request.getParameter("novo");

        JSONObject obj = new JSONObject();
        if (acao == null || url == null) {
            obj.put("erro", "Solicitao invlida");
            obj.writeJSONString(response.getWriter());
            return;
        }/* w w w .  ja v a 2  s  .c  o  m*/

        if ((acao.equals("renomear") || acao.equals("nova")) && novo == null) {
            obj.put("erro", "Solicitao invlida");
            obj.writeJSONString(response.getWriter());
            return;
        }

        try {
            Conta conta = (Conta) request.getSession().getAttribute("conta");
            Store store = Logar.getImapStore(conta);

            Folder pasta = null;

            if (!acao.equals("nova")) {
                URLName urln = new URLName(url);
                pasta = store.getFolder(urln);

                if (pasta.isOpen())
                    pasta.close(false);
            }

            switch (acao) {
            case "renomear":
                if (pasta.renameTo(store.getFolder(novo))) {
                    obj.put("sucesso", "Renomeado com sucesso");
                } else {
                    obj.put("erro", "Erro ao renomear a pasta");
                }
                break;
            case "esvaziar":
                pasta.open(Folder.READ_WRITE);
                pasta.setFlags(1, pasta.getMessageCount(), new Flags(Flags.Flag.DELETED), true);
                pasta.expunge();
                obj.put("sucesso", "Esvaziado com sucesso");
                break;
            case "excluir":
                if (pasta.delete(true)) {
                    obj.put("sucesso", "Excludo com sucesso");
                } else {
                    obj.put("erro", "Erro ao excluir a pasta");
                }
                break;
            case "nova":
                pasta = store.getFolder(novo);
                if (!pasta.exists()) {
                    if (pasta.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES)) {
                        obj.put("sucesso", "Criado com sucesso");
                    } else {
                        obj.put("erro", "Erro ao criar a pasta");
                    }
                } else {
                    obj.put("erro", "Erro ao criar a pasta");
                }
                break;
            }
        } catch (MessagingException ex) {
            obj.put("erro", "Erro ao processar solicitao");
        }

        obj.writeJSONString(response.getWriter());

        return;
    }

    String metodo = request.getParameter("acao");
    if (metodo == null) {
        return;
    } else if (metodo.equals("nova")) {
        EntityManager em = BD.getEntityManager();

        try {
            em.getTransaction().begin();

            Login usuario = Logar.getLogin(request.getSession());

            Conta conta = new Conta();
            conta.setEmail(request.getParameter("email"));
            conta.setImapHost(request.getParameter("imapHost"));
            conta.setImapPort(Integer.parseInt(request.getParameter("imapPort")));
            conta.setImapPassword(request.getParameter("imapPassword"));
            conta.setImapUser(request.getParameter("imapLogin"));
            conta.setSmtpHost(request.getParameter("smtpHost"));
            conta.setSmtpPort(Integer.parseInt(request.getParameter("smtpPort")));
            conta.setSmtpPassword(request.getParameter("smtpPassword"));
            conta.setSmtpUser(request.getParameter("smtpLogin"));
            conta.setIdLogin(usuario);

            em.persist(conta);
            em.merge(usuario);

            em.getTransaction().commit();
            em.refresh(conta);
            em.refresh(usuario);

            request.setAttribute("mensagem", "sucesso");
        } catch (Logar.NaoAutenticadoException | PersistenceException ex) {
            em.getTransaction().rollback();

            request.setAttribute("mensagem", "erro");
        }

        request.getRequestDispatcher("/conf.jsp?metodo=nova").forward(request, response);
    } else if (metodo.equals("conta")) {
        EntityManager em = BD.getEntityManager();

        try {
            em.getTransaction().begin();

            Conta conta = (Conta) request.getSession().getAttribute("conta");

            em.refresh(conta);
            conta.setEmail(request.getParameter("email"));
            conta.setImapHost(request.getParameter("imapHost"));
            conta.setImapPort(Integer.parseInt(request.getParameter("imapPort")));
            conta.setImapPassword(request.getParameter("imapPassword"));
            conta.setImapUser(request.getParameter("imapLogin"));
            conta.setSmtpHost(request.getParameter("smtpHost"));
            conta.setSmtpPort(Integer.parseInt(request.getParameter("smtpPort")));
            conta.setSmtpPassword(request.getParameter("smtpPassword"));
            conta.setSmtpUser(request.getParameter("smtpLogin"));

            em.getTransaction().commit();

            request.setAttribute("mensagem", "sucesso");
        } catch (PersistenceException ex) {
            em.getTransaction().rollback();

            request.setAttribute("mensagem", "erro");
        }

        request.getRequestDispatcher("/conf.jsp?metodo=conta").forward(request, response);

    } else if (metodo.equals("senha")) {
        EntityManager em = BD.getEntityManager();

        try {

            Login usuario = Logar.getLogin(request.getSession());

            em.refresh(usuario);

            String senatu = request.getParameter("senAtu"), novasen = request.getParameter("senNova"),
                    novasen2 = request.getParameter("senNova2");

            if (novasen.equals(novasen2) && senatu.equals(usuario.getSenha())) {

                em.getTransaction().begin();

                usuario.setSenha(novasen);

                em.getTransaction().commit();

                request.setAttribute("mensagem", "sucesso");
            } else {
                if (!novasen.equals(novasen2))
                    request.setAttribute("mensagem", "senneq");
                else
                    request.setAttribute("mensagem", "antsen");
            }
        } catch (Logar.NaoAutenticadoException | PersistenceException ex) {
            em.getTransaction().rollback();

            request.setAttribute("mensagem", "erro");
        }

        request.getRequestDispatcher("/conf.jsp?metodo=senha").forward(request, response);

    }
}

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

public String renameFolder(String orig, String newname) throws MessagingException {
    FolderCache fc = getFolderCache(orig);
    FolderCache fcparent = fc.getParent();
    Folder oldfolder = fc.getFolder();
    destroyFolderCache(fc);//  w  w  w . j  a v a2s.  c om
    Folder newfolder = fcparent.getFolder().getFolder(newname);
    boolean done = oldfolder.renameTo(newfolder);
    if (!done) {
        throw new MessagingException("Rename failed");
    }

    //trick for Dovecot on NethServer: under shared folders, create and destroy a fake folder
    //or rename will not work correctly
    if (isUnderSharedFolder(newfolder.getFullName())) {
        Map<String, String> map = ((IMAPStore) store).id(null);
        if (map != null && map.containsKey("name") && map.get("name").equalsIgnoreCase("dovecot")) {
            String trickName = "_________" + System.currentTimeMillis();
            Folder trickFolder = fcparent.getFolder().getFolder(trickName);
            try {
                trickFolder.create(Folder.READ_ONLY);
                trickFolder.delete(true);
            } catch (MessagingException exc) {

            }
        }
    }

    addFoldersCache(fcparent, newfolder);
    return newfolder.getFullName();
}

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

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

    try {/*w  ww  .  ja  v  a  2s .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);
    }
}

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

public FolderCache moveFolder(String source, String dest) throws MessagingException {
    Folder oldfolder = getFolder(source);
    String oldname = oldfolder.getName();
    //System.out.println("moveFolder "+source+" -> "+dest);
    Folder newfolder;//from ww  w .j av  a 2  s  .c  o  m
    if (dest != null && dest.trim().length() > 0) {
        String newname = dest + folderSeparator + oldname;
        newfolder = getFolder(newname);
    } else {
        if (hasDifferentDefaultFolder) {
            String prefix = getDefaultFolder().getFullName();
            String newname = oldname;
            if (prefix != null)
                newname = prefix + folderSeparator + newname;
            newfolder = getFolder(newname);
        } else {
            String newname = oldname;
            if (folderPrefix != null) {
                newname = folderPrefix + newname;
            }
            newfolder = getFolder(newname);
        }
    }
    FolderCache fcsrc = getFolderCache(source);
    if (fcsrc != null) {
        destroyFolderCache(fcsrc);
    }
    boolean done = oldfolder.renameTo(newfolder);
    if (done) {
        if (dest != null) {
            FolderCache tfc = getFolderCache(newfolder.getParent().getFullName());
            return addFoldersCache(tfc, newfolder);
        } else {
            return addFoldersCache(fcRoot, newfolder);
        }
    }
    return null;
}