| 1 |
/* |
| 2 |
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.util.*; |
| 12 |
import java.io.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
|
| 15 |
/* |
| 16 |
* Demo app that exercises the namespace interfaces. |
| 17 |
* Show the namespaces supported by a store. |
| 18 |
* |
| 19 |
* @author Bill Shannon |
| 20 |
*/ |
| 21 |
|
| 22 |
public class namespace { |
| 23 |
|
| 24 |
static String protocol; |
| 25 |
static String host = null; |
| 26 |
static String user = null; |
| 27 |
static String password = null; |
| 28 |
static String url = null; |
| 29 |
static int port = -1; |
| 30 |
static boolean debug = false; |
| 31 |
static String suser = "other"; |
| 32 |
|
| 33 |
public static void main(String argv[]) { |
| 34 |
int msgnum = -1; |
| 35 |
int optind; |
| 36 |
|
| 37 |
for (optind = 0; optind < argv.length; optind++) { |
| 38 |
if (argv[optind].equals("-T")) { |
| 39 |
protocol = argv[++optind]; |
| 40 |
} else if (argv[optind].equals("-H")) { |
| 41 |
host = argv[++optind]; |
| 42 |
} else if (argv[optind].equals("-U")) { |
| 43 |
user = argv[++optind]; |
| 44 |
} else if (argv[optind].equals("-P")) { |
| 45 |
password = argv[++optind]; |
| 46 |
} else if (argv[optind].equals("-D")) { |
| 47 |
debug = true; |
| 48 |
} else if (argv[optind].equals("-L")) { |
| 49 |
url = argv[++optind]; |
| 50 |
} else if (argv[optind].equals("-p")) { |
| 51 |
port = Integer.parseInt(argv[++optind]); |
| 52 |
} else if (argv[optind].equals("-u")) { |
| 53 |
suser = argv[++optind]; |
| 54 |
} else if (argv[optind].equals("--")) { |
| 55 |
optind++; |
| 56 |
break; |
| 57 |
} else if (argv[optind].startsWith("-")) { |
| 58 |
System.out.println( |
| 59 |
"Usage: namespace [-L url] [-T protocol] [-H host] [-p port] [-U user]"); |
| 60 |
System.out.println( |
| 61 |
"\t[-P password] [-u other-user] [-D]"); |
| 62 |
System.exit(1); |
| 63 |
} else { |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
// Get a Properties object |
| 70 |
Properties props = System.getProperties(); |
| 71 |
|
| 72 |
// Get a Session object |
| 73 |
Session session = Session.getInstance(props, null); |
| 74 |
session.setDebug(debug); |
| 75 |
|
| 76 |
// Get a Store object |
| 77 |
Store store = null; |
| 78 |
if (url != null) { |
| 79 |
URLName urln = new URLName(url); |
| 80 |
store = session.getStore(urln); |
| 81 |
store.connect(); |
| 82 |
} else { |
| 83 |
if (protocol != null) |
| 84 |
store = session.getStore(protocol); |
| 85 |
else |
| 86 |
store = session.getStore(); |
| 87 |
|
| 88 |
// Connect |
| 89 |
if (host != null || user != null || password != null) |
| 90 |
store.connect(host, port, user, password); |
| 91 |
else |
| 92 |
store.connect(); |
| 93 |
} |
| 94 |
|
| 95 |
printFolders("Personal", store.getPersonalNamespaces()); |
| 96 |
printFolders("User \"" + suser + "\"", |
| 97 |
store.getUserNamespaces(suser)); |
| 98 |
printFolders("Shared", store.getSharedNamespaces()); |
| 99 |
|
| 100 |
store.close(); |
| 101 |
} catch (Exception ex) { |
| 102 |
System.out.println("Oops, got exception! " + ex.getMessage()); |
| 103 |
ex.printStackTrace(); |
| 104 |
} |
| 105 |
System.exit(0); |
| 106 |
} |
| 107 |
|
| 108 |
private static void printFolders(String name, Folder[] folders) |
| 109 |
throws MessagingException { |
| 110 |
System.out.println(name + " Namespace:"); |
| 111 |
if (folders == null || folders.length == 0) { |
| 112 |
System.out.println(" <none>"); |
| 113 |
return; |
| 114 |
} |
| 115 |
for (int i = 0; i < folders.length; i++) { |
| 116 |
String fn = folders[i].getFullName(); |
| 117 |
if (fn.length() == 0) |
| 118 |
fn = "<default folder>"; |
| 119 |
try { |
| 120 |
System.out.println(" " + fn + |
| 121 |
", delimiter \"" + folders[i].getSeparator() + "\""); |
| 122 |
Folder[] fl = folders[i].list(); |
| 123 |
if (fl.length > 0) { |
| 124 |
System.out.println(" Subfolders:"); |
| 125 |
for (int j = 0; j < fl.length; j++) |
| 126 |
System.out.println(" " + fl[j].getFullName()); |
| 127 |
} |
| 128 |
} catch (FolderNotFoundException ex) { } |
| 129 |
} |
| 130 |
} |
| 131 |
} |