1 7 package org.jboss.security.auth.callback; 8 9 import java.io.BufferedReader ; 10 import java.io.InputStreamReader ; 11 import java.io.IOException ; 12 import javax.security.auth.callback.Callback ; 13 import javax.security.auth.callback.CallbackHandler ; 14 import javax.security.auth.callback.NameCallback ; 15 import javax.security.auth.callback.PasswordCallback ; 16 import javax.security.auth.callback.UnsupportedCallbackException ; 17 18 27 public class ConsoleInputHandler implements CallbackHandler 28 { 29 35 public void handle(Callback [] callbacks) throws 36 UnsupportedCallbackException 37 { 38 for (int i = 0; i < callbacks.length; i++) 39 { 40 Callback c = callbacks[i]; 41 if (c instanceof NameCallback ) 42 { 43 NameCallback nc = (NameCallback ) c; 44 String prompt = nc.getPrompt(); 45 if( prompt == null ) 46 prompt = "Enter Username: "; 47 System.out.print(prompt); 49 InputStreamReader isr = new InputStreamReader (System.in); 50 BufferedReader br = new BufferedReader (isr); 51 try 52 { 53 String username = br.readLine(); 54 nc.setName(username); 55 } 56 catch(IOException e) 57 { 58 throw new SecurityException ("Failed to obtain username, ioe="+e.getMessage()); 59 } 60 } 61 else if (c instanceof PasswordCallback ) 62 { 63 PasswordCallback pc = (PasswordCallback ) c; 64 String prompt = pc.getPrompt(); 65 if( prompt == null ) 66 prompt = "Enter Password: "; 67 System.out.print(prompt); 69 InputStreamReader isr = new InputStreamReader (System.in); 70 BufferedReader br = new BufferedReader (isr); 71 try 72 { 73 String password = br.readLine(); 74 pc.setPassword(password.toCharArray()); 75 } 76 catch(IOException e) 77 { 78 throw new SecurityException ("Failed to obtain password, ioe="+e.getMessage()); 79 } 80 } 81 else 82 { 83 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 84 } 85 } 86 } 87 } 88 | Popular Tags |