KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TtyAuthenticator


1 /*
2  * @(#)TtyAuthenticator.java 1.2 04/01/08
3  *
4  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
5  */

6
7 import java.io.*;
8 import java.net.*;
9 import javax.mail.*;
10 import javax.mail.PasswordAuthentication JavaDoc;
11 import javax.mail.Authenticator JavaDoc;
12
13 /**
14  * A simple Authenticator that prompts for the user name and password on stdin.
15  * Puts up a dialog something like:
16  * <p> <pre>
17  * Connecting to &lt;protocol&gt; mail service on host &lt;addr&gt;, port &lt;port&gt;.
18  * &lt;prompt&gt;
19  *
20  * User Name: [defaultUserName]
21  * Password:
22  * </pre> <p>
23  *
24  * @author Bill Shannon
25  */

26
27 public class TtyAuthenticator extends Authenticator JavaDoc {
28
29     /**
30      * @return The PasswordAuthentication collected from the
31      * user, or null if none is provided.
32      */

33     protected PasswordAuthentication JavaDoc getPasswordAuthentication() {
34     BufferedReader in = new BufferedReader(
35                 new InputStreamReader((System.in)));
36     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
37     sb.append("Connecting to ");
38     sb.append(getRequestingProtocol());
39     sb.append(" mail service on host ");
40     sb.append(getRequestingSite().getHostName());
41     int port = getRequestingPort();
42     if (port > 0) {
43         sb.append(", port ");
44         sb.append(port);
45     }
46     sb.append(".");
47     System.out.println(sb.toString());
48     String JavaDoc prompt = getRequestingPrompt();
49     if (prompt != null)
50         System.out.println(prompt);
51     System.out.println();
52     String JavaDoc userName = get(in, "User Name", getDefaultUserName());
53     String JavaDoc password = get(in, "Password", null);
54     if (userName == null)
55         return null;
56     else
57         return new PasswordAuthentication JavaDoc(userName, password);
58     }
59
60     private static final String JavaDoc get(BufferedReader in,
61                 String JavaDoc name, String JavaDoc value) {
62     PrintStream p = System.out;
63
64     p.print(name + ": ");
65     if (value != null)
66         p.print("[" + value + "] ");
67     p.flush();
68
69     try {
70         String JavaDoc s = in.readLine();
71         if (s.length() == 0)
72         return value;
73         else
74         return s;
75     } catch (IOException e) {
76         return value;
77     }
78     }
79
80     // main program, for debugging.
81
// Usage: java TtyAuthenticator host port protocol prompt defaultUser
82
public static void main(String JavaDoc argv[]) throws Exception JavaDoc {
83     Session sess = Session.getInstance(System.getProperties(),
84                     new TtyAuthenticator());
85     PasswordAuthentication JavaDoc pw = sess.requestPasswordAuthentication(
86         InetAddress.getByName(argv[0]),
87         Integer.parseInt(argv[1]), argv[2], z(argv[3]), z(argv[4]));
88     System.out.println("User: " + n(pw.getUserName()));
89     System.out.println("Password: " + n(pw.getPassword()));
90     }
91
92     private static final String JavaDoc n(String JavaDoc s) {
93     return s == null ? "<null>" : s;
94     }
95
96     private static final String JavaDoc z(String JavaDoc s) {
97     return s.length() > 0 ? s : null;
98     }
99 }
100
Popular Tags