Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/DefaultHandlerImpl.java
51705 views
/*1* Copyright (c) 2000, 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 java.io.*;24import java.util.Arrays;25import javax.security.auth.callback.*;2627/**28* A default CallbackHandler implementation.29*/30public class DefaultHandlerImpl implements CallbackHandler {3132/**33* Invoke an array of Callbacks.34*35* <p>36*37* @param callbacks an array of <code>Callback</code> objects which contain38* the information requested by an underlying security39* service to be retrieved or displayed.40*41* @exception java.io.IOException if an input or output error occurs. <p>42*43* @exception UnsupportedCallbackException if the implementation of this44* method does not support one or more of the Callbacks45* specified in the <code>callbacks</code> parameter.46*/47public void handle(Callback[] callbacks)48throws IOException, UnsupportedCallbackException {4950for (int i = 0; i < callbacks.length; i++) {51if (callbacks[i] instanceof TextOutputCallback) {5253// display the message according to the specified type54TextOutputCallback toc = (TextOutputCallback)callbacks[i];55switch (toc.getMessageType()) {56case TextOutputCallback.INFORMATION:57System.out.println(toc.getMessage());58break;59case TextOutputCallback.ERROR:60System.out.println("ERROR: " + toc.getMessage());61break;62case TextOutputCallback.WARNING:63System.out.println("WARNING: " + toc.getMessage());64break;65default:66throw new IOException("Unsupported message type: " +67toc.getMessageType());68}6970} else if (callbacks[i] instanceof NameCallback) {7172// prompt the user for a username73NameCallback nc = (NameCallback)callbacks[i];7475// ignore the provided defaultName76System.err.print(nc.getPrompt());77System.err.flush();78nc.setName((new BufferedReader79(new InputStreamReader(System.in))).readLine());8081} else if (callbacks[i] instanceof PasswordCallback) {8283// prompt the user for sensitive information84PasswordCallback pc = (PasswordCallback)callbacks[i];85System.err.print(pc.getPrompt());86System.err.flush();87pc.setPassword(readPassword(System.in));8889} else {90throw new UnsupportedCallbackException91(callbacks[i], "Unrecognized Callback");92}93}94}9596// Reads user password from given input stream.97private char[] readPassword(InputStream in) throws IOException {9899char[] lineBuffer;100char[] buf;101int i;102103buf = lineBuffer = new char[128];104105int room = buf.length;106int offset = 0;107int c;108109loop: while (true) {110switch (c = in.read()) {111case -1:112case '\n':113break loop;114115case '\r':116int c2 = in.read();117if ((c2 != '\n') && (c2 != -1)) {118if (!(in instanceof PushbackInputStream)) {119in = new PushbackInputStream(in);120}121((PushbackInputStream)in).unread(c2);122} else123break loop;124125default:126if (--room < 0) {127buf = new char[offset + 128];128room = buf.length - offset - 1;129System.arraycopy(lineBuffer, 0, buf, 0, offset);130Arrays.fill(lineBuffer, ' ');131lineBuffer = buf;132}133buf[offset++] = (char) c;134break;135}136}137138if (offset == 0) {139return null;140}141142char[] ret = new char[offset];143System.arraycopy(buf, 0, ret, 0, offset);144Arrays.fill(buf, ' ');145146return ret;147}148}149150151