source: gli/trunk/src/org/greenstone/gatherer/GAuthenticator.java@ 17551

Last change on this file since 17551 was 17551, checked in by ak19, 16 years ago

Authenticator now only prompts for proxy details for WGet and no longer pops up a second dialog requesting the same proxy authentication information for the general proxy.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.net.*;
42import java.util.*;
43import javax.swing.*;
44
45
46import org.greenstone.gatherer.gui.GLIButton;
47
48
49/** Provides a graphic authenticator for network password requests.
50 * @author John Thompson, Greenstone Digital Library, University of Waikato
51 * @version 2.3
52 */
53public class GAuthenticator
54 extends Authenticator
55{
56 static public Hashtable authentications = new Hashtable();
57
58 /** Indicates if this authentication prompt been cancelled, and if so rolls-back authentication. */
59 private boolean authentication_cancelled = false;
60 /** The button used to cancel a prompt. */
61 private JButton cancel_button = null;
62 /** The button used to submit the login/password. */
63 private JButton ok_button = null;
64 /** A reference to the dialog prompt created so inner classes can dispose of it. */
65 private JDialog dialog = null;
66 /** The password is a special starred out password field. */
67 private JPasswordField password = null;
68 /** The default size of this dialog. */
69 static final private Dimension SIZE = new Dimension(470,160);
70
71 /** Constructor. */
72 public GAuthenticator() {
73 }
74
75 /** Prompt the user for authentication using a pretty dialog box.
76 * @return A <strong>PasswordAuthentication</strong> object containing the login and password valuees the user has submitted.
77 * @see org.greenstone.gatherer.GAuthenticator.AuthenticationActionListener
78 * @see org.greenstone.gatherer.GAuthenticator.RequestFocusListener
79 */
80 protected PasswordAuthentication getPasswordAuthentication(String username_str, String password_str) {
81
82 // Don't prompt if the details were already saved for the same host and port. This is necessary
83 // when running wget because wget requires proxy authentication. And without the following, the
84 // regular proxy authentication would also popup a dialog requesting the same information.
85
86 String key = getRequestingHost() + ":" + getRequestingPort();
87 String value = (String)authentications.get(key);
88
89 if(value != null) { // authentication for this host and port was already stored
90 // Arguments may be null. If so, retrieve them from the stored value
91 if(username_str == null) {
92 username_str = value.substring(0, value.indexOf("@"));
93 }
94 if(password_str == null) {
95 password_str = value.substring(value.indexOf("@") + 1);
96 }
97 return new PasswordAuthentication(username_str, password_str.toCharArray());
98 }
99
100 // Component definition.
101 dialog = new JDialog (Gatherer.g_man, Dictionary.get("GAuthenticator.Title"), true);
102 dialog.setModal(true);
103 dialog.setSize(SIZE);
104 JPanel content_pane = (JPanel) dialog.getContentPane();
105 JLabel title_label = new JLabel(getMessageString());
106
107 JPanel user_panel = new JPanel();
108 JLabel username_label = new JLabel(Dictionary.get("GAuthenticator.Username"));
109 JTextField username = new JTextField();
110 username.setToolTipText(Dictionary.get("GAuthenticator.Username_Tooltip"));
111 if (username_str != null) {
112 username.setText(username_str);
113 }
114
115 JPanel password_panel = new JPanel();
116 JLabel password_label = new JLabel(Dictionary.get("GAuthenticator.Password"));
117 password = new JPasswordField();
118 password.setEchoChar('*');
119 password.setToolTipText(Dictionary.get("GAuthenticator.Password_Tooltip"));
120 if (password_str != null) {
121 password.setText(password_str);
122 }
123
124 JPanel button_panel = new JPanel();
125 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
126 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
127
128 // Connection
129 cancel_button.addActionListener(new AuthenticationActionListener(true));
130 ok_button.addActionListener(new AuthenticationActionListener(false));
131 password.addActionListener(new AuthenticationActionListener(false));
132 username.addActionListener(new RequestFocusListener(password));
133
134 // Layout
135 user_panel.setLayout(new GridLayout(1,2));
136 user_panel.add(username_label);
137 user_panel.add(username);
138
139 password_panel.setLayout(new GridLayout(1,2));
140 password_panel.add(password_label);
141 password_panel.add(password);
142
143 button_panel.setLayout(new GridLayout(1,2));
144 button_panel.add(ok_button);
145 button_panel.add(cancel_button);
146
147 content_pane.setLayout(new GridLayout(4,1,0,2));
148 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
149 content_pane.add(title_label);
150 content_pane.add(user_panel);
151 content_pane.add(password_panel);
152 content_pane.add(button_panel);
153
154 // Position the window
155 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
156 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
157 dialog.setVisible(true);
158 if(!authentication_cancelled) {
159 // Store the authentication
160 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
161 return new PasswordAuthentication(username.getText(), password.getPassword());
162 } else {
163 return null;
164 }
165 }
166
167 protected PasswordAuthentication getPasswordAuthentication() {
168 return getPasswordAuthentication(null,null);
169 }
170
171 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
172 protected String getMessageString()
173 {
174 return getRequestingPrompt();
175 }
176
177
178 /** Detects actions upon any control that attempt to submit the current details for authentication. */
179 private class AuthenticationActionListener
180 implements ActionListener {
181 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
182 private boolean cancel_action = false;
183 /** Constructor.
184 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
185 */
186 public AuthenticationActionListener(boolean cancel_action) {
187 this.cancel_action = cancel_action;
188 }
189 /** Any implementation of an ActionListener must include this method so that we can be informed when an action has been performed on our registered controls, allowing us to dispose of the authentication dialog after determining if this is a submit action or a cancel one.
190 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
191 */
192 public void actionPerformed(ActionEvent event) {
193 authentication_cancelled = cancel_action;
194 dialog.dispose();
195 }
196 }
197
198 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
199 private class RequestFocusListener
200 implements ActionListener {
201 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
202 private Component target = null;
203 /** Constructor.
204 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
205 */
206 public RequestFocusListener(Component target) {
207 this.target = target;
208 }
209 /** Any implementation of an ActionListener must include this method so that we can be informed when an action has been performed on our registered controls, allowing us to request focus in the target control.
210 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
211 */
212 public void actionPerformed(ActionEvent event) {
213 target.requestFocus();
214 }
215 }
216}
Note: See TracBrowser for help on using the repository browser.