1 11 package org.eclipse.jsch.ui; 12 13 import org.eclipse.core.runtime.OperationCanceledException; 14 import org.eclipse.jface.dialogs.IDialogConstants; 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jsch.core.IJSchService; 17 import org.eclipse.jsch.internal.ui.*; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Shell; 20 21 import com.jcraft.jsch.*; 22 23 34 public class UserInfoPrompter implements UserInfo, UIKeyboardInteractive{ 35 36 private String passphrase; 37 private String password; 38 private final Session session; 39 private int attemptCount; 40 41 48 public UserInfoPrompter(Session session){ 49 super(); 50 this.session=session; 51 session.setUserInfo(this); 52 } 53 54 59 public Session getSession(){ 60 return session; 61 } 62 63 68 public String getPassphrase(){ 69 return passphrase; 70 } 71 72 77 public String getPassword(){ 78 return password; 79 } 80 81 89 public void setPassphrase(String passphrase){ 90 this.passphrase=passphrase; 91 } 92 93 101 public void setPassword(String password){ 102 this.password=password; 103 } 104 105 110 public boolean promptPassphrase(String message){ 111 String _passphrase=promptSecret(message); 112 if(_passphrase!=null){ 113 setPassphrase(_passphrase); 114 } 115 return _passphrase!=null; 116 } 117 118 123 public boolean promptPassword(String message){ 124 String _password=promptSecret(message); 125 if(_password!=null){ 126 setPassword(_password); 127 } 128 return _password!=null; 129 } 130 131 private String promptSecret(final String message){ 132 final String [] result=new String [1]; 134 Display display=Display.getCurrent(); 135 if(display!=null){ 136 result[0]=promptForPassword(message); 137 } 138 else{ 139 Display.getDefault().syncExec(new Runnable (){ 141 public void run(){ 142 result[0]=promptForPassword(message); 143 } 144 }); 145 } 146 147 if(result[0]==null){ 148 throw new OperationCanceledException(); 149 } 150 return result[0]; 151 } 152 153 String promptForPassword(final String message){ 154 String username=getSession().getUserName(); 155 UserValidationDialog dialog=new UserValidationDialog(null, null, 156 (username==null) ? "" : username, message); dialog.setUsernameMutable(false); 158 dialog.open(); 159 return dialog.getPassword(); 160 } 161 162 168 public String [] promptKeyboardInteractive(String destination, String name, 169 String instruction, String [] prompt, boolean[] echo){ 170 if(prompt.length==0){ 171 return new String [0]; 173 } 174 try{ 175 if(attemptCount==0&&password!=null&&prompt.length==1 176 &&prompt[0].trim().equalsIgnoreCase("password:")){ attemptCount++; 180 return new String [] {password}; 181 } 182 String [] result=iromptForKeyboradInteractiveInUI(destination, name, 183 instruction, prompt, echo); 184 if(result==null) 185 return null; if(result.length==1&&prompt.length==1 187 &&prompt[0].trim().equalsIgnoreCase("password:")){ password=result[0]; 189 } 190 attemptCount++; 191 return result; 192 } 193 catch(OperationCanceledException e){ 194 return null; 195 } 196 } 197 198 private String [] iromptForKeyboradInteractiveInUI(final String destination, 199 final String name, final String instruction, final String [] prompt, 200 final boolean[] echo){ 201 final String [][] result=new String [1][]; 202 Display display=Display.getCurrent(); 203 if(display!=null){ 204 result[0]=internalPromptForUserInteractive(destination, name, 205 instruction, prompt, echo); 206 } 207 else{ 208 Display.getDefault().syncExec(new Runnable (){ 210 public void run(){ 211 result[0]=internalPromptForUserInteractive(destination, name, 212 instruction, prompt, echo); 213 } 214 }); 215 } 216 return result[0]; 217 } 218 219 String [] internalPromptForUserInteractive(String destination, 220 String name, String instruction, String [] prompt, boolean[] echo){ 221 String domain=null; 222 String userName=getSession().getUserName(); 223 KeyboardInteractiveDialog dialog=new KeyboardInteractiveDialog(null, 224 domain, destination, name, userName, instruction, prompt, echo); 225 dialog.open(); 226 String [] _result=dialog.getResult(); 227 return _result; 228 } 229 230 235 public boolean promptYesNo(String question){ 236 int prompt=prompt(MessageDialog.QUESTION, Messages.UserInfoPrompter_0, 237 question, new int[] {IDialogConstants.YES_ID, IDialogConstants.NO_ID}, 238 0 ); 242 return prompt==0; 243 } 244 245 250 public void showMessage(String message){ 251 prompt(MessageDialog.INFORMATION, Messages.UserInfoPrompter_1, message, 252 new int[] {IDialogConstants.OK_ID}, 0); 253 } 254 255 private int prompt(final int promptType, final String title, 256 final String message, final int[] promptResponses, 257 final int defaultResponse){ 258 final Display display=getStandardDisplay(); 259 final int[] retval=new int[1]; 260 final String [] buttons=new String [promptResponses.length]; 261 for(int i=0; i<promptResponses.length; i++){ 262 int prompt=promptResponses[i]; 263 switch(prompt){ 264 case IDialogConstants.OK_ID: 265 buttons[i]=IDialogConstants.OK_LABEL; 266 break; 267 case IDialogConstants.CANCEL_ID: 268 buttons[i]=IDialogConstants.CANCEL_LABEL; 269 break; 270 case IDialogConstants.NO_ID: 271 buttons[i]=IDialogConstants.NO_LABEL; 272 break; 273 case IDialogConstants.YES_ID: 274 buttons[i]=IDialogConstants.YES_LABEL; 275 break; 276 } 277 } 278 279 display.syncExec(new Runnable (){ 280 public void run(){ 281 final MessageDialog dialog=new MessageDialog(new Shell(display), title, 282 null , message, promptType, buttons, 283 defaultResponse); 284 retval[0]=dialog.open(); 285 } 286 }); 287 return retval[0]; 288 } 289 290 private Display getStandardDisplay(){ 291 Display display=Display.getCurrent(); 292 if(display==null){ 293 display=Display.getDefault(); 294 } 295 return display; 296 } 297 298 } 299 | Popular Tags |