Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/env/EnvProxySearchStrategy.java

raw symbols SCIP
1 package com.github.markusbernhardt.proxy.search.env;
2
3 import java.net.ProxySelector;
4 import java.util.Properties;
5
6 import com.github.markusbernhardt.proxy.ProxySearchStrategy;
7 import com.github.markusbernhardt.proxy.selector.misc.ProtocolDispatchSelector;
8 import com.github.markusbernhardt.proxy.selector.whitelist.ProxyBypassListSelector;
9 import com.github.markusbernhardt.proxy.util.Logger;
10 import com.github.markusbernhardt.proxy.util.ProxyUtil;
11 import com.github.markusbernhardt.proxy.util.Logger.LogLevel;
12
13 /*****************************************************************************
14 * Reads some environment variables and extracts the proxy settings from them.
15 * These variables are mainly set on linux / unix environments. The following
16 * variables are read per default:
17 * <ul>
18 * <li><i>http_proxy</i> -&gt; This will be used for http / https</li>
19 * <li><i>https_proxy</i> -&gt; Will be used for https, if not set then
20 * http_proxy is used instead.</li>
21 * <li><i>ftp_proxy</i> -&gt; Used for FTP.</li>
22 * <li><i>no_proxy</i> -&gt; a no proxy white list.</li>
23 * </ul>
24 *
25 * @author Markus Bernhardt, Copyright 2016
26 * @author Bernd Rosstauscher, Copyright 2009
27 ****************************************************************************/
28
29 public class EnvProxySearchStrategy implements ProxySearchStrategy {
30
31 private String httpEnv;
32 private String httpsEnv;
33 private String ftpEnv;
34 private String noProxyEnv;
35
36 private String httpProxy;
37 private String httpsProxy;
38 private String ftpProxy;
39 private String noProxy;
40
41 /*************************************************************************
42 * Constructor Will use the default environment variables.
43 ************************************************************************/
44
45 public EnvProxySearchStrategy() {
46 this("http_proxy", "https_proxy", "ftp_proxy", "no_proxy");
47 }
48
49 /*************************************************************************
50 * Constructor
51 *
52 * @param httpEnv
53 * name of environment variable
54 * @param httpsEnv
55 * name of environment variable
56 * @param ftpEnv
57 * name of environment variable
58 * @param noProxyEnv
59 * name of environment variable
60 ************************************************************************/
61
62 public EnvProxySearchStrategy(String httpEnv, String httpsEnv, String ftpEnv, String noProxyEnv) {
63 super();
64 this.httpEnv = httpEnv;
65 this.httpsEnv = httpsEnv;
66 this.ftpEnv = ftpEnv;
67 this.noProxyEnv = noProxyEnv;
68
69 loadProxySettings();
70 }
71
72 /*************************************************************************
73 * Loads the proxy settings from the system environment variables.
74 ************************************************************************/
75
76 private void loadProxySettings() {
77 this.httpProxy = System.getenv(this.httpEnv);
78 this.httpsProxy = System.getenv(this.httpsEnv);
79 this.ftpProxy = System.getenv(this.ftpEnv);
80 this.noProxy = System.getenv(this.noProxyEnv);
81 }
82
83 /*************************************************************************
84 * Loads the settings and stores them in a properties map.
85 *
86 * @return the settings.
87 ************************************************************************/
88
89 public Properties readSettings() {
90 Properties result = new Properties();
91 result.setProperty(this.httpEnv, this.httpProxy);
92 result.setProperty(this.httpsEnv, this.httpsProxy);
93 result.setProperty(this.ftpEnv, this.ftpProxy);
94 result.setProperty(this.noProxyEnv, this.noProxy);
95 return result;
96 }
97
98 /*************************************************************************
99 * Loads the proxy settings from environment variables.
100 *
101 * @return a configured ProxySelector, null if none is found.
102 ************************************************************************/
103
104 @Override
105 public ProxySelector getProxySelector() {
106
107 Logger.log(getClass(), LogLevel.TRACE, "Inspecting environment variables.");
108
109 // Check if http_proxy var is set.
110 ProxySelector httpPS = ProxyUtil.parseProxySettings(this.httpProxy);
111 if (httpPS == null) {
112 return null;
113 }
114
115 Logger.log(getClass(), LogLevel.TRACE, "Http Proxy is {0}", this.httpProxy);
116 ProtocolDispatchSelector ps = new ProtocolDispatchSelector();
117 ps.setSelector("http", httpPS);
118
119 ProxySelector httpsPS = ProxyUtil.parseProxySettings(this.httpsProxy);
120 Logger.log(getClass(), LogLevel.TRACE, "Https Proxy is {0}", httpsPS == null ? this.httpsProxy : httpsPS);
121 ps.setSelector("https", httpsPS != null ? httpsPS : httpPS);
122
123 ProxySelector ftpPS = ProxyUtil.parseProxySettings(this.ftpProxy);
124 if (ftpPS != null) {
125 Logger.log(getClass(), LogLevel.TRACE, "Ftp Proxy is {0}", this.ftpProxy);
126 ps.setSelector("ftp", ftpPS);
127 }
128
129 // Wrap with white list support
130 ProxySelector result = ps;
131 if (this.noProxy != null && this.noProxy.trim().length() > 0) {
132 Logger.log(getClass(), LogLevel.TRACE, "Using proxy bypass list: {0}", this.noProxy);
133 result = new ProxyBypassListSelector(this.noProxy, ps);
134 }
135
136 return result;
137 }
138
139 /*************************************************************************
140 * Gets the printable name of the search strategy.
141 *
142 * @return the printable name of the search strategy
143 ************************************************************************/
144
145 @Override
146 public String getName() {
147 return "env";
148 }
149
150 }