Source for gnu.javax.security.auth.callback.AbstractCallbackHandler

   1: /* AbstractCallbackHandler.java --
   2:    Copyright (C) 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  19: USA
  20: 
  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.
  25: 
  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.  */
  37: 
  38: 
  39: package gnu.javax.security.auth.callback;
  40: 
  41: import gnu.java.security.Engine;
  42: 
  43: import java.io.IOException;
  44: import java.lang.reflect.InvocationTargetException;
  45: import java.util.PropertyResourceBundle;
  46: import java.util.ResourceBundle;
  47: 
  48: import java.security.NoSuchAlgorithmException;
  49: import java.security.NoSuchProviderException;
  50: import java.security.Provider;
  51: import java.security.Security;
  52: 
  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;
  63: 
  64: public abstract class AbstractCallbackHandler implements CallbackHandler
  65: {
  66: 
  67:   // Fields.
  68:   // -------------------------------------------------------------------------
  69: 
  70:   private static final String SERVICE = "CallbackHandler";
  71: 
  72:   protected final ResourceBundle messages;
  73: 
  74:   private final String name;
  75: 
  76:   // Constructors.
  77:   // -------------------------------------------------------------------------
  78: 
  79:   protected AbstractCallbackHandler (final String name)
  80:   {
  81:     super();
  82:     messages = PropertyResourceBundle.getBundle("gnu/javax/security/auth/callback/MessagesBundle");
  83:     this.name = name;
  84:   }
  85: 
  86:   /**
  87:    * Create an instance of <code>CallbackHandler</code> of the designated
  88:    * <code>type</code> from the first Security Provider which offers it.
  89:    *
  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>.
  95:    */
  96:   public static CallbackHandler getInstance(String type)
  97:       throws NoSuchAlgorithmException
  98:   {
  99:     Provider[] p = Security.getProviders();
 100:     NoSuchAlgorithmException lastException = null;
 101:     for (int i = 0; i < p.length; i++)
 102:       try
 103:         {
 104:           return getInstance(type, p[i]);
 105:         }
 106:       catch (NoSuchAlgorithmException x)
 107:         {
 108:           lastException = x;
 109:         }
 110:     if (lastException != null)
 111:       throw lastException;
 112:     throw new NoSuchAlgorithmException(type);
 113:   }
 114: 
 115:   /**
 116:    * Create an instance of <code>CallbackHandler</code> of the designated
 117:    * <code>type</code> from the named security <code>provider</code>.
 118:    *
 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.
 128:    */
 129:   public static CallbackHandler getInstance(String type, String provider)
 130:       throws NoSuchAlgorithmException, NoSuchProviderException
 131:   {
 132:     if (provider == null)
 133:       throw new IllegalArgumentException("provider MUST NOT be null");
 134:     Provider p = Security.getProvider(provider);
 135:     if (p == null)
 136:       throw new NoSuchProviderException(provider);
 137:     return getInstance(type, p);
 138:   }
 139: 
 140:   /**
 141:    * Create an instance of <code>CallbackHandler</code> of the designated
 142:    * <code>type</code> from the designated security <code>provider</code>.
 143:    *
 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.
 153:    */
 154:   public static CallbackHandler getInstance(String type, Provider provider)
 155:     throws NoSuchAlgorithmException
 156:   {
 157:     StringBuilder sb = new StringBuilder("CallbackHandler of type [")
 158:         .append(type).append("] from provider[")
 159:         .append(provider).append("] could not be created");
 160:     Throwable cause;
 161:     try
 162:       {
 163:         return (CallbackHandler) Engine.getInstance(SERVICE, type, provider);
 164:       }
 165:     catch (InvocationTargetException x)
 166:       {
 167:         cause = x.getCause();
 168:         if (cause instanceof NoSuchAlgorithmException)
 169:           throw (NoSuchAlgorithmException) cause;
 170:         if (cause == null)
 171:           cause = x;
 172:       }
 173:     catch (ClassCastException x)
 174:       {
 175:         cause = x;
 176:       }
 177:     NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
 178:     x.initCause(cause);
 179:     throw x;
 180:   }
 181: 
 182:   public void handle(Callback[] callbacks)
 183:     throws IOException, UnsupportedCallbackException
 184:   {
 185:     if (callbacks == null)
 186:       throw new NullPointerException();
 187:     for (int i = 0; i < callbacks.length; i++)
 188:       {
 189:         if (callbacks[i] == null)
 190:           continue;
 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]);
 205:         else
 206:           handleOther(callbacks[i]);
 207:       }
 208:   }
 209: 
 210:   public final String getName ()
 211:   {
 212:     return name;
 213:   }
 214: 
 215:   // Abstract methods.
 216:   // -------------------------------------------------------------------------
 217: 
 218:   /**
 219:    * Handles a {@link ChoiceCallback}.
 220:    *
 221:    * @param callback The choice callback.
 222:    * @throws IOException If an I/O error occurs.
 223:    */
 224:   protected abstract void handleChoice(ChoiceCallback callback)
 225:     throws IOException;
 226: 
 227:   /**
 228:    * Handles a {@link ConfirmationCallback}.
 229:    *
 230:    * @param callback The confirmation callback.
 231:    * @throws IOException If an I/O error occurs.
 232:    */
 233:   protected abstract void handleConfirmation(ConfirmationCallback callback)
 234:     throws IOException;
 235: 
 236:   /**
 237:    * Handles a {@link LanguageCallback}.
 238:    *
 239:    * @param callback The language callback.
 240:    * @throws IOException If an I/O error occurs.
 241:    */
 242:   protected abstract void handleLanguage(LanguageCallback callback)
 243:     throws IOException;
 244: 
 245:   /**
 246:    * Handles a {@link NameCallback}.
 247:    *
 248:    * @param callback The name callback.
 249:    * @throws IOException If an I/O error occurs.
 250:    */
 251:   protected abstract void handleName(NameCallback callback)
 252:     throws IOException;
 253: 
 254:   /**
 255:    * Handles a {@link PasswordCallback}.
 256:    *
 257:    * @param callback The password callback.
 258:    * @throws IOException If an I/O error occurs.
 259:    */
 260:   protected abstract void handlePassword(PasswordCallback callback)
 261:     throws IOException;
 262: 
 263:   /**
 264:    * Handles a {@link TextInputCallback}.
 265:    *
 266:    * @param callback The text input callback.
 267:    * @throws IOException If an I/O error occurs.
 268:    */
 269:   protected abstract void handleTextInput(TextInputCallback callback)
 270:     throws IOException;
 271: 
 272:   /**
 273:    * Handles a {@link TextOutputCallback}.
 274:    *
 275:    * @param callback The text output callback.
 276:    * @throws IOException If an I/O error occurs.
 277:    */
 278:   protected abstract void handleTextOutput(TextOutputCallback callback)
 279:     throws IOException;
 280: 
 281:   /**
 282:    * Handles an unknown callback. The default implementation simply throws
 283:    * an {@link UnsupportedCallbackException}.
 284:    *
 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
 288:    *   supported.
 289:    */
 290:   protected void handleOther(Callback callback)
 291:     throws IOException, UnsupportedCallbackException
 292:   {
 293:     throw new UnsupportedCallbackException(callback);
 294:   }
 295: }