1 package org.jacorb.demo.sas; 2 3 import javax.security.auth.callback.Callback ; 4 import javax.security.auth.callback.CallbackHandler ; 5 import javax.security.auth.callback.NameCallback ; 6 import javax.security.auth.callback.PasswordCallback ; 7 import javax.security.auth.callback.TextOutputCallback ; 8 import javax.security.auth.callback.UnsupportedCallbackException ; 9 10 public class JaasTxtCalbackHandler implements CallbackHandler { 11 12 private String myUsername = ""; 13 private char[] myPassword = null; 14 15 22 public JaasTxtCalbackHandler() { 23 } 24 25 public void setMyUsername(String username) { 26 myUsername = username; 27 } 28 29 public void setMyPassword(char[] password) { 30 myPassword = password; 31 } 32 33 41 public void handle(Callback [] callbacks) throws UnsupportedCallbackException { 42 43 for (int i = 0; i < callbacks.length; i++) { 44 if (callbacks[i] instanceof TextOutputCallback ) { 45 TextOutputCallback tc = (TextOutputCallback ) callbacks[i]; 46 47 String text; 48 switch (tc.getMessageType()) { 49 case TextOutputCallback.INFORMATION : 50 text = ""; 51 break; 52 case TextOutputCallback.WARNING : 53 text = "Warning: "; 54 break; 55 case TextOutputCallback.ERROR : 56 text = "Error: "; 57 break; 58 default : 59 throw new UnsupportedCallbackException ( 60 callbacks[i], 61 "Unrecognized message type"); 62 } 63 64 String message = tc.getMessage(); 65 if (message != null) { 66 text += message; 67 } 68 if (text != null) { 69 System.err.println(text); 70 } 71 72 } else if (callbacks[i] instanceof NameCallback ) { 73 NameCallback nc = (NameCallback ) callbacks[i]; 74 nc.setName(myUsername); 75 76 } else if (callbacks[i] instanceof PasswordCallback ) { 77 PasswordCallback pc = (PasswordCallback ) callbacks[i]; 78 pc.setPassword(myPassword); 79 80 } else { 81 throw new UnsupportedCallbackException (callbacks[i], "Unrecognized Callback"); 82 } 83 } 84 } 85 86 } 87 | Popular Tags |