Example usage for javax.mail Folder isSubscribed

List of usage examples for javax.mail Folder isSubscribed

Introduction

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

Prototype

public boolean isSubscribed() 

Source Link

Document

Returns true if this Folder is subscribed.

Usage

From source file:folderlist.java

static void dumpFolder(Folder folder, boolean recurse, String tab) throws Exception {
    System.out.println(tab + "Name:      " + folder.getName());
    System.out.println(tab + "Full Name: " + folder.getFullName());
    System.out.println(tab + "URL:       " + folder.getURLName());

    if (verbose) {
        if (!folder.isSubscribed())
            System.out.println(tab + "Not Subscribed");

        if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
            if (folder.hasNewMessages())
                System.out.println(tab + "Has New Messages");
            System.out.println(tab + "Total Messages:  " + folder.getMessageCount());
            System.out.println(tab + "New Messages:    " + folder.getNewMessageCount());
            System.out.println(tab + "Unread Messages: " + folder.getUnreadMessageCount());
        }//  w  w  w  .j  ava 2 s .c  o  m
        if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
            System.out.println(tab + "Is Directory");

        /*
         * Demonstrate use of IMAP folder attributes
         * returned by the IMAP LIST response.
         */
        if (folder instanceof IMAPFolder) {
            IMAPFolder f = (IMAPFolder) folder;
            String[] attrs = f.getAttributes();
            if (attrs != null && attrs.length > 0) {
                System.out.println(tab + "IMAP Attributes:");
                for (int i = 0; i < attrs.length; i++)
                    System.out.println(tab + "    " + attrs[i]);
            }
        }
    }

    System.out.println();

    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
        if (recurse) {
            Folder[] f = folder.list();
            for (int i = 0; i < f.length; i++)
                dumpFolder(f[i], recurse, tab + "    ");
        }
    }
}

From source file:MainClass.java

static void dumpFolder(Folder folder, boolean recurse, String tab) throws Exception {
    System.out.println(tab + "Name:      " + folder.getName());
    System.out.println(tab + "Full Name: " + folder.getFullName());
    System.out.println(tab + "URL:       " + folder.getURLName());

    if (verbose) {
        if (!folder.isSubscribed())
            System.out.println(tab + "Not Subscribed");

        if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
            if (folder.hasNewMessages())
                System.out.println(tab + "Has New Messages");
            System.out.println(tab + "Total Messages:  " + folder.getMessageCount());
            System.out.println(tab + "New Messages:    " + folder.getNewMessageCount());
            System.out.println(tab + "Unread Messages: " + folder.getUnreadMessageCount());
        }/*from w  w  w  . j a  v a 2 s . co  m*/
        if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
            System.out.println(tab + "Is Directory");

        /*
         * Demonstrate use of IMAP folder attributes returned by the IMAP LIST
         * response.
         */
        if (folder instanceof IMAPFolder) {
            IMAPFolder f = (IMAPFolder) folder;
            String[] attrs = f.getAttributes();
            if (attrs != null && attrs.length > 0) {
                System.out.println(tab + "IMAP Attributes:");
                for (int i = 0; i < attrs.length; i++)
                    System.out.println(tab + "    " + attrs[i]);
            }
        }
    }

    System.out.println();

    if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
        if (recurse) {
            Folder[] f = folder.list();
            for (int i = 0; i < f.length; i++)
                dumpFolder(f[i], recurse, tab + "    ");
        }
    }
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Construct the folder subtree for the given folder and append it to
 * xml_parent.//from  w w  w .  j  a v  a  2 s. c om
 *
 * N.b. this method does not necessarily create a new XML Folder Element.
 * If called with subscribed_only and the target Folder (and in some
 * cases its descendants) are not subscribed, no Element will be created
 * and 0 will be returned.
 * <P>
 * Pop servers don't support nesting at all, so you'll just get a single
 * level out of this method.
 * <P>
 * There is a critical subscribed_only difference in behavior between
 * Maildir and mbox type mail servers.
 * Maildir folders are all HOLDS_MESSAGES, whether empty or not, and
 * these folders have a subscribed attribute which the user can set,
 * and which we honor.
 * mbox folders, on the other hand, have no subscribed attribute for
 * their !HOLDS_MESSAGE folders, so we must recurse to all of the
 * descendant HOLDS_MESSAGE folders to see if we should show.
 *
 * @param folder the folder where we begin
 * @param xml_parent XML Element where the gathered information will be
 *                   appended
 * @param subscribed_only Only add 'subscribed' folders
 * @param doCount Whether to generate message counts for Elements
 *                corresponding to HOLDS_MESSAGE folders.
 * @returns maximum depth of the folder tree (needed to calculate the
 *     necessary columns in a table).  Returns 0 if no XML elements added.
 */
