source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java@ 6529

Last change on this file since 6529 was 6529, checked in by Don-vip, 10 years ago

Various stuff:

  • see #9414: remove old DeprecatedTags test
  • refactor some classes in gui.preferences package
  • improve javadoc
  • Property svn:eol-style set to native
File size: 15.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.server;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trc;
6
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.GridBagConstraints;
10import java.awt.GridBagLayout;
11import java.awt.Insets;
12import java.awt.event.ItemEvent;
13import java.awt.event.ItemListener;
14import java.net.Authenticator.RequestorType;
15import java.net.PasswordAuthentication;
16import java.net.ProxySelector;
17import java.util.HashMap;
18import java.util.Map;
19
20import javax.swing.BorderFactory;
21import javax.swing.ButtonGroup;
22import javax.swing.JLabel;
23import javax.swing.JPanel;
24import javax.swing.JRadioButton;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.gui.help.HelpUtil;
28import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
29import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
30import org.openstreetmap.josm.gui.widgets.JosmTextField;
31import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
32import org.openstreetmap.josm.io.DefaultProxySelector;
33import org.openstreetmap.josm.io.auth.CredentialsAgent;
34import org.openstreetmap.josm.io.auth.CredentialsAgentException;
35import org.openstreetmap.josm.io.auth.CredentialsManager;
36import org.openstreetmap.josm.tools.GBC;
37
38/**
39 * Component allowing input of proxy settings.
40 */
41public class ProxyPreferencesPanel extends VerticallyScrollablePanel {
42
43 /**
44 * The proxy policy is how JOSM will use proxy information.
45 */
46 public enum ProxyPolicy {
47 /** No proxy: JOSM will access Internet resources directly */
48 NO_PROXY("no-proxy"),
49 /** Use system settings: JOSM will use system proxy settings */
50 USE_SYSTEM_SETTINGS("use-system-settings"),
51 /** Use HTTP proxy: JOSM will use the given HTTP proxy, configured manually */
52 USE_HTTP_PROXY("use-http-proxy"),
53 /** Use HTTP proxy: JOSM will use the given SOCKS proxy */
54 USE_SOCKS_PROXY("use-socks-proxy");
55
56 private String policyName;
57 ProxyPolicy(String policyName) {
58 this.policyName = policyName;
59 }
60
61 /**
62 * Replies the policy name, to be stored in proxy preferences.
63 * @return the policy unique name
64 */
65 public String getName() {
66 return policyName;
67 }
68
69 /**
70 * Retrieves a proxy policy from its name found in preferences.
71 * @param policyName The policy name
72 * @return The proxy policy matching the given name, or {@code null}
73 */
74 static public ProxyPolicy fromName(String policyName) {
75 if (policyName == null) return null;
76 policyName = policyName.trim().toLowerCase();
77 for(ProxyPolicy pp: values()) {
78 if (pp.getName().equals(policyName))
79 return pp;
80 }
81 return null;
82 }
83 }
84
85 /** Property key for proxy policy */
86 public static final String PROXY_POLICY = "proxy.policy";
87 /** Property key for HTTP proxy host */
88 public static final String PROXY_HTTP_HOST = "proxy.http.host";
89 /** Property key for HTTP proxy port */
90 public static final String PROXY_HTTP_PORT = "proxy.http.port";
91 /** Property key for SOCKS proxy host */
92 public static final String PROXY_SOCKS_HOST = "proxy.socks.host";
93 /** Property key for SOCKS proxy port */
94 public static final String PROXY_SOCKS_PORT = "proxy.socks.port";
95 /** Property key for proxy username */
96 public static final String PROXY_USER = "proxy.user";
97 /** Property key for proxy password */
98 public static final String PROXY_PASS = "proxy.pass";
99
100 private Map<ProxyPolicy, JRadioButton> rbProxyPolicy;
101 private JosmTextField tfProxyHttpHost;
102 private JosmTextField tfProxyHttpPort;
103 private JosmTextField tfProxySocksHost;
104 private JosmTextField tfProxySocksPort;
105 private JosmTextField tfProxyHttpUser;
106 private JosmPasswordField tfProxyHttpPassword;
107
108 private JPanel pnlHttpProxyConfigurationPanel;
109 private JPanel pnlSocksProxyConfigurationPanel;
110
111 /**
112 * Builds the panel for the HTTP proxy configuration
113 *
114 * @return panel with HTTP proxy configuration
115 */
116 protected JPanel buildHttpProxyConfigurationPanel() {
117 JPanel pnl = new JPanel(new GridBagLayout()) {
118 @Override
119 public Dimension getMinimumSize() {
120 return getPreferredSize();
121 }
122 };
123 GridBagConstraints gc = new GridBagConstraints();
124
125 gc.anchor = GridBagConstraints.WEST;
126 gc.insets = new Insets(5,5,0,0);
127 gc.fill = GridBagConstraints.HORIZONTAL;
128 gc.weightx = 0.0;
129 pnl.add(new JLabel(tr("Host:")), gc);
130
131 gc.gridx = 1;
132 gc.weightx = 1.0;
133 pnl.add(tfProxyHttpHost = new JosmTextField(),gc);
134
135 gc.gridy = 1;
136 gc.gridx = 0;
137 gc.fill = GridBagConstraints.NONE;
138 gc.weightx = 0.0;
139 pnl.add(new JLabel(trc("server", "Port:")), gc);
140
141 gc.gridx = 1;
142 gc.weightx = 1.0;
143 pnl.add(tfProxyHttpPort = new JosmTextField(5),gc);
144 tfProxyHttpPort.setMinimumSize(tfProxyHttpPort.getPreferredSize());
145
146 gc.gridy = 2;
147 gc.gridx = 0;
148 gc.gridwidth = 2;
149 gc.fill = GridBagConstraints.HORIZONTAL;
150 gc.weightx = 1.0;
151 pnl.add(new JMultilineLabel(tr("Please enter a username and a password if your proxy requires authentication.")), gc);
152
153 gc.gridy = 3;
154 gc.gridx = 0;
155 gc.gridwidth = 1;
156 gc.fill = GridBagConstraints.NONE;
157 gc.weightx = 0.0;
158 pnl.add(new JLabel(tr("User:")), gc);
159
160 gc.gridy = 3;
161 gc.gridx = 1;
162 gc.weightx = 1.0;
163 pnl.add(tfProxyHttpUser = new JosmTextField(20),gc);
164 tfProxyHttpUser.setMinimumSize(tfProxyHttpUser.getPreferredSize());
165
166 gc.gridy = 4;
167 gc.gridx = 0;
168 gc.weightx = 0.0;
169 pnl.add(new JLabel(tr("Password:")), gc);
170
171 gc.gridx = 1;
172 gc.weightx = 1.0;
173 pnl.add(tfProxyHttpPassword = new JosmPasswordField(20),gc);
174 tfProxyHttpPassword.setMinimumSize(tfProxyHttpPassword.getPreferredSize());
175
176 // add an extra spacer, otherwise the layout is broken
177 gc.gridy = 5;
178 gc.gridx = 0;
179 gc.gridwidth = 2;
180 gc.fill = GridBagConstraints.BOTH;
181 gc.weightx = 1.0;
182 gc.weighty = 1.0;
183 pnl.add(new JPanel(), gc);
184 return pnl;
185 }
186
187 /**
188 * Builds the panel for the SOCKS proxy configuration
189 *
190 * @return panel with SOCKS proxy configuration
191 */
192 protected JPanel buildSocksProxyConfigurationPanel() {
193 JPanel pnl = new JPanel(new GridBagLayout()) {
194 @Override
195 public Dimension getMinimumSize() {
196 return getPreferredSize();
197 }
198 };
199 GridBagConstraints gc = new GridBagConstraints();
200 gc.anchor = GridBagConstraints.WEST;
201 gc.insets = new Insets(5,5,0,0);
202 gc.fill = GridBagConstraints.HORIZONTAL;
203 gc.weightx = 0.0;
204 pnl.add(new JLabel(tr("Host:")), gc);
205
206 gc.gridx = 1;
207 gc.weightx = 1.0;
208 pnl.add(tfProxySocksHost = new JosmTextField(20),gc);
209
210 gc.gridy = 1;
211 gc.gridx = 0;
212 gc.weightx = 0.0;
213 gc.fill = GridBagConstraints.NONE;
214 pnl.add(new JLabel(trc("server", "Port:")), gc);
215
216 gc.gridx = 1;
217 gc.weightx = 1.0;
218 pnl.add(tfProxySocksPort = new JosmTextField(5), gc);
219 tfProxySocksPort.setMinimumSize(tfProxySocksPort.getPreferredSize());
220
221 // add an extra spacer, otherwise the layout is broken
222 gc.gridy = 2;
223 gc.gridx = 0;
224 gc.gridwidth = 2;
225 gc.fill = GridBagConstraints.BOTH;
226 gc.weightx = 1.0;
227 gc.weighty = 1.0;
228 pnl.add(new JPanel(), gc);
229 return pnl;
230 }
231
232 protected JPanel buildProxySettingsPanel() {
233 JPanel pnl = new JPanel(new GridBagLayout());
234 GridBagConstraints gc = new GridBagConstraints();
235
236 ButtonGroup bgProxyPolicy = new ButtonGroup();
237 rbProxyPolicy = new HashMap<ProxyPolicy, JRadioButton>();
238 ProxyPolicyChangeListener policyChangeListener = new ProxyPolicyChangeListener();
239 for (ProxyPolicy pp: ProxyPolicy.values()) {
240 rbProxyPolicy.put(pp, new JRadioButton());
241 bgProxyPolicy.add(rbProxyPolicy.get(pp));
242 rbProxyPolicy.get(pp).addItemListener(policyChangeListener);
243 }
244
245 // radio button "No proxy"
246 gc.gridx = 0;
247 gc.gridy = 0;
248 gc.fill = GridBagConstraints.HORIZONTAL;
249 gc.anchor = GridBagConstraints.NORTHWEST;
250 gc.weightx = 0.0;
251 pnl.add(rbProxyPolicy.get(ProxyPolicy.NO_PROXY),gc);
252
253 gc.gridx = 1;
254 gc.weightx = 1.0;
255 pnl.add(new JLabel(tr("No proxy")), gc);
256
257 // radio button "System settings"
258 gc.gridx = 0;
259 gc.gridy = 1;
260 gc.weightx = 0.0;
261 pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS),gc);
262
263 gc.gridx = 1;
264 gc.weightx = 1.0;
265 String msg;
266 if (DefaultProxySelector.willJvmRetrieveSystemProxies()) {
267 msg = tr("Use standard system settings");
268 } else {
269 msg = tr("Use standard system settings (disabled. Start JOSM with <tt>-Djava.net.useSystemProxies=true</tt> to enable)");
270 }
271 pnl.add(new JMultilineLabel("<html>" + msg + "</html>"), gc);
272
273 // radio button http proxy
274 gc.gridx = 0;
275 gc.gridy = 2;
276 gc.weightx = 0.0;
277 pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY),gc);
278
279 gc.gridx = 1;
280 gc.weightx = 1.0;
281 pnl.add(new JLabel(tr("Manually configure a HTTP proxy")),gc);
282
283 // the panel with the http proxy configuration parameters
284 gc.gridx = 1;
285 gc.gridy = 3;
286 gc.fill = GridBagConstraints.HORIZONTAL;
287 gc.weightx = 1.0;
288 gc.weighty = 0.0;
289 pnl.add(pnlHttpProxyConfigurationPanel = buildHttpProxyConfigurationPanel(),gc);
290
291 // radio button SOCKS proxy
292 gc.gridx = 0;
293 gc.gridy = 4;
294 gc.weightx = 0.0;
295 pnl.add(rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY),gc);
296
297 gc.gridx = 1;
298 gc.weightx = 1.0;
299 pnl.add(new JLabel(tr("Use a SOCKS proxy")),gc);
300
301 // the panel with the SOCKS configuration parameters
302 gc.gridx = 1;
303 gc.gridy = 5;
304 gc.fill = GridBagConstraints.BOTH;
305 gc.anchor = GridBagConstraints.WEST;
306 gc.weightx = 1.0;
307 gc.weighty = 0.0;
308 pnl.add(pnlSocksProxyConfigurationPanel = buildSocksProxyConfigurationPanel(),gc);
309
310 return pnl;
311 }
312
313 /**
314 * Initializes the panel with the values from the preferences
315 */
316 public void initFromPreferences() {
317 String policy = Main.pref.get(PROXY_POLICY, null);
318 ProxyPolicy pp = ProxyPolicy.fromName(policy);
319 if (pp == null) {
320 pp = ProxyPolicy.NO_PROXY;
321 }
322 rbProxyPolicy.get(pp).setSelected(true);
323 String value = Main.pref.get("proxy.host", null);
324 if (value != null) {
325 // legacy support
326 tfProxyHttpHost.setText(value);
327 Main.pref.put("proxy.host", null);
328 } else {
329 tfProxyHttpHost.setText(Main.pref.get(PROXY_HTTP_HOST, ""));
330 }
331 value = Main.pref.get("proxy.port", null);
332 if (value != null) {
333 // legacy support
334 tfProxyHttpPort.setText(value);
335 Main.pref.put("proxy.port", null);
336 } else {
337 tfProxyHttpPort.setText(Main.pref.get(PROXY_HTTP_PORT, ""));
338 }
339 tfProxySocksHost.setText(Main.pref.get(PROXY_SOCKS_HOST, ""));
340 tfProxySocksPort.setText(Main.pref.get(PROXY_SOCKS_PORT, ""));
341
342 if (pp.equals(ProxyPolicy.USE_SYSTEM_SETTINGS) && ! DefaultProxySelector.willJvmRetrieveSystemProxies()) {
343 Main.warn(tr("JOSM is configured to use proxies from the system setting, but the JVM is not configured to retrieve them. Resetting preferences to ''No proxy''"));
344 pp = ProxyPolicy.NO_PROXY;
345 rbProxyPolicy.get(pp).setSelected(true);
346 }
347
348 // save the proxy user and the proxy password to a credentials store managed by
349 // the credentials manager
350 CredentialsAgent cm = CredentialsManager.getInstance();
351 try {
352 PasswordAuthentication pa = cm.lookup(RequestorType.PROXY, tfProxyHttpHost.getText());
353 if (pa == null) {
354 tfProxyHttpUser.setText("");
355 tfProxyHttpPassword.setText("");
356 } else {
357 tfProxyHttpUser.setText(pa.getUserName() == null ? "" : pa.getUserName());
358 tfProxyHttpPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
359 }
360 } catch(CredentialsAgentException e) {
361 e.printStackTrace();
362 tfProxyHttpUser.setText("");
363 tfProxyHttpPassword.setText("");
364 }
365 }
366
367 protected void updateEnabledState() {
368 boolean isHttpProxy = rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected();
369 for (Component c: pnlHttpProxyConfigurationPanel.getComponents()) {
370 c.setEnabled(isHttpProxy);
371 }
372
373 boolean isSocksProxy = rbProxyPolicy.get(ProxyPolicy.USE_SOCKS_PROXY).isSelected();
374 for (Component c: pnlSocksProxyConfigurationPanel.getComponents()) {
375 c.setEnabled(isSocksProxy);
376 }
377
378 rbProxyPolicy.get(ProxyPolicy.USE_SYSTEM_SETTINGS).setEnabled(DefaultProxySelector.willJvmRetrieveSystemProxies());
379 }
380
381 class ProxyPolicyChangeListener implements ItemListener {
382 @Override
383 public void itemStateChanged(ItemEvent arg0) {
384 updateEnabledState();
385 }
386 }
387
388 /**
389 * Constructs a new {@code ProxyPreferencesPanel}.
390 */
391 public ProxyPreferencesPanel() {
392 setLayout(new GridBagLayout());
393 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
394 add(buildProxySettingsPanel(), GBC.eop().anchor(GridBagConstraints.NORTHWEST).fill(GridBagConstraints.BOTH));
395
396 initFromPreferences();
397 updateEnabledState();
398
399 HelpUtil.setHelpContext(this, HelpUtil.ht("/Preferences/Connection#ProxySettings"));
400 }
401
402 /**
403 * Saves the current values to the preferences
404 */
405 public void saveToPreferences() {
406 ProxyPolicy policy = null;
407 for (ProxyPolicy pp: ProxyPolicy.values()) {
408 if (rbProxyPolicy.get(pp).isSelected()) {
409 policy = pp;
410 break;
411 }
412 }
413 if (policy == null) {
414 policy = ProxyPolicy.NO_PROXY;
415 }
416 Main.pref.put(PROXY_POLICY, policy.getName());
417 Main.pref.put(PROXY_HTTP_HOST, tfProxyHttpHost.getText());
418 Main.pref.put(PROXY_HTTP_PORT, tfProxyHttpPort.getText());
419 Main.pref.put(PROXY_SOCKS_HOST, tfProxySocksHost.getText());
420 Main.pref.put(PROXY_SOCKS_PORT, tfProxySocksPort.getText());
421
422 // update the proxy selector
423 ProxySelector selector = ProxySelector.getDefault();
424 if (selector instanceof DefaultProxySelector) {
425 ((DefaultProxySelector)selector).initFromPreferences();
426 }
427
428 CredentialsAgent cm = CredentialsManager.getInstance();
429 try {
430 PasswordAuthentication pa = new PasswordAuthentication(
431 tfProxyHttpUser.getText().trim(),
432 tfProxyHttpPassword.getPassword()
433 );
434 cm.store(RequestorType.PROXY, tfProxyHttpHost.getText(), pa);
435 } catch(CredentialsAgentException e) {
436 e.printStackTrace();
437 }
438 }
439}
Note: See TracBrowser for help on using the repository browser.