1 /* AbstractCallbackHandler.java --
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 This file is a 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 of the License, or (at
9 your option) 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; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
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
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 gnu.javax.security.auth.callback;
41 import gnu.java.security.Engine;
43 import java.io.IOException;
44 import java.lang.reflect.InvocationTargetException;
45 import java.util.PropertyResourceBundle;
46 import java.util.ResourceBundle;
48 import java.security.NoSuchAlgorithmException;
49 import java.security.NoSuchProviderException;
50 import java.security.Provider;
51 import java.security.Security;
53 import javax.security.auth.callback.Callback;
54 import javax.security.auth.callback.CallbackHandler;
55 import javax.security.auth.callback.ChoiceCallback;
56 import javax.security.auth.callback.ConfirmationCallback;
57 import javax.security.auth.callback.LanguageCallback;
58 import javax.security.auth.callback.NameCallback;
59 import javax.security.auth.callback.PasswordCallback;
60 import javax.security.auth.callback.TextInputCallback;
61 import javax.security.auth.callback.TextOutputCallback;
62 import javax.security.auth.callback.UnsupportedCallbackException;
64 public abstract class AbstractCallbackHandler implements CallbackHandler
68 // -------------------------------------------------------------------------
70 private static final String SERVICE = "CallbackHandler";
72 protected final ResourceBundle messages;
74 private final String name;
77 // -------------------------------------------------------------------------
79 protected AbstractCallbackHandler (final String name)
82 messages = PropertyResourceBundle.getBundle("gnu/javax/security/auth/callback/MessagesBundle");
87 * Create an instance of <code>CallbackHandler</code> of the designated
88 * <code>type</code> from the first Security Provider which offers it.
90 * @param type the type of callback handler to create.
91 * @return a newly created instance of <code>ClassbackHandler</code>.
92 * @throws NoSuchAlgorithmException if no security provider is found to offer
93 * an implementation of <code>CallbackHandler</code> of the
94 * designated <code>type</code>.
96 public static CallbackHandler getInstance(String type)
97 throws NoSuchAlgorithmException
99 Provider[] p = Security.getProviders();
100 NoSuchAlgorithmException lastException = null;
101 for (int i = 0; i < p.length; i++)
104 return getInstance(type, p[i]);
106 catch (NoSuchAlgorithmException x)
110 if (lastException != null)
112 throw new NoSuchAlgorithmException(type);
116 * Create an instance of <code>CallbackHandler</code> of the designated
117 * <code>type</code> from the named security <code>provider</code>.
119 * @param type the type of callback handler to create.
120 * @param provider a named security provider to use.
121 * @return a newly created instance of <code>ClassbackHandler</code>.
122 * @throws NoSuchAlgorithmException if no security provider is found to offer
123 * an implementation of <code>CallbackHandler</code> of the
124 * designated <code>type</code>.
125 * @throws IllegalArgumentException if either <code>type</code> or
126 * <code>provider</code> is <code>null</code>, or if
127 * <code>type</code> is an empty string.
129 public static CallbackHandler getInstance(String type, String provider)
130 throws NoSuchAlgorithmException, NoSuchProviderException
132 if (provider == null)
133 throw new IllegalArgumentException("provider MUST NOT be null");
134 Provider p = Security.getProvider(provider);
136 throw new NoSuchProviderException(provider);
137 return getInstance(type, p);
141 * Create an instance of <code>CallbackHandler</code> of the designated
142 * <code>type</code> from the designated security <code>provider</code>.
144 * @param type the type of callback handler to create.
145 * @param provider a security provider to use.
146 * @return a newly created instance of <code>ClassbackHandler</code>.
147 * @throws NoSuchAlgorithmException if no security provider is found to offer
148 * an implementation of <code>CallbackHandler</code> of the
149 * designated <code>type</code>.
150 * @throws IllegalArgumentException if either <code>type</code> or
151 * <code>provider</code> is <code>null</code>, or if
152 * <code>type</code> is an empty string.
154 public static CallbackHandler getInstance(String type, Provider provider)
155 throws NoSuchAlgorithmException
157 StringBuilder sb = new StringBuilder("CallbackHandler of type [")
158 .append(type).append("] from provider[")
159 .append(provider).append("] could not be created");
163 return (CallbackHandler) Engine.getInstance(SERVICE, type, provider);
165 catch (InvocationTargetException x)
167 cause = x.getCause();
168 if (cause instanceof NoSuchAlgorithmException)
169 throw (NoSuchAlgorithmException) cause;
173 catch (ClassCastException x)
177 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
182 public void handle(Callback[] callbacks)
183 throws IOException, UnsupportedCallbackException
185 if (callbacks == null)
186 throw new NullPointerException();
187 for (int i = 0; i < callbacks.length; i++)
189 if (callbacks[i] == null)
191 if (callbacks[i] instanceof ChoiceCallback)
192 handleChoice((ChoiceCallback) callbacks[i]);
193 else if (callbacks[i] instanceof ConfirmationCallback)
194 handleConfirmation((ConfirmationCallback) callbacks[i]);
195 else if (callbacks[i] instanceof LanguageCallback)
196 handleLanguage((LanguageCallback) callbacks[i]);
197 else if (callbacks[i] instanceof NameCallback)
198 handleName((NameCallback) callbacks[i]);
199 else if (callbacks[i] instanceof PasswordCallback)
200 handlePassword((PasswordCallback) callbacks[i]);
201 else if (callbacks[i] instanceof TextInputCallback)
202 handleTextInput((TextInputCallback) callbacks[i]);
203 else if (callbacks[i] instanceof TextOutputCallback)
204 handleTextOutput((TextOutputCallback) callbacks[i]);
206 handleOther(callbacks[i]);
210 public final String getName ()
216 // -------------------------------------------------------------------------
219 * Handles a {@link ChoiceCallback}.
221 * @param callback The choice callback.
222 * @throws IOException If an I/O error occurs.
224 protected abstract void handleChoice(ChoiceCallback callback)
228 * Handles a {@link ConfirmationCallback}.
230 * @param callback The confirmation callback.
231 * @throws IOException If an I/O error occurs.
233 protected abstract void handleConfirmation(ConfirmationCallback callback)
237 * Handles a {@link LanguageCallback}.
239 * @param callback The language callback.
240 * @throws IOException If an I/O error occurs.
242 protected abstract void handleLanguage(LanguageCallback callback)
246 * Handles a {@link NameCallback}.
248 * @param callback The name callback.
249 * @throws IOException If an I/O error occurs.
251 protected abstract void handleName(NameCallback callback)
255 * Handles a {@link PasswordCallback}.
257 * @param callback The password callback.
258 * @throws IOException If an I/O error occurs.
260 protected abstract void handlePassword(PasswordCallback callback)
264 * Handles a {@link TextInputCallback}.
266 * @param callback The text input callback.
267 * @throws IOException If an I/O error occurs.
269 protected abstract void handleTextInput(TextInputCallback callback)
273 * Handles a {@link TextOutputCallback}.
275 * @param callback The text output callback.
276 * @throws IOException If an I/O error occurs.
278 protected abstract void handleTextOutput(TextOutputCallback callback)
282 * Handles an unknown callback. The default implementation simply throws
283 * an {@link UnsupportedCallbackException}.
285 * @param callback The callback to handle.
286 * @throws IOException If an I/O error occurs.
287 * @throws UnsupportedCallbackException If the specified callback is not
290 protected void handleOther(Callback callback)
291 throws IOException, UnsupportedCallbackException
293 throw new UnsupportedCallbackException(callback);