source: josm/trunk/src/org/openstreetmap/josm/io/DefaultProxySelector.java@ 6361

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

fix #7019 - Don't perform proxy resolution when not needed (slows down startup and preferences save of instances frequently switching between "no proxy" and "manual proxy" if the proxy cannot be resolved)

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.net.InetSocketAddress;
8import java.net.Proxy;
9import java.net.Proxy.Type;
10import java.net.ProxySelector;
11import java.net.SocketAddress;
12import java.net.URI;
13import java.util.Collections;
14import java.util.List;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
18import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel.ProxyPolicy;
19
20/**
21 * This is the default proxy selector used in JOSM.
22 *
23 */
24public class DefaultProxySelector extends ProxySelector {
25 /**
26 * The {@link ProxySelector} provided by the JDK will retrieve proxy information
27 * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
28 * is defined <strong>at startup</strong>. It has no effect if the property is set
29 * later by the application.
30 *
31 * We therefore read the property at class loading time and remember it's value.
32 */
33 private static boolean JVM_WILL_USE_SYSTEM_PROXIES = false;
34 static {
35 String v = System.getProperty("java.net.useSystemProxies");
36 if (v != null && v.equals(Boolean.TRUE.toString())) {
37 JVM_WILL_USE_SYSTEM_PROXIES = true;
38 }
39 }
40
41 /**
42 * The {@link ProxySelector} provided by the JDK will retrieve proxy information
43 * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
44 * is defined <strong>at startup</strong>. If the property is set later by the application,
45 * this has no effect.
46 *
47 * @return true, if <tt>java.net.useSystemProxies</tt> was set to true at class initialization time
48 *
49 */
50 public static boolean willJvmRetrieveSystemProxies() {
51 return JVM_WILL_USE_SYSTEM_PROXIES;
52 }
53
54 private ProxyPolicy proxyPolicy;
55 private InetSocketAddress httpProxySocketAddress;
56 private InetSocketAddress socksProxySocketAddress;
57 private ProxySelector delegate;
58
59 /**
60 * A typical example is:
61 * <pre>
62 * PropertySelector delegate = PropertySelector.getDefault();
63 * PropertySelector.setDefault(new DefaultPropertySelector(delegate));
64 * </pre>
65 *
66 * @param delegate the proxy selector to delegate to if system settings are used. Usually
67 * this is the proxy selector found by ProxySelector.getDefault() before this proxy
68 * selector is installed
69 */
70 public DefaultProxySelector(ProxySelector delegate) {
71 this.delegate = delegate;
72 initFromPreferences();
73 }
74
75 protected int parseProxyPortValue(String property, String value) {
76 if (value == null) return 0;
77 int port = 0;
78 try {
79 port = Integer.parseInt(value);
80 } catch (NumberFormatException e) {
81 Main.error(tr("Unexpected format for port number in in preference ''{0}''. Got ''{1}''.", property, value));
82 Main.error(tr("The proxy will not be used."));
83 return 0;
84 }
85 if (port <= 0 || port > 65535) {
86 Main.error(tr("Illegal port number in preference ''{0}''. Got {1}.", property, port));
87 Main.error(tr("The proxy will not be used."));
88 return 0;
89 }
90 return port;
91 }
92
93 /**
94 * Initializes the proxy selector from the setting in the preferences.
95 *
96 */
97 public void initFromPreferences() {
98 String value = Main.pref.get(ProxyPreferencesPanel.PROXY_POLICY);
99 if (value.length() == 0) {
100 proxyPolicy = ProxyPolicy.NO_PROXY;
101 } else {
102 proxyPolicy = ProxyPolicy.fromName(value);
103 if (proxyPolicy == null) {
104 Main.warn(tr("Unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.", ProxyPreferencesPanel.PROXY_POLICY, value));
105 proxyPolicy = ProxyPolicy.NO_PROXY;
106 }
107 }
108 String host = Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_HOST, null);
109 int port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_HTTP_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_PORT, null));
110 httpProxySocketAddress = null;
111 if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
112 if (host != null && !host.trim().isEmpty() && port > 0) {
113 httpProxySocketAddress = new InetSocketAddress(host, port);
114 } else {
115 Main.warn(tr("Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''.", host, port));
116 Main.warn(tr("The proxy will not be used."));
117 }
118 }
119
120 host = Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_HOST, null);
121 port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_SOCKS_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_PORT, null));
122 socksProxySocketAddress = null;
123 if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
124 if (host != null && !host.trim().isEmpty() && port > 0) {
125 socksProxySocketAddress = new InetSocketAddress(host,port);
126 } else {
127 Main.warn(tr("Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''.", host, port));
128 Main.warn(tr("The proxy will not be used."));
129 }
130 }
131 }
132
133 @Override
134 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
135 // Just log something. The network stack will also throw an exception which will be caught somewhere else
136 //
137 Main.error(tr("Connection to proxy ''{0}'' for URI ''{1}'' failed. Exception was: {2}", sa.toString(), uri.toString(), ioe.toString()));
138 }
139
140 @Override
141 public List<Proxy> select(URI uri) {
142 Proxy proxy;
143 switch(proxyPolicy) {
144 case USE_SYSTEM_SETTINGS:
145 if (!JVM_WILL_USE_SYSTEM_PROXIES) {
146 Main.warn(tr("The JVM is not configured to lookup proxies from the system settings. The property ''java.net.useSystemProxies'' was missing at startup time. Will not use a proxy."));
147 return Collections.singletonList(Proxy.NO_PROXY);
148 }
149 // delegate to the former proxy selector
150 List<Proxy> ret = delegate.select(uri);
151 return ret;
152 case NO_PROXY:
153 return Collections.singletonList(Proxy.NO_PROXY);
154 case USE_HTTP_PROXY:
155 if (httpProxySocketAddress == null)
156 return Collections.singletonList(Proxy.NO_PROXY);
157 proxy = new Proxy(Type.HTTP, httpProxySocketAddress);
158 return Collections.singletonList(proxy);
159 case USE_SOCKS_PROXY:
160 if (socksProxySocketAddress == null)
161 return Collections.singletonList(Proxy.NO_PROXY);
162 proxy = new Proxy(Type.SOCKS, socksProxySocketAddress);
163 return Collections.singletonList(proxy);
164 }
165 // should not happen
166 return null;
167 }
168}
Note: See TracBrowser for help on using the repository browser.