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

Last change on this file since 2936 was 2852, checked in by mjulius, 14 years ago

fix messages for io

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