ClientCallbackHandler.java revision 2362
893N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Aimport javax.security.auth.callback.*;
893N/Aimport java.util.*;
893N/Aimport java.io.*;
893N/Aimport javax.security.sasl.RealmCallback;
893N/Aimport javax.security.sasl.RealmChoiceCallback;
893N/Aimport com.sun.security.auth.callback.TextCallbackHandler;
893N/A
893N/Apublic final class ClientCallbackHandler extends TextCallbackHandler {
893N/A private String username = "john";
893N/A private String password = "test123";
3471N/A private boolean auto;
893N/A
893N/A public ClientCallbackHandler(boolean auto) {
3471N/A super();
3471N/A this.auto = auto;
3471N/A }
893N/A
893N/A public void handle(Callback[] callbacks) throws UnsupportedCallbackException,
3471N/A IOException {
893N/A NameCallback ncb = null;
893N/A PasswordCallback pcb = null;
893N/A RealmChoiceCallback rccb = null;
3471N/A
3471N/A List namePw = new ArrayList(3);
3471N/A
3471N/A for (int i = 0; i < callbacks.length; i++) {
893N/A if (callbacks[i] instanceof NameCallback) {
3471N/A if (auto) {
893N/A ((NameCallback)callbacks[i]).setName(username);
893N/A } else {
893N/A // To be processed by TextCallbackHandler
893N/A namePw.add(callbacks[i]);
893N/A }
893N/A } else if (callbacks[i] instanceof PasswordCallback) {
3471N/A if (auto) {
893N/A ((PasswordCallback)callbacks[i]).setPassword(
3471N/A password.toCharArray());
893N/A } else {
893N/A // To be processed by TextCallbackHandler
3471N/A namePw.add(callbacks[i]);
893N/A }
893N/A } else if (callbacks[i] instanceof RealmChoiceCallback) {
893N/A RealmChoiceCallback rcb = (RealmChoiceCallback) callbacks[i];
893N/A if (!auto) {
3471N/A System.err.println(rcb.getPrompt());
893N/A }
893N/A
893N/A String[] choices = rcb.getChoices();
893N/A
893N/A if (!auto) {
893N/A for (int j=0; j < choices.length; j++) {
893N/A System.err.println(j + ":" + choices[j]);
893N/A }
}
int selection;
if (auto) {
selection = 0;
} else {
System.err.print("Enter choice number: ");
String result = readLine();
if (result.equals("")) {
selection = rcb.getDefaultChoice();
} else {
selection = Integer.parseInt(result);
}
}
rcb.setSelectedIndex(selection);
} else if (callbacks[i] instanceof RealmCallback) {
RealmCallback rcb = (RealmCallback) callbacks[i];
String realm = rcb.getDefaultText();
if (auto) {
if (realm != null) {
rcb.setText(realm);
}
} else {
if (realm == null) {
System.err.print(rcb.getPrompt());
} else {
System.err.print(rcb.getPrompt() + " [" + realm + "] ");
}
System.err.flush();
String result = readLine();
if (result.equals("")) {
result = realm;
}
rcb.setText(result);
}
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
// Process name/password callbacks using superclass
if (namePw.size() > 0) {
Callback[] np = new Callback[namePw.size()];
namePw.toArray(np);
super.handle(np);
}
}
/* Reads a line of input */
private String readLine() throws IOException {
return new BufferedReader
(new InputStreamReader(System.in)).readLine();
}
}