org.eclipse.aether.repository.Proxy Java Examples

The following examples show how to use org.eclipse.aether.repository.Proxy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MavenConfig.java    From galleon with Apache License 2.0 6 votes vote down vote up
public RemoteRepository buildRemoteRepository(MavenRemoteRepository repo, MavenProxySelector selector, Proxy proxy) throws ArtifactException {
    RemoteRepository.Builder builder = new RemoteRepository.Builder(repo.getName(),
            repo.getType(), repo.getUrl());
    builder.setSnapshotPolicy(new org.eclipse.aether.repository.RepositoryPolicy(repo.getEnableSnapshot() == null ? isSnapshotEnabled() : repo.getEnableSnapshot(),
            repo.getSnapshotUpdatePolicy() == null ? getDefaultSnapshotPolicy() : repo.getSnapshotUpdatePolicy(),
            CHECKSUM_POLICY_WARN));
    builder.setReleasePolicy(new org.eclipse.aether.repository.RepositoryPolicy(repo.getEnableRelease() == null ? isReleaseEnabled() : repo.getEnableRelease(),
            repo.getReleaseUpdatePolicy() == null ? getDefaultReleasePolicy() : repo.getReleaseUpdatePolicy(),
            CHECKSUM_POLICY_WARN));
    try {
        if (selector != null && selector.proxyFor(new URL(repo.getUrl()).getHost())) {
            builder.setProxy(proxy);
        }
    } catch (MalformedURLException ex) {
        throw new ArtifactException(ex.getMessage(), ex);
    }
    return builder.build();
}
 
Example #2
Source File: JreProxySelector.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
public Proxy getProxy(final String url) {
  try {
    final java.net.ProxySelector systemSelector = java.net.ProxySelector.getDefault();
    if (systemSelector == null) {
      return null;
    }
    final URI uri = new URI(url).parseServerAuthority();
    final List<java.net.Proxy> selected = systemSelector.select(uri);
    if (selected == null || selected.isEmpty()) {
      return null;
    }
    for (java.net.Proxy proxy : selected) {
      if (proxy.type() == java.net.Proxy.Type.HTTP && isValid(proxy.address())) {
        final String proxyType = chooseProxyType(uri.getScheme());
        if (proxyType != null) {
          final InetSocketAddress addr = (InetSocketAddress)proxy.address();
          return new Proxy(proxyType, addr.getHostName(), addr.getPort(), JreProxyAuthentication.INSTANCE);
        }
      }
    }
  }
  catch (Throwable e) {
    // URL invalid or not accepted by selector or no selector at all, simply use no proxy
  }
  return null;
}
 
Example #3
Source File: MavenArtifactResolvingHelper.java    From wildfly-swarm with Apache License 2.0 6 votes vote down vote up
protected static RemoteRepository buildRemoteRepository(final String id, final String url,
                                                        final Authentication auth, final Proxy proxy) {
    RemoteRepository.Builder builder = new RemoteRepository.Builder(id, "default", url);
    if (auth != null &&
            auth.getUsername() != null &&
            auth.getPassword() != null) {
        builder.setAuthentication(new AuthenticationBuilder()
                                          .addUsername(auth.getUsername())
                                          .addPassword(auth.getPassword()).build());
    }

    if (proxy != null) {
        builder.setProxy(proxy);
    }

    return builder.build();
}
 
Example #4
Source File: AetherStubDownloader.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private List<RemoteRepository> remoteRepositories(
		StubRunnerOptions stubRunnerOptions) {
	if (stubRunnerOptions.stubRepositoryRoot == null) {
		return new ArrayList<>();
	}
	final String[] repos = stubRunnerOptions.getStubRepositoryRootAsString()
			.split(",");
	final List<RemoteRepository> remoteRepos = new ArrayList<>();
	for (int i = 0; i < repos.length; i++) {
		if (StringUtils.hasText(repos[i])) {
			final RemoteRepository.Builder builder = new RemoteRepository.Builder(
					"remote" + i, "default", repos[i]).setAuthentication(
							resolveAuthentication(stubRunnerOptions));
			if (stubRunnerOptions.getProxyOptions() != null) {
				final StubRunnerProxyOptions p = stubRunnerOptions.getProxyOptions();
				builder.setProxy(new Proxy(null, p.getProxyHost(), p.getProxyPort()));
			}
			remoteRepos.add(builder.build());
		}
	}
	if (log.isDebugEnabled()) {
		log.debug("Using the following remote repos " + remoteRepos);
	}
	return remoteRepos;
}
 
