2004-09-21 Andreas Tobler <a.tobler@schweiz.ch>
[official-gcc.git] / libjava / javax / security / auth / callback / CallbackHandler.java
blob289999c5ee18bf90ee4719f04ca6a297ebd790cb
1 /* CallbackHandler.java -- base interface for callback handlers.
2 Copyright (C) 2003, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.security.auth.callback;
41 import java.io.IOException;
43 /**
44 * <p>An application implements a <code>CallbackHandler</code> and passes it to
45 * underlying security services so that they may interact with the application
46 * to retrieve specific authentication data, such as usernames and passwords, or
47 * to display certain information, such as error and warning messages.</p>
49 * <p><code>CallbackHandler</code>s are implemented in an application-dependent
50 * fashion. For example, implementations for an application with a graphical
51 * user interface (GUI) may pop up windows to prompt for requested information
52 * or to display error messages. An implementation may also choose to obtain
53 * requested information from an alternate source without asking the end user.</p>
55 * <p>Underlying security services make requests for different types of
56 * information by passing individual Callbacks to the <code>CallbackHandler</code>.
57 * The <code>CallbackHandler</code> implementation decides how to retrieve and
58 * display information depending on the {@link Callback}s passed to it. For
59 * example, if the underlying service needs a username and password to
60 * authenticate a user, it uses a {@link NameCallback} and
61 * {@link PasswordCallback}. The <code>CallbackHandler</code> can then choose
62 * to prompt for a username and password serially, or to prompt for both in a
63 * single window.</p>
65 * <p>A default <code>CallbackHandler</code> class implementation may be
66 * specified in the <code>auth.login.defaultCallbackHandler</code> security
67 * property. The security property can be set in the Java security properties
68 * file located in the file named
69 * <code>&lt;JAVA_HOME>/lib/security/java.security</code>, where
70 * <code>&lt;JAVA_HOME></code> refers to the directory where the SDK was
71 * installed.</p>
73 * <p>If the security property is set to the fully qualified name of a
74 * <code>CallbackHandler</code> implementation class, then a
75 * <code>LoginContext</code>will load the specified <code>CallbackHandler</code>
76 * and pass it to the underlying <code>LoginModules</code>. The
77 * <code>LoginContext</code> only loads the default handler if one was not
78 * provided.</p>
80 * <p>All default handler implementations must provide a public zero-argument
81 * constructor.</p>
83 * @version $Revision: 1.1 $
85 public interface CallbackHandler
88 /**
89 * <p>Retrieve or display the information requested in the provided
90 * {@link Callback}s.</p>
92 * <p>The <code>handle()</code> method implementation checks the instance(s)
93 * of the {@link Callback} object(s) passed in to retrieve or display the
94 * requested information. The following example is provided to help
95 * demonstrate what an <code>handle()</code> method implementation might look
96 * like. This example code is for guidance only. Many details, including
97 * proper error handling, are left out for simplicity.</p>
99 * <pre>
100 *public void handle(Callback[] callbacks)
101 *throws IOException, UnsupportedCallbackException {
102 * for (int i = 0; i < callbacks.length; i++) {
103 * if (callbacks[i] instanceof TextOutputCallback) {
104 * // display the message according to the specified type
105 * TextOutputCallback toc = (TextOutputCallback)callbacks[i];
106 * switch (toc.getMessageType()) {
107 * case TextOutputCallback.INFORMATION:
108 * System.out.println(toc.getMessage());
109 * break;
110 * case TextOutputCallback.ERROR:
111 * System.out.println("ERROR: " + toc.getMessage());
112 * break;
113 * case TextOutputCallback.WARNING:
114 * System.out.println("WARNING: " + toc.getMessage());
115 * break;
116 * default:
117 * throw new IOException("Unsupported message type: "
118 * + toc.getMessageType());
120 * } else if (callbacks[i] instanceof NameCallback) {
121 * // prompt the user for a username
122 * NameCallback nc = (NameCallback)callbacks[i];
123 * // ignore the provided defaultName
124 * System.err.print(nc.getPrompt());
125 * System.err.flush();
126 * nc.setName((new BufferedReader(
127 * new InputStreamReader(System.in))).readLine());
128 * } else if (callbacks[i] instanceof PasswordCallback) {
129 * // prompt the user for sensitive information
130 * PasswordCallback pc = (PasswordCallback)callbacks[i];
131 * System.err.print(pc.getPrompt());
132 * System.err.flush();
133 * pc.setPassword(readPassword(System.in));
134 * } else {
135 * throw new UnsupportedCallbackException(
136 * callbacks[i], "Unrecognized Callback");
141 * // Reads user password from given input stream.
142 *private char[] readPassword(InputStream in) throws IOException {
143 * // insert code to read a user password from the input stream
145 * </pre>
147 * @param callbacks an array of {@link Callback} objects provided by an
148 * underlying security service which contains the information requested to
149 * be retrieved or displayed.
150 * @throws IOException if an input or output error occurs.
151 * @throws UnsupportedCallbackException if the implementation of this method
152 * does not support one or more of the Callbacks specified in the
153 * <code>callbacks</code> parameter.
155 void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException;