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