KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > security > auth > callback > DialogCallbackHandler


1 /*
2  * @(#)DialogCallbackHandler.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.security.auth.callback;
9
10 /* JAAS imports */
11 import javax.security.auth.callback.Callback JavaDoc;
12 import javax.security.auth.callback.CallbackHandler JavaDoc;
13 import javax.security.auth.callback.ConfirmationCallback JavaDoc;
14 import javax.security.auth.callback.NameCallback JavaDoc;
15 import javax.security.auth.callback.PasswordCallback JavaDoc;
16 import javax.security.auth.callback.TextOutputCallback JavaDoc;
17 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
18
19 /* Java imports */
20 import java.awt.Component JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.Box JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JOptionPane JavaDoc;
27 import javax.swing.JPasswordField JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29
30 /**
31  * <p>
32  * Uses a Swing dialog window to query the user for answers to
33  * authentication questions.
34  * This can be used by a JAAS application to instantiate a
35  * CallbackHandler
36  * @see javax.security.auth.callback
37  */

38 public class DialogCallbackHandler implements CallbackHandler JavaDoc {
39
40     /* -- Fields -- */
41
42     /* The parent window, or null if using the default parent */
43     private Component JavaDoc parentComponent;
44     private static final int JPasswordFieldLen = 8 ;
45
46     /* -- Methods -- */
47
48     /**
49      * Creates a callback dialog with the default parent window.
50      */

51     public DialogCallbackHandler() { }
52
53     /**
54      * Creates a callback dialog and specify the parent window.
55      *
56      * @param parentComponent the parent window -- specify <code>null</code>
57      * for the default parent
58      */

59     public DialogCallbackHandler(Component JavaDoc parentComponent) {
60     this.parentComponent = parentComponent;
61     }
62     
63     /*
64      * An interface for recording actions to carry out if the user
65      * clicks OK for the dialog.
66      */

67     private static interface Action {
68      void perform();
69     }
70
71     /**
72      * Handles the specified set of callbacks.
73      *
74      * @param callbacks the callbacks to handle
75      * @throws UnsupportedCallbackException if the callback is not an
76      * instance of NameCallback or PasswordCallback
77      */

78
79     public void handle(Callback JavaDoc[] callbacks)
80     throws UnsupportedCallbackException JavaDoc
81     {
82     /* Collect messages to display in the dialog */
83     final List JavaDoc messages = new ArrayList JavaDoc(3);
84
85     /* Collection actions to perform if the user clicks OK */
86     final List JavaDoc okActions = new ArrayList JavaDoc(2);
87
88     ConfirmationInfo confirmation = new ConfirmationInfo();
89
90     for (int i = 0; i < callbacks.length; i++) {
91         if (callbacks[i] instanceof TextOutputCallback JavaDoc) {
92         TextOutputCallback JavaDoc tc = (TextOutputCallback JavaDoc) callbacks[i];
93
94         switch (tc.getMessageType()) {
95         case TextOutputCallback.INFORMATION:
96             confirmation.messageType = JOptionPane.INFORMATION_MESSAGE;
97             break;
98         case TextOutputCallback.WARNING:
99             confirmation.messageType = JOptionPane.WARNING_MESSAGE;
100             break;
101         case TextOutputCallback.ERROR:
102             confirmation.messageType = JOptionPane.ERROR_MESSAGE;
103             break;
104         default:
105             throw new UnsupportedCallbackException JavaDoc(
106             callbacks[i], "Unrecognized message type");
107         }
108
109         messages.add(tc.getMessage());
110
111         } else if (callbacks[i] instanceof NameCallback JavaDoc) {
112         final NameCallback JavaDoc nc = (NameCallback JavaDoc) callbacks[i];
113
114         JLabel JavaDoc prompt = new JLabel JavaDoc(nc.getPrompt());
115
116         final JTextField JavaDoc name = new JTextField JavaDoc();
117         String JavaDoc defaultName = nc.getDefaultName();
118         if (defaultName != null) {
119             name.setText(defaultName);
120         }
121         
122         /*
123          * Put the prompt and name in a horizontal box,
124          * and add that to the set of messages.
125          */

126         Box JavaDoc namePanel = Box.createHorizontalBox();
127         namePanel.add(prompt);
128         namePanel.add(name);
129         messages.add(namePanel);
130
131         /* Store the name back into the callback if OK */
132         okActions.add(new Action() {
133             public void perform() {
134             nc.setName(name.getText());
135             }
136         });
137
138         } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
139         final PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) callbacks[i];
140
141         JLabel JavaDoc prompt = new JLabel JavaDoc(pc.getPrompt());
142
143         final JPasswordField JavaDoc password =
144                     new JPasswordField JavaDoc(JPasswordFieldLen);
145         if (!pc.isEchoOn()) {
146             password.setEchoChar('*');
147         }
148
149         Box JavaDoc passwordPanel = Box.createHorizontalBox();
150         passwordPanel.add(prompt);
151         passwordPanel.add(password);
152         messages.add(passwordPanel);
153   
154         okActions.add(new Action() {
155             public void perform() {
156             pc.setPassword(password.getPassword());
157             }
158         });
159
160         } else if (callbacks[i] instanceof ConfirmationCallback JavaDoc) {
161         ConfirmationCallback JavaDoc cc = (ConfirmationCallback JavaDoc)callbacks[i];
162
163         confirmation.setCallback(cc);
164         if (cc.getPrompt() != null) {
165             messages.add(cc.getPrompt());
166         }
167
168         } else {
169         throw new UnsupportedCallbackException JavaDoc(
170             callbacks[i], "Unrecognized Callback");
171         }
172     }
173
174     /* Display the dialog */
175     int result = JOptionPane.showOptionDialog(
176         parentComponent,
177         messages.toArray(),
178         "Confirmation", /* title */
179         confirmation.optionType,
180         confirmation.messageType,
181         null, /* icon */
182         confirmation.options, /* options */
183         confirmation.initialValue); /* initialValue */
184
185     /* Perform the OK actions */
186     if (result == JOptionPane.OK_OPTION
187         || result == JOptionPane.YES_OPTION)
188     {
189         Iterator JavaDoc iterator = okActions.iterator();
190         while (iterator.hasNext()) {
191         ((Action) iterator.next()).perform();
192         }
193     }
194     confirmation.handleResult(result);
195     }
196
197     /*
198      * Provides assistance with translating between JAAS and Swing
199      * confirmation dialogs.
200      */

