Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.util.Properties;
  33. import javax.mail.*;
  34.  
  35. import com.sun.mail.imap.*;
  36.  
  37. /**
  38.  * Demo app that exercises the Message interfaces.
  39.  * List information about folders.
  40.  *
  41.  * @author John Mani
  42.  * @author Bill Shannon
  43.  */
  44.  
  45. public class folderlist {
  46.     static String protocol = null;
  47.     static String host = null;
  48.     static String user = null;
  49.     static String password = null;
  50.     static String url = null;
  51.     static String root = null;
  52.     static String pattern = "%";
  53.     static boolean recursive = false;
  54.     static boolean verbose = false;
  55.     static boolean debug = false;
  56.  
  57.     public static void main(String argv[]) throws Exception {
  58.         int optind;
  59.         for (optind = 0; optind < argv.length; optind++) {
  60.             if (argv[optind].equals("-T")) {
  61.                 protocol = argv[++optind];
  62.             } else if (argv[optind].equals("-H")) {
  63.                 host = argv[++optind];
  64.             } else if (argv[optind].equals("-U")) {
  65.                 user = argv[++optind];
  66.             } else if (argv[optind].equals("-P")) {
  67.                 password = argv[++optind];
  68.             } else if (argv[optind].equals("-L")) {
  69.                 url = argv[++optind];
  70.             } else if (argv[optind].equals("-R")) {
  71.                 root = argv[++optind];
  72.             } else if (argv[optind].equals("-r")) {
  73.                 recursive = true;
  74.             } else if (argv[optind].equals("-v")) {
  75.                 verbose = true;
  76.             } else if (argv[optind].equals("-D")) {
  77.                 debug = true;
  78.             } else if (argv[optind].equals("--")) {
  79.                 optind++;
  80.                 break;
  81.             } else if (argv[optind].startsWith("-")) {
  82.                 System.out.println(
  83. "Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]");
  84.                 System.out.println(
  85. "\t[-R root] [-r] [-v] [-D] [pattern]");
  86.                 System.exit(1);
  87.             } else {
  88.                 break;
  89.             }
  90.         }
  91.         if (optind < argv.length)
  92.             pattern = argv[optind];
  93.  
  94.         // Get a Properties object
  95.         Properties props = System.getProperties();
  96.  
  97.         // Get a Session object
  98.         Session session = Session.getInstance(props, null);
  99.         session.setDebug(debug);
  100.  
  101.         // Get a Store object
  102.         Store store = null;
  103.         Folder rf = null;
  104.         if (url != null) {
  105.             URLName urln = new URLName(url);
  106.             store = session.getStore(urln);
  107.             store.connect();
  108.         } else {
  109.             if (protocol != null)
  110.                 store = session.getStore(protocol);
  111.             else
  112.                 store = session.getStore();
  113.  
  114.             // Connect
  115.             if (host != null || user != null || password != null)
  116.                 store.connect(host, user, password);
  117.             else
  118.                 store.connect();
  119.         }
  120.  
  121.         // List namespace
  122.         if (root != null)
  123.             rf = store.getFolder(root);
  124.         else
  125.             rf = store.getDefaultFolder();
  126.  
  127.         dumpFolder(rf, false, "");
  128.         if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
  129.             Folder[] f = rf.list(pattern);
  130.             for (int i = 0; i < f.length; i++)
  131.                 dumpFolder(f[i], recursive, "    ");
  132.         }
  133.  
  134.         store.close();
  135.     }
  136.  
  137.     static void dumpFolder(Folder folder, boolean recurse, String tab)
  138.                                         throws Exception {
  139.         System.out.println(tab + "Name:      " + folder.getName());
  140.         System.out.println(tab + "Full Name: " + folder.getFullName());
  141.         System.out.println(tab + "URL:       " + folder.getURLName());
  142.  
  143.         if (verbose) {
  144.             if (!folder.isSubscribed())
  145.                 System.out.println(tab + "Not Subscribed");
  146.  
  147.             if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
  148.                 if (folder.hasNewMessages())
  149.                     System.out.println(tab + "Has New Messages");
  150.                 System.out.println(tab + "Total Messages:  " +
  151.                                                 folder.getMessageCount());
  152.                 System.out.println(tab + "New Messages:    " +
  153.                                                 folder.getNewMessageCount());
  154.                 System.out.println(tab + "Unread Messages: " +
  155.                                                 folder.getUnreadMessageCount());
  156.             }
  157.             if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0)
  158.                 System.out.println(tab + "Is Directory");
  159.  
  160.             /*
  161.              * Demonstrate use of IMAP folder attributes
  162.              * returned by the IMAP LIST response.
  163.              */
  164.             if (folder instanceof IMAPFolder) {
  165.                 IMAPFolder f = (IMAPFolder)folder;
  166.                 String[] attrs = f.getAttributes();
  167.                 if (attrs != null && attrs.length > 0) {
  168.                     System.out.println(tab + "IMAP Attributes:");
  169.                     for (int i = 0; i < attrs.length; i++)
  170.                         System.out.println(tab + "    " + attrs[i]);
  171.                 }
  172.             }
  173.         }
  174.  
  175.         System.out.println();
  176.  
  177.         if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) {
  178.             if (recurse) {
  179.                 Folder[] f = folder.list();
  180.                 for (int i = 0; i < f.length; i++)
  181.                     dumpFolder(f[i], recurse, tab + "    ");
  182.             }
  183.         }
  184.     }
  185. }
  186.