Example #5
Source File: BootstrapMavenContext.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a {@link org.apache.maven.settings.Proxy} to a {@link Proxy}.
 *
 * @param proxy Maven proxy settings, may be {@code null}.
 * @return Aether repository proxy or {@code null} if given {@link org.apache.maven.settings.Proxy} is {@code null}.
 */
private static Proxy toAetherProxy(org.apache.maven.settings.Proxy proxy) {
    if (proxy == null) {
        return null;
    }
    Authentication auth = null;
    if (proxy.getUsername() != null) {
        auth = new AuthenticationBuilder()
                .addUsername(proxy.getUsername())
                .addPassword(proxy.getPassword())
                .build();
    }
    return new Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(), auth);
}
 
Example #6
Source File: JreProxySelector.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String chooseProxyType(final String protocol) {
  if (Proxy.TYPE_HTTP.equals(protocol)) {
    return Proxy.TYPE_HTTP;
  }
  if (Proxy.TYPE_HTTPS.equals(protocol)) {
    return Proxy.TYPE_HTTPS;
  }
  return null;
}
 
Example #7
Source File: MavenConfig.java    From galleon with Apache License 2.0 5 votes vote down vote up
public List<RemoteRepository> getMissingDefaultRepositories(Set<String> configuredRepos, MavenProxySelector selector, Proxy proxy) throws ArtifactException {
    List<RemoteRepository> lst = new ArrayList<>();
    if (!configuredRepos.contains(CENTRAL_REPO_URL)) {
        lst.add(buildRemoteRepository(CENTRAL_REPO, selector, proxy));
    }
    if (!configuredRepos.contains(JBOSS_REPO_URL)) {
        lst.add(buildRemoteRepository(JBOSS_REPO, selector, proxy));
    }
    if (!configuredRepos.contains(GA_REPO_URL)) {
        lst.add(buildRemoteRepository(GA_REPO, selector, proxy));
    }
    return lst;
}
 
Example #8
Source File: Helper.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static Proxy getProxy ( final String url )
{
    final ProxySelector ps = ProxySelector.getDefault ();
    if ( ps == null )
    {
        logger.debug ( "No proxy selector found" );
        return null;
    }

    final List<java.net.Proxy> proxies = ps.select ( URI.create ( url ) );
    for ( final java.net.Proxy proxy : proxies )
    {
        if ( proxy.type () != Type.HTTP )
        {
            logger.debug ( "Unsupported proxy type: {}", proxy.type () );
            continue;
        }

        final SocketAddress addr = proxy.address ();
        logger.debug ( "Proxy address: {}", addr );

        if ( ! ( addr instanceof InetSocketAddress ) )
        {
            logger.debug ( "Unsupported proxy address type: {}", addr.getClass () );
            continue;
        }

        final InetSocketAddress inetAddr = (InetSocketAddress)addr;

        return new Proxy ( Proxy.TYPE_HTTP, inetAddr.getHostString (), inetAddr.getPort () );
    }

    logger.debug ( "No proxy found" );
    return null;
}
 
Example #9
Source File: CompositeProxySelector.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
public Proxy getProxy(RemoteRepository repository) {
	for (ProxySelector selector : this.selectors) {
		Proxy proxy = selector.getProxy(repository);
		if (proxy != null) {
			return proxy;
		}
	}
	return null;
}
 
