KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > auth > callback > DialogCallbackHandler


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Jeremie Laurent, Yann Petiot, Frederic Rinaldi
22  * --------------------------------------------------------------------------
23  * $Id: DialogCallbackHandler.java,v 1.5 2004/12/10 08:54:21 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.auth.callback;
28
29 import java.awt.GridLayout JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import javax.security.auth.callback.Callback JavaDoc;
33 import javax.security.auth.callback.CallbackHandler JavaDoc;
34 import javax.security.auth.callback.NameCallback JavaDoc;
35 import javax.security.auth.callback.PasswordCallback JavaDoc;
36 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
37 import javax.swing.BoxLayout JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JOptionPane JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.JPasswordField JavaDoc;
42 import javax.swing.JTextField JavaDoc;
43
44 /**
45  * Uses a Swing dialog window to query the user for answers to authentication
46  * questions. This can be used by a JAAS application to instantiate a
47  * CallbackHandler
48  * @author Jeremie Laurent, Yann Petiot, Frederic Rinaldi
49  * @author Florent Benoit. Integration in the JOnAS 3.1.2 tree
50  */

51 public class DialogCallbackHandler implements CallbackHandler JavaDoc {
52
53     /**
54      * The text field where the user will put its login.
55      */

56     private JTextField JavaDoc loginField = null;
57
58     /**
59      * The password field where the user will put its password.
60      */

61     private JTextField JavaDoc passwordField = null;
62
63     /**
64      * If the button cancel is clicked, the user won't be prompted no more.
65      */

66     private boolean cancelled = false;
67
68     /**
69      * The title of the dialog box
70      */

71     private String JavaDoc title = "Login Dialog";
72
73     /**
74      * The label for the username label
75      */

76     private String JavaDoc username = "Username ";
77
78     /**
79      * The label for the password label
80      */

81     private String JavaDoc password = "Password ";
82
83     /**
84      * The label for the login button
85      */

86     private String JavaDoc loginButton = "Login !";
87
88     /**
89      * The label for the cancel button
90      */

91     private String JavaDoc cancelButton = "Cancel";
92
93     /**
94      * Maximum length for fields
95      */

96     private static final int MAX_FIELD_LENGTH = 20;
97
98     /**
99      * the length of the password textfield
100      */

101     private int passwordLength = MAX_FIELD_LENGTH;
102
103     /**
104      * the length for the username textfield
105      */

106     private int usernameLength = MAX_FIELD_LENGTH;
107
108     /**
109      * the character which will be displayed in the password textfield if
110      * echoCharOn is true
111      */

112     private char echoChar = '*';
113
114     /**
115      * whether the echo is on or not : display the echoChar in the password
116      * textfield
117      */

118     private boolean echoCharOn = false;
119
120     /**
121      * an array of String where are described the label of the buttons
122      */

123     private String JavaDoc[] connectOptionNames;
124
125     /**
126      * The constructor to create a callback dialog with the default parent
127      * window.
128      */

129     public DialogCallbackHandler() {
130         int i = 0;
131         connectOptionNames = new String JavaDoc[2];
132         connectOptionNames[i] = loginButton;
133         connectOptionNames[++i] = cancelButton;
134     }
135
136     /**
137      * The constructor to create a callback dialog with the default parent
138      * window
139      * @param title the title of the dialog box
140      */

141     public DialogCallbackHandler(String JavaDoc title) {
142         this();
143         this.title = title;
144     }
145
146     /**
147      * The constructor to create a callback dialog with the default parent
148      * window
149      * @param title the title of the dialog box
150      * @param username the label of the username label
151      * @param password the label of the password label
152      */

153     public DialogCallbackHandler(String JavaDoc title, String JavaDoc username, String JavaDoc password) {
154         this();
155         this.title = title;
156         this.username = username;
157         this.password = password;
158     }
159
160     /**
161      * The constructor to create a callback dialog with the default parent
162      * window
163      * @param title the title of the dialog box
164      * @param username the label of the username label
165      * @param password the label of the password label
166      * @param loginButton the label of the login button
167      * @param cancelButton the label of the cancel button
168      * @param usernameLength the length of the username field
169      * @param passwordLength the length of the password field
170      * @param echoChar the character to display when entering the password
171      * implies that echoCharOn = true
172      */

173     public DialogCallbackHandler(String JavaDoc title, String JavaDoc username, String JavaDoc password, String JavaDoc loginButton,
174             String JavaDoc cancelButton, int usernameLength, int passwordLength, char echoChar) {
175         this(title, username, password);
176         this.echoCharOn = true;
177         this.echoChar = echoChar;
178         this.loginButton = loginButton;
179         this.cancelButton = cancelButton;
180         this.passwordLength = passwordLength;
181         this.usernameLength = usernameLength;
182         this.echoChar = echoChar;
183     }
184
185     /**
186      * /** This method allows to create a new instance of JDialog initialised
187      * with the parameters.
188      * @param isEchoOn the value of passwordCallback.isEchoOn() called in handle
189      */

190     private void dialogInit(boolean isEchoOn) {
191
192         // to determine whether the echo char must be displayed or not
193
// we use a "OR" between the isEchoOn=passwordCallback.isEchoOn() and
194
// the echoCharOn=the value of the variable echoCharOn set in the
195
// OptionPaneCallbackHandler constructor.
196
echoCharOn = (isEchoOn || echoCharOn);
197         dialogInit();
198     }
199
200     /**
201      * This method allows to create a new instance of JDialog initialised with
202      * the parameters.
203      */

204     private void dialogInit() {
205
206         // for the login
207
JLabel JavaDoc userNameLabel = new JLabel JavaDoc(username, JLabel.RIGHT);
208         loginField = new JTextField JavaDoc("");
209
210         // for the password
211
JLabel JavaDoc passwordLabel = new JLabel JavaDoc(password, JLabel.RIGHT);
212
213         // because of the bug of Java (in SDK 1.3) it isn't possible to display
214
// password without an echo character in a password field, we have to
215
// create a JTextField or a JPasswordField depending on echoCharOn.
216
if (!echoCharOn) { // password needs to be hidden
217
passwordField = new JPasswordField JavaDoc(passwordLength);
218             ((JPasswordField JavaDoc) passwordField).setEchoChar(echoChar);
219         } else {
220             passwordField = new JTextField JavaDoc(passwordLength);
221         }
222
223         // creation of a JPanel to put the other panels on
224
JPanel JavaDoc connectionPanel = new JPanel JavaDoc(false);
225         connectionPanel.setLayout(new BoxLayout JavaDoc(connectionPanel, BoxLayout.X_AXIS));
226
227         // to this panel we add the labels
228
JPanel JavaDoc namePanel = new JPanel JavaDoc(false);
229         namePanel.setLayout(new GridLayout JavaDoc(0, 1));
230         namePanel.add(userNameLabel);
231         namePanel.add(passwordLabel);
232
233         // to this panel we add the fields
234
JPanel JavaDoc fieldPanel = new JPanel JavaDoc(false);
235         fieldPanel.setLayout(new GridLayout JavaDoc(0, 1));
236         fieldPanel.add(loginField);
237         fieldPanel.add(passwordField);
238
239         // we add the 2 panels created in the first one
240
connectionPanel.add(namePanel);
241         connectionPanel.add(fieldPanel);
242
243         // Connect or quit
244
//System.out.println("Displaying dialog...");
245
int choice = JOptionPane.showOptionDialog(null, // the default frame
246
connectionPanel, // the object to display
247
title, // the title string
248
JOptionPane.OK_CANCEL_OPTION, // the options available
249
JOptionPane.INFORMATION_MESSAGE, // the kind of message
250
null, // the icon to display
251
connectOptionNames, // the possible choices
252
loginField); // the default selection
253

254         if (choice == JOptionPane.OK_OPTION) {
255             cancelled = false;
256         } else if (choice == JOptionPane.CANCEL_OPTION) {
257             // why is this value never got ?
258
// JOptionPane.CANCEL_OPTION=2 but choice=1
259
cancelled = true;
260         } else if (choice == JOptionPane.CLOSED_OPTION) {
261             cancelled = true;
262         } else {
263             cancelled = true; // another bug of Java
264
}
265
266         if (cancelled) {
267             loginField.setText("Invalid");
268             passwordField.setText("Invalid");
269         }
270     }
271
272     /**
273      * Invoke an array of Callbacks. <p>
274      * @param callbacks an array of <code>Callback</code> objects which
275      * contain the information requested by an underlying security
276      * service to be retrieved or displayed.
277      * @exception java.io.IOException if an input or output error occurs. <p>
278      * @exception UnsupportedCallbackException if the implementation of this
279      * method does not support one or more of the Callbacks specified
280      * in the <code>callbacks</code> parameter.
281      */

282     public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
283
284         // if the user clicked on the cancel button the the system won't
285
// prompt him its informations
286
if (cancelled) {
287             return;
288         }
289
290         // recreate a new JDialog with the property to display or not
291
// the password echo
292
int i = 0;
293         boolean found = false;
294         while ((i < callbacks.length) && !found) {
295             if (callbacks[i] instanceof PasswordCallback JavaDoc) {
296                 found = true;
297                 dialogInit(((PasswordCallback JavaDoc) callbacks[i]).isEchoOn());
298             }
299             // maybe we'll have to create a ObjectCallback object see later.
300
i++;
301         }
302         // if there's no instance of PasswordCallback in the parameter
303
// callbacks then there's a problem here !
304
// TO DO : a new dialogInit to integrate an ObjectCallback for example
305
// (for pki...)
306
// while waiting...
307
if (!found) {
308             dialogInit();
309         }
310
311         // get the informations of the user
312
for (i = 0; i < callbacks.length; i++) {
313
314             if (callbacks[i] instanceof NameCallback JavaDoc) {
315                 ((NameCallback JavaDoc) callbacks[i]).setName(loginField.getText());
316             } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
317                 ((PasswordCallback JavaDoc) callbacks[i]).setPassword((passwordField.getText()).toCharArray());
318             } else {
319                 throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
320             }
321         }
322     }
323 }
Popular Tags