Statistics
| Revision:

root / trunk / libraries / libCorePlugin / src / com / iver / core / preferences / network / FirewallPage.java @ 8895

History | View | Annotate | Download (10.6 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 com.iver.core.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 com.iver.andami.PluginServices;
126
import com.iver.andami.preferences.AbstractPreferencePage;
127
import com.iver.andami.preferences.StoreException;
128

    
129
public class FirewallPage extends AbstractPreferencePage {
130

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

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

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

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

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

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

    
192
        }
193

    
194
        public void initializeValues() {
195

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

    
209

    
210
                httpEnabled.addActionListener(new ActionHandler());
211

    
212

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

    
220
                socksEnabled.addActionListener(new ActionHandler());
221

    
222
        }
223

    
224
        public String getID() {
225
                return id;
226
        }
227

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

    
232
        public JPanel getPanel() {
233
                return this;
234
        }
235

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

    
240
                try {
241
                        StringBuffer strUrl = new StringBuffer();
242
                        // add "http://" prefix if it wasn't included.
243
                        strUrl.append(httpHost.getText().toLowerCase().startsWith("http://")
244
                                        ? httpHost.getText().toLowerCase() : "http://"+httpHost.getText().toLowerCase());
245

    
246
                        // add port
247
                        strUrl.append(!httpPort.getText().equals("")
248
                                        ? ":"+httpPort.getText() : "");
249
                        httpURL = new URL(strUrl.toString());
250
                        prefs.putBoolean("firewall.http.enabled", httpEnabled.isSelected());
251
                        prefs.put("firewall.http.host", httpHost.getText());
252
                        prefs.put("firewall.http.port", httpPort.getText());
253
                        String proxyUser = httpUser.getText();
254
                        String proxyPassword = new String(httpPass.getPassword());
255
                        prefs.put("firewall.http.user", proxyUser);
256
                        prefs.put("firewall.http.password", proxyPassword);
257
                        prefs.put("firewall.http.nonProxyHosts", httpNonProxy.getText());
258

    
259
                        if (httpEnabled.isSelected()) {
260
                                systemSettings.put("http.proxyHost", httpURL.getHost());
261
                                systemSettings.put("http.proxyPort", httpURL.getPort()+"");
262
                                systemSettings.put("http.proxyUserName", proxyUser);
263
                                systemSettings.put("http.proxyPassword", proxyPassword);
264
                        } else {
265
                                systemSettings.remove("http.proxyHost");
266
                                systemSettings.remove("http.proxyPort");
267
                                systemSettings.remove("http.proxyUserName");
268
                                systemSettings.remove("http.proxyPassword");
269
                                prefs.remove("firewall.http.host");
270
                                prefs.remove("firewall.http.port");
271
                                prefs.remove("firewall.http.user");
272
                                prefs.remove("firewall.http.password");
273
                                prefs.remove("firewall.http.nonProxyHosts");
274

    
275
                        }
276

    
277
                        System.setProperties(systemSettings);
278
                        if (proxyUser != null )
279
                        {
280
                                Authenticator.setDefault(new ProxyAuth(proxyUser,
281
                                                                proxyPassword));
282
                        } else {
283
                                Authenticator.setDefault(new ProxyAuth("", ""));
284
                        }
285
                        
286
                } catch (MalformedURLException e) {
287
                        if (httpEnabled.isSelected()) {
288
                                throw new StoreException(PluginServices.getText(this, "options.firewall.http.incorrect_host"),e);
289
                        }
290
                }
291

    
292
                try {
293
                        socksURL = new URL(socksHost.getText()+":"+socksPort.getText());
294

    
295
                        prefs.putBoolean("firewall.socks.enabled", socksEnabled.isSelected());
296
                        prefs.put("firewall.socks.host", socksHost.getText());
297
                        prefs.put("firewall.socks.port", socksPort.getText());
298

    
299
                        if (socksEnabled.isSelected()) {
300
                                systemSettings.put("socksProxyHost", socksURL.getHost());
301
                                systemSettings.put("socksProxyPort", socksURL.getPort()+"");
302
                        } else {
303
                                systemSettings.remove("socksProxyHost");
304
                                systemSettings.remove("socksProxyPort");
305
                        }
306

    
307
                        System.setProperties(systemSettings);
308

    
309
                } catch (MalformedURLException e) {
310
                        if (socksEnabled.isSelected()) {
311
                                throw new StoreException(PluginServices.getText(this, "options.firewall.socks.incorrect_host"),e);
312
                        }
313
                }
314
        }
315

    
316
        public void initializeDefaults() {
317
                httpEnabled.setSelected(false);
318
                httpHost.setText("");
319
                httpPort.setText("");
320
                httpUser.setText("");
321
                httpPass.setText("");
322
                httpNonProxy.setText("");
323
                socksEnabled.setSelected(false);
324
                socksHost.setText("");
325
                socksPort.setText("");
326

    
327
        }
328

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

    
341
        public ImageIcon getIcon() {
342
                return icon;
343
        }
344

    
345
        public boolean isValueChanged() {
346
                return hasChanged();
347
        }
348

    
349
        public void setChangesApplied() {
350
                setChanged(false);
351
        }
352

    
353

    
354
}