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

Revision 4196, 7.0 KB checked in by bastiK, 11 months ago (diff)

remove unnecessary warnings when starting with fresh preferences

  • Property svn:eol-style set to native
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.ProxySelector;
10import java.net.SocketAddress;
11import java.net.URI;
12import java.net.Proxy.Type;
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 {@see 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    {
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 {@see 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            System.err.println(tr("Unexpected format for port number in in preference ''{0}''. Got ''{1}''.", property, value));
82            System.err.println(tr("The proxy will not be used."));
83            return 0;
84        }
85        if (port <= 0 || port >  65535) {
86            System.err.println(tr("Illegal port number in preference ''{0}''. Got {1}.", property, port));
87            System.err.println(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                System.err.println(tr("Warning: 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        if (host != null && ! host.trim().equals("") && port > 0) {
111            httpProxySocketAddress = new InetSocketAddress(host,port);
112        } else {
113            httpProxySocketAddress = null;
114            if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
115                System.err.println(tr("Warning: Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''.", host, port));
116                System.err.println(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        if (host != null && ! host.trim().equals("") && port > 0) {
123            socksProxySocketAddress = new InetSocketAddress(host,port);
124        } else {
125            socksProxySocketAddress = null;
126            if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
127                System.err.println(tr("Warning: Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''.", host, port));
128                System.err.println(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
136        // somewhere else
137        //
138        System.out.println(tr("Error: Connection to proxy ''{0}'' for URI ''{1}'' failed. Exception was: {2}", sa.toString(), uri.toString(), ioe.toString()));
139    }
140
141    @Override
142    public List<Proxy> select(URI uri) {
143        Proxy proxy;
144        switch(proxyPolicy) {
145        case USE_SYSTEM_SETTINGS:
146            if (!JVM_WILL_USE_SYSTEM_PROXIES) {
147                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."));
148                return Collections.singletonList(Proxy.NO_PROXY);
149            }
150            // delegate to the former proxy selector
151            List<Proxy> ret = delegate.select(uri);
152            return ret;
153        case NO_PROXY:
154            return Collections.singletonList(Proxy.NO_PROXY);
155        case USE_HTTP_PROXY:
156            if (httpProxySocketAddress == null)
157                return Collections.singletonList(Proxy.NO_PROXY);
158            proxy = new Proxy(Type.HTTP, httpProxySocketAddress);
159            return Collections.singletonList(proxy);
160        case USE_SOCKS_PROXY:
161            if (socksProxySocketAddress == null)
162                return Collections.singletonList(Proxy.NO_PROXY);
163            proxy = new Proxy(Type.SOCKS, socksProxySocketAddress);
164            return Collections.singletonList(proxy);
165        }
166        // should not happen
167        return null;
168    }
169}
Note: See TracBrowser for help on using the repository browser.