RemoteUserInfo.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.remote.tunnel;
00002 
00003 import org.eclipse.jface.dialogs.Dialog;
00004 import org.eclipse.jface.dialogs.MessageDialog;
00005 import org.eclipse.jface.dialogs.TitleAreaDialog;
00006 import org.eclipse.jface.layout.GridDataFactory;
00007 import org.eclipse.jface.layout.GridLayoutFactory;
00008 import org.eclipse.swt.SWT;
00009 import org.eclipse.swt.widgets.Composite;
00010 import org.eclipse.swt.widgets.Control;
00011 import org.eclipse.swt.widgets.Label;
00012 import org.eclipse.swt.widgets.Shell;
00013 import org.eclipse.swt.widgets.Text;
00014 
00015 import com.jcraft.jsch.UIKeyboardInteractive;
00016 import com.jcraft.jsch.UserInfo;
00017 
00018 import edu.rice.cs.hpc.remote.ui.PasswordDialog;
00019 
00020 /********************************************
00021  * 
00022  * Remote user information to store and ask for password
00023  * This class is needed for SSH tunneling
00024  *
00025  ********************************************/
00026 public class RemoteUserInfo 
00027 implements UserInfo, UIKeyboardInteractive
00028 {
00029     final private Shell shell;
00030 
00031     private String password;
00032     private String user, hostname;
00033     private int port;
00034     
00035     public RemoteUserInfo(Shell shell)
00036     {           
00037         this.shell = shell;
00038     }
00039     
00040     
00041     public void setInfo(String user, String hostname, int port)
00042     {
00043         this.user     = user;
00044         this.hostname = hostname;
00045         this.port     = port;
00046     }
00047     
00048     // --------------------------------------------------------------------------------------
00049     // override methods
00050     // --------------------------------------------------------------------------------------
00051     
00052     @Override
00053     public boolean promptPassword(String message) {
00054         PasswordDialog dialog = new PasswordDialog(shell, "Input password for " + hostname + ":" + port,
00055                 "password for user " + user, null, null);
00056         
00057         boolean ret =  dialog.open() == Dialog.OK;
00058         
00059         if (ret)
00060             password = dialog.getValue();
00061         
00062         return ret;
00063     }
00064 
00065     @Override
00066     public String getPassword() {
00067         return password;
00068     }
00069 
00070     @Override
00071     public boolean promptPassphrase(String message) {
00072         return false;
00073     }
00074 
00075     @Override
00076     public String getPassphrase() {
00077         return null;
00078     }
00079 
00080     @Override
00081     public boolean promptYesNo(String message) {
00082         return true;
00083     }
00084 
00085     @Override
00086     public void showMessage(String message) {
00087         MessageDialog.openInformation(shell, "Information", message);
00088     }
00089 
00090 
00091     @Override
00092     public String[] promptKeyboardInteractive(String destination, String name,
00093             String instruction, String[] prompt, boolean[] echo) 
00094     {
00095         KeyboardInteractiveDialog dlg = new KeyboardInteractiveDialog(shell, destination, name, instruction, prompt);
00096         if (dlg.open() == Dialog.OK)
00097         {
00098             return dlg.inputs;
00099         }
00100         return null;
00101     }           
00102 
00103     /********************************************************************
00104      * 
00105      * dialog class for communicating with keyboard interactive style SSH
00106      * 
00107      * Some SSH servers in national labs require SSH client to support 
00108      * keyboard interactive SSH, it doesn't work with traditional SSH client.
00109      *
00110      ********************************************************************/
00111     static private class KeyboardInteractiveDialog extends TitleAreaDialog
00112     {
00113         private final String destination, name, instruction;
00114         private final String []prompts;
00115         private Text []answers;
00116         
00117         /*** output variable based on user's input. 
00118          *   The caller needs to retrieve this variable once the user clicks OK button
00119          *   **/
00120         String []inputs;
00121         
00122         public KeyboardInteractiveDialog(Shell parentShell, String destination, String name,
00123                 String instruction, String[] prompt) {
00124             super(parentShell);
00125             this.destination = destination;
00126             this.name        = name;
00127             this.instruction = instruction;
00128             this.prompts     = prompt;
00129         }
00130         
00131         @Override
00132         /*
00133          * (non-Javadoc)
00134          * @see org.eclipse.jface.dialogs.Dialog#create()
00135          */
00136         public void create()
00137         {
00138             super.create();
00139             setTitle(destination + ":" + name);
00140             setMessage(instruction);
00141         }
00142         
00143         @Override
00144         /*
00145          * (non-Javadoc)
00146          * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
00147          */
00148         protected Control createDialogArea(Composite parent)
00149         {
00150             Composite area = (Composite) super.createDialogArea(parent);
00151             Composite container = new Composite(area, SWT.NONE);
00152             GridDataFactory.fillDefaults().grab(true, true).applyTo(container);
00153             GridLayoutFactory.fillDefaults().numColumns(1).applyTo(container);
00154             
00155             answers = new Text[prompts.length];
00156             
00157             for(int i=0; i<prompts.length; i++) 
00158             {
00159                 Label lbl = new Label(container, SWT.WRAP | SWT.LEFT);
00160                 lbl.setText(prompts[i]);
00161                 
00162                 answers[i] = new Text(container, SWT.PASSWORD);
00163                 answers[i].setText("");
00164                 GridDataFactory.fillDefaults().grab(true, false).applyTo(answers[i]);
00165             }
00166             return area;
00167         }
00168         
00169         @Override
00170         /*
00171          * (non-Javadoc)
00172          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
00173          */
00174         protected void okPressed()
00175         {
00176             inputs = new String[answers.length];
00177             
00178             for(int i=0; i<answers.length; i++)
00179             {
00180                 inputs[i] = answers[i].getText();
00181             }
00182             super.okPressed();
00183         }
00184     }
00185 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1