1 7 8 package com.sun.security.auth.callback; 9 10 11 import javax.security.auth.callback.Callback ; 12 import javax.security.auth.callback.CallbackHandler ; 13 import javax.security.auth.callback.ConfirmationCallback ; 14 import javax.security.auth.callback.NameCallback ; 15 import javax.security.auth.callback.PasswordCallback ; 16 import javax.security.auth.callback.TextOutputCallback ; 17 import javax.security.auth.callback.UnsupportedCallbackException ; 18 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.PushbackInputStream ; 25 import java.util.Arrays ; 26 27 import sun.security.util.Password; 28 29 37 38 public class TextCallbackHandler implements CallbackHandler { 39 40 47 public TextCallbackHandler() { } 48 49 57 public void handle(Callback [] callbacks) 58 throws IOException , UnsupportedCallbackException 59 { 60 ConfirmationCallback confirmation = null; 61 62 for (int i = 0; i < callbacks.length; i++) { 63 if (callbacks[i] instanceof TextOutputCallback ) { 64 TextOutputCallback tc = (TextOutputCallback ) callbacks[i]; 65 66 String text; 67 switch (tc.getMessageType()) { 68 case TextOutputCallback.INFORMATION: 69 text = ""; 70 break; 71 case TextOutputCallback.WARNING: 72 text = "Warning: "; 73 break; 74 case TextOutputCallback.ERROR: 75 text = "Error: "; 76 break; 77 default: 78 throw new UnsupportedCallbackException ( 79 callbacks[i], "Unrecognized message type"); 80 } 81 82 String message = tc.getMessage(); 83 if (message != null) { 84 text += message; 85 } 86 if (text != null) { 87 System.err.println(text); 88 } 89 90 } else if (callbacks[i] instanceof NameCallback ) { 91 NameCallback nc = (NameCallback ) callbacks[i]; 92 93 if (nc.getDefaultName() == null) { 94 System.err.print(nc.getPrompt()); 95 } else { 96 System.err.print(nc.getPrompt() + 97 " [" + nc.getDefaultName() + "] "); 98 } 99 System.err.flush(); 100 101 String result = readLine(); 102 if (result.equals("")) { 103 result = nc.getDefaultName(); 104 } 105 106 nc.setName(result); 107 108 } else if (callbacks[i] instanceof PasswordCallback ) { 109 PasswordCallback pc = (PasswordCallback ) callbacks[i]; 110 111 System.err.print(pc.getPrompt()); 112 System.err.flush(); 113 114 pc.setPassword(Password.readPassword(System.in)); 115 116 } else if (callbacks[i] instanceof ConfirmationCallback ) { 117 confirmation = (ConfirmationCallback ) callbacks[i]; 118 119 } else { 120 throw new UnsupportedCallbackException ( 121 callbacks[i], "Unrecognized Callback"); 122 } 123 } 124 125 126 if (confirmation != null) { 127 doConfirmation(confirmation); 128 } 129 } 130 131 132 private String readLine() throws IOException { 133 return new BufferedReader 134 (new InputStreamReader (System.in)).readLine(); 135 } 136 137 private void doConfirmation(ConfirmationCallback confirmation) 138 throws IOException , UnsupportedCallbackException 139 { 140 String prefix; 141 int messageType = confirmation.getMessageType(); 142 switch (messageType) { 143 case ConfirmationCallback.WARNING: 144 prefix = "Warning: "; 145 break; 146 case ConfirmationCallback.ERROR: 147 prefix = "Error: "; 148 break; 149 case ConfirmationCallback.INFORMATION: 150 prefix = ""; 151 break; 152 default: 153 throw new UnsupportedCallbackException ( 154 confirmation, "Unrecognized message type: " + messageType); 155 } 156 157 class OptionInfo { 158 String name; 159 int value; 160 OptionInfo(String name, int value) { 161 this.name = name; 162 this.value = value; 163 } 164 } 165 166 OptionInfo[] options; 167 int optionType = confirmation.getOptionType(); 168 switch (optionType) { 169 case ConfirmationCallback.YES_NO_OPTION: 170 options = new OptionInfo[] { 171 new OptionInfo("Yes", ConfirmationCallback.YES), 172 new OptionInfo("No", ConfirmationCallback.NO) 173 }; 174 break; 175 case ConfirmationCallback.YES_NO_CANCEL_OPTION: 176 options = new OptionInfo[] { 177 new OptionInfo("Yes", ConfirmationCallback.YES), 178 new OptionInfo("No", ConfirmationCallback.NO), 179 new OptionInfo("Cancel", ConfirmationCallback.CANCEL) 180 }; 181 break; 182 case ConfirmationCallback.OK_CANCEL_OPTION: 183 options = new OptionInfo[] { 184 new OptionInfo("OK", ConfirmationCallback.OK), 185 new OptionInfo("Cancel", ConfirmationCallback.CANCEL) 186 }; 187 break; 188 case ConfirmationCallback.UNSPECIFIED_OPTION: 189 String [] optionStrings = confirmation.getOptions(); 190 options = new OptionInfo[optionStrings.length]; 191 for (int i = 0; i < options.length; i++) { 192 options[i].value = i; 193 } 194 break; 195 default: 196 throw new UnsupportedCallbackException ( 197 confirmation, "Unrecognized option type: " + optionType); 198 } 199 200 int defaultOption = confirmation.getDefaultOption(); 201 202 String prompt = confirmation.getPrompt(); 203 if (prompt == null) { 204 prompt = ""; 205 } 206 prompt = prefix + prompt; 207 if (!prompt.equals("")) { 208 System.err.println(prompt); 209 } 210 211 for (int i = 0; i < options.length; i++) { 212 if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) { 213 System.err.println( 215 i + ". " + options[i].name + 216 (i == defaultOption ? " [default]" : "")); 217 } else { 218 System.err.println( 220 i + ". " + options[i].name + 221 (options[i].value == defaultOption ? " [default]" : "")); 222 } 223 } 224 System.err.print("Enter a number: "); 225 System.err.flush(); 226 int result; 227 try { 228 result = Integer.parseInt(readLine()); 229 if (result < 0 || result > (options.length - 1)) { 230 result = defaultOption; 231 } 232 result = options[result].value; 233 } catch (NumberFormatException e) { 234 result = defaultOption; 235 } 236 237 confirmation.setSelectedIndex(result); 238 } 239 } 240 | Popular Tags |