KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)TextCallbackHandler.java 1.8 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.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.PushbackInputStream JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import sun.security.util.Password;
28
29 /**
30  * <p>
31  * Prompts and reads from the command line for answers to authentication
32  * questions.
33  * This can be used by a JAAS application to instantiate a
34  * CallbackHandler
35  * @see javax.security.auth.callback
36  */

37
38 public class TextCallbackHandler implements CallbackHandler JavaDoc {
39
40     /**
41      * <p>Creates a callback handler that prompts and reads from the
42      * command line for answers to authentication questions.
43      * This can be used by JAAS applications to instantiate a
44      * CallbackHandler.
45
46      */

47     public TextCallbackHandler() { }
48
49     /**
50      * Handles the specified set of callbacks.
51      *
52      * @param callbacks the callbacks to handle
53      * @throws IOException if an input or output error occurs.
54      * @throws UnsupportedCallbackException if the callback is not an
55      * instance of NameCallback or PasswordCallback
56      */

57     public void handle(Callback JavaDoc[] callbacks)
58     throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
59     {
60     ConfirmationCallback JavaDoc confirmation = null;
61
62     for (int i = 0; i < callbacks.length; i++) {
63         if (callbacks[i] instanceof TextOutputCallback JavaDoc) {
64         TextOutputCallback JavaDoc tc = (TextOutputCallback JavaDoc) callbacks[i];
65
66         String JavaDoc text;
67         switch (tc.getMessageType()) {
68         case TextOutputCallback.INFORMATION:
69             text = "";
70             break;
71         case TextOutputCallback.WARNING:
72             text = "Warning: ";
73             break;
74         case TextOutputCallback.ERROR:
75             text = "Error: ";
76             break;
77         default:
78             throw new UnsupportedCallbackException JavaDoc(
79             callbacks[i], "Unrecognized message type");
80         }
81
82         String JavaDoc message = tc.getMessage();
83         if (message != null) {
84             text += message;
85         }
86         if (text != null) {
87             System.err.println(text);
88         }
89         
90         } else if (callbacks[i] instanceof NameCallback JavaDoc) {
91         NameCallback JavaDoc nc = (NameCallback JavaDoc) callbacks[i];
92
93         if (nc.getDefaultName() == null) {
94             System.err.print(nc.getPrompt());
95         } else {
96             System.err.print(nc.getPrompt() +
97                 " [" + nc.getDefaultName() + "] ");
98         }
99         System.err.flush();
100
101         String JavaDoc result = readLine();
102         if (result.equals("")) {
103             result = nc.getDefaultName();
104         }
105
106         nc.setName(result);
107  
108         } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
109         PasswordCallback JavaDoc pc = (PasswordCallback JavaDoc) callbacks[i];
110
111         System.err.print(pc.getPrompt());
112         System.err.flush();
113
114         pc.setPassword(Password.readPassword(System.in));
115   
116         } else if (callbacks[i] instanceof ConfirmationCallback JavaDoc) {
117         confirmation = (ConfirmationCallback JavaDoc) callbacks[i];
118
119         } else {
120         throw new UnsupportedCallbackException JavaDoc(
121             callbacks[i], "Unrecognized Callback");
122         }
123     }
124         
125     /* Do the confirmation callback last. */
126     if (confirmation != null) {
127         doConfirmation(confirmation);
128     }
129     }
130
131     /* Reads a line of input */
132     private String JavaDoc readLine() throws IOException JavaDoc {
133     return new BufferedReader JavaDoc
134         (new InputStreamReader JavaDoc(System.in)).readLine();
135     }
136
137     private void doConfirmation(ConfirmationCallback JavaDoc confirmation)
138     throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
139     {
140     String JavaDoc prefix;
141     int messageType = confirmation.getMessageType();
142     switch (messageType) {
143     case ConfirmationCallback.WARNING:
144         prefix = "Warning: ";
145         break;
146     case ConfirmationCallback.ERROR:
147         prefix = "Error: ";
148         break;
149     case ConfirmationCallback.INFORMATION:
150         prefix = "";
151         break;
152     default:
153         throw new UnsupportedCallbackException JavaDoc(
154         confirmation, "Unrecognized message type: " + messageType);
155     }
156
157     class OptionInfo {
158         String JavaDoc name;
159         int value;
160         OptionInfo(String JavaDoc name, int value) {
161         this.name = name;
162         this.value = value;
163         }
164     }
165
166     OptionInfo[] options;
167     int optionType = confirmation.getOptionType();
168     switch (optionType) {
169     case ConfirmationCallback.YES_NO_OPTION:
170         options = new OptionInfo[] {
171         new OptionInfo("Yes", ConfirmationCallback.YES),
172         new OptionInfo("No", ConfirmationCallback.NO)
173         };
174         break;
175     case ConfirmationCallback.YES_NO_CANCEL_OPTION:
176         options = new OptionInfo[] {
177         new OptionInfo("Yes", ConfirmationCallback.YES),
178         new OptionInfo("No", ConfirmationCallback.NO),
179         new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
180         };
181         break;
182     case ConfirmationCallback.OK_CANCEL_OPTION:
183         options = new OptionInfo[] {
184         new OptionInfo("OK", ConfirmationCallback.OK),
185         new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
186         };
187         break;
188     case ConfirmationCallback.UNSPECIFIED_OPTION:
189         String JavaDoc[] optionStrings = confirmation.getOptions();
190         options = new OptionInfo[optionStrings.length];
191         for (int i = 0; i < options.length; i++) {
192         options[i].value = i;
193         }
194         break;
195     default:
196         throw new UnsupportedCallbackException JavaDoc(
197         confirmation, "Unrecognized option type: " + optionType);
198     }
199
200     int defaultOption = confirmation.getDefaultOption();
201
202     String JavaDoc prompt = confirmation.getPrompt();
203     if (prompt == null) {
204         prompt = "";
205     }
206     prompt = prefix + prompt;
207     if (!prompt.equals("")) {
208         System.err.println(prompt);
209     }
210
211     for (int i = 0; i < options.length; i++) {
212         if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) {
213         // defaultOption is an index into the options array
214
System.err.println(
215             i + ". " + options[i].name +
216             (i == defaultOption ? " [default]" : ""));
217         } else {
218         // defaultOption is an option value
219
System.err.println(
220             i + ". " + options[i].name +
221             (options[i].value == defaultOption ? " [default]" : ""));
222         }
223     }
224     System.err.print("Enter a number: ");
225     System.err.flush();
226     int result;
227     try {
228         result = Integer.parseInt(readLine());
229         if (result < 0 || result > (options.length - 1)) {
230         result = defaultOption;
231         }
232         result = options[result].value;
233     } catch (NumberFormatException JavaDoc e) {
234         result = defaultOption;
235     }
236
237     confirmation.setSelectedIndex(result);
238     }
239 }
240
Popular Tags