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

Last change on this file since 8283 was 7187, checked in by Don-vip, 10 years ago

fix #10079 - typos in messages and javadoc

  • Property svn:eol-style set to native
File size: 8.6 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.Proxy.Type;
10import java.net.ProxySelector;
11import java.net.SocketAddress;
12import java.net.URI;
13import java.util.Arrays;
14import java.util.Collections;
15import java.util.HashSet;
16import java.util.List;
17import java.util.Set;
18import java.util.TreeSet;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
22import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel.ProxyPolicy;
23
24/**
25 * This is the default proxy selector used in JOSM.
26 *
27 */
28public class DefaultProxySelector extends ProxySelector {
29
30 private static final List<Proxy> NO_PROXY_LIST = Collections.singletonList(Proxy.NO_PROXY);
31
32 private static final String IPV4_LOOPBACK = "127.0.0.1";
33 private static final String IPV6_LOOPBACK = "::1";
34
35 /**
36 * The {@link ProxySelector} provided by the JDK will retrieve proxy information
37 * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
38 * is defined <strong>at startup</strong>. It has no effect if the property is set
39 * later by the application.
40 *
41 * We therefore read the property at class loading time and remember it's value.
42 */
43 private static boolean JVM_WILL_USE_SYSTEM_PROXIES = false;
44 static {
45 String v = System.getProperty("java.net.useSystemProxies");
46 if (v != null && v.equals(Boolean.TRUE.toString())) {
47 JVM_WILL_USE_SYSTEM_PROXIES = true;
48 }
49 }
50
51 /**
52 * The {@link ProxySelector} provided by the JDK will retrieve proxy information
53 * from the system settings, if the system property <tt>java.net.useSystemProxies</tt>
54 * is defined <strong>at startup</strong>. If the property is set later by the application,
55 * this has no effect.
56 *
57 * @return true, if <tt>java.net.useSystemProxies</tt> was set to true at class initialization time
58 *
59 */
60 public static boolean willJvmRetrieveSystemProxies() {
61 return JVM_WILL_USE_SYSTEM_PROXIES;
62 }
63
64 private ProxyPolicy proxyPolicy;
65 private InetSocketAddress httpProxySocketAddress;
66 private InetSocketAddress socksProxySocketAddress;
67 private ProxySelector delegate;
68
69 private final Set<String> errorResources = new HashSet<>();
70 private final Set<String> errorMessages = new HashSet<>();
71 private Set<String> proxyExceptions;
72
73 /**
74 * A typical example is:
75 * <pre>
76 * PropertySelector delegate = PropertySelector.getDefault();
77 * PropertySelector.setDefault(new DefaultPropertySelector(delegate));
78 * </pre>
79 *
80 * @param delegate the proxy selector to delegate to if system settings are used. Usually
81 * this is the proxy selector found by ProxySelector.getDefault() before this proxy
82 * selector is installed
83 */
84 public DefaultProxySelector(ProxySelector delegate) {
85 this.delegate = delegate;
86 initFromPreferences();
87 }
88
89 protected int parseProxyPortValue(String property, String value) {
90 if (value == null) return 0;
91 int port = 0;
92 try {
93 port = Integer.parseInt(value);
94 } catch (NumberFormatException e) {
95 Main.error(tr("Unexpected format for port number in preference ''{0}''. Got ''{1}''.", property, value));
96 Main.error(tr("The proxy will not be used."));
97 return 0;
98 }
99 if (port <= 0 || port > 65535) {
100 Main.error(tr("Illegal port number in preference ''{0}''. Got {1}.", property, port));
101 Main.error(tr("The proxy will not be used."));
102 return 0;
103 }
104 return port;
105 }
106
107 /**
108 * Initializes the proxy selector from the setting in the preferences.
109 *
110 */
111 public final void initFromPreferences() {
112 String value = Main.pref.get(ProxyPreferencesPanel.PROXY_POLICY);
113 if (value.length() == 0) {
114 proxyPolicy = ProxyPolicy.NO_PROXY;
115 } else {
116 proxyPolicy = ProxyPolicy.fromName(value);
117 if (proxyPolicy == null) {
118 Main.warn(tr("Unexpected value for preference ''{0}'' found. Got ''{1}''. Will use no proxy.", ProxyPreferencesPanel.PROXY_POLICY, value));
119 proxyPolicy = ProxyPolicy.NO_PROXY;
120 }
121 }
122 String host = Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_HOST, null);
123 int port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_HTTP_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_HTTP_PORT, null));
124 httpProxySocketAddress = null;
125 if (proxyPolicy.equals(ProxyPolicy.USE_HTTP_PROXY)) {
126 if (host != null && !host.trim().isEmpty() && port > 0) {
127 httpProxySocketAddress = new InetSocketAddress(host, port);
128 } else {
129 Main.warn(tr("Unexpected parameters for HTTP proxy. Got host ''{0}'' and port ''{1}''.", host, port));
130 Main.warn(tr("The proxy will not be used."));
131 }
132 }
133
134 host = Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_HOST, null);
135 port = parseProxyPortValue(ProxyPreferencesPanel.PROXY_SOCKS_PORT, Main.pref.get(ProxyPreferencesPanel.PROXY_SOCKS_PORT, null));
136 socksProxySocketAddress = null;
137 if (proxyPolicy.equals(ProxyPolicy.USE_SOCKS_PROXY)) {
138 if (host != null && !host.trim().isEmpty() && port > 0) {
139 socksProxySocketAddress = new InetSocketAddress(host,port);
140 } else {
141 Main.warn(tr("Unexpected parameters for SOCKS proxy. Got host ''{0}'' and port ''{1}''.", host, port));
142 Main.warn(tr("The proxy will not be used."));
143 }
144 }
145 proxyExceptions = new HashSet<>(
146 Main.pref.getCollection(ProxyPreferencesPanel.PROXY_EXCEPTIONS,
147 Arrays.asList(new String[]{"localhost", IPV4_LOOPBACK, IPV6_LOOPBACK}))
148 );
149 }
150
151 @Override
152 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
153 // Just log something. The network stack will also throw an exception which will be caught somewhere else
154 Main.error(tr("Connection to proxy ''{0}'' for URI ''{1}'' failed. Exception was: {2}", sa.toString(), uri.toString(), ioe.toString()));
155 // Remember errors to give a friendly user message asking to review proxy configuration
156 errorResources.add(uri.toString());
157 errorMessages.add(ioe.toString());
158 }
159
160 /**
161 * Returns the set of current proxy resources that failed to be retrieved.
162 * @return the set of current proxy resources that failed to be retrieved
163 * @since 6523
164 */
165 public final Set<String> getErrorResources() {
166 return new TreeSet<>(errorResources);
167 }
168
169 /**
170 * Returns the set of current proxy error messages.
171 * @return the set of current proxy error messages
172 * @since 6523
173 */
174 public final Set<String> getErrorMessages() {
175 return new TreeSet<>(errorMessages);
176 }
177
178 /**
179 * Clear the sets of failed resources and error messages.
180 * @since 6523
181 */
182 public final void clearErrors() {
183 errorResources.clear();
184 errorMessages.clear();
185 }
186
187 /**
188 * Determines if proxy errors have occured.
189 * @return {@code true} if errors have occured, {@code false} otherwise.
190 * @since 6523
191 */
192 public final boolean hasErrors() {
193 return !errorResources.isEmpty();
194 }
195
196 @Override
197 public List<Proxy> select(URI uri) {
198 if (uri != null && proxyExceptions.contains(uri.getHost())) {
199 return NO_PROXY_LIST;
200 }
201 switch(proxyPolicy) {
202 case USE_SYSTEM_SETTINGS:
203 if (!JVM_WILL_USE_SYSTEM_PROXIES) {
204 Main.warn(tr("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."));
205 return NO_PROXY_LIST;
206 }
207 // delegate to the former proxy selector
208 return delegate.select(uri);
209 case NO_PROXY:
210 return NO_PROXY_LIST;
211 case USE_HTTP_PROXY:
212 if (httpProxySocketAddress == null)
213 return NO_PROXY_LIST;
214 return Collections.singletonList(new Proxy(Type.HTTP, httpProxySocketAddress));
215 case USE_SOCKS_PROXY:
216 if (socksProxySocketAddress == null)
217 return NO_PROXY_LIST;
218 return Collections.singletonList(new Proxy(Type.SOCKS, socksProxySocketAddress));
219 }
220 // should not happen
221 return null;
222 }
223}
Note: See TracBrowser for help on using the repository browser.