~ubuntu-branches/ubuntu/natty/icedtea-web/natty-proposed

« back to all changes in this revision

Viewing changes to plugin/icedteanp/java/sun/applet/PasswordAuthenticationDialog.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-11-24 13:23:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101124132328-2xb9z39vxga63vr9
Tags: 1.0~20101124-0ubuntu1
* Update to hg 20101124.
* Fix xulrunner dependencies for natty.
* Build-depend on pkg-config and libgtk2.0-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* PasswordAuthenticationDialog -- requests authentication information from users
2
 
   Copyright (C) 2009  Red Hat
3
 
 
4
 
This file is part of IcedTea.
5
 
 
6
 
IcedTea is free software; you can redistribute it and/or modify
7
 
it under the terms of the GNU General Public License as published by
8
 
the Free Software Foundation; either version 2, or (at your option)
9
 
any later version.
10
 
 
11
 
IcedTea is distributed in the hope that it will be useful, but
12
 
WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
General Public License for more details.
15
 
 
16
 
You should have received a copy of the GNU General Public License
17
 
along with IcedTea; see the file COPYING.  If not, write to the
18
 
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 
02110-1301 USA.
20
 
 
21
 
Linking this library statically or dynamically with other modules is
22
 
making a combined work based on this library.  Thus, the terms and
23
 
conditions of the GNU General Public License cover the whole
24
 
combination.
25
 
 
26
 
As a special exception, the copyright holders of this library give you
27
 
permission to link this library with independent modules to produce an
28
 
executable, regardless of the license terms of these independent
29
 
modules, and to copy and distribute the resulting executable under
30
 
terms of your choice, provided that you also meet, for each linked
31
 
independent module, the terms and conditions of the license of that
32
 
module.  An independent module is a module which is not derived from
33
 
or based on this library.  If you modify this library, you may extend
34
 
this exception to your version of the library, but you are not
35
 
obligated to do so.  If you do not wish to do so, delete this
36
 
exception statement from your version. */
37
 
 
38
 
package sun.applet;
39
 
 
40
 
import java.awt.Dimension;
41
 
import java.awt.GridBagConstraints;
42
 
import java.awt.GridBagLayout;
43
 
import java.awt.Insets;
44
 
import java.awt.event.ActionEvent;
45
 
import java.awt.event.ActionListener;
46
 
import java.net.PasswordAuthentication;
47
 
 
48
 
import javax.swing.JButton;
49
 
import javax.swing.JDialog;
50
 
import javax.swing.JLabel;
51
 
import javax.swing.JPasswordField;
52
 
import javax.swing.JTextField;
53
 
import javax.swing.SwingUtilities;
54
 
 
55
 
/**
56
 
 * Modal non-minimizable dialog to request http authentication credentials
57
 
 */
58
 
 
59
 
public class PasswordAuthenticationDialog extends JDialog {
60
 
    
61
 
    private JLabel jlInfo = new JLabel("");
62
 
    private JTextField jtfUserName = new JTextField();
63
 
    private JPasswordField jpfPassword = new JPasswordField();
64
 
    private boolean userCancelled;
65
 
 
66
 
    public PasswordAuthenticationDialog() {
67
 
        initialize();
68
 
    }
69
 
 
70
 
    /**
71
 
     * Initialized the dialog components
72
 
     */
73
 
    
74
 
