DialogCallbackHandler.java

00001 
00027 package org.objectweb.jonas.security.auth.callback;
00028 
00029 import java.awt.GridLayout;
00030 import java.io.IOException;
00031 
00032 import javax.security.auth.callback.Callback;
00033 import javax.security.auth.callback.CallbackHandler;
00034 import javax.security.auth.callback.NameCallback;
00035 import javax.security.auth.callback.PasswordCallback;
00036 import javax.security.auth.callback.UnsupportedCallbackException;
00037 import javax.swing.BoxLayout;
00038 import javax.swing.JLabel;
00039 import javax.swing.JOptionPane;
00040 import javax.swing.JPanel;
00041 import javax.swing.JPasswordField;
00042 import javax.swing.JTextField;
00043 
00051 public class DialogCallbackHandler implements CallbackHandler {
00052 
00056     private JTextField loginField = null;
00057 
00061     private JTextField passwordField = null;
00062 
00066     private boolean cancelled = false;
00067 
00071     private String title = "Login Dialog";
00072 
00076     private String username = "Username  ";
00077 
00081     private String password = "Password  ";
00082 
00086     private String loginButton = "Login !";
00087 
00091     private String cancelButton = "Cancel";
00092 
00096     private static final int MAX_FIELD_LENGTH = 20;
00097 
00101     private int passwordLength = MAX_FIELD_LENGTH;
00102 
00106     private int usernameLength = MAX_FIELD_LENGTH;
00107 
00112     private char echoChar = '*';
00113 
00118     private boolean echoCharOn = false;
00119 
00123     private String[] connectOptionNames;
00124 
00129     public DialogCallbackHandler() {
00130         int i = 0;
00131         connectOptionNames = new String[2];
00132         connectOptionNames[i] = loginButton;
00133         connectOptionNames[++i] = cancelButton;
00134     }
00135 
00141     public DialogCallbackHandler(String title) {
00142         this();
00143         this.title = title;
00144     }
00145 
00153     public DialogCallbackHandler(String title, String username, String password) {
00154         this();
00155         this.title = title;
00156         this.username = username;
00157         this.password = password;
00158     }
00159 
00173     public DialogCallbackHandler(String title, String username, String password, String loginButton,
00174             String cancelButton, int usernameLength, int passwordLength, char echoChar) {
00175         this(title, username, password);
00176         this.echoCharOn = true;
00177         this.echoChar = echoChar;
00178         this.loginButton = loginButton;
00179         this.cancelButton = cancelButton;
00180         this.passwordLength = passwordLength;
00181         this.usernameLength = usernameLength;
00182         this.echoChar = echoChar;
00183     }
00184 
00190     private void dialogInit(boolean isEchoOn) {
00191 
00192         // to determine whether the echo char must be displayed or not
00193         // we use a "OR" between the isEchoOn=passwordCallback.isEchoOn() and
00194         // the echoCharOn=the value of the variable echoCharOn set in the
00195         // OptionPaneCallbackHandler constructor.
00196         echoCharOn = (isEchoOn || echoCharOn);
00197         dialogInit();
00198     }
00199 
00204     private void dialogInit() {
00205 
00206         // for the login
00207         JLabel userNameLabel = new JLabel(username, JLabel.RIGHT);
00208         loginField = new JTextField("");
00209 
00210         // for the password
00211         JLabel passwordLabel = new JLabel(password, JLabel.RIGHT);
00212 
00213         // because of the bug of Java (in SDK 1.3) it isn't possible to display
00214         // password without an echo character in a password field, we have to
00215         // create a JTextField or a JPasswordField depending on echoCharOn.
00216         if (!echoCharOn) { // password needs to be hidden
00217             passwordField = new JPasswordField(passwordLength);
00218             ((JPasswordField) passwordField).setEchoChar(echoChar);
00219         } else {
00220             passwordField = new JTextField(passwordLength);
00221         }
00222 
00223         // creation of a JPanel to put the other panels on
00224         JPanel connectionPanel = new JPanel(false);
00225         connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.X_AXIS));
00226 
00227         // to this panel we add the labels
00228         JPanel namePanel = new JPanel(false);
00229         namePanel.setLayout(new GridLayout(0, 1));
00230         namePanel.add(userNameLabel);
00231         namePanel.add(passwordLabel);
00232 
00233         // to this panel we add the fields
00234         JPanel fieldPanel = new JPanel(false);
00235         fieldPanel.setLayout(new GridLayout(0, 1));
00236         fieldPanel.add(loginField);
00237         fieldPanel.add(passwordField);
00238 
00239         // we add the 2 panels created in the first one
00240         connectionPanel.add(namePanel);
00241         connectionPanel.add(fieldPanel);
00242 
00243         // Connect or quit
00244         //System.out.println("Displaying dialog...");
00245         int choice = JOptionPane.showOptionDialog(null, // the default frame
00246                 connectionPanel, // the object to display
00247                 title, // the title string
00248                 JOptionPane.OK_CANCEL_OPTION, // the options available
00249                 JOptionPane.INFORMATION_MESSAGE, // the kind of message
00250                 null, // the icon to display
00251                 connectOptionNames, // the possible choices
00252                 loginField); // the default selection
00253 
00254         if (choice == JOptionPane.OK_OPTION) {
00255             cancelled = false;
00256         } else if (choice == JOptionPane.CANCEL_OPTION) {
00257             // why is this value never got ?
00258             // JOptionPane.CANCEL_OPTION=2 but choice=1
00259             cancelled = true;
00260         } else if (choice == JOptionPane.CLOSED_OPTION) {
00261             cancelled = true;
00262         } else {
00263             cancelled = true; // another bug of Java
00264         }
00265 
00266         if (cancelled) {
00267             loginField.setText("Invalid");
00268             passwordField.setText("Invalid");
00269         }
00270     }
00271 
00282     public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
00283 
00284         // if the user clicked on the cancel button the the system won't
00285         // prompt him its informations
00286         if (cancelled) {
00287             return;
00288         }
00289 
00290         // recreate a new JDialog with the property to display or not
00291         // the password echo
00292         int i = 0;
00293         boolean found = false;
00294         while ((i < callbacks.length) && !found) {
00295             if (callbacks[i] instanceof PasswordCallback) {
00296                 found = true;
00297                 dialogInit(((PasswordCallback) callbacks[i]).isEchoOn());
00298             }
00299             // maybe we'll have to create a ObjectCallback object see later.
00300             i++;
00301         }
00302         // if there's no instance of PasswordCallback in the parameter
00303         // callbacks then there's a problem here !
00304         // TO DO : a new dialogInit to integrate an ObjectCallback for example
00305         // (for pki...)
00306         // while waiting...
00307         if (!found) {
00308             dialogInit();
00309         }
00310 
00311         // get the informations of the user
00312         for (i = 0; i < callbacks.length; i++) {
00313 
00314             if (callbacks[i] instanceof NameCallback) {
00315                 ((NameCallback) callbacks[i]).setName(loginField.getText());
00316             } else if (callbacks[i] instanceof PasswordCallback) {
00317                 ((PasswordCallback) callbacks[i]).setPassword((passwordField.getText()).toCharArray());
00318             } else {
00319                 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
00320             }
00321         }
00322     }
00323 }

Generated on Tue Feb 15 15:05:21 2005 for JOnAS by  doxygen 1.3.9.1