Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / security / auth / callback / CallbackHandler.java
blobed1195a89e3d38d815c0848715f69950e45d862a
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>
84 public interface CallbackHandler
87 /**
88 * <p>Retrieve or display the information requested in the provided
89 * {@link Callback}s.</p>
91 * <p>The <code>handle()</code> method implementation checks the instance(s)
92 * of the {@link Callback} object(s) passed in to retrieve or display the
93 * requested information. The following example is provided to help
94 * demonstrate what an <code>handle()</code> method implementation might look
95 * like. This example code is for guidance only. Many details, including
96 * proper error handling, are left out for simplicity.</p>
98 * <pre>
99 *public void handle(Callback[] callbacks)
100 *throws IOException, UnsupportedCallbackException {
101 * for (int i = 0; i < callbacks.length; i++) {
102 * if (callbacks[i] instanceof TextOutputCallback) {
103 * // display the message according to the specified type
104 * TextOutputCallback toc = (TextOutputCallback)callbacks[i];
105 * switch (toc.getMessageType()) {
106 * case TextOutputCallback.INFORMATION:
107 * System.out.println(toc.getMessage());
108 * break;
109 * case TextOutputCallback.ERROR:
110 * System.out.println("ERROR: " + toc.getMessage());
111 * break;
112 * case TextOutputCallback.WARNING:
113 * System.out.println("WARNING: " + toc.getMessage());
114 * break;
115 * default:
116 * throw new IOException("Unsupported message type: "
117 * + toc.getMessageType());
119 * } else if (callbacks[i] instanceof NameCallback) {
120 * // prompt the user for a username
121 * NameCallback nc = (NameCallback)callbacks[i];
122 * // ignore the provided defaultName
123 * System.err.print(nc.getPrompt());
124 * System.err.flush();
125 * nc.setName((new BufferedReader(
126 * new InputStreamReader(System.in))).readLine());
127 * } else if (callbacks[i] instanceof PasswordCallback) {
128 * // prompt the user for sensitive information
129 * PasswordCallback pc = (PasswordCallback)callbacks[i];
130 * System.err.print(pc.getPrompt());
131 * System.err.flush();
132 * pc.setPassword(readPassword(System.in));
133 * } else {
134 * throw new UnsupportedCallbackException(
135 * callbacks[i], "Unrecognized Callback");
140 * // Reads user password from given input stream.
141 *private char[] readPassword(InputStream in) throws IOException {
142 * // insert code to read a user password from the input stream
144 * </pre>
146 * @param callbacks an array of {@link Callback} objects provided by an
147 * underlying security service which contains the information requested to
148 * be retrieved or displayed.
149 * @throws IOException if an input or output error occurs.
150 * @throws UnsupportedCallbackException if the implementation of this method
151 * does not support one or more of the Callbacks specified in the
152 * <code>callbacks</code> parameter.
154 void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException;