0N/A/*
2362N/A * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.Arrays;
0N/Aimport javax.security.auth.callback.*;
0N/A
0N/A/**
0N/A * A default CallbackHandler implementation.
0N/A */
0N/Apublic class DefaultHandlerImpl implements CallbackHandler {
0N/A
0N/A /**
0N/A * Invoke an array of Callbacks.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param callbacks an array of <code>Callback</code> objects which contain
0N/A * the information requested by an underlying security
0N/A * service to be retrieved or displayed.
0N/A *
0N/A * @exception java.io.IOException if an input or output error occurs. <p>
0N/A *
0N/A * @exception UnsupportedCallbackException if the implementation of this
0N/A * method does not support one or more of the Callbacks
0N/A * specified in the <code>callbacks</code> parameter.
0N/A */
0N/A public void handle(Callback[] callbacks)
0N/A throws IOException, UnsupportedCallbackException {
0N/A
0N/A for (int i = 0; i < callbacks.length; i++) {
0N/A if (callbacks[i] instanceof TextOutputCallback) {
0N/A
0N/A // display the message according to the specified type
0N/A TextOutputCallback toc = (TextOutputCallback)callbacks[i];
0N/A switch (toc.getMessageType()) {
0N/A case TextOutputCallback.INFORMATION:
0N/A System.out.println(toc.getMessage());
0N/A break;
0N/A case TextOutputCallback.ERROR:
0N/A System.out.println("ERROR: " + toc.getMessage());
0N/A break;
0N/A case TextOutputCallback.WARNING:
0N/A System.out.println("WARNING: " + toc.getMessage());
0N/A break;
0N/A default:
0N/A throw new IOException("Unsupported message type: " +
0N/A toc.getMessageType());
0N/A }
0N/A
0N/A } else if (callbacks[i] instanceof NameCallback) {
0N/A
0N/A // prompt the user for a username
0N/A NameCallback nc = (NameCallback)callbacks[i];
0N/A
0N/A // ignore the provided defaultName
0N/A System.err.print(nc.getPrompt());
0N/A System.err.flush();
0N/A nc.setName((new BufferedReader
0N/A (new InputStreamReader(System.in))).readLine());
0N/A
0N/A } else if (callbacks[i] instanceof PasswordCallback) {
0N/A
0N/A // prompt the user for sensitive information
0N/A PasswordCallback pc = (PasswordCallback)callbacks[i];
0N/A System.err.print(pc.getPrompt());
0N/A System.err.flush();
0N/A pc.setPassword(readPassword(System.in));
0N/A
0N/A } else {
0N/A throw new UnsupportedCallbackException
0N/A (callbacks[i], "Unrecognized Callback");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Reads user password from given input stream.
0N/A private char[] readPassword(InputStream in) throws IOException {
0N/A
0N/A char[] lineBuffer;
0N/A char[] buf;
0N/A int i;
0N/A
0N/A buf = lineBuffer = new char[128];
0N/A
0N/A int room = buf.length;
0N/A int offset = 0;
0N/A int c;
0N/A
0N/Aloop: while (true) {
0N/A switch (c = in.read()) {
0N/A case -1:
0N/A case '\n':
0N/A break loop;
0N/A
0N/A case '\r':
0N/A int c2 = in.read();
0N/A if ((c2 != '\n') && (c2 != -1)) {
0N/A if (!(in instanceof PushbackInputStream)) {
0N/A in = new PushbackInputStream(in);
0N/A }
0N/A ((PushbackInputStream)in).unread(c2);
0N/A } else
0N/A break loop;
0N/A
0N/A default:
0N/A if (--room < 0) {
0N/A buf = new char[offset + 128];
0N/A room = buf.length - offset - 1;
0N/A System.arraycopy(lineBuffer, 0, buf, 0, offset);
0N/A Arrays.fill(lineBuffer, ' ');
0N/A lineBuffer = buf;
0N/A }
0N/A buf[offset++] = (char) c;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (offset == 0) {
0N/A return null;
0N/A }
0N/A
0N/A char[] ret = new char[offset];
0N/A System.arraycopy(buf, 0, ret, 0, offset);
0N/A Arrays.fill(buf, ' ');
0N/A
0N/A return ret;
0N/A }
0N/A}