Search in sources :

Example 76 with Proxy

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);
}
Also used : ProxySelector(java.net.ProxySelector) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Proxy(java.net.Proxy) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) InetSocketAddress(java.net.InetSocketAddress) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) Test(org.junit.Test)

Example 77 with Proxy

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;
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress)

Example 78 with 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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Proxy(java.net.Proxy) URL(java.net.URL)

Example 79 with Proxy

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();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress)

Example 80 with Proxy

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());
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) ImmutableList(com.google.common.collect.ImmutableList) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) Proxy(java.net.Proxy) DownloadConfig(com.facebook.buck.cli.DownloadConfig) HumanReadableException(com.facebook.buck.util.HumanReadableException) Map(java.util.Map) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Proxy (java.net.Proxy)132 InetSocketAddress (java.net.InetSocketAddress)71 URL (java.net.URL)44 IOException (java.io.IOException)35 Test (org.junit.Test)22 URI (java.net.URI)21 ProxySelector (java.net.ProxySelector)20 HttpURLConnection (java.net.HttpURLConnection)19 SocketAddress (java.net.SocketAddress)19 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)13 URISyntaxException (java.net.URISyntaxException)11 ServerSocket (java.net.ServerSocket)10 Socket (java.net.Socket)9 PasswordAuthentication (java.net.PasswordAuthentication)8 InterruptedIOException (java.io.InterruptedIOException)6 URLConnection (java.net.URLConnection)6 SSLServerSocket (javax.net.ssl.SSLServerSocket)6 List (java.util.List)5 SSLSocket (javax.net.ssl.SSLSocket)5 OkHttpClient (okhttp3.OkHttpClient)5