KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > awt > AWTAuthenticationDialog


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.gui.awt;
21
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.Label JavaDoc;
26 import java.awt.Panel JavaDoc;
27 import java.awt.TextField JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
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 /**
39  * Dialog that may be used to ask for username, password and optionally domain
40  * (NTML) when proxy authentication is required.
41  *
42  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
43  */

44 public class AWTAuthenticationDialog {
45
46     /**
47      * Prompt for credentials.
48      *
49      * @param authenticator authenticator
50      * @return <code>falsE</code> if cancelled.
51      */

52     public static boolean promptForCredentials(boolean proxy, HttpAuthenticator authenticator) {
53         Panel JavaDoc p = new Panel JavaDoc(new GridBagLayout JavaDoc());
54         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
55         gbc.anchor = GridBagConstraints.WEST;
56         gbc.fill = GridBagConstraints.HORIZONTAL;
57         gbc.insets = new Insets JavaDoc(2, 2, 2, 2);
58
59         // Realm
60
if (authenticator instanceof BasicAuthentication) {
61             gbc.weightx = 0.0;
62             UIUtil.gridBagAdd(p, new Label JavaDoc(Messages.getString("AuthenticationDialog.realm")), gbc, GridBagConstraints.RELATIVE); //$NON-NLS-1$
63
gbc.weightx = 1.0;
64             UIUtil.gridBagAdd(p, new Label JavaDoc(((BasicAuthentication) authenticator).getRealm()), gbc, GridBagConstraints.REMAINDER);
65         }
66
67         // Realm
68
TextField JavaDoc domain = null;
69         if (authenticator instanceof NTLMAuthentication) {
70             gbc.weightx = 0.0;
71             UIUtil.gridBagAdd(p, new Label JavaDoc(Messages.getString("AuthenticationDialog.domain")), gbc, GridBagConstraints.RELATIVE); //$NON-NLS-1$
72
domain = new TextField JavaDoc("", 15); //$NON-NLS-1$
73
gbc.weightx = 1.0;
74             UIUtil.gridBagAdd(p, domain, gbc, GridBagConstraints.REMAINDER);
75         }
76
77         // Username
78
gbc.weightx = 0.0;
79         UIUtil.gridBagAdd(p, new Label JavaDoc(Messages.getString("AuthenticationDialog.username")), gbc, GridBagConstraints.RELATIVE); //$NON-NLS-1$
80
final TextField JavaDoc username = new TextField JavaDoc("", 15); //$NON-NLS-1$
81
gbc.weightx = 1.0;
82         UIUtil.gridBagAdd(p, username, gbc, GridBagConstraints.REMAINDER);
83         username.requestFocus();
84
85         // Password
86
gbc.weightx = 0.0;
87         UIUtil.gridBagAdd(p, new Label JavaDoc(Messages.getString("AuthenticationDialog.password")), gbc, GridBagConstraints.RELATIVE); //$NON-NLS-1$
88
final TextField JavaDoc password = new TextField JavaDoc("", 15); //$NON-NLS-1$
89
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 JavaDoc() {
96             public void actionPerformed(ActionEvent JavaDoc evt) {
97                 dialog.choice(OptionDialog.CHOICE_OK);
98             }
99         });
100         password.addActionListener(new ActionListener JavaDoc() {
101             public void actionPerformed(ActionEvent JavaDoc evt) {
102                 dialog.choice(OptionDialog.CHOICE_OK);
103             }
104         });
105         if (domain != null) {
106             domain.addActionListener(new ActionListener JavaDoc() {
107                 public void actionPerformed(ActionEvent JavaDoc evt) {
108                     dialog.choice(OptionDialog.CHOICE_OK);
109                 }
110             });
111         }
112
113         if (dialog.dialogPrompt(null, Messages.getString("AuthenticationDialog.title")) == OptionDialog.CHOICE_OK) { //$NON-NLS-1$
114
authenticator.setCredentials(new PasswordCredentials(username.getText().trim(), password.getText()));
115             if (authenticator instanceof NTLMAuthentication && !domain.getText().trim().equals("")) { //$NON-NLS-1$
116
((NTLMAuthentication) authenticator).setDomain(domain.getText().trim());
117             }
118             return true;
119         }
120
121         return false;
122     }
123
124 }
125
Popular Tags