Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libCorePlugin / src / org / gvsig / coreplugin / preferences / network / FirewallPage.java @ 38564

History | View | Annotate | Download (10.9 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
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 License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
/* CVS MESSAGES:
43
*
44
* $Id$
45
* $Log$
46
* Revision 1.9  2006/11/21 10:59:45  fjp
47
* Fallo proxy con usuario y contrase?a
48
*
49
* Revision 1.8  2006/11/20 17:29:43  fjp
50
* Fallo proxy con usuario y contrase?a
51
*
52
* Revision 1.7  2006/10/18 07:55:43  jaume
53
* *** empty log message ***
54
*
55
* Revision 1.6  2006/08/22 12:23:05  jaume
56
* improved perfomance when saving changes
57
*
58
* Revision 1.5  2006/08/22 07:37:17  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.4  2006/08/04 11:45:12  caballero
62
* lanzo una excepci?n cuando falla el m?todo storeValues
63
*
64
* Revision 1.3  2006/07/31 10:02:31  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.2  2006/06/13 07:43:08  fjp
68
* Ajustes sobre los cuadros de dialogos de preferencias
69
*
70
* Revision 1.1  2006/06/12 16:04:28  caballero
71
* Preferencias
72
*
73
* Revision 1.11  2006/06/06 10:26:31  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.10  2006/06/05 17:07:17  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.9  2006/06/05 17:00:44  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.8  2006/06/05 16:57:59  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.7  2006/06/05 14:45:06  jaume
86
* *** empty log message ***
87
*
88
* Revision 1.6  2006/06/05 11:00:09  jaume
89
* *** empty log message ***
90
*
91
* Revision 1.5  2006/06/05 10:39:02  jaume
92
* *** empty log message ***
93
*
94
* Revision 1.4  2006/06/05 10:13:40  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.3  2006/06/05 10:06:08  jaume
98
* *** empty log message ***
99
*
100
* Revision 1.2  2006/06/05 09:51:56  jaume
101
* *** empty log message ***
102
*
103
* Revision 1.1  2006/06/02 10:50:18  jaume
104
* *** empty log message ***
105
*
106
*
107
*/
108
package org.gvsig.coreplugin.preferences.network;
109

    
110
import java.awt.event.ActionEvent;
111
import java.awt.event.ActionListener;
112
import java.net.Authenticator;
113
import java.net.MalformedURLException;
114
import java.net.PasswordAuthentication;
115
import java.net.URL;
116
import java.util.Properties;
117
import java.util.prefs.Preferences;
118

    
119
import javax.swing.ImageIcon;
120
import javax.swing.JCheckBox;
121
import javax.swing.JPanel;
122
import javax.swing.JPasswordField;
123
import javax.swing.JTextField;
124

    
125
import org.gvsig.andami.PluginServices;
126
import org.gvsig.andami.preferences.AbstractPreferencePage;
127
import org.gvsig.andami.preferences.StoreException;
128

    
129

    
130
public class FirewallPage extends AbstractPreferencePage {
131

    
132
        // Quiz? en vez de usar un prefs cada vez deber?amos de tener una clase gvSIG.java
133
        // donde se guarden todas las preferencias, queda m?s limpio y m?s claro si se hace
134
        // un: gvSIG.getProperty("gvsig.connection") que lo de abajo.
135
        private static Preferences prefs = Preferences.userRoot().node( "gvsig.connection" );
136
        private JCheckBox httpEnabled;
137
        private JTextField httpHost;
138
        private JTextField httpPort;
139
        private JTextField httpUser;
140
        private JPasswordField httpPass;
141
        private JTextField httpNonProxy;
142
        private JCheckBox socksEnabled;
143
        private JTextField socksHost;
144
        private JTextField socksPort;
145
        protected static String id = FirewallPage.class.getName();
146
        private ImageIcon icon;
147

    
148
        private static final class ProxyAuth extends Authenticator {
149
                private PasswordAuthentication auth;
150

    
151
                private ProxyAuth(String user, String pass) {
152
                        auth = new PasswordAuthentication(user, pass.toCharArray());
153
                }
154

    
155
                protected PasswordAuthentication getPasswordAuthentication() {
156
                        return auth;
157
                }
158
        }
159

    
160
        public FirewallPage() {
161
                super();
162
                //icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/shield.png"));
163
                icon=PluginServices.getIconTheme().get("edit-setup-firewall");
164
                setParentID(NetworkPage.id);
165
//                 checkbox
166
                addComponent(httpEnabled = new JCheckBox(PluginServices.getText(this,
167
                        "options.firewall.http.enabled")));
168
                // proxy host
169
                addComponent(PluginServices.getText(this, "options.firewall.http.host") + ":",
170
                        httpHost = new JTextField("", 15));
171
                // proxy port
172
                addComponent(PluginServices.getText(this, "options.firewall.http.port") + ":",
173
                        httpPort = new JTextField("", 15));
174
                // proxy username
175
                addComponent(PluginServices.getText(this, "options.firewall.http.user") + ":",
176
                        httpUser = new JTextField("", 15));
177
                // proxy password
178
                addComponent(PluginServices.getText(this, "options.firewall.http.password") + ":",
179
                        httpPass = new JPasswordField("", 15));
180
                // no proxy for
181
                addComponent(PluginServices.getText(this, "options.firewall.http.nonProxy") + ":",
182
                        httpNonProxy = new JTextField("", 15));
183

    
184
//                 checkbox
185
                addComponent(socksEnabled = new JCheckBox(PluginServices.getText(this,
186
                        "options.firewall.socks.enabled")));
187
                // proxy host
188
                addComponent(PluginServices.getText(this, "options.firewall.socks.host") + ":",
189
                        socksHost = new JTextField("", 15));
190
                // proxy port
191
                addComponent(PluginServices.getText(this, "options.firewall.socks.port") + ":",
192
                        socksPort = new JTextField("", 15));
193

    
194
        }
195

    
196
        public void initializeValues() {
197

    
198
                boolean enabled = prefs.getBoolean("firewall.http.enabled", false);
199
                httpEnabled.setSelected(enabled);
200
                httpHost.setEnabled(enabled);
201
                httpHost.setText(prefs.get("firewall.http.host",""));
202
                httpPort.setEnabled(enabled);
203
                httpPort.setText(prefs.get("firewall.http.port",""));
204
                httpUser.setEnabled(enabled);
205
                httpUser.setText(prefs.get("firewall.http.user",""));
206
                httpPass.setEnabled(enabled);
207
                httpPass.setText(prefs.get("firewall.http.password",""));
208
                httpNonProxy.setEnabled(enabled);
209
                httpNonProxy.setText(prefs.get("firewall.http.nonProxyHosts",""));
210

    
211

    
212
                httpEnabled.addActionListener(new ActionHandler());
213

    
214

    
215
                enabled = prefs.getBoolean("firewall.socks.enabled", false);
216
                socksEnabled.setSelected(enabled);
217
                socksHost.setEnabled(enabled);
218
                socksHost.setText(prefs.get("firewall.socks.host",""));
219
                socksPort.setEnabled(enabled);
220
                socksPort.setText(prefs.get("firewall.socks.port",""));
221

    
222
                socksEnabled.addActionListener(new ActionHandler());
223

    
224
        }
225

    
226
        public String getID() {
227
                return id;
228
        }
229

    
230
        public String getTitle() {
231
                return PluginServices.getText(this, "pref.network.firewall");
232
        }
233

    
234
        public JPanel getPanel() {
235
                return this;
236
        }
237

    
238
        public void storeValues() throws StoreException{
239
                Properties systemSettings = System.getProperties();
240
                URL httpURL, socksURL;
241

    
242
                try {
243
                        httpURL = new URL(createURL(httpHost.getText(), httpPort.getText()));
244
                        prefs.putBoolean("firewall.http.enabled", httpEnabled.isSelected());
245
                        prefs.put("firewall.http.host", httpHost.getText());
246
                        prefs.put("firewall.http.port", httpPort.getText());
247
                        String proxyUser = httpUser.getText();
248
                        String proxyPassword = new String(httpPass.getPassword());
249
                        prefs.put("firewall.http.user", proxyUser);
250
                        prefs.put("firewall.http.password", proxyPassword);
251
                        prefs.put("firewall.http.nonProxyHosts", httpNonProxy.getText());
252

    
253
                        if (httpEnabled.isSelected()) {
254
                                systemSettings.put("http.proxySet", "true");
255
                                systemSettings.put("http.proxyHost", httpURL.getHost());
256
                                systemSettings.put("http.proxyPort", httpURL.getPort()+"");
257
                                systemSettings.put("http.proxyUserName", proxyUser);
258
                                systemSettings.put("http.proxyPassword", proxyPassword);
259
                        } else {
260
                                systemSettings.put("http.proxySet", "false");
261
                                systemSettings.remove("http.proxyHost");
262
                                systemSettings.remove("http.proxyPort");
263
                                systemSettings.remove("http.proxyUserName");
264
                                systemSettings.remove("http.proxyPassword");
265
                                prefs.remove("firewall.http.host");
266
                                prefs.remove("firewall.http.port");
267
                                prefs.remove("firewall.http.user");
268
                                prefs.remove("firewall.http.password");
269
                                prefs.remove("firewall.http.nonProxyHosts");
270

    
271
                        }
272

    
273
                        System.setProperties(systemSettings);
274
                        if (proxyUser != null )
275
                        {
276
                                Authenticator.setDefault(new ProxyAuth(proxyUser,
277
                                                                proxyPassword));
278
                        } else {
279
                                Authenticator.setDefault(new ProxyAuth("", ""));
280
                        }
281

    
282
                } catch (MalformedURLException e) {
283
                        if (httpEnabled.isSelected()) {
284
                                throw new StoreException(PluginServices.getText(this, "options.firewall.http.incorrect_host"),e);
285
                        }
286
                }
287

    
288
                try {
289
                        socksURL = new URL(createURL(socksHost.getText(),socksPort.getText()));
290

    
291
                        prefs.putBoolean("firewall.socks.enabled", socksEnabled.isSelected());
292
                        prefs.put("firewall.socks.host", socksHost.getText());
293
                        prefs.put("firewall.socks.port", socksPort.getText());
294

    
295
                        if (socksEnabled.isSelected()) {
296
                                systemSettings.put("socksProxyHost", socksURL.getHost());
297
                                systemSettings.put("socksProxyPort", socksURL.getPort()+"");
298
                        } else {
299
                                systemSettings.remove("socksProxyHost");
300
                                systemSettings.remove("socksProxyPort");
301
                        }
302

    
303
                        System.setProperties(systemSettings);
304

    
305
                } catch (MalformedURLException e) {
306
                        if (socksEnabled.isSelected()) {
307
                                throw new StoreException(PluginServices.getText(this, "options.firewall.socks.incorrect_host"),e);
308
                        }
309
                }
310
        }
311

    
312
        public void initializeDefaults() {
313
                httpEnabled.setSelected(false);
314
                httpHost.setText("");
315
                httpPort.setText("");
316
                httpUser.setText("");
317
                httpPass.setText("");
318
                httpNonProxy.setText("");
319
                socksEnabled.setSelected(false);
320
                socksHost.setText("");
321
                socksPort.setText("");
322

    
323
        }
324

    
325
        private class ActionHandler implements ActionListener {
326
                public void actionPerformed(ActionEvent evt) {
327
                        httpHost.setEnabled(httpEnabled.isSelected());
328
                        httpPort.setEnabled(httpEnabled.isSelected());
329
                        httpUser.setEnabled(httpEnabled.isSelected());
330
                        httpPass.setEnabled(httpEnabled.isSelected());
331
                        httpNonProxy.setEnabled(httpEnabled.isSelected());
332
                        socksHost.setEnabled(socksEnabled.isSelected());
333
                        socksPort.setEnabled(socksEnabled.isSelected());
334
                }
335
        }
336

    
337
        public ImageIcon getIcon() {
338
                return icon;
339
        }
340

    
341
        public boolean isValueChanged() {
342
                return hasChanged();
343
        }
344

    
345
        public void setChangesApplied() {
346
                setChanged(false);
347
        }        
348

    
349
        private String createURL(String host, String port){
350
                StringBuffer strUrl = new StringBuffer();
351
                // add "http://" prefix if it wasn't included.
352
                strUrl.append(host.toLowerCase().startsWith("http://")
353
                                ? host.toLowerCase() : "http://"+ host.toLowerCase());
354

    
355
                // add port
356
                strUrl.append(!port.equals("")
357
                                ? ":" + port : "");
358
                return strUrl.toString();
359
        }
360
}