View Javadoc

1   package no.feide.mellon.jaas.callbackhandlers;
2   
3   
4   import java.io.*;
5   
6   import javax.security.auth.callback.*;
7   
8   /***
9    * ConsoleCallbackHandler prompts and reads from the 
10   * command line for username and password.
11   *
12   * @author Rikke Amilde Løvlid
13   * 
14   */
15  
16  public class CommandlineCallbackHandler implements CallbackHandler {
17   
18      public CommandlineCallbackHandler() {
19      }
20  
21      /***
22       * Handles the specified set of callbacks.
23       * This class supports NameCallback and PasswordCallback.
24       *
25       * @param   callbacks the callbacks to handle
26       * @throws  IOException if an input or output error occurs.
27       * @throws  UnsupportedCallbackException if the callback is not an
28       * 			instance of NameCallback or PasswordCallback
29       */
30      public void handle(Callback[] callbacks) throws java.io.IOException, UnsupportedCallbackException {
31  
32          for (int i = 0; i < callbacks.length; i++) {
33  
34              if (callbacks[i] instanceof NameCallback) {
35              	NameCallback nc = (NameCallback)callbacks[i];
36              	
37                  System.out.print("\t\t" +nc.getPrompt());
38                  String name=(new BufferedReader(new InputStreamReader(System.in))).readLine();
39                  nc.setName(name);
40              } 
41              else if (callbacks[i] instanceof PasswordCallback) {
42              	PasswordCallback pc = (PasswordCallback)callbacks[i];
43              	
44                  System.out.print("\t\t" + pc.getPrompt());
45                  String password=(new BufferedReader(new InputStreamReader(System.in))).readLine();
46                  pc.setPassword(password.toCharArray());
47              }   
48              else {
49                  throw(new UnsupportedCallbackException(callbacks[i], "Callback class not supported"));
50              }
51          }
52      }
53  }