Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCorePlugin / src / com / iver / core / preferences / network / FirewallPage.java @ 8884

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

    
107
import java.awt.event.ActionEvent;
108
import java.awt.event.ActionListener;
109
import java.net.Authenticator;
110
import java.net.MalformedURLException;
111
import java.net.PasswordAuthentication;
112
import java.net.URL;
113
import java.util.Properties;
114
import java.util.prefs.Preferences;
115

    
116
import javax.swing.ImageIcon;
117
import javax.swing.JCheckBox;
118
import javax.swing.JPanel;
119
import javax.swing.JPasswordField;
120
import javax.swing.JTextField;
121

    
122
import com.iver.andami.PluginServices;
123
import com.iver.andami.preferences.AbstractPreferencePage;
124
import com.iver.andami.preferences.StoreException;
125

    
126
public class FirewallPage extends AbstractPreferencePage {
127

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

    
144
        private static final class ProxyAuth extends Authenticator {
145
                private PasswordAuthentication auth;
146

    
147
                private ProxyAuth(String user, String pass) {
148
                        auth = new PasswordAuthentication(user, pass.toCharArray());
149
                }
150

    
151
                protected PasswordAuthentication getPasswordAuthentication() {
152
                        return auth;
153
                }
154
        }
155

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

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

    
189
        }
190

    
191
        public void initializeValues() {
192

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

    
206

    
207
                httpEnabled.addActionListener(new ActionHandler());
208

    
209

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

    
217
                socksEnabled.addActionListener(new ActionHandler());
218

    
219
        }
220

    
221
        public String getID() {
222
                return id;
223
        }
224

    
225
        public String getTitle() {
226
                return PluginServices.getText(this, "pref.network.firewall");
227
        }
228

    
229
        public JPanel getPanel() {
230
                return this;
231
        }
232

    
233
        public void storeValues() throws StoreException{
234
                Properties systemSettings = System.getProperties();
235
                URL httpURL, socksURL;
236

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

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

    
256
                        if (httpEnabled.isSelected()) {
257
                                systemSettings.put("http.proxyHost", httpURL.getHost());
258
                                systemSettings.put("http.proxyPort", httpURL.getPort()+"");
259
                                systemSettings.put("http.proxyUserName", proxyUser);
260
                                systemSettings.put("http.proxyPassword", proxyPassword);
261
                        } else {
262
                                systemSettings.remove("http.proxyHost");
263
                                systemSettings.remove("http.proxyPort");
264
                                systemSettings.remove("http.proxyUserName");
265
                                systemSettings.remove("http.proxyPassword");
266
                        }
267

    
268
                        System.setProperties(systemSettings);
269
                        if (proxyUser != null )
270
                        {
271
                                Authenticator.setDefault(new ProxyAuth(proxyUser,
272
                                                                proxyPassword));
273
                        } else {
274
                                Authenticator.setDefault(new ProxyAuth("", ""));
275
                        }
276
                        
277
                } catch (MalformedURLException e) {
278
                        if (httpEnabled.isSelected()) {
279
                                throw new StoreException(PluginServices.getText(this, "options.firewall.http.incorrect_host"),e);
280
                        }
281
                }
282

    
283
                try {
284
                        socksURL = new URL(socksHost.getText()+":"+socksPort.getText());
285

    
286
                        prefs.putBoolean("firewall.socks.enabled", socksEnabled.isSelected());
287
                        prefs.put("firewall.socks.host", socksHost.getText());
288
                        prefs.put("firewall.socks.port", socksPort.getText());
289

    
290
                        if (socksEnabled.isSelected()) {
291
                                systemSettings.put("socksProxyHost", socksURL.getHost());
292
                                systemSettings.put("socksProxyPort", socksURL.getPort()+"");
293
                        } else {
294
                                systemSettings.remove("socksProxyHost");
295
                                systemSettings.remove("socksProxyPort");
296
                        }
297

    
298
                        System.setProperties(systemSettings);
299

    
300
                } catch (MalformedURLException e) {
301
                        if (socksEnabled.isSelected()) {
302
                                throw new StoreException(PluginServices.getText(this, "options.firewall.socks.incorrect_host"),e);
303
                        }
304
                }
305
        }
306

    
307
        public void initializeDefaults() {
308
                httpEnabled.setSelected(false);
309
                httpHost.setText("");
310
                httpPort.setText("");
311
                httpUser.setText("");
312
                httpPass.setText("");
313
                httpNonProxy.setText("");
314
                socksEnabled.setSelected(false);
315
                socksHost.setText("");
316
                socksPort.setText("");
317

    
318
        }
319

    
320
        private class ActionHandler implements ActionListener {
321
                public void actionPerformed(ActionEvent evt) {
322
                        httpHost.setEnabled(httpEnabled.isSelected());
323
                        httpPort.setEnabled(httpEnabled.isSelected());
324
                        httpUser.setEnabled(httpEnabled.isSelected());
325
                        httpPass.setEnabled(httpEnabled.isSelected());
326
                        httpNonProxy.setEnabled(httpEnabled.isSelected());
327
                        socksHost.setEnabled(socksEnabled.isSelected());
328
                        socksPort.setEnabled(socksEnabled.isSelected());
329
                }
330
        }
331

    
332
        public ImageIcon getIcon() {
333
                return icon;
334
        }
335

    
336
        public boolean isValueChanged() {
337
                return hasChanged();
338
        }
339

    
340
        public void setChangesApplied() {
341
                setChanged(false);
342
        }
343

    
344

    
345
}