| 1 |
package capsule; |
| 2 |
|
| 3 |
import java.util.Map; |
| 4 |
import java.util.Properties; |
| 5 |
|
| 6 |
import org.eclipse.aether.repository.Proxy; |
| 7 |
import org.eclipse.aether.repository.ProxySelector; |
| 8 |
import org.eclipse.aether.repository.RemoteRepository; |
| 9 |
import org.eclipse.aether.util.repository.AuthenticationBuilder; |
| 10 |
import org.eclipse.aether.util.repository.DefaultProxySelector; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class SystemProxySelector implements ProxySelector { |
| 22 |
private static final int LOG_NONE = 0; |
| 23 |
private static final int LOG_QUIET = 1; |
| 24 |
private static final int LOG_VERBOSE = 2; |
| 25 |
private static final int LOG_DEBUG = 3; |
| 26 |
private static final String LOG_PREFIX = "CAPSULE: "; |
| 27 |
private final int logLevel; |
| 28 |
|
| 29 |
private final DefaultProxySelector target = new DefaultProxySelector(); |
| 30 |
private final Map<String, String> env; |
| 31 |
private final Properties props; |
| 32 |
private int count; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public SystemProxySelector(int logLevel) { |
| 48 |
this(System.getenv(), System.getProperties(), logLevel); |
| 49 |
} |
| 50 |
|
| 51 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
| 52 |
SystemProxySelector(Map env, Properties props, int logLevel) { |
| 53 |
this.env = env; |
| 54 |
this.props = props; |
| 55 |
this.logLevel = logLevel; |
| 56 |
init(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Proxy getProxy(RemoteRepository repository) { |
| 61 |
return target.getProxy(repository); |
| 62 |
} |
| 63 |
|
| 64 |
protected void init() { |
| 65 |
addProxyFromProperties("http"); |
| 66 |
addProxyFromProperties("https"); |
| 67 |
|
| 68 |
addProxyFromEnv("http"); |
| 69 |
addProxyFromEnv("https"); |
| 70 |
|
| 71 |
addProxyFromEnv("HTTP"); |
| 72 |
addProxyFromEnv("HTTPS"); |
| 73 |
} |
| 74 |
|
| 75 |
protected void addProxyFromEnv(String type) { |
| 76 |
checkValidProtocol(type); |
| 77 |
|
| 78 |
String key = type + (isUpper(type) ? "_PROXY" : "_proxy"); |
| 79 |
String[] items = parseProxy(env.get(key), "http".equals(type) ? "80" : "443"); |
| 80 |
if (items == null) |
| 81 |
return; |
| 82 |
|
| 83 |
String[] credentials = parseCredentials(env.get(key)); |
| 84 |
|
| 85 |
String noProxy = env.get(isUpper(type) ? "NO_PROXY" : "no_proxy"); |
| 86 |
|
| 87 |
if (noProxy != null) |
| 88 |
noProxy = noProxy.replace(',', '|'); |
| 89 |
|
| 90 |
Proxy proxy; |
| 91 |
if (credentials != null) { |
| 92 |
AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder(); |
| 93 |
authenticationBuilder.addUsername(credentials[0]); |
| 94 |
if (credentials[1] != null) |
| 95 |
authenticationBuilder.addPassword(credentials[1]); |
| 96 |
|
| 97 |
proxy = new Proxy(type.toLowerCase(), items[0], Integer.parseInt(items[1]), authenticationBuilder.build()); |
| 98 |
} else { |
| 99 |
proxy = new Proxy(type.toLowerCase(), items[0], Integer.parseInt(items[1])); |
| 100 |
} |
| 101 |
target.add(proxy, noProxy); |
| 102 |
count++; |
| 103 |
|
| 104 |
|
| 105 |
if (isLogging(LOG_VERBOSE)) |
| 106 |
log(LOG_VERBOSE, String.format("Adding `%s` proxy: %s [from system environment]", type, proxy)); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
protected void addProxyFromProperties(String type) { |
| 111 |
checkValidProtocol(type); |
| 112 |
|
| 113 |
String host = props.getProperty(type + ".proxyHost"); |
| 114 |
if (host == null || host.isEmpty()) |
| 115 |
return; |
| 116 |
|
| 117 |
String port = props.getProperty(type + ".proxyPort"); |
| 118 |
if (port == null || port.isEmpty()) |
| 119 |
port = "http".equals(type) ? "80" : "443"; |
| 120 |
|
| 121 |
String nonProxy = props.getProperty(type + ".nonProxyHosts"); |
| 122 |
String username = props.getProperty(type + ".proxyUser"); |
| 123 |
String password = props.getProperty(type + ".proxyPassword"); |
| 124 |
|
| 125 |
Proxy proxy; |
| 126 |
if (username != null) { |
| 127 |
AuthenticationBuilder authenticationBuilder = new AuthenticationBuilder(); |
| 128 |
authenticationBuilder.addUsername(username); |
| 129 |
if (password != null) |
| 130 |
authenticationBuilder.addPassword(password); |
| 131 |
|
| 132 |
proxy = new Proxy(type, host, Integer.parseInt(port), authenticationBuilder.build()); |
| 133 |
} else { |
| 134 |
proxy = new Proxy(type, host, Integer.parseInt(port)); |
| 135 |
} |
| 136 |
|
| 137 |
target.add(proxy, nonProxy); |
| 138 |
count++; |
| 139 |
|
| 140 |
|
| 141 |
if (isLogging(LOG_VERBOSE)) |
| 142 |
log(LOG_VERBOSE, String.format("Adding `%s` proxy: %s [from Java system properties]", type, proxy)); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static String[] parseCredentials(String url) { |
| 153 |
String[] result = new String[2]; |
| 154 |
|
| 155 |
if (url == null || url.isEmpty()) |
| 156 |
return null; |
| 157 |
|
| 158 |
int p = url.indexOf("://"); |
| 159 |
if (p != -1) |
| 160 |
url = url.substring(p + 3); |
| 161 |
|
| 162 |
if ((p = url.indexOf('@')) != -1) { |
| 163 |
String credentials = url.substring(0, p); |
| 164 |
|
| 165 |
if ((p = credentials.indexOf(':')) != -1) { |
| 166 |
result[0] = credentials.substring(0, p); |
| 167 |
result[1] = credentials.substring(p + 1); |
| 168 |
} else { |
| 169 |
result[0] = credentials; |
| 170 |
} |
| 171 |
} else { |
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static String[] parseProxy(String url, String defPort) { |
| 186 |
String[] result = new String[2]; |
| 187 |
|
| 188 |
if (url == null || url.isEmpty()) |
| 189 |
return null; |
| 190 |
|
| 191 |
int p = url.indexOf("://"); |
| 192 |
if (p != -1) |
| 193 |
url = url.substring(p + 3); |
| 194 |
|
| 195 |
if ((p = url.indexOf('@')) != -1) |
| 196 |
url = url.substring(p + 1); |
| 197 |
|
| 198 |
if ((p = url.indexOf(':')) != -1) { |
| 199 |
result[0] = url.substring(0, p); |
| 200 |
result[1] = url.substring(p + 1); |
| 201 |
} else { |
| 202 |
result[0] = url; |
| 203 |
result[1] = defPort; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
p = result[0].indexOf("/"); |
| 208 |
if( p != -1 ) { |
| 209 |
result[0] = result[0].substring(0,p); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
p = result[1].indexOf("/"); |
| 214 |
if( p != -1 ) { |
| 215 |
result[1] = result[1].substring(0,p); |
| 216 |
} |
| 217 |
|
| 218 |
return result; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
int getCount() { |
| 225 |
return count; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public boolean isValid() { |
| 232 |
return count > 0; |
| 233 |
} |
| 234 |
|
| 235 |
private boolean isUpper(String str) { |
| 236 |
for (int i = 0; i < str.length(); i++) { |
| 237 |
if (Character.isLowerCase(str.charAt(i))) |
| 238 |
return false; |
| 239 |
} |
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
protected final boolean isLogging(int level) { |
| 244 |
return level <= logLevel; |
| 245 |
} |
| 246 |
|
| 247 |
protected final void log(int level, String str) { |
| 248 |
if (isLogging(level)) |
| 249 |
System.err.println(LOG_PREFIX + str); |
| 250 |
} |
| 251 |
|
| 252 |
protected void checkValidProtocol(String protocol) { |
| 253 |
if ("http".equalsIgnoreCase(protocol)) |
| 254 |
return; |
| 255 |
if ("https".equalsIgnoreCase(protocol)) |
| 256 |
return; |
| 257 |
throw new IllegalArgumentException(String.format("Illegal proxy protocol: '%s'", protocol)); |
| 258 |
} |
| 259 |
} |