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

Last change on this file since 3122 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 7.4 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 @SuppressWarnings("unused")
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.
50 *
51 * @return true, if <tt>java.net.useSystemProxies</tt> was set to true at class initialization time
52 *
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>
69 *
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);
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."));
87 return 0;
88 }
89 if (port <= 0 || port > 65535) {
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."));
92 return 0;
93 }
94 return port;
95 }
96
97 /**
98 * Initializes the proxy selector from the setting in the preferences.
99 *
100 */
101 public void initFromPreferences() {
102 String value = Main.pref.get(ProxyPreferencesPanel.PROXY_POLICY);
103 if (value.length() == 0) {
104 System.err.println(tr("Warning: no preference ''{0}'' found.", ProxyPreferencesPanel.PROXY_POLICY));
105 System.err.println(tr("The proxy will not be used."));
106 proxyPolicy = ProxyPolicy.NO_PROXY;
107 } else {
108 proxyPolicy= ProxyPolicy.fromName(value);
109 if (proxyPolicy == null) {
110 System.err.println(tr("Warning: unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.", ProxyPreferencesPanel.PROXY_POLICY, value));
111 proxyPolicy = ProxyPolicy.NO_PROXY;
112 }
113 }
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));
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)) {
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."));
123 }
124 }
125
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));
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)) {
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."));
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) {
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."));
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}
Note: See TracBrowser for help on using the repository browser.