Example #10
Source File: MavenArtifactResolvingHelper.java    From thorntail with Apache License 2.0 5 votes vote down vote up
private RemoteRepository buildRemoteRepository(final String id, final String url, final Authentication auth,
                                               final ArtifactRepositoryPolicy releasesPolicy, final ArtifactRepositoryPolicy snapshotsPolicy) {
    RemoteRepository.Builder builder = new RemoteRepository.Builder(id, "default", url);
    if (auth != null
            && auth.getUsername() != null
            && auth.getPassword() != null) {
        builder.setAuthentication(new AuthenticationBuilder()
                                          .addUsername(auth.getUsername())
                                          .addPassword(auth.getPassword()).build());
    }

    builder.setSnapshotPolicy(new RepositoryPolicy(snapshotsPolicy.isEnabled(), snapshotsPolicy.getUpdatePolicy(), snapshotsPolicy.getChecksumPolicy()));
    builder.setReleasePolicy(new RepositoryPolicy(releasesPolicy.isEnabled(), releasesPolicy.getUpdatePolicy(), releasesPolicy.getChecksumPolicy()));

    RemoteRepository repository = builder.build();

    final RemoteRepository mirror = session.getMirrorSelector().getMirror(repository);

    if (mirror != null) {
        final org.eclipse.aether.repository.Authentication mirrorAuth = session.getAuthenticationSelector()
                .getAuthentication(mirror);
        RemoteRepository.Builder mirrorBuilder = new RemoteRepository.Builder(mirror)
                .setId(repository.getId())
                .setSnapshotPolicy(new RepositoryPolicy(snapshotsPolicy.isEnabled(), snapshotsPolicy.getUpdatePolicy(), snapshotsPolicy.getChecksumPolicy()))
                .setReleasePolicy(new RepositoryPolicy(releasesPolicy.isEnabled(), releasesPolicy.getUpdatePolicy(), releasesPolicy.getChecksumPolicy()));
        if (mirrorAuth != null) {
            mirrorBuilder.setAuthentication(mirrorAuth);
        }
        repository = mirrorBuilder.build();
    }

    Proxy proxy = session.getProxySelector().getProxy(repository);

    if (proxy != null) {
        repository = new RemoteRepository.Builder(repository).setProxy(proxy).build();
    }

    return repository;
}
 
Example #11
Source File: MavenInitializer.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public static RemoteRepository buildRemoteRepository(final RepositorySystemSession session,
                                                     final String id, final String url, final String username, final String password) {
    RemoteRepository.Builder builder = new RemoteRepository.Builder(id, "default", url);
    if (username != null) {
        builder.setAuthentication(new AuthenticationBuilder()
                .addUsername(username)
                .addPassword(password).build());
    }

    RemoteRepository repository = builder.build();

    final RemoteRepository mirror = session.getMirrorSelector().getMirror(repository);

    if (mirror != null) {
        final org.eclipse.aether.repository.Authentication mirrorAuth = session.getAuthenticationSelector()
                .getAuthentication(mirror);
        RemoteRepository.Builder mirrorBuilder = new RemoteRepository.Builder(mirror)
                .setId(repository.getId());
        if (mirrorAuth != null) {
            mirrorBuilder.setAuthentication(mirrorAuth);
        }
        repository = mirrorBuilder.build();
    }

    Proxy proxy = session.getProxySelector().getProxy(repository);

    if (proxy != null) {
        repository = new RemoteRepository.Builder(repository).setProxy(proxy).build();
    }

    return repository;
}
 
Example #12
Source File: MavenArtifactResolver.java    From spring-cloud-deployer with Apache License 2.0 5 votes vote down vote up
private DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system, String localRepoPath) {
	DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
	LocalRepository localRepo = new LocalRepository(localRepoPath);
	session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
	session.setOffline(this.properties.isOffline());
	session.setUpdatePolicy(this.properties.getUpdatePolicy());
	session.setChecksumPolicy(this.properties.getChecksumPolicy());
	if (this.properties.isEnableRepositoryListener()) {
		session.setRepositoryListener(new LoggingRepositoryListener());
	}
	if (this.properties.getConnectTimeout() != null) {
		session.setConfigProperty(ConfigurationProperties.CONNECT_TIMEOUT, this.properties.getConnectTimeout());
	}
	if (this.properties.getRequestTimeout() != null) {
		session.setConfigProperty(ConfigurationProperties.REQUEST_TIMEOUT, this.properties.getRequestTimeout());
	}
	if (isProxyEnabled()) {
		DefaultProxySelector proxySelector = new DefaultProxySelector();
		Proxy proxy = new Proxy(this.properties.getProxy().getProtocol(),
				this.properties.getProxy().getHost(),
				this.properties.getProxy().getPort(),
				this.proxyAuthentication);
		proxySelector.add(proxy, this.properties.getProxy().getNonProxyHosts());
		session.setProxySelector(proxySelector);
	}
	// wagon configs
	for (Entry<String, MavenProperties.RemoteRepository> entry : this.properties.getRemoteRepositories().entrySet()) {
		session.setConfigProperty("aether.connector.wagon.config." + entry.getKey(), entry.getValue().getWagon());
	}
	return session;
}
 
