Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit proxy configuration in code

Tags:

java

proxy

jgit

I am able to clone repo using clone command in JGit

Repo is http and of course it's failing to clone when I am behind proxy

Could you help me with code sample how to configure JGit with proxy in java

thanks!

like image 247
Alan Harper Avatar asked Jun 04 '13 15:06

Alan Harper


1 Answers

JGit uses the standard ProxySelector mechanism for the Http connection. As of Today, the field org.eclipse.jgit.transport.TransportHttp.proxySelector used by the framework, is not overridable. It is configurable, though, customizing the JVM default proxy selector as in:

ProxySelector.setDefault(new ProxySelector() {
    final ProxySelector delegate = ProxySelector.getDefault();

    @Override
    public List<Proxy> select(URI uri) {
            // Filter the URIs to be proxied
        if (uri.toString().contains("github")
                && uri.toString().contains("https")) {
            return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress
                    .createUnresolved("localhost", 3128)));
        }
        if (uri.toString().contains("github")
                && uri.toString().contains("http")) {
            return Arrays.asList(new Proxy(Type.HTTP, InetSocketAddress
                    .createUnresolved("localhost", 3129)));
        }
            // revert to the default behaviour
        return delegate == null ? Arrays.asList(Proxy.NO_PROXY)
                : delegate.select(uri);
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        if (uri == null || sa == null || ioe == null) {
            throw new IllegalArgumentException(
                    "Arguments can't be null.");
        }
    }
});
like image 124
Carlo Pellegrini Avatar answered Oct 05 '22 06:10

Carlo Pellegrini