KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleAuthenticator


1 /*
2  * @(#)SimpleAuthenticator.java 1.7 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38 /*
39  * @(#)SimpleAuthenticator.java 1.7 01/05/23
40  *
41  * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
42  * All Rights Reserved.
43  */

44
45 import javax.mail.*;
46 import java.net.InetAddress JavaDoc;
47 import java.awt.*;
48 import javax.swing.*;
49
50 /**
51  * Simple Authenticator for requesting password information.
52  *
53  * @version 1.7, 01/05/23
54  * @author Christopher Cotton
55  * @author Bill Shannon
56  */

57
58 public class SimpleAuthenticator extends Authenticator {
59
60     Frame frame;
61     String JavaDoc username;
62     String JavaDoc password;
63
64     public SimpleAuthenticator(Frame f) {
65     this.frame = f;
66     }
67
68     protected PasswordAuthentication getPasswordAuthentication() {
69
70     // given a prompt?
71
String JavaDoc prompt = getRequestingPrompt();
72     if (prompt == null)
73         prompt = "Please login...";
74
75     // protocol
76
String JavaDoc protocol = getRequestingProtocol();
77     if (protocol == null)
78         protocol = "Unknown protocol";
79
80     // get the host
81
String JavaDoc host = null;
82     InetAddress JavaDoc inet = getRequestingSite();
83     if (inet != null)
84         host = inet.getHostName();
85     if (host == null)
86         host = "Unknown host";
87
88     // port
89
String JavaDoc port = "";
90     int portnum = getRequestingPort();
91     if (portnum != -1)
92         port = ", port " + portnum + " ";
93
94     // Build the info string
95
String JavaDoc info = "Connecting to " + protocol + " mail service on host " +
96                                 host + port;
97
98     //JPanel d = new JPanel();
99
// XXX - for some reason using a JPanel here causes JOptionPane
100
// to display incorrectly, so we workaround the problem using
101
// an anonymous JComponent.
102
JComponent d = new JComponent() { };
103
104     GridBagLayout gb = new GridBagLayout();
105     GridBagConstraints c = new GridBagConstraints();
106     d.setLayout(gb);
107     c.insets = new Insets(2, 2, 2, 2);
108
109     c.anchor = GridBagConstraints.WEST;
110     c.gridwidth = GridBagConstraints.REMAINDER;
111     c.weightx = 0.0;
112     d.add(constrain(new JLabel(info), gb, c));
113     d.add(constrain(new JLabel(prompt), gb, c));
114
115     c.gridwidth = 1;
116     c.anchor = GridBagConstraints.EAST;
117     c.fill = GridBagConstraints.NONE;
118     c.weightx = 0.0;
119     d.add(constrain(new JLabel("Username:"), gb, c));
120
121     c.anchor = GridBagConstraints.EAST;
122     c.fill = GridBagConstraints.HORIZONTAL;
123     c.gridwidth = GridBagConstraints.REMAINDER;
124     c.weightx = 1.0;
125     String JavaDoc user = getDefaultUserName();
126     JTextField username = new JTextField(user, 20);
127     d.add(constrain(username, gb, c));
128
129     c.gridwidth = 1;
130     c.fill = GridBagConstraints.NONE;
131     c.anchor = GridBagConstraints.EAST;
132     c.weightx = 0.0;
133     d.add(constrain(new JLabel("Password:"), gb, c));
134
135     c.anchor = GridBagConstraints.EAST;
136     c.fill = GridBagConstraints.HORIZONTAL;
137     c.gridwidth = GridBagConstraints.REMAINDER;
138     c.weightx = 1.0;
139     JPasswordField password = new JPasswordField("", 20);
140     d.add(constrain(password, gb, c));
141     // XXX - following doesn't work
142
if (user != null && user.length() > 0)
143         password.requestFocus();
144     else
145         username.requestFocus();
146     
147     int result = JOptionPane.showConfirmDialog(frame, d, "Login",
148         JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
149     
150     if (result == JOptionPane.OK_OPTION)
151         return new PasswordAuthentication(username.getText(),
152                         password.getText());
153     else
154         return null;
155     }
156
157     private Component constrain(Component cmp,
158                     GridBagLayout gb, GridBagConstraints c) {
159     gb.setConstraints(cmp, c);
160     return (cmp);
161     }
162 }
163
Popular Tags