Example #13
Source File: MavenArtifactResolvingHelper.java    From wildfly-swarm with Apache License 2.0 5 votes vote down vote up
public MavenArtifactResolvingHelper(ArtifactResolver resolver,
                                    RepositorySystem system,
                                    RepositorySystemSession session,
                                    Proxy proxy) {
    this.resolver = resolver;
    this.system = system;
    this.session = session;
    this.proxy = proxy;
    this.remoteRepositories.add(buildRemoteRepository("jboss-public-repository-group",
                                                      "http://repository.jboss.org/nexus/content/groups/public/",
                                                      null,
                                                      this.proxy));
}
 
Example #14
Source File: MvnSettingsTestCase.java    From galleon with Apache License 2.0 5 votes vote down vote up
@Test
public void testProxy() throws Exception {
    RepositorySystem system = Util.newRepositorySystem();
    MavenConfig config = new MavenConfig();
    InputStream stream = MvnSettingsTestCase.class.getClassLoader().
            getResourceAsStream("settings_cli_test_proxy.xml");
    File tmp = File.createTempFile("cli_mvn_test", null);
    tmp.deleteOnExit();
    Files.copy(stream, tmp.toPath(), StandardCopyOption.REPLACE_EXISTING);
    config.setSettings(tmp.toPath());
    MavenMvnSettings settings = new MavenMvnSettings(config, system, null);
    ProxySelector proxy = settings.getSession().getProxySelector();
    assertEquals(6, settings.getRepositories().size());
    for (RemoteRepository remote : settings.getRepositories()) {
        if (remote.getId().equals("repo1")) {
            assertNull(proxy.getProxy(remote));
            assertNull(remote.getProxy());
        }
        if (remote.getId().equals("repo2")) {
            assertNull(proxy.getProxy(remote));
            assertNull(remote.getProxy());
        }
        if (remote.getId().equals("repo3")) {
            Proxy p = proxy.getProxy(remote);
            assertEquals("proxy1", p.getHost());
            assertNotNull(remote.getProxy());
        }
    }
}
 
Example #15
Source File: GithubImporter.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
private static RepositorySystemSession newSession(RepositorySystem system) {
	DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
	LocalRepository localRepo = new LocalRepository("target/local-repo");
	session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
	// set possible proxies and mirrors
	session.setProxySelector(new DefaultProxySelector().add(new Proxy(Proxy.TYPE_HTTP, "host", 3625),
			Arrays.asList("localhost", "127.0.0.1")));
	session.setMirrorSelector(
			new DefaultMirrorSelector().add("my-mirror", "http://mirror", "default", false, "external:*", null));
	return session;
}
 
Example #16
Source File: Utils.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static Proxy getProxy(RemoteRepository repo, ProxySelector ... selectors) {
  Proxy proxy = null;
  
  for (ProxySelector selector : selectors) {
    if (selector != null) {
      proxy = selector.getProxy(repo);
    }
    
    if (proxy != null) return proxy;
  }
  
  return proxy;
}
 
Example #17
Source File: ChainedProxySelector.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Proxy getProxy(RemoteRepository repo) {
  Proxy proxy = null;
  
  for (ProxySelector selector : selectors) {
    if (selector != null) {
      proxy = selector.getProxy(repo);
    }
    
    if (proxy != null) return proxy;
  }
  
  return proxy;
}
 
Example #18
Source File: CompositeProxySelector.java    From spring-init with Apache License 2.0 5 votes vote down vote up
@Override
public Proxy getProxy(RemoteRepository repository) {
	for (ProxySelector selector : this.selectors) {
		Proxy proxy = selector.getProxy(repository);
		if (proxy != null) {
			return proxy;
		}
	}
	return null;
}
 
