1 /*
2  * @(#) $Id: DefaultCallbackHandler.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.uam;
11
12import java.io.BufferedReader;
13import java.io.InputStreamReader;
14import java.io.IOException;
15import javax.security.auth.callback.Callback;
16import javax.security.auth.callback.CallbackHandler;
17import javax.security.auth.callback.NameCallback;
18import javax.security.auth.callback.PasswordCallback;
19
20public class DefaultCallbackHandler implements CallbackHandler {
21    public void handle(Callback[] cb) {
22        try {
23            for (int i = 0; i < cb.length; i++){
24                if (cb[i] instanceof NameCallback){
25                    NameCallback nc = (NameCallback)cb[i];
26                    System.out.print(nc.getPrompt() + " ");
27                    System.out.flush();
28                    String name = new BufferedReader(new InputStreamReader(System.in)).readLine();
29                    nc.setName(name);
30                } else if (cb[i] instanceof PasswordCallback){
31                    PasswordCallback pc = (PasswordCallback)cb[i];
32                    System.out.print(pc.getPrompt() + " ");
33                    System.out.flush();
34                    String pw = new BufferedReader(new InputStreamReader(System.in)).readLine();
35                    pc.setPassword(pw.toCharArray());
36                    pw = null;
37                }
38            }
39        } catch (IOException ioe){
40            System.out.println("ioe = " + ioe);
41        }
42    }
43}