protected int getFolderTree(Folder folder, Element xml_parent, boolean subscribed_only, boolean doCount) {
    int generatedDepth = 0;
    int folderType;
    Element xml_folder;

    try {
        folderType = folder.getType();
    } catch (MessagingException ex) {
        log.error("Can't get enough info from server to even make Gui node", ex);
        xml_parent.setAttribute("error", "For child '" + folder.getName() + ":  " + ex.getMessage());
        return 0;
    }
    boolean holds_folders = (folderType & Folder.HOLDS_FOLDERS) != 0;
    boolean holds_messages = (folderType & Folder.HOLDS_MESSAGES) != 0;
    // Sanity check:
    if ((!holds_folders) && !holds_messages) {
        log.fatal("Folder can hold neither folders nor messages: " + folder.getFullName());
        throw new RuntimeException("Folder can hold neither folders nor messages: " + folder.getFullName());
    }
    if (subscribed_only && holds_messages && !folder.isSubscribed())
        return generatedDepth; // Return right away and save a LOT OF WORK
    // N.b. we honor folder.isSubscribed() only for holds_message
    // folders.  That means all Maildir server folders, and all
    // mbox server folders except for mbox directories.  In this
    // last case, we must recurse to determine whether to show folder.
    String id = generateFolderHash(folder);
    xml_folder = model.createFolder(id, folder.getName(), holds_folders, holds_messages);
    // XMLUserModel.createFolder() declares no throws.  If any Exceptions
    // are expected from it, move the statement above into the try block.
    // The xml_folder Element here will be orphaned and GC'd if we don't
    // appendChild (in which case we return 0).

    if (doCount && holds_messages)
        try {
            // this folder will definitely be added!
            /* This folder can contain messages */
            Element messagelist = model.createMessageList();

            int total_messages = folder.getMessageCount();
            int new_messages = folder.getNewMessageCount();

            if (total_messages == -1 || new_messages == -1 || !folder.isOpen()) {
                folder.open(Folder.READ_ONLY);
                total_messages = folder.getMessageCount();
                new_messages = folder.getNewMessageCount();
            }
            if (folder.isOpen())
                folder.close(false);

            messagelist.setAttribute("total", total_messages + "");
            messagelist.setAttribute("new", new_messages + "");
            log.debug("Counted " + new_messages + '/' + total_messages + " for folder " + folder.getFullName());
            xml_folder.appendChild(messagelist);
        } catch (MessagingException ex) {
            log.warn("Failed to count messages in folder '" + folder.getFullName() + "'", ex);
            xml_folder.setAttribute("error", ex.getMessage());
        }

    int descendantDepth = 0;
    if (holds_folders)
        try {
            Set<String> fullNameSet = new HashSet<String>();

            /* Recursively add subfolders to the XML model */
            // DO NOT USE listSubscribed(), because with !HOLDS_MESSAGE
            // folders, that skips non-Message Folders which may contain
            // subscribed descendants!
            for (Folder f : folder.list()) {
                if (!fullNameSet.add(f.getFullName())) {
                    log.warn("Skipping duplicate subfolder returned by mail" + " server:  " + f.getFullName());
                    continue;
                }
                if (subscribed_only && (f.getType() & Folder.HOLDS_MESSAGES) != 0 && !f.isSubscribed())
                    continue;
                /* If we recursed here, the getFolderTree() would
                 * just return 0 and no harm done.
                 * Just helping performance by preventing a recursion
                 * here.
                 * For comment on the logic here, see the same test
                 * towards the top of this method (before recursion).
                 */
                int depth = getFolderTree(f, xml_folder, subscribed_only, doCount);
                if (depth > descendantDepth)
                    descendantDepth = depth;
            }
            generatedDepth += descendantDepth;
        } catch (MessagingException ex) {
            xml_folder.setAttribute("error", ex.getMessage());
        }

    // We've already validated that if subscribed_only and holds_message
    //  then folder is subcribed.  Also verified either holds_m or holds_f.
    //  Only have to check the !holds_message case.
    if (subscribed_only && (!holds_messages) && descendantDepth < 1) {
        xml_folder = null;
        // Unnecessary, but may encourage GC
        return generatedDepth;
    }

    /* We ALWAYS return only subscribed folders except for these two
     * distinct cases: */
    xml_folder.setAttribute("subscribed",
            ((holds_messages && !folder.isSubscribed()) || ((!holds_messages) && descendantDepth < 1)) ? "false"
                    : "true");
    // N.b. our Element's "subscribed" element does not correspond 1:1
    // to Folder.isSubscribed(), since non-message-holding Folders have
    // no "subscribed" attribute.
    folders.put(id, folder);

    xml_parent.appendChild(xml_folder);
    generatedDepth++; // Add the count for xml_folder
    return generatedDepth;
}

