FileDocCategorySizeDatePackage
TtyAuthenticator.javaAPI DocGlassfish v2 API2679Mon Oct 17 14:54:20 BST 2005None

TtyAuthenticator

public class TtyAuthenticator extends Authenticator
A simple Authenticator that prompts for the user name and password on stdin. Puts up a dialog something like:

Connecting to <protocol> mail service on host <addr>, port <port>.
<prompt>

User Name: [defaultUserName]
Password:

author
Bill Shannon

Fields Summary
Constructors Summary
Methods Summary
private static final java.lang.Stringget(java.io.BufferedReader in, java.lang.String name, java.lang.String value)

	PrintStream p = System.out;

	p.print(name + ": ");
	if (value != null)
	    p.print("[" + value + "] ");
	p.flush();

	try {
	    String s = in.readLine();
	    if (s.length() == 0)
		return value;
	    else
		return s;
	} catch (IOException e) {
	    return value;
	}
    
protected javax.mail.PasswordAuthenticationgetPasswordAuthentication()

return
The PasswordAuthentication collected from the user, or null if none is provided.

	BufferedReader in = new BufferedReader(
				new InputStreamReader((System.in)));
	StringBuffer sb = new StringBuffer();
	sb.append("Connecting to ");
	sb.append(getRequestingProtocol());
	sb.append(" mail service on host ");
	sb.append(getRequestingSite().getHostName());
	int port = getRequestingPort();
	if (port > 0) {
	    sb.append(", port ");
	    sb.append(port);
	}
	sb.append(".");
	System.out.println(sb.toString());
	String prompt = getRequestingPrompt();
	if (prompt != null)
	    System.out.println(prompt);
	System.out.println();
	String userName = get(in, "User Name", getDefaultUserName());
	String password = get(in, "Password", null);
	if (userName == null)
	    return null;
	else
	    return new PasswordAuthentication(userName, password);
    
public static voidmain(java.lang.String[] argv)

	Session sess = Session.getInstance(System.getProperties(),
					new TtyAuthenticator());
	PasswordAuthentication pw = sess.requestPasswordAuthentication(
		InetAddress.getByName(argv[0]),
		Integer.parseInt(argv[1]), argv[2], z(argv[3]), z(argv[4]));
	System.out.println("User: " + n(pw.getUserName()));
	System.out.println("Password: " + n(pw.getPassword()));
    
private static final java.lang.Stringn(java.lang.String s)

	return s == null ? "<null>" : s;
    
private static final java.lang.Stringz(java.lang.String s)

	return s.length() > 0 ? s : null;