source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/GAuthenticator.java

Last change on this file was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

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 operationMode = REGULAR; // reset the state of the Authenticator to normal mode
110 return new PasswordAuthentication(username_str, password_str.toCharArray());
111 }
112 }
113
114 // Component definition.
115 dialog = new JDialog (Gatherer.g_man, Dictionary.get("GAuthenticator.Title"), true);
116 dialog.setModal(true);
117 dialog.setSize(SIZE);
118 JPanel content_pane = (JPanel) dialog.getContentPane();
119 JLabel title_label = new JLabel(getMessageString());
120
121 JPanel user_panel = new JPanel();
122 JLabel username_label = new JLabel(Dictionary.get("GAuthenticator.Username"));
123 JTextField username = new JTextField();
124 username.setToolTipText(Dictionary.get("GAuthenticator.Username_Tooltip"));
125 if (username_str != null) {
126 username.setText(username_str);
127 }
128
129 JPanel password_panel = new JPanel();
130 JLabel password_label = new JLabel(Dictionary.get("GAuthenticator.Password"));
131 password = new JPasswordField();
132 password.setEchoChar('*');
133 password.setToolTipText(Dictionary.get("GAuthenticator.Password_Tooltip"));
134 if (password_str != null) {
135 password.setText(password_str);
136 }
137
138 JPanel button_panel = new JPanel();
139 ok_button = new GLIButton(Dictionary.get("General.OK"), Dictionary.get("General.OK_Tooltip"));
140 cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
141
142 // Connection
143 cancel_button.addActionListener(new AuthenticationActionListener(true));
144 ok_button.addActionListener(new AuthenticationActionListener(false));
145 password.addActionListener(new AuthenticationActionListener(false));
146 username.addActionListener(new RequestFocusListener(password));
147
148 // Layout
149 user_panel.setLayout(new GridLayout(1,2));
150 user_panel.add(username_label);
151 user_panel.add(username);
152
153 password_panel.setLayout(new GridLayout(1,2));
154 password_panel.add(password_label);
155 password_panel.add(password);
156
157 button_panel.setLayout(new GridLayout(1,2));
158 button_panel.add(ok_button);
159 button_panel.add(cancel_button);
160
161 content_pane.setLayout(new GridLayout(4,1,0,2));
162 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
163 content_pane.add(title_label);
164 content_pane.add(user_panel);
165 content_pane.add(password_panel);
166 content_pane.add(button_panel);
167
168 // Position the window
169 Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
170 dialog.setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
171 dialog.setVisible(true);
172 if(!authentication_cancelled) {
173 // Store the authentication
174 authentications.put(getRequestingHost() + ":" + getRequestingPort(), username.getText() + "@" + new String(password.getPassword()));
175 return new PasswordAuthentication(username.getText(), password.getPassword());
176 } else {
177 return null;
178 }
179 }
180
181 protected PasswordAuthentication getPasswordAuthentication() {
182 return getPasswordAuthentication(null,null);
183 }
184
185 /** This is defined so it can be overridden by subclasses (getRequestingPrompt is final). */
186 protected String getMessageString()
187 {
188 return getRequestingPrompt();
189 }
190
191
192 /** Detects actions upon any control that attempt to submit the current details for authentication. */
193 private class AuthenticationActionListener
194 implements ActionListener {
195 /** <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise. */
196 private boolean cancel_action = false;
197 /** Constructor.
198 * @param cancel_action <i>true</i> if this authentication action cancels the authentication, <i>false</i> otherwise.
199 */
200 public AuthenticationActionListener(boolean cancel_action) {
201 this.cancel_action = cancel_action;
202 }
203 /** 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.
204 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
205 */
206 public void actionPerformed(ActionEvent event) {
207 authentication_cancelled = cancel_action;
208 dialog.dispose();
209 }
210 }
211
212 /** This listener detects actions on registered controls, and when they occur ensures the focus is moved to some targetted component. */
213 private class RequestFocusListener
214 implements ActionListener {
215 /*The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control. */
216 private Component target = null;
217 /** Constructor.
218 * @param target The <strong>Component</strong> you wish to gain focus when an action is performed on a registered control.
219 */
220 public RequestFocusListener(Component target) {
221 this.target = target;
222 }
223 /** 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.
224 * @param event An <strong>ActionEvent</strong> with information about the event that fired this method.
225 */
226 public void actionPerformed(ActionEvent event) {
227 target.requestFocus();
228 }
229 }
230}
Note: See TracBrowser for help on using the repository browser.