History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/security/auth/callback/TextCallbackHandler.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/security/auth/callback/TextCallbackHandler.java+261−0
@@ -0,0 +1,261 @@
1+/*
2+ * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.security.auth.callback;
27+
28+/* JAAS imports */
29+import javax.security.auth.callback.Callback;
30+import javax.security.auth.callback.CallbackHandler;
31+import javax.security.auth.callback.ConfirmationCallback;
32+import javax.security.auth.callback.NameCallback;
33+import javax.security.auth.callback.PasswordCallback;
34+import javax.security.auth.callback.TextOutputCallback;
35+import javax.security.auth.callback.UnsupportedCallbackException;
36+
37+/* Java imports */
38+import java.io.BufferedReader;
39+import java.io.IOException;
40+import java.io.InputStream;
41+import java.io.InputStreamReader;
42+import java.io.PushbackInputStream;
43+import java.util.Arrays;
44+
45+import sun.security.util.Password;
46+
47+/**
48+ * <p>
49+ * Prompts and reads from the command line for answers to authentication
50+ * questions.
51+ * This can be used by a JAAS application to instantiate a
52+ * CallbackHandler
53+ * @see javax.security.auth.callback
54+ */
55+
56+public class TextCallbackHandler implements CallbackHandler {
57+
58+ /**
59+ * <p>Creates a callback handler that prompts and reads from the
60+ * command line for answers to authentication questions.
61+ * This can be used by JAAS applications to instantiate a
62+ * CallbackHandler.
63+
64+ */
65+ public TextCallbackHandler() { }
66+
67+ /**
68+ * Handles the specified set of callbacks.
69+ *
70+ * @param callbacks the callbacks to handle
71+ * @throws IOException if an input or output error occurs.
72+ * @throws UnsupportedCallbackException if the callback is not an
73+ * instance of NameCallback or PasswordCallback
74+ */
75+ public void handle(Callback[] callbacks)
76+ throws IOException, UnsupportedCallbackException
77+ {
78+ ConfirmationCallback confirmation = null;
79+
80+ for (int i = 0; i < callbacks.length; i++) {
81+ if (callbacks[i] instanceof TextOutputCallback) {
82+ TextOutputCallback tc = (TextOutputCallback) callbacks[i];
83+
84+ String text;
85+ switch (tc.getMessageType()) {
86+ case TextOutputCallback.INFORMATION:
87+ text = "";
88+ break;
89+ case TextOutputCallback.WARNING:
90+ text = "Warning: ";
91+ break;
92+ case TextOutputCallback.ERROR:
93+ text = "Error: ";
94+ break;
95+ default:
96+ throw new UnsupportedCallbackException(
97+ callbacks[i], "Unrecognized message type");
98+ }
99+
100+ String message = tc.getMessage();
101+ if (message != null) {
102+ text += message;
103+ }
104+ if (text != null) {
105+ System.err.println(text);
106+ }
107+
108+ } else if (callbacks[i] instanceof NameCallback) {
109+ NameCallback nc = (NameCallback) callbacks[i];
110+
111+ if (nc.getDefaultName() == null) {
112+ System.err.print(nc.getPrompt());
113+ } else {
114+ System.err.print(nc.getPrompt() +
115+ " [" + nc.getDefaultName() + "] ");
116+ }
117+ System.err.flush();
118+
119+ String result = readLine();
120+ if (result.equals("")) {
121+ result = nc.getDefaultName();
122+ }
123+
124+ nc.setName(result);
125+
126+ } else if (callbacks[i] instanceof PasswordCallback) {
127+ PasswordCallback pc = (PasswordCallback) callbacks[i];
128+
129+ System.err.print(pc.getPrompt());
130+ System.err.flush();
131+
132+ pc.setPassword(Password.readPassword(System.in, pc.isEchoOn()));
133+
134+ } else if (callbacks[i] instanceof ConfirmationCallback) {
135+ confirmation = (ConfirmationCallback) callbacks[i];
136+
137+ } else {
138+ throw new UnsupportedCallbackException(
139+ callbacks[i], "Unrecognized Callback");
140+ }
141+ }
142+
143+ /* Do the confirmation callback last. */
144+ if (confirmation != null) {
145+ doConfirmation(confirmation);
146+ }
147+ }
148+
149+ /* Reads a line of input */
150+ private String readLine() throws IOException {
151+ String result = new BufferedReader
152+ (new InputStreamReader(System.in)).readLine();
153+ if (result == null) {
154+ throw new IOException("Cannot read from System.in");
155+ }
156+ return result;
157+ }
158+
159+ private void doConfirmation(ConfirmationCallback confirmation)
160+ throws IOException, UnsupportedCallbackException
161+ {
162+ String prefix;
163+ int messageType = confirmation.getMessageType();
164+ switch (messageType) {
165+ case ConfirmationCallback.WARNING:
166+ prefix = "Warning: ";
167+ break;
168+ case ConfirmationCallback.ERROR:
169+ prefix = "Error: ";
170+ break;
171+ case ConfirmationCallback.INFORMATION:
172+ prefix = "";
173+ break;
174+ default:
175+ throw new UnsupportedCallbackException(
176+ confirmation, "Unrecognized message type: " + messageType);
177+ }
178+
179+ class OptionInfo {
180+ String name;
181+ int value;
182+ OptionInfo(String name, int value) {
183+ this.name = name;
184+ this.value = value;
185+ }
186+ }
187+
188+ OptionInfo[] options;
189+ int optionType = confirmation.getOptionType();
190+ switch (optionType) {
191+ case ConfirmationCallback.YES_NO_OPTION:
192+ options = new OptionInfo[] {
193+ new OptionInfo("Yes", ConfirmationCallback.YES),
194+ new OptionInfo("No", ConfirmationCallback.NO)
195+ };
196+ break;
197+ case ConfirmationCallback.YES_NO_CANCEL_OPTION:
198+ options = new OptionInfo[] {
199+ new OptionInfo("Yes", ConfirmationCallback.YES),
200+ new OptionInfo("No", ConfirmationCallback.NO),
201+ new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
202+ };
203+ break;
204+ case ConfirmationCallback.OK_CANCEL_OPTION:
205+ options = new OptionInfo[] {
206+ new OptionInfo("OK", ConfirmationCallback.OK),
207+ new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
208+ };
209+ break;
210+ case ConfirmationCallback.UNSPECIFIED_OPTION:
211+ String[] optionStrings = confirmation.getOptions();
212+ options = new OptionInfo[optionStrings.length];
213+ for (int i = 0; i < options.length; i++) {
214+ options[i] = new OptionInfo(optionStrings[i], i);
215+ }
216+ break;
217+ default:
218+ throw new UnsupportedCallbackException(
219+ confirmation, "Unrecognized option type: " + optionType);
220+ }
221+
222+ int defaultOption = confirmation.getDefaultOption();
223+
224+ String prompt = confirmation.getPrompt();
225+ if (prompt == null) {
226+ prompt = "";
227+ }
228+ prompt = prefix + prompt;
229+ if (!prompt.equals("")) {
230+ System.err.println(prompt);
231+ }
232+
233+ for (int i = 0; i < options.length; i++) {
234+ if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) {
235+ // defaultOption is an index into the options array
236+ System.err.println(
237+ i + ". " + options[i].name +
238+ (i == defaultOption ? " [default]" : ""));
239+ } else {
240+ // defaultOption is an option value
241+ System.err.println(
242+ i + ". " + options[i].name +
243+ (options[i].value == defaultOption ? " [default]" : ""));
244+ }
245+ }
246+ System.err.print("Enter a number: ");
247+ System.err.flush();
248+ int result;
249+ try {
250+ result = Integer.parseInt(readLine());
251+ if (result < 0 || result > (options.length - 1)) {
252+ result = defaultOption;
253+ }
254+ result = options[result].value;
255+ } catch (NumberFormatException e) {
256+ result = defaultOption;
257+ }
258+
259+ confirmation.setSelectedIndex(result);
260+ }
261+}