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

Last change on this file since 2641 was 2641, checked in by Gubaer, 16 years ago

new: supports system defined proxies if JOSM is started with -Djava.net.useSystemProxies=true
fixed #1641: JOSM doesn't allow for setting HTTP proxy user/password distrinct from OSM server user/password
fixed #2865: SOCKS Proxy Support
fixed #4182: Proxy Authentication

File size: 7.2 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.ProxyPreferences;
19import org.openstreetmap.josm.gui.preferences.ProxyPreferences.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}''. Proxy won't be used.", property, value));
85 return 0;
86 }
87 if (port <= 0 || port > 65535) {
88 System.err.println(tr("Illegal port number in preference ''{0}''. Got {1}. Proxy won't be used.", property, port));
89 return 0;
90 }
91 return port;
92 }
93
94 /**
95 * Initializes the proxy selector from the setting in the preferences.
96 *
97 */
98 public void initFromPreferences() {
99 String value = Main.pref.get(ProxyPreferences.PROXY_POLICY);
100 if (value == null) {
101 System.err.println(tr("Warning: no preference ''{0}'' found. Will use no proxy.", ProxyPreferences.PROXY_POLICY));
102 proxyPolicy = ProxyPolicy.NO_PROXY;
103 } else {
104 proxyPolicy= ProxyPolicy.fromName(value);
105 if (proxyPolicy == null) {
106 System.err.println(tr("Warning: unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.", ProxyPreferences.PROXY_POLICY, value));
107 proxyPolicy = ProxyPolicy.NO_PROXY;
108 }
109 }
110 String host = Main.pref.get(ProxyPreferences.PROXY_HTTP_HOST, null);
111 int port = parseProxyPortValue(ProxyPreferences.PROXY_HTTP_PORT, Main.pref.get(ProxyPreferences.PROXY_HTTP_PORT, null));
112 if (host != null && ! host.trim().equals("") && port > 0) {
113 httpProxySocketAddress = new InetSocketAddress(host,port);
114 } else {
115 httpProxySocketAddress = null;
116 if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
117 System.err.println(tr("Warning: Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''. Proxy won't be used", host, port));
118 }
119 }
120
121 host = Main.pref.get(ProxyPreferences.PROXY_SOCKS_HOST, null);
122 port = parseProxyPortValue(ProxyPreferences.PROXY_SOCKS_PORT, Main.pref.get(ProxyPreferences.PROXY_SOCKS_PORT, null));
123 if (host != null && ! host.trim().equals("") && port > 0) {
124 socksProxySocketAddress = new InetSocketAddress(host,port);
125 } else {
126 socksProxySocketAddress = null;
127 if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
128 System.err.println(tr("Warning: Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''. Proxy won't be used", host, port));
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. Won't 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.