Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/ProxySearch.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy;
2
3 import java.awt.GraphicsEnvironment;
4 import java.net.ProxySelector;
5 import java.text.MessageFormat;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import com.github.markusbernhardt.proxy.search.browser.firefox.FirefoxProxySearchStrategy;
10 import com.github.markusbernhardt.proxy.search.browser.ie.IEProxySearchStrategy;
11 import com.github.markusbernhardt.proxy.search.desktop.DesktopProxySearchStrategy;
12 import com.github.markusbernhardt.proxy.search.desktop.gnome.GnomeDConfProxySearchStrategy;
13 import com.github.markusbernhardt.proxy.search.desktop.gnome.GnomeProxySearchStrategy;
14 import com.github.markusbernhardt.proxy.search.desktop.kde.KdeProxySearchStrategy;
15 import com.github.markusbernhardt.proxy.search.desktop.win.WinProxySearchStrategy;
16 import com.github.markusbernhardt.proxy.search.env.EnvProxySearchStrategy;
17 import com.github.markusbernhardt.proxy.search.java.JavaProxySearchStrategy;
18 import com.github.markusbernhardt.proxy.selector.misc.BufferedProxySelector;
19 import com.github.markusbernhardt.proxy.selector.misc.BufferedProxySelector.CacheScope;
20 import com.github.markusbernhardt.proxy.selector.misc.ProxyListFallbackSelector;
21 import com.github.markusbernhardt.proxy.selector.pac.PacProxySelector;
22 import com.github.markusbernhardt.proxy.util.Logger;
23 import com.github.markusbernhardt.proxy.util.Logger.LogBackEnd;
24 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
25 import com.github.markusbernhardt.proxy.util.PlatformUtil;
26 import com.github.markusbernhardt.proxy.util.ProxyException;
27
28 /*****************************************************************************
29 * Main class to setup and initialize the proxy detection system.<br>
30 * This class can be used to select a proxy discovery strategy.<br>
31 * Implements the "Builder" pattern.<br>
32 * Use <code>addStrategy</code> to add one or more search strategies.<br>
33 * If you are done call the <code>getProxySelector</code> method. <br>
34 * Then the strategies are asked one after the other for a ProxySelector until
35 * an valid selector is found. <br>
36 * <p>
37 * Invoke the static <code>getDefaultProxySearch</code> method to use a default
38 * search strategy.
39 * </p>
40 *
41 * @author Markus Bernhardt, Copyright 2016
42 * @author Bernd Rosstauscher, Copyright 2009
43 ****************************************************************************/
44
45 public class ProxySearch implements ProxySearchStrategy {
46
47 private static final int DEFAULT_PAC_CACHE_SIZE = 20;
48
49 private static final long DEFAULT_PAC_CACHE_TTL = 1000 * 60 * 10; // 10
50 // Minutes
51
52 private static final CacheScope DEFAULT_PAC_CACHE_SCOPE = CacheScope.CACHE_SCOPE_HOST;
53
54 private List<ProxySearchStrategy> strategies;
55 private int pacCacheSize;
56 private long pacCacheTTL;
57 private CacheScope pacCacheScope;
58
59 /*****************************************************************************
60 * Types of proxy detection supported by the builder.
61 ****************************************************************************/
62
63 public enum Strategy {
64 /// Use the platform settings.
65 OS_DEFAULT,
66 /// Use the settings of the platforms default browser.
67 BROWSER,
68 /// Use Firefox settings
69 FIREFOX,
70 /// Use InternetExplorer settings
71 IE,
72 /// Use environment variables for proxy settings.
73 ENV_VAR,
74 /// Use windows default proxy settings.
75 WIN,
76 /// Use KDE desktop default proxy settings.
77 KDE,
78 /// Use KDE desktop default proxy settings.
79 GNOME,
80 /// Use Java Networking system properties
81 JAVA
82 }
83
84 /*************************************************************************
85 * Constructor
86 ************************************************************************/
87
88 public ProxySearch() {
89 super();
90 this.strategies = new ArrayList<ProxySearchStrategy>();
91 this.pacCacheSize = DEFAULT_PAC_CACHE_SIZE;
92 this.pacCacheTTL = DEFAULT_PAC_CACHE_TTL;
93 this.pacCacheScope = DEFAULT_PAC_CACHE_SCOPE;
94 }
95
96 /*************************************************************************
97 * Sets up a ProxySearch that uses a default search strategy suitable for
98 * every platform.
99 *
100 * @return a ProxySearch initialized with default settings.
101 ************************************************************************/
102
103 public static ProxySearch getDefaultProxySearch() {
104 ProxySearch s = new ProxySearch();
105
106 // Test if we are a server or a client.
107 boolean headless = GraphicsEnvironment.isHeadless();
108
109 if (headless) {
110 s.addStrategy(Strategy.JAVA);
111 s.addStrategy(Strategy.OS_DEFAULT);
112 s.addStrategy(Strategy.ENV_VAR);
113 } else {
114 s.addStrategy(Strategy.JAVA);
115 s.addStrategy(Strategy.BROWSER);
116 s.addStrategy(Strategy.OS_DEFAULT);
117 s.addStrategy(Strategy.ENV_VAR);
118 }
119 Logger.log(ProxySearch.class, LogLevel.TRACE, "Using default search priority: {0}", s);
120
121 return s;
122 }
123
124 /*************************************************************************
125 * Adds an search strategy to the list of proxy searches strategies.
126 *
127 * @param strategy
128 * the search strategy to add.
129 ************************************************************************/
130
131 public void addStrategy(Strategy strategy) {
132 switch (strategy) {
133 case OS_DEFAULT:
134 this.strategies.add(new DesktopProxySearchStrategy());
135 break;
136 case BROWSER:
137 this.strategies.add(getDefaultBrowserStrategy());
138 break;
139 case FIREFOX:
140 this.strategies.add(new FirefoxProxySearchStrategy());
141 break;
142 case IE:
143 this.strategies.add(new IEProxySearchStrategy());
144 break;
145 case ENV_VAR:
146 this.strategies.add(new EnvProxySearchStrategy());
147 break;
148 case WIN:
149 this.strategies.add(new WinProxySearchStrategy());
150 break;
151 case KDE:
152 this.strategies.add(new KdeProxySearchStrategy());
153 break;
154 case GNOME:
155 this.strategies.add(new GnomeDConfProxySearchStrategy());
156 this.strategies.add(new GnomeProxySearchStrategy());
157 break;
158 case JAVA:
159 this.strategies.add(new JavaProxySearchStrategy());
160 break;
161 default:
162 throw new IllegalArgumentException("Unknown strategy code!");
163 }
164 }
165
166 /*************************************************************************
167 * Sets the cache size of the PAC proxy selector cache. This defines the
168 * number of URLs that are cached together with the PAC script result. This
169 * improves performance because for URLs that are in the cache the script is
170 * not executed again. You have to set this before you add any strategies
171 * that may create a PAC script proxy selector.
172 *
173 * @param size
174 * of the cache. Set it to 0 to disable caching.
175 * @param ttl
176 * is the time to live of the cache entries as amount of
177 * milliseconds.
178 * @param cacheScope
179 * the desired cache scope.
180 ************************************************************************/
181
182 public void setPacCacheSettings(int size, long ttl, CacheScope cacheScope) {
183 this.pacCacheSize = size;
184 this.pacCacheTTL = ttl;
185 this.pacCacheScope = cacheScope;
186 }
187
188 /*************************************************************************
189 * Gets the search strategy for the platforms default browser.
190 *
191 * @return a ProxySearchStrategy, null if no supported browser was found.
192 ************************************************************************/
193
194 private ProxySearchStrategy getDefaultBrowserStrategy() {
195 switch (PlatformUtil.getDefaultBrowser()) {
196 case IE:
197 return new IEProxySearchStrategy();
198 case FIREFOX:
199 return new FirefoxProxySearchStrategy();
200 }
201 return null;
202 }
203
204 /*************************************************************************
205 * Gets the proxy selector that will use the configured search order.
206 *
207 * @return a ProxySelector, null if none was found for the current builder
208 * configuration.
209 ************************************************************************/
210
211 @Override
212 public ProxySelector getProxySelector() {
213 Logger.log(getClass(), LogLevel.TRACE, "Executing search strategies to find proxy selector");
214 for (ProxySearchStrategy strat : this.strategies) {
215 try {
216 ProxySelector selector = strat.getProxySelector();
217 if (selector != null) {
218 selector = installBufferingAndFallbackBehaviour(selector);
219 Logger.log(getClass(), LogLevel.INFO, "Proxy found for " + strat.getName());
220
221 return selector;
222 } else {
223 Logger.log(getClass(), LogLevel.INFO,
224 "No proxy found for " + strat.getName() + ". Trying next one.");
225 }
226 } catch (ProxyException e) {
227 Logger.log(getClass(), LogLevel.DEBUG, "Strategy {0} failed trying next one.", e);
228 // Ignore and try next strategy.
229 }
230 }
231
232 return null;
233 }
234
235 /*************************************************************************
236 * Gets the printable name of the search strategy.
237 *
238 * @return the printable name of the search strategy
239 ************************************************************************/
240
241 @Override
242 public String getName() {
243 return "default";
244 }
245
246 /*************************************************************************
247 * If it is PAC and we have caching enabled set it here.
248 *
249 * @param selector
250 * the proxy selector to wrap
251 * @return the wrapped proxy selector or the passed in selector if nothing
252 * is done.
253 ************************************************************************/
254
255 private ProxySelector installBufferingAndFallbackBehaviour(ProxySelector selector) {
256 if (selector instanceof PacProxySelector) {
257 if (this.pacCacheSize > 0) {
258 selector = new BufferedProxySelector(this.pacCacheSize, this.pacCacheTTL, selector, pacCacheScope);
259 }
260 selector = new ProxyListFallbackSelector(selector);
261 }
262 return selector;
263 }
264
265 /*************************************************************************
266 * toString
267 *
268 * @see java.lang.Object#toString()
269 ************************************************************************/
270 @Override
271 public String toString() {
272 StringBuilder sb = new StringBuilder("Proxy search: ");
273 for (ProxySearchStrategy strat : this.strategies) {
274 sb.append(strat);
275 sb.append(" ");
276 }
277 return sb.toString();
278 }
279
280 /*************************************************************************
281 * For testing only. Will print the logging &amp; proxy information to the
282 * console.
283 *
284 * @param args
285 * the command line arguments.
286 ************************************************************************/
287
288 public static void main(String[] args) {
289 ProxySearch ps = ProxySearch.getDefaultProxySearch();
290 Logger.setBackend(new LogBackEnd() {
291
292 public void log(Class<?> clazz, LogLevel loglevel, String msg, Object... params) {
293 System.out.println(MessageFormat.format(msg, params));
294 }
295
296 public boolean isLogginEnabled(LogLevel logLevel) {
297 return true;
298 }
299 });
300 ps.getProxySelector();
301 }
302
303 }