Path: blob/master/test/jdk/com/sun/security/sasl/ClientCallbackHandler.java
41152 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.security.auth.callback.*;24import java.util.*;25import java.io.*;26import javax.security.sasl.RealmCallback;27import javax.security.sasl.RealmChoiceCallback;28import com.sun.security.auth.callback.TextCallbackHandler;2930public final class ClientCallbackHandler extends TextCallbackHandler {31private String username = "john";32private String password = "test123";33private boolean auto;3435public ClientCallbackHandler(boolean auto) {36super();37this.auto = auto;38}3940public void handle(Callback[] callbacks) throws UnsupportedCallbackException,41IOException {42NameCallback ncb = null;43PasswordCallback pcb = null;44RealmChoiceCallback rccb = null;4546List namePw = new ArrayList(3);4748for (int i = 0; i < callbacks.length; i++) {49if (callbacks[i] instanceof NameCallback) {50if (auto) {51((NameCallback)callbacks[i]).setName(username);52} else {53// To be processed by TextCallbackHandler54namePw.add(callbacks[i]);55}56} else if (callbacks[i] instanceof PasswordCallback) {57if (auto) {58((PasswordCallback)callbacks[i]).setPassword(59password.toCharArray());60} else {61// To be processed by TextCallbackHandler62namePw.add(callbacks[i]);63}64} else if (callbacks[i] instanceof RealmChoiceCallback) {65RealmChoiceCallback rcb = (RealmChoiceCallback) callbacks[i];66if (!auto) {67System.err.println(rcb.getPrompt());68}6970String[] choices = rcb.getChoices();7172if (!auto) {73for (int j=0; j < choices.length; j++) {74System.err.println(j + ":" + choices[j]);75}76}7778int selection;79if (auto) {80selection = 0;81} else {82System.err.print("Enter choice number: ");83String result = readLine();84if (result.equals("")) {85selection = rcb.getDefaultChoice();86} else {87selection = Integer.parseInt(result);88}89}90rcb.setSelectedIndex(selection);9192} else if (callbacks[i] instanceof RealmCallback) {93RealmCallback rcb = (RealmCallback) callbacks[i];94String realm = rcb.getDefaultText();9596if (auto) {97if (realm != null) {98rcb.setText(realm);99}100} else {101if (realm == null) {102System.err.print(rcb.getPrompt());103} else {104System.err.print(rcb.getPrompt() + " [" + realm + "] ");105}106107System.err.flush();108109String result = readLine();110if (result.equals("")) {111result = realm;112}113rcb.setText(result);114}115} else {116throw new UnsupportedCallbackException(callbacks[i]);117}118}119120// Process name/password callbacks using superclass121if (namePw.size() > 0) {122Callback[] np = new Callback[namePw.size()];123namePw.toArray(np);124125super.handle(np);126}127}128129/* Reads a line of input */130private String readLine() throws IOException {131return new BufferedReader132(new InputStreamReader(System.in)).readLine();133}134}135136137