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