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

   1: /* ConsoleCallbackHandler.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 java.io.BufferedReader;
  42: import java.io.InputStreamReader;
  43: import java.io.IOException;
  44: import java.io.PrintStream;
  45: 
  46: import java.util.Iterator;
  47: import java.util.Locale;
  48: import java.util.StringTokenizer;
  49: import java.util.TreeSet;
  50: 
  51: import javax.security.auth.callback.ChoiceCallback;
  52: import javax.security.auth.callback.ConfirmationCallback;
  53: import javax.security.auth.callback.LanguageCallback;
  54: import javax.security.auth.callback.NameCallback;
  55: import javax.security.auth.callback.PasswordCallback;
  56: import javax.security.auth.callback.TextInputCallback;
  57: import javax.security.auth.callback.TextOutputCallback;
  58: 
  59: /**
  60:  * An implementation of {@link CallbackHandler} that reads and writes
  61:  * information to and from <code>System.in</code> and <code>System.out</code>.
  62:  */
  63: public class ConsoleCallbackHandler extends AbstractCallbackHandler
  64: {
  65: 
  66:   // Fields.
  67:   // -------------------------------------------------------------------------
  68: 
  69:   private final PrintStream out;
  70: 
  71:   // Constructors.
  72:   // -------------------------------------------------------------------------
  73: 
  74:   public ConsoleCallbackHandler()
  75:   {
  76:     this (System.out);
  77:   }
  78: 
  79:   public ConsoleCallbackHandler (final PrintStream out)
  80:   {
  81:     super ("CONSOLE");
  82:     this.out = out;
  83:   }
  84: 
  85:   // Instance methods.
  86:   // -------------------------------------------------------------------------
  87: 
  88:   protected void handleChoice(ChoiceCallback c) throws IOException
  89:   {
  90:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  91:     out.println(c.getPrompt());
  92:     out.print('(');
  93:     String[] choices = c.getChoices();
  94:     for (int i = 0; i < choices.length; i++)
  95:       {
  96:         out.print(choices[i]);
  97:         if (i != choices.length - 1)
  98:           out.print(", ");
  99:       }
 100:     out.print(") ");
 101:     if (c.getDefaultChoice() >= 0 && c.getDefaultChoice() < choices.length)
 102:       {
 103:         out.print('[');
 104:         out.print(choices[c.getDefaultChoice()]);
 105:         out.print("] ");
 106:       }
 107:     String reply = in.readLine();
 108:     if (reply == null || reply.length() == 0)
 109:       {
 110:         c.setSelectedIndex(c.getDefaultChoice());
 111:         return;
 112:       }
 113:     if (!c.allowMultipleSelections())
 114:       {
 115:         for (int i = 0; i < choices.length; i++)
 116:           {
 117:             if (reply.trim().equals(choices[i]))
 118:               {
 119:                 c.setSelectedIndex(i);
 120:                 return;
 121:               }
 122:           }
 123:         c.setSelectedIndex(c.getDefaultChoice());
 124:       }
 125:     else
 126:       {
 127:         TreeSet indices = new TreeSet();
 128:         StringTokenizer tok = new StringTokenizer(reply, ",");
 129:         String[] replies = new String[tok.countTokens()];
 130:         int idx = 0;
 131:         while (tok.hasMoreTokens())
 132:           {
 133:             replies[idx++] = tok.nextToken().trim();
 134:           }
 135:         for (int i = 0; i < choices.length; i++)
 136:           for (int j = 0; j < replies.length; i++)
 137:             {
 138:               if (choices[i].equals(replies[j]))
 139:                 {
 140:                   indices.add(Integer.valueOf(i));
 141:                 }
 142:             }
 143:         if (indices.size() == 0)
 144:           c.setSelectedIndex(c.getDefaultChoice());
 145:         else
 146:           {
 147:             int[] ii = new int[indices.size()];
 148:             int i = 0;
 149:             for (Iterator it = indices.iterator(); it.hasNext(); )
 150:               ii[i++] = ((Integer) it.next()).intValue();
 151:             c.setSelectedIndexes(ii);
 152:           }
 153:       }
 154:   }
 155: 
 156:   protected void handleConfirmation(ConfirmationCallback c) throws IOException
 157:   {
 158:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 159:     if (c.getPrompt() != null)
 160:       out.print(c.getPrompt());
 161: 
 162:     String[] choices = null;
 163:     int[] values = null;
 164:     switch (c.getOptionType())
 165:       {
 166:       case ConfirmationCallback.OK_CANCEL_OPTION:
 167:         out.print(messages.getString("callback.okCancel"));
 168:         choices = new String[] {
 169:           messages.getString("callback.ok"),
 170:           messages.getString("callback.cancel"),
 171:           messages.getString("callback.shortOk"),
 172:           messages.getString("callback.shortCancel")
 173:         };
 174:         values = new int[] {
 175:           ConfirmationCallback.OK, ConfirmationCallback.CANCEL,
 176:           ConfirmationCallback.OK, ConfirmationCallback.CANCEL
 177:         };
 178:         break;
 179: 
 180:       case ConfirmationCallback.YES_NO_CANCEL_OPTION:
 181:         out.print(messages.getString("callback.yesNoCancel"));
 182:         choices = new String[] {
 183:           messages.getString("callback.yes"),
 184:           messages.getString("callback.no"),
 185:           messages.getString("callback.cancel"),
 186:           messages.getString("callback.shortYes"),
 187:           messages.getString("callback.shortNo"),
 188:           messages.getString("callback.shortCancel")
 189:         };
 190:         values = new int[] {
 191:           ConfirmationCallback.YES, ConfirmationCallback.NO,
 192:           ConfirmationCallback.CANCEL, ConfirmationCallback.YES,
 193:           ConfirmationCallback.NO, ConfirmationCallback.CANCEL
 194:         };
 195:         break;
 196: 
 197:       case ConfirmationCallback.YES_NO_OPTION:
 198:         out.print(messages.getString("callback.yesNo"));
 199:         choices = new String[] { messages.getString("callback.yes"),
 200:                                  messages.getString("callback.no"),
 201:                                  messages.getString("callback.shortYes"),
 202:                                  messages.getString("callback.shortNo") };
 203:         values = new int[] { ConfirmationCallback.YES,
 204:                              ConfirmationCallback.NO,
 205:                              ConfirmationCallback.YES,
 206:                              ConfirmationCallback.NO };
 207:         int defaultOption = c.getDefaultOption();
 208:         if (defaultOption > -1 && defaultOption < choices.length)
 209:           {
 210:             out.print("[");
 211:             out.print(choices[defaultOption]);
 212:             out.print("] ");
 213:           }
 214:         break;
 215: 
 216:       case ConfirmationCallback.UNSPECIFIED_OPTION:
 217:         choices = c.getOptions();
 218:         values = new int[choices.length];
 219:         for (int i = 0; i < values.length; i++)
 220:           values[i] = i;
 221:         out.print('(');
 222:         for (int i = 0; i < choices.length; i++)
 223:           {
 224:             out.print(choices[i]);
 225:             if (i != choices.length - 1)
 226:               out.print(", ");
 227:           }
 228:         out.print(") [");
 229:         out.print(choices[c.getDefaultOption()]);
 230:         out.print("] ");
 231:         break;
 232: 
 233:       default:
 234:         throw new IllegalArgumentException();
 235:       }
 236:     String reply = in.readLine();
 237:     if (reply == null)
 238:       {
 239:         c.setSelectedIndex(c.getDefaultOption());
 240:         return;
 241:       }
 242:     reply = reply.trim();
 243:     for (int i = 0; i < choices.length; i++)
 244:       if (reply.equalsIgnoreCase(choices[i]))
 245:         {
 246:           c.setSelectedIndex(values[i]);
 247:           return;
 248:         }
 249:     c.setSelectedIndex(c.getDefaultOption());
 250:   }
 251: 
 252:   protected void handleLanguage(LanguageCallback c) throws IOException
 253:   {
 254:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 255:     out.print(messages.getString("callback.language"));
 256:     String reply = null;
 257:     reply = in.readLine();
 258:     if (reply == null)
 259:       {
 260:         c.setLocale(Locale.getDefault());
 261:       }
 262:     else
 263:       {
 264:         c.setLocale(new Locale(reply.trim()));
 265:       }
 266:   }
 267: 
 268:   protected void handleName(NameCallback c) throws IOException
 269:   {
 270:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 271:     out.print(c.getPrompt());
 272:     String name = in.readLine();
 273:     if (name != null)
 274:       c.setName(name.trim());
 275:   }
 276: 
 277:   protected void handlePassword(PasswordCallback c) throws IOException
 278:   {
 279:     out.print(c.getPrompt());
 280:     BufferedReader in =
 281:       new BufferedReader(new InputStreamReader(System.in));
 282:     String pass = in.readLine();
 283:     c.setPassword(pass.toCharArray());
 284:   }
 285: 
 286:   protected void handleTextInput(TextInputCallback c) throws IOException
 287:   {
 288:     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 289:     out.print(c.getPrompt());
 290:     String text = in.readLine();
 291:     if (text != null)
 292:       c.setText(text);
 293:   }
 294: 
 295:   protected void handleTextOutput(TextOutputCallback c)
 296:   {
 297:     out.print(c.getMessage());
 298:   }
 299: }