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

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

Correction to previous changes I made to GAuthenticator: it now only uses any previously stored values for username and password when the GAuthenticator operates in the new DOWNLOAD mode, which is set (and soon thereafter restored to the default REGULAR mode) in DownloadPane.java for (wget) downloads.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 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 /** For Wget downloads, we want to avoid the second automatic request for proxy authentication
72 * But the settings for that specific case will otherwise interfere with how this GAuthenticator
73 * is to function in the usual situation. For this reason, we use two modes.*/
74 static final public int REGULAR = 0;
75 static final public int DOWNLOAD = 1;
76 static private int operationMode = REGULAR;
77
78 public static void setMode(int mode) {
79 operationMode = mode;
80 }
81
82 /** Constructor. */
83 public GAuthenticator() {
84 }
85
86 /** Prompt the user for authentication using a pretty dialog box.
87 * @return A <strong>PasswordAuthentication</strong> object containing the login and password valuees the user has submitted.
88 * @see org.greenstone.gatherer.GAuthenticator.AuthenticationActionListener
89 * @see org.greenstone.gatherer.GAuthenticator.RequestFocusListener
90 */
91 protected PasswordAuthentication getPasswordAuthentication(String username_str, String password_str) {
92 if(operationMode == DOWNLOAD) { // special handling of proxy authentication popup for Wget downloads
93
94 // Don't prompt if the details were already saved for the same host and port. This is necessary
95 // when running wget because wget requires proxy authentication. And without the following, the
96 // regular proxy authentication would also popup a dialog requesting the same information.
97
98 String key = getRequestingHost() + ":" + getRequestingPort();
99 String value = (String)authentications.get(key);
100
101 if(value != null) { // authentication for this host and port was already stored
102 // Arguments may be null. If so, retrieve them from the stored value
103 if(username_str == null) {
104 username_str = value.substring(0, value.indexOf("@"));
105 }
106 if(password_str == null) {
107 password_str = value.substring(value.indexOf("@") + 1);
108 }
109 return new PasswordAuthentication(username_str, password_str.toCharArray());
110 }
111 }
112
113 // Component definition.
114 dialog = new JDialog (Gatherer.g_man, Dictionary.get("GAuthenticator.Title"), true);
115 dialog.setModal(true);
116 dialog.setSize(SIZE);
117 JPanel content_pane = (JPanel) dialog.getContentPane();
118 JLabel title_label = new JLabel(getMessageString());
119
120 JPanel user_panel = new JPanel();
121 JLabel username_label = new JLabel(Dictionary.get("GAuthenticator.Username"));
122 JTextField username = new JTextField();
123 username.setToolTipText(Dictionary.get("GAuthenticator.Username_Tooltip"));
124 if (username_str != null) {
125 username.setText(username_str);
126 }
127
128 JPanel password_panel = new JPanel();
129 JLabel password_label = new JLabel(Dictionary.get("GAuthenticator.Password"));
130 password = new JPasswordField();
131 password.setEchoChar('*');
132 password.setToolTipText(Dictionary.get("GAuthenticator.Password_Tooltip"));
133 if (password_str != null) {
134 password.setText(password_str);
135 }
136
137 JPanel button_panel = new JPanel();
138 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
139 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
140
141 // Connection
142 cancel_button.addActionListener(new AuthenticationActionListener(true));
143 ok_button.addActionListener(new AuthenticationActionListener(false));
144 password.addActionListener(new AuthenticationActionListener(false));
145 username.addActionListener(new RequestFocusListener(password));
146
147 // Layout
148 user_panel.setLayout(new GridLayout(1,2));
149 user_panel.add(username_label);
150 user_panel.add(username);
151
152 password_panel.setLayout(new GridLayout(1,2));
153 password_panel.add(password_label);
154 password_panel.add(password);
155
156 button_panel.setLayout(new GridLayout(1,2));
157 button_panel.add(ok_button);
158 button_panel.add(cancel_button);
159
160 content_pane.setLayout(new GridLayout(4,1,0,2));
161 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
162 content_pane.add(title_label);
163 content_pane.add(user_panel);
164 content_pane.add(password_panel);
165 content_pane.add(button_panel);
166
167 // Position the window
168 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
169 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
170 dialog.setVisible(true);
171 if(!authentication_cancelled) {
172 // Store the authentication
173 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
174 return new PasswordAuthentication(username.getText(), password.getPassword());
175 } else {
176 return null;
177 }
178 }
179
180 protected PasswordAuthentication getPasswordAuthentication() {
181 return getPasswordAuthentication(null,null);
182 }
183
184 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
185 protected String getMessageString()
186 {
187 return getRequestingPrompt();
188 }
189
190
191 /** Detects actions upon any control that attempt to submit the current details for authentication. */
192 private class AuthenticationActionListener
193 implements ActionListener {
194 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
195 private boolean cancel_action = false;
196 /** Constructor.
197 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
198 */
199 public AuthenticationActionListener(boolean cancel_action) {
200 this.cancel_action = cancel_action;
201 }
202 /** 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.
203 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
204 */
205 public void actionPerformed(ActionEvent event) {
206 authentication_cancelled = cancel_action;
207 dialog.dispose();
208 }
209 }
210
211 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
212 private class RequestFocusListener
213 implements ActionListener {
214 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
215 private Component target = null;
216 /** Constructor.
217 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
218 */
219 public RequestFocusListener(Component target) {
220 this.target = target;
221 }
222 /** 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.
223 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
224 */
225 public void actionPerformed(ActionEvent event) {
226 target.requestFocus();
227 }
228 }
229}
Note: See TracBrowser for help on using the repository browser.