1 19 20 package com.sslexplorer.agent.client.gui.awt; 21 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.Insets ; 25 import java.awt.Label ; 26 import java.awt.Panel ; 27 import java.awt.TextField ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 31 import com.maverick.http.BasicAuthentication; 32 import com.maverick.http.HttpAuthenticator; 33 import com.maverick.http.NTLMAuthentication; 34 import com.maverick.http.PasswordCredentials; 35 import com.sshtools.ui.awt.UIUtil; 36 import com.sshtools.ui.awt.options.OptionDialog; 37 38 44 public class AWTAuthenticationDialog { 45 46 52 public static boolean promptForCredentials(boolean proxy, HttpAuthenticator authenticator) { 53 Panel p = new Panel (new GridBagLayout ()); 54 GridBagConstraints gbc = new GridBagConstraints (); 55 gbc.anchor = GridBagConstraints.WEST; 56 gbc.fill = GridBagConstraints.HORIZONTAL; 57 gbc.insets = new Insets (2, 2, 2, 2); 58 59 if (authenticator instanceof BasicAuthentication) { 61 gbc.weightx = 0.0; 62 UIUtil.gridBagAdd(p, new Label (Messages.getString("AuthenticationDialog.realm")), gbc, GridBagConstraints.RELATIVE); gbc.weightx = 1.0; 64 UIUtil.gridBagAdd(p, new Label (((BasicAuthentication) authenticator).getRealm()), gbc, GridBagConstraints.REMAINDER); 65 } 66 67 TextField domain = null; 69 if (authenticator instanceof NTLMAuthentication) { 70 gbc.weightx = 0.0; 71 UIUtil.gridBagAdd(p, new Label (Messages.getString("AuthenticationDialog.domain")), gbc, GridBagConstraints.RELATIVE); domain = new TextField ("", 15); gbc.weightx = 1.0; 74 UIUtil.gridBagAdd(p, domain, gbc, GridBagConstraints.REMAINDER); 75 } 76 77 gbc.weightx = 0.0; 79 UIUtil.gridBagAdd(p, new Label (Messages.getString("AuthenticationDialog.username")), gbc, GridBagConstraints.RELATIVE); final TextField username = new TextField ("", 15); gbc.weightx = 1.0; 82 UIUtil.gridBagAdd(p, username, gbc, GridBagConstraints.REMAINDER); 83 username.requestFocus(); 84 85 gbc.weightx = 0.0; 87 UIUtil.gridBagAdd(p, new Label (Messages.getString("AuthenticationDialog.password")), gbc, GridBagConstraints.RELATIVE); final TextField password = new TextField ("", 15); gbc.weightx = 1.0; 90 UIUtil.gridBagAdd(p, password, gbc, GridBagConstraints.REMAINDER); 91 password.requestFocus(); 92 password.setEchoChar('*'); 93 94 final OptionDialog dialog = new OptionDialog(OptionDialog.QUESTION, p, OptionDialog.CHOICES_OK_CANCEL, null); 95 username.addActionListener(new ActionListener () { 96 public void actionPerformed(ActionEvent evt) { 97 dialog.choice(OptionDialog.CHOICE_OK); 98 } 99 }); 100 password.addActionListener(new ActionListener () { 101 public void actionPerformed(ActionEvent evt) { 102 dialog.choice(OptionDialog.CHOICE_OK); 103 } 104 }); 105 if (domain != null) { 106 domain.addActionListener(new ActionListener () { 107 public void actionPerformed(ActionEvent evt) { 108 dialog.choice(OptionDialog.CHOICE_OK); 109 } 110 }); 111 } 112 113 if (dialog.dialogPrompt(null, Messages.getString("AuthenticationDialog.title")) == OptionDialog.CHOICE_OK) { authenticator.setCredentials(new PasswordCredentials(username.getText().trim(), password.getText())); 115 if (authenticator instanceof NTLMAuthentication && !domain.getText().trim().equals("")) { ((NTLMAuthentication) authenticator).setDomain(domain.getText().trim()); 117 } 118 return true; 119 } 120 121 return false; 122 } 123 124 } 125 | Popular Tags |