6

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!

3
  • 2
    Have you tried using the classical way of setting an HTTP proxy at the JVM level? Commented Jun 4, 2013 at 15:16
  • as mention in the question, I need to set it up in code... Commented Jun 4, 2013 at 15:30
  • 1
    The question really was if it worked if you used the standard way. Your question does not really tell. Commented Jun 4, 2013 at 15:36

2 Answers 2

12

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.");
        }
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Are you using localhost as a placeholder where we should configure a value for the proxy address, or is it localhost?
Yes, substitute localhost/3128 with the host and port of your proxy
3

In complement of Carlo Pellegrini answer, if your proxy requires some authentication, you should configure an Authenticator, like (based on Authenticated HTTP proxy with Java question):

    Authenticator.setDefault(new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            // If proxy is non authenticated for some URLs, the requested URL is the endpoint (and not the proxy host)
            // In this case the authentication should not be the one of proxy ... so return null (and JGit CredentialsProvider will be used)
            if (super.getRequestingHost().equals("localhost")) {
                return new PasswordAuthentication("foo", "bar".toCharArray());
            }
            return null;
        }
    });

    ProxySelector.setDefault(new ProxySelector() {...});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.