    public void initialize() {
75
 
 
76
 
        setTitle("IcedTea Java Plugin - Authorization needed to proceed");
77
 
 
78
 
        setLayout(new GridBagLayout());
79
 
 
80
 
        JLabel jlUserName = new JLabel("Username: ");
81
 
        JLabel jlPassword = new JLabel("Password: ");
82
 
        JButton jbOK = new JButton("OK");
83
 
        JButton jbCancel = new JButton("Cancel");
84
 
 
85
 
        jtfUserName.setSize(20, 10);
86
 
        jpfPassword.setSize(20, 10);
87
 
 
88
 
        GridBagConstraints c;
89
 
        
90
 
        c = new GridBagConstraints();
91
 
        c.fill = c.HORIZONTAL;
92
 
        c.gridx = 0;
93
 
        c.gridy = 0;
94
 
        c.gridwidth = 2;
95
 
        c.insets = new Insets(10, 5, 3, 3);
96
 
        add(jlInfo, c);
97
 
        
98
 
        c = new GridBagConstraints();
99
 
        c.gridx = 0;
100
 
        c.gridy = 1;
101
 
        c.insets = new Insets(10, 5, 3, 3);
102
 
        add(jlUserName, c);
103
 
        
104
 
        c = new GridBagConstraints();
105
 
        c.fill = c.HORIZONTAL;
106
 
        c.gridx = 1;
107
 
        c.gridy = 1;
108
 
        c.insets = new Insets(10, 5, 3, 3);
109
 
        c.weightx = 1.0;
110
 
        add(jtfUserName, c);
111
 
 
112
 
 
113
 
        c = new GridBagConstraints();
114
 
        c.gridx = 0;
115
 
        c.gridy = 2;
116
 
        c.insets = new Insets(5, 5, 3, 3);
117
 
        add(jlPassword, c);
118
 
        
119
 
        c = new GridBagConstraints();
120
 
        c.fill = c.HORIZONTAL;
121
 
        c.gridx = 1;
122
 
        c.gridy = 2;
123
 
        c.insets = new Insets(5, 5, 3, 3);
124
 
        c.weightx = 1.0;
125
 
        add(jpfPassword, c);
126
 
 
127
 
        c = new GridBagConstraints();
128
 
        c.anchor = c.SOUTHEAST;
129
 
        c.gridx = 1;
130
 
        c.gridy = 3;
131
 
        c.insets = new Insets(5, 5, 3, 70);
132
 
        c.weightx = 0.0;
133
 
        add(jbCancel, c);
134
 
        
135
 
        c = new GridBagConstraints();
136
 
        c.anchor = c.SOUTHEAST;
137
 
        c.gridx = 1;
138
 
        c.gridy = 3;
139
 
        c.insets = new Insets(5, 5, 3, 3);
140
 
        c.weightx = 0.0;
141
 
        add(jbOK, c);
142
 
        
143
 
        setMinimumSize(new Dimension(400,150));
144
 
        setMaximumSize(new Dimension(1024,150));
145
 
        setAlwaysOnTop(true);
146
 
        
147
 
        setSize(400,150);
148
 
        setLocationRelativeTo(null);
149
 
 
150
 
        // OK => read supplied info and pass it on
151
 
        jbOK.addActionListener(new ActionListener() {
152
 
            public void actionPerformed(ActionEvent e) {
153
 
                userCancelled = false;
154
 
                dispose();
155
 
            }
156
 
        });
157
 
        
158
 
        // Cancel => discard supplied info and pass on an empty auth
159
 
        jbCancel.addActionListener(new ActionListener() {
160
 
            public void actionPerformed(ActionEvent e) {
161
 
                userCancelled = true;
162
 
                dispose();
163
 
            }
164
 
        });
165
 
        
166
 
        // "return" key in either user or password field => OK
167
 
 
168
 
        jtfUserName.addActionListener(new ActionListener() {
169
 
            public void actionPerformed(ActionEvent e) {
170
 
                userCancelled = false;
171
 
                dispose();
172
 
            }
173
 
        });
174
 
        
175
 
        jpfPassword.addActionListener(new ActionListener() {
176
 
            public void actionPerformed(ActionEvent e) {
177
 
                userCancelled = false;
178
 
                dispose();
179
 
            }
180
 
        });
181
 
    }
182
 
 
183
 
    /**
184
 
     * Present a dialog to the user asking them for authentication information
185
 
     * 
186
 
     * @param hostThe host for with authentication is needed
187
 
     * @param port The port being accessed
188
 
     * @param prompt The prompt (realm) as presented by the server
189
 
     * @param type The type of server (proxy/web)
190
 
     * @return PasswordAuthentication containing the credentials (empty credentials if user cancelled)
191
 
     */
192
 
    protected PasswordAuthentication askUser(String host, int port, String prompt, String type) {
193
 
        PasswordAuthentication auth = null;
194
 
 
195
 
        host += port != -1 ? ":" + port : "";
196
 
 
197
 
        // This frame is reusable. So reset everything first.
198
 
        userCancelled = true;
199
 
        jlInfo.setText("<html>The " + type + " server at " + host + " is requesting authentication. It says \"" + prompt + "\"</html>");
200
 
 
201
 
        try {
202
 
            SwingUtilities.invokeAndWait( new Runnable() {
203
 
                public void run() {
204
 
                    // show dialog to user
205
 
                    setVisible(true);
206
 
                }
207
 
            });
208
 
        
209
 
            PluginDebug.debug("password dialog shown");
210
 
            
211
 
            // wait until dialog is gone
212
 
            while (this.isShowing()) {
213
 
                try {
214
 
                    Thread.sleep(200);
215
 
                } catch (InterruptedException ie) {
216
 
                }
217
 
            }
218
 
            
219
 
            PluginDebug.debug("password dialog closed");
220
 
 
221
 
            if (!userCancelled) {
222
 
                auth = new PasswordAuthentication(jtfUserName.getText(), jpfPassword.getText().toCharArray());
223
 
            }
224
 
        } catch (Exception e) {
225
 
            e.printStackTrace();
226
 
            
227
 
            // Nothing else we can do. Empty auth will be returned
228
 
        }
229
 
 
230
 
        return auth;
231
 
    }
232
 
 
233
 
    public static void main(String[] args) {
234
 
        PasswordAuthenticationDialog frame = new PasswordAuthenticationDialog();
235
 
 
236
 
        PasswordAuthentication auth = frame.askUser("127.0.0.1", 3128, "Password for local proxy", "proxy");
237
 
 
238
 
        System.err.println("Auth info: " + auth.getUserName() + ":" + new String(auth.getPassword()));
239
 
        System.exit(0);
240
 
    }
241
 
}