Example #19
Source File: MavenProxySelector.java    From galleon with Apache License 2.0 4 votes vote down vote up
@Override
public Proxy getProxy(RemoteRepository repo) {
    return proxyFor(repo.getHost()) ? proxy : null;
}
 
Example #20
Source File: Utils.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static DefaultRepositorySystemSession getRepositorySession(RepositorySystem repoSystem, WorkspaceReader workspace) {
  
  DefaultRepositorySystemSession repoSystemSession = MavenRepositorySystemUtils.newSession();
  
  String repoLocation = System.getProperty("user.home") + File.separator
          + ".m2" + File.separator + "repository/";
  ChainedProxySelector proxySelector = new ChainedProxySelector();
  try {
    Settings effectiveSettings = loadMavenSettings();
    if(effectiveSettings.getLocalRepository() != null) {
      repoLocation = effectiveSettings.getLocalRepository();
    }

    List<Mirror> mirrors = effectiveSettings.getMirrors();
    if(!mirrors.isEmpty()) {
      DefaultMirrorSelector mirrorSelector = new DefaultMirrorSelector();
      for (Mirror mirror : mirrors) mirrorSelector.add(
              String.valueOf(mirror.getId()), mirror.getUrl(), mirror.getLayout(), false,
              mirror.getMirrorOf(), mirror.getMirrorOfLayouts());
      repoSystemSession.setMirrorSelector(mirrorSelector);
    }

    List<Server> servers = effectiveSettings.getServers();
    if(!servers.isEmpty()) {
      DefaultAuthenticationSelector selector = new DefaultAuthenticationSelector();
      for (Server server : servers) {
        AuthenticationBuilder auth = new AuthenticationBuilder();
        auth.addUsername(server.getUsername()).addPassword(PASSWORD_DECRYPTER.decrypt(server.getPassword()));
        auth.addPrivateKey(server.getPrivateKey(), PASSWORD_DECRYPTER.decrypt(server.getPassphrase()));
        selector.add(server.getId(), auth.build());
      }
      repoSystemSession.setAuthenticationSelector(new ConservativeAuthenticationSelector(selector));
    }

    // extract any proxies configured in the settings - we need to pass these
    // on so that any repositories declared in a dependency POM file can be
    // accessed through the proxy too.
    List<org.apache.maven.settings.Proxy> proxies =
        effectiveSettings.getProxies().stream().filter((p) -> p.isActive())
            .collect(Collectors.toList());
    
    if(!proxies.isEmpty()) {
      DefaultProxySelector defaultSelector = new DefaultProxySelector();
      for (org.apache.maven.settings.Proxy proxy : proxies) {
        defaultSelector.add(
            new Proxy(proxy.getProtocol(), proxy.getHost(), proxy.getPort(),
                new AuthenticationBuilder().addUsername(proxy.getUsername())
                    .addPassword(PASSWORD_DECRYPTER.decrypt(proxy.getPassword())).build()),
            proxy.getNonProxyHosts());
      }

      proxySelector.addSelector(defaultSelector);
    }

    // pass through the "offline" setting
    repoSystemSession.setOffline(effectiveSettings.isOffline());
  } catch(SettingsBuildingException | SecDispatcherException | RuntimeException e) {
    log.warn(
            "Unable to load Maven settings, using default repository location, and no mirrors, proxy or authentication settings.",
            e);
  }

  LocalRepository localRepo = new LocalRepository(repoLocation);
  log.debug("Using local repository at: " + repoLocation);
  repoSystemSession.setLocalRepositoryManager(repoSystem
          .newLocalRepositoryManager(repoSystemSession, localRepo));
  
  //repoSystemSession.setWorkspaceReader(new SimpleMavenCache(new File("repo")));      
  if (workspace != null) repoSystemSession.setWorkspaceReader(workspace);

  // try JRE proxies after any configured in settings
  proxySelector.addSelector(new JreProxySelector());

  // set proxy selector for any repositories discovered in dependency poms
  repoSystemSession.setProxySelector(proxySelector);

  return repoSystemSession;
}
 
Example #21
Source File: JreProxySelector.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Proxy getProxy(RemoteRepository repository) {
  return getProxy(repository.getUrl());
}