From source file:net.wastl.webmail.server.WebMailSession.java

/**
 * Refresh Information about folders./*from   ww  w . ja va  2  s .co  m*/
 * Tries to connect folders that are not yet connected.
 *
 * @doCount display message counts for user
 */
public void refreshFolderInformation(boolean subscribed_only, boolean doCount) {
    /* Right now, doCount corresponds exactly to subscribed_only.
     * When we add a user preference setting or one-time action,
     * to present messages from all folders, we will have
     * subscribed_only false and doCount true. */

    //log.fatal("Invoking refreshFolderInformation(boolean, boolean)",
    //new Throwable("Thread Dump"));  FOR DEBUGGING
    setEnv();
    if (folders == null)
        folders = new Hashtable<String, Folder>();
    Folder rootFolder = null;
    String cur_mh_id = "";
    Enumeration mailhosts = user.mailHosts();
    int max_depth = 0;
    int folderType;

    while (mailhosts.hasMoreElements()) {
        cur_mh_id = (String) mailhosts.nextElement();

        MailHostData mhd = user.getMailHost(cur_mh_id);

        URLName url = new URLName(mhd.getHostURL());

        Element mailhost = model.createMailhost(mhd.getName(), mhd.getID(), url.toString());

        int depth = 0;

        try {
            rootFolder = getRootFolder(cur_mh_id);

            try {
                rootFolder.setSubscribed(true);
            } catch (MessagingException ex) {
                // Only IMAP supports subscription
                log.warn("Folder.setSubscribed failed.  " + "Probably a non-supporting mail service: " + ex);
            }
        } catch (MessagingException ex) {
            mailhost.setAttribute("error", ex.getMessage());
            log.warn("Failed to connect and get Root folder from (" + url + ')', ex);
            return;
        }

        try {
            depth = getFolderTree(rootFolder.getFolder("INBOX"), mailhost, subscribed_only, doCount);
            log.debug("Loaded INBOX folders below Root to a depth of " + depth);
            String extraFolderPath = ((imapBasedir == null) ? "~" : imapBasedir) + mhd.getLogin();
            //String extraFolderPath = "/home/" + mhd.getLogin();
            Folder nonInboxBase = rootFolder.getFolder(extraFolderPath);
            log.debug("Trying extra base dir " + nonInboxBase.getFullName());
            if (nonInboxBase.exists()) {
                folderType = nonInboxBase.getType();
                if ((folderType & Folder.HOLDS_MESSAGES) != 0) {
                    // Can only Subscribe to Folders which may hold Msgs.
                    nonInboxBase.setSubscribed(true);
                    if (!nonInboxBase.isSubscribed())
                        log.error("A bug in JavaMail or in the server is " + "preventing subscription to '"
                                + nonInboxBase.getFullName() + "' on '" + url
                                + "'.  Folders will not be visible.");
                }
                int extraDepth = extraDepth = getFolderTree(nonInboxBase, mailhost, subscribed_only, doCount);
                if (extraDepth > depth)
                    depth = extraDepth;
                log.debug("Loaded additional folders from below " + nonInboxBase.getFullName()
                        + " with max depth of " + extraDepth);
            }
        } catch (Exception ex) {
            if (!url.getProtocol().startsWith("pop"))
                mailhost.setAttribute("error", ex.getMessage());
            log.warn("Failed to fetch child folders from (" + url + ')', ex);
        }
        if (depth > max_depth)
            max_depth = depth;
        model.addMailhost(mailhost);
    }
    model.setStateVar("max folder depth", (1 + max_depth) + "");
}

From source file:org.apache.hupa.server.handler.FetchFoldersHandler.java

/**
 * Create a new IMAPFolder from the given Folder
 * // w  w w. jav  a  2  s .  c  o m
 * @param folder Current folder
 * @return imapFolder Created IMAPFolder
 * @throws ActionException If an error occurs
 * @throws MessagingException If an error occurs
 */
private IMAPFolder createIMAPFolder(Folder folder) throws ActionException {

    String fullName = folder.getFullName();
    String delimiter;
    IMAPFolder iFolder = null;

    try {
        logger.debug("Creating folder: " + fullName + " for user: " + getUser());
        delimiter = String.valueOf(folder.getSeparator());
        iFolder = new IMAPFolder(fullName);
        iFolder.setDelimiter(delimiter);
        if ("[Gmail]".equals(folder.getFullName()))
            return iFolder;
        iFolder.setMessageCount(folder.getMessageCount());
        iFolder.setSubscribed(folder.isSubscribed());
        iFolder.setUnseenMessageCount(folder.getUnreadMessageCount());
    } catch (MessagingException e) {
        logger.error("Unable to construct folder " + folder.getFullName(), e);
    }

    return iFolder;
}