use of java.net.Proxy in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesACustomRoutePlanner.
@Test
public void usesACustomRoutePlanner() throws Exception {
final HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
return ImmutableList.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.52.1", 8080)));
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
final CloseableHttpClient httpClient = builder.using(configuration).using(routePlanner).createClient(apacheBuilder, connectionManager, "test").getClient();
assertThat(httpClient).isNotNull();
assertThat(spyHttpClientBuilderField("routePlanner", apacheBuilder)).isSameAs(routePlanner);
assertThat(spyHttpClientField("routePlanner", httpClient)).isSameAs(routePlanner);
}
use of java.net.Proxy in project gitblit by gitblit.
the class PluginManager method getProxy.
protected Proxy getProxy(URL url) {
String proxyHost = runtimeManager.getSettings().getString(Keys.plugins.httpProxyHost, "");
String proxyPort = runtimeManager.getSettings().getString(Keys.plugins.httpProxyPort, "");
if (!StringUtils.isEmpty(proxyHost) && !StringUtils.isEmpty(proxyPort)) {
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
} else {
return java.net.Proxy.NO_PROXY;
}
}
use of java.net.Proxy in project gitblit by gitblit.
the class PluginManager method getConnection.
protected URLConnection getConnection(URL url) throws IOException {
java.net.Proxy proxy = getProxy(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
if (java.net.Proxy.Type.DIRECT != proxy.type()) {
String auth = getProxyAuthorization(url);
conn.setRequestProperty("Proxy-Authorization", auth);
}
String username = null;
String password = null;
if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
// set basic authentication header
String auth = Base64.encodeBytes((username + ":" + password).getBytes());
conn.setRequestProperty("Authorization", "Basic " + auth);
}
// configure timeouts
conn.setConnectTimeout(connectTimeout * 1000);
conn.setReadTimeout(readTimeout * 1000);
switch(conn.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_TEMP:
case HttpURLConnection.HTTP_MOVED_PERM:
// handle redirects by closing this connection and opening a new
// one to the new location of the requested resource
String newLocation = conn.getHeaderField("Location");
if (!StringUtils.isEmpty(newLocation)) {
logger.info("following redirect to {0}", newLocation);
conn.disconnect();
return getConnection(new URL(newLocation));
}
}
return conn;
}
use of java.net.Proxy in project buck by facebook.
the class DownloadConfig method getProxy.
public Optional<Proxy> getProxy() {
Optional<String> proxyHost = delegate.getValue("download", "proxy_host");
Optional<Long> proxyPort = delegate.getLong("download", "proxy_port");
String proxyType = delegate.getValue("download", "proxy_type").orElse("HTTP");
LOG.debug("Got proxy: " + proxyHost + " " + proxyPort + " from " + delegate);
if (proxyHost.isPresent() && proxyPort.isPresent()) {
Proxy.Type type = Proxy.Type.valueOf(proxyType);
long port = proxyPort.get();
Proxy p = new Proxy(type, new InetSocketAddress(proxyHost.get(), (int) port));
return Optional.of(p);
}
return Optional.empty();
}
use of java.net.Proxy in project buck by facebook.
the class StackedDownloader method createFromConfig.
public static Downloader createFromConfig(BuckConfig config, Optional<Path> androidSdkRoot) {
ImmutableList.Builder<Downloader> downloaders = ImmutableList.builder();
DownloadConfig downloadConfig = new DownloadConfig(config);
Optional<Proxy> proxy = downloadConfig.getProxy();
HttpDownloader httpDownloader = new HttpDownloader(proxy);
for (Map.Entry<String, String> kv : downloadConfig.getAllMavenRepos().entrySet()) {
String repo = Preconditions.checkNotNull(kv.getValue());
// Check the type.
if (repo.startsWith("http:") || repo.startsWith("https://")) {
String repoName = kv.getKey();
Optional<PasswordAuthentication> credentials = downloadConfig.getRepoCredentials(repoName);
downloaders.add(new RemoteMavenDownloader(httpDownloader, repo, credentials));
} else if (repo.startsWith("file:")) {
try {
URL url = new URL(repo);
Preconditions.checkNotNull(url.getPath());
downloaders.add(new OnDiskMavenDownloader(config.resolvePathThatMayBeOutsideTheProjectFilesystem(Paths.get(url.getPath()))));
} catch (FileNotFoundException e) {
throw new HumanReadableException(e, "Error occurred when attempting to use %s " + "as a local Maven repository as configured in .buckconfig. See " + "https://buckbuild.com/concept/buckconfig.html#maven_repositories for how to " + "configure this setting", repo);
} catch (MalformedURLException e) {
throw new HumanReadableException("Unable to determine path from %s", repo);
}
} else {
try {
downloaders.add(new OnDiskMavenDownloader(Preconditions.checkNotNull(config.resolvePathThatMayBeOutsideTheProjectFilesystem(Paths.get(repo)))));
} catch (FileNotFoundException e) {
throw new HumanReadableException(e, "Error occurred when attempting to use %s " + "as a local Maven repository as configured in .buckconfig. See " + "https://buckbuild.com/concept/buckconfig.html#maven_repositories for how to " + "configure this setting", repo);
}
}
}
if (androidSdkRoot.isPresent()) {
Path androidMavenRepo = androidSdkRoot.get().resolve("extras/android/m2repository");
try {
downloaders.add(new OnDiskMavenDownloader(androidMavenRepo));
} catch (FileNotFoundException e) {
LOG.warn("Android Maven repo %s doesn't exist", androidMavenRepo.toString());
}
Path googleMavenRepo = androidSdkRoot.get().resolve("extras/google/m2repository");
try {
downloaders.add(new OnDiskMavenDownloader(googleMavenRepo));
} catch (FileNotFoundException e) {
LOG.warn("Google Maven repo '%s' doesn't exist", googleMavenRepo.toString());
}
}
// Add a default downloader
// TODO(shs96c): Remove the maven_repo check
Optional<String> defaultMavenRepo = downloadConfig.getMavenRepo();
if (defaultMavenRepo.isPresent()) {
LOG.warn("Please configure maven repos by adding them to a 'maven_repositories' " + "section in your buckconfig");
}
downloaders.add(downloadConfig.getMaxNumberOfRetries().map(retries -> (Downloader) RetryingDownloader.from(httpDownloader, retries)).orElse(httpDownloader));
return new StackedDownloader(downloaders.build());
}
Aggregations