201     private static class ConfirmationInfo {
202
203     private int[] translations;
204
205     int optionType = JOptionPane.OK_CANCEL_OPTION;
206     Object JavaDoc[] options = null;
207     Object JavaDoc initialValue = null;
208
209     int messageType = JOptionPane.QUESTION_MESSAGE;
210
211     private ConfirmationCallback JavaDoc callback;
212
213     /* Set the confirmation callback handler */
214     void setCallback(ConfirmationCallback JavaDoc callback)
215         throws UnsupportedCallbackException JavaDoc
216     {
217         this.callback = callback;
218
219         int confirmationOptionType = callback.getOptionType();
220         switch (confirmationOptionType) {
221         case ConfirmationCallback.YES_NO_OPTION:
222         optionType = JOptionPane.YES_NO_OPTION;
223         translations = new int[] {
224             JOptionPane.YES_OPTION, ConfirmationCallback.YES,
225             JOptionPane.NO_OPTION, ConfirmationCallback.NO,
226             JOptionPane.CLOSED_OPTION, ConfirmationCallback.NO
227         };
228         break;
229         case ConfirmationCallback.YES_NO_CANCEL_OPTION:
230         optionType = JOptionPane.YES_NO_CANCEL_OPTION;
231         translations = new int[] {
232             JOptionPane.YES_OPTION, ConfirmationCallback.YES,
233             JOptionPane.NO_OPTION, ConfirmationCallback.NO,
234             JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL,
235             JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL
236         };
237         break;
238         case ConfirmationCallback.OK_CANCEL_OPTION:
239         optionType = JOptionPane.OK_CANCEL_OPTION;
240         translations = new int[] {
241             JOptionPane.OK_OPTION, ConfirmationCallback.OK,
242             JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL,
243             JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL
244         };
245         break;
246         case ConfirmationCallback.UNSPECIFIED_OPTION:
247         options = callback.getOptions();
248         /*
249          * There's no way to know if the default option means
250          * to cancel the login, but there isn't a better way
251          * to guess this.
252          */

253         translations = new int[] {
254             JOptionPane.CLOSED_OPTION, callback.getDefaultOption()
255         };
256         break;
257         default:
258         throw new UnsupportedCallbackException JavaDoc(
259             callback,
260             "Unrecognized option type: " + confirmationOptionType);
261         }
262
263         int confirmationMessageType = callback.getMessageType();
264         switch (confirmationMessageType) {
265         case ConfirmationCallback.WARNING:
266         messageType = JOptionPane.WARNING_MESSAGE;
267         break;
268         case ConfirmationCallback.ERROR:
269         messageType = JOptionPane.ERROR_MESSAGE;
270         break;
271         case ConfirmationCallback.INFORMATION:
272         messageType = JOptionPane.INFORMATION_MESSAGE;
273         break;
274         default:
275         throw new UnsupportedCallbackException JavaDoc(
276             callback,
277             "Unrecognized message type: " + confirmationMessageType);
278         }
279     }
280
281
282     /* Process the result returned by the Swing dialog */
283     void handleResult(int result) {
284         if (callback == null) {
285         return;
286         }
287
288         for (int i = 0; i < translations.length; i += 2) {
289         if (translations[i] == result) {
290             result = translations[i + 1];
291             break;
292         }
293         }
294         callback.setSelectedIndex(result);
295     }
296     }
297 }
298
Popular Tags