2013-06-04 17 views
5

jestem w stanie sklonować repo poleceniem klon w JGitJGit konfiguracja proxy w kodzie

Repo jest http i oczywiście to braku sklonować gdy jestem za proxy

mógłbyś mi pomóc z próbki kodu how skonfigurować JGit z proxy w java

dziękuję!

+2

Czy próbowałeś już korzystać z klasycznego sposobu ustawiania proxy HTTP na poziomie JVM? – fge

+0

jako wzmianka w pytaniu, muszę ustawić w kodzie ... –

+1

Pytanie naprawdę było, jeśli zadziałało, jeśli używałeś standardowego sposobu. Twoje pytanie tak naprawdę nie mówi. – fge

Odpowiedz

8

JGit używa standardowego mechanizmu ProxySelector dla połączenia HTTP. Od dzisiaj pole org.eclipse.jgit.transport.TransportHttp.proxySelector używane przez framework nie jest możliwe do zastąpienia. To jest konfigurowalny, choć dostosowywania selektor domyślne proxy JVM jak w:

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."); 
     } 
    } 
}); 
1

W dopełnieniem Carlo Pellegrini odpowiedź, jeśli serwer proxy wymaga uwierzytelniania, należy skonfigurować Authenticator, jak (na podstawie Authenticated HTTP proxy with Java pytanie):

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() {...});