Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / preferences / network / FirewallPage.java @ 682

History | View | Annotate | Download (10.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id$
27
* $Log$
28
* Revision 1.9  2006/11/21 10:59:45  fjp
29
* Fallo proxy con usuario y contrase?a
30
*
31
* Revision 1.8  2006/11/20 17:29:43  fjp
32
* Fallo proxy con usuario y contrase?a
33
*
34
* Revision 1.7  2006/10/18 07:55:43  jaume
35
* *** empty log message ***
36
*
37
* Revision 1.6  2006/08/22 12:23:05  jaume
38
* improved perfomance when saving changes
39
*
40
* Revision 1.5  2006/08/22 07:37:17  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.4  2006/08/04 11:45:12  caballero
44
* lanzo una excepci?n cuando falla el m?todo storeValues
45
*
46
* Revision 1.3  2006/07/31 10:02:31  jaume
47
* *** empty log message ***
48
*
49
* Revision 1.2  2006/06/13 07:43:08  fjp
50
* Ajustes sobre los cuadros de dialogos de preferencias
51
*
52
* Revision 1.1  2006/06/12 16:04:28  caballero
53
* Preferencias
54
*
55
* Revision 1.11  2006/06/06 10:26:31  jaume
56
* *** empty log message ***
57
*
58
* Revision 1.10  2006/06/05 17:07:17  jaume
59
* *** empty log message ***
60
*
61
* Revision 1.9  2006/06/05 17:00:44  jaume
62
* *** empty log message ***
63
*
64
* Revision 1.8  2006/06/05 16:57:59  jaume
65
* *** empty log message ***
66
*
67
* Revision 1.7  2006/06/05 14:45:06  jaume
68
* *** empty log message ***
69
*
70
* Revision 1.6  2006/06/05 11:00:09  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.5  2006/06/05 10:39:02  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.4  2006/06/05 10:13:40  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.3  2006/06/05 10:06:08  jaume
80
* *** empty log message ***
81
*
82
* Revision 1.2  2006/06/05 09:51:56  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.1  2006/06/02 10:50:18  jaume
86
* *** empty log message ***
87
*
88
*
89
*/
90
package org.gvsig.coreplugin.preferences.network;
91

    
92
import java.awt.event.ActionEvent;
93
import java.awt.event.ActionListener;
94
import java.net.Authenticator;
95
import java.net.MalformedURLException;
96
import java.net.PasswordAuthentication;
97
import java.net.URL;
98
import java.util.Properties;
99
import java.util.prefs.Preferences;
100

    
101
import javax.swing.ImageIcon;
102
import javax.swing.JCheckBox;
103
import javax.swing.JPanel;
104
import javax.swing.JPasswordField;
105
import javax.swing.JTextField;
106

    
107
import org.gvsig.andami.PluginServices;
108
import org.gvsig.andami.preferences.AbstractPreferencePage;
109
import org.gvsig.andami.preferences.StoreException;
110

    
111

    
112
public class FirewallPage extends AbstractPreferencePage {
113

    
114
        // Quiz? en vez de usar un prefs cada vez deber?amos de tener una clase gvSIG.java
115
        // donde se guarden todas las preferencias, queda m?s limpio y m?s claro si se hace
116
        // un: gvSIG.getProperty("gvsig.connection") que lo de abajo.
117
        private static Preferences prefs = Preferences.userRoot().node( "gvsig.connection" );
118
        private JCheckBox httpEnabled;
119
        private JTextField httpHost;
120
        private JTextField httpPort;
121
        private JTextField httpUser;
122
        private JPasswordField httpPass;
123
        private JTextField httpNonProxy;
124
        private JCheckBox socksEnabled;
125
        private JTextField socksHost;
126
        private JTextField socksPort;
127
        protected static String id = FirewallPage.class.getName();
128
        private ImageIcon icon;
129

    
130
        private static final class ProxyAuth extends Authenticator {
131
                private PasswordAuthentication auth;
132

    
133
                private ProxyAuth(String user, String pass) {
134
                        auth = new PasswordAuthentication(user, pass.toCharArray());
135
                }
136

    
137
                protected PasswordAuthentication getPasswordAuthentication() {
138
                        return auth;
139
                }
140
        }
141

    
142
        public FirewallPage() {
143
                super();
144
                icon=PluginServices.getIconTheme().get("edit-setup-firewall");
145
                setParentID(NetworkPage.id);
146
//                 checkbox
147
                addComponent(httpEnabled = new JCheckBox(PluginServices.getText(this,
148
                        "options.firewall.http.enabled")));
149
                // proxy host
150
                addComponent(PluginServices.getText(this, "options.firewall.http.host") + ":",
151
                        httpHost = new JTextField("", 15));
152
                // proxy port
153
                addComponent(PluginServices.getText(this, "options.firewall.http.port") + ":",
154
                        httpPort = new JTextField("", 15));
155
                // proxy username
156
                addComponent(PluginServices.getText(this, "options.firewall.http.user") + ":",
157
                        httpUser = new JTextField("", 15));
158
                // proxy password
159
                addComponent(PluginServices.getText(this, "options.firewall.http.password") + ":",
160
                        httpPass = new JPasswordField("", 15));
161
                // no proxy for
162
                addComponent(PluginServices.getText(this, "options.firewall.http.nonProxy") + ":",
163
                        httpNonProxy = new JTextField("", 15));
164

    
165
//                 checkbox
166
                addComponent(socksEnabled = new JCheckBox(PluginServices.getText(this,
167
                        "options.firewall.socks.enabled")));
168
                // proxy host
169
                addComponent(PluginServices.getText(this, "options.firewall.socks.host") + ":",
170
                        socksHost = new JTextField("", 15));
171
                // proxy port
172
                addComponent(PluginServices.getText(this, "options.firewall.socks.port") + ":",
173
                        socksPort = new JTextField("", 15));
174

    
175
        }
176

    
177
        public void initializeValues() {
178

    
179
                boolean enabled = prefs.getBoolean("firewall.http.enabled", false);
180
                httpEnabled.setSelected(enabled);
181
                httpHost.setEnabled(enabled);
182
                httpHost.setText(prefs.get("firewall.http.host",""));
183
                httpPort.setEnabled(enabled);
184
                httpPort.setText(prefs.get("firewall.http.port",""));
185
                httpUser.setEnabled(enabled);
186
                httpUser.setText(prefs.get("firewall.http.user",""));
187
                httpPass.setEnabled(enabled);
188
                httpPass.setText(prefs.get("firewall.http.password",""));
189
                httpNonProxy.setEnabled(enabled);
190
                httpNonProxy.setText(prefs.get("firewall.http.nonProxyHosts",""));
191

    
192

    
193
                httpEnabled.addActionListener(new ActionHandler());
194

    
195

    
196
                enabled = prefs.getBoolean("firewall.socks.enabled", false);
197
                socksEnabled.setSelected(enabled);
198
                socksHost.setEnabled(enabled);
199
                socksHost.setText(prefs.get("firewall.socks.host",""));
200
                socksPort.setEnabled(enabled);
201
                socksPort.setText(prefs.get("firewall.socks.port",""));
202

    
203
                socksEnabled.addActionListener(new ActionHandler());
204

    
205
        }
206

    
207
        public String getID() {
208
                return id;
209
        }
210

    
211
        public String getTitle() {
212
                return PluginServices.getText(this, "pref.network.firewall");
213
        }
214

    
215
        public JPanel getPanel() {
216
                return this;
217
        }
218

    
219
        public void storeValues() throws StoreException{
220
                Properties systemSettings = System.getProperties();
221
                URL httpURL, socksURL;
222

    
223
                try {
224
                        httpURL = new URL(createURL(httpHost.getText(), httpPort.getText()));
225
                        prefs.putBoolean("firewall.http.enabled", httpEnabled.isSelected());
226
                        prefs.put("firewall.http.host", httpHost.getText());
227
                        prefs.put("firewall.http.port", httpPort.getText());
228
                        String proxyUser = httpUser.getText();
229
                        String proxyPassword = new String(httpPass.getPassword());
230
                        prefs.put("firewall.http.user", proxyUser);
231
                        prefs.put("firewall.http.password", proxyPassword);
232
                        prefs.put("firewall.http.nonProxyHosts", httpNonProxy.getText());
233

    
234
                        if (httpEnabled.isSelected()) {
235
                                systemSettings.put("http.proxySet", "true");
236
                                systemSettings.put("http.proxyHost", httpURL.getHost());
237
                                systemSettings.put("http.proxyPort", httpURL.getPort()+"");
238
                                systemSettings.put("http.proxyUserName", proxyUser);
239
                                systemSettings.put("http.proxyPassword", proxyPassword);
240
                        } else {
241
                                systemSettings.put("http.proxySet", "false");
242
                                systemSettings.remove("http.proxyHost");
243
                                systemSettings.remove("http.proxyPort");
244
                                systemSettings.remove("http.proxyUserName");
245
                                systemSettings.remove("http.proxyPassword");
246
                                prefs.remove("firewall.http.host");
247
                                prefs.remove("firewall.http.port");
248
                                prefs.remove("firewall.http.user");
249
                                prefs.remove("firewall.http.password");
250
                                prefs.remove("firewall.http.nonProxyHosts");
251

    
252
                        }
253

    
254
                        System.setProperties(systemSettings);
255
                        if (proxyUser != null )
256
                        {
257
                                Authenticator.setDefault(new ProxyAuth(proxyUser,
258
                                                                proxyPassword));
259
                        } else {
260
                                Authenticator.setDefault(new ProxyAuth("", ""));
261
                        }
262

    
263
                } catch (MalformedURLException e) {
264
                        if (httpEnabled.isSelected()) {
265
                                throw new StoreException(PluginServices.getText(this, "options.firewall.http.incorrect_host"),e);
266
                        }
267
                }
268

    
269
                try {
270
                        socksURL = new URL(createURL(socksHost.getText(),socksPort.getText()));
271

    
272
                        prefs.putBoolean("firewall.socks.enabled", socksEnabled.isSelected());
273
                        prefs.put("firewall.socks.host", socksHost.getText());
274
                        prefs.put("firewall.socks.port", socksPort.getText());
275

    
276
                        if (socksEnabled.isSelected()) {
277
                                systemSettings.put("socksProxyHost", socksURL.getHost());
278
                                systemSettings.put("socksProxyPort", socksURL.getPort()+"");
279
                        } else {
280
                                systemSettings.remove("socksProxyHost");
281
                                systemSettings.remove("socksProxyPort");
282
                        }
283

    
284
                        System.setProperties(systemSettings);
285

    
286
                } catch (MalformedURLException e) {
287
                        if (socksEnabled.isSelected()) {
288
                                throw new StoreException(PluginServices.getText(this, "options.firewall.socks.incorrect_host"),e);
289
                        }
290
                }
291
        }
292

    
293
        public void initializeDefaults() {
294
                httpEnabled.setSelected(false);
295
                httpHost.setText("");
296
                httpPort.setText("");
297
                httpUser.setText("");
298
                httpPass.setText("");
299
                httpNonProxy.setText("");
300
                socksEnabled.setSelected(false);
301
                socksHost.setText("");
302
                socksPort.setText("");
303

    
304
        }
305

    
306
        private class ActionHandler implements ActionListener {
307
                public void actionPerformed(ActionEvent evt) {
308
                        httpHost.setEnabled(httpEnabled.isSelected());
309
                        httpPort.setEnabled(httpEnabled.isSelected());
310
                        httpUser.setEnabled(httpEnabled.isSelected());
311
                        httpPass.setEnabled(httpEnabled.isSelected());
312
                        httpNonProxy.setEnabled(httpEnabled.isSelected());
313
                        socksHost.setEnabled(socksEnabled.isSelected());
314
                        socksPort.setEnabled(socksEnabled.isSelected());
315
                }
316
        }
317

    
318
        public ImageIcon getIcon() {
319
                return icon;
320
        }
321

    
322
        public boolean isValueChanged() {
323
                return hasChanged();
324
        }
325

    
326
        public void setChangesApplied() {
327
                setChanged(false);
328
        }        
329

    
330
        private String createURL(String host, String port){
331
                StringBuffer strUrl = new StringBuffer();
332
                // add "http://" prefix if it wasn't included.
333
                strUrl.append(host.toLowerCase().startsWith("http://")
334
                                ? host.toLowerCase() : "http://"+ host.toLowerCase());
335

    
336
                // add port
337
                strUrl.append(!port.equals("")
338
                                ? ":" + port : "");
339
                return strUrl.toString();
340
        }
341
}