Search in sources :

Example 11 with ProxyConnector

use of org.apache.archiva.admin.model.beans.ProxyConnector in project archiva by apache.

the class AbstractRestService method getArtifactUrl.

protected String getArtifactUrl(Artifact artifact, String repositoryId) throws ArchivaRestServiceException {
    try {
        if (httpServletRequest == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(getBaseUrl());
        sb.append("/repository");
        // we must replace it with a valid managed one available for the user.
        if (StringUtils.isEmpty(repositoryId)) {
            List<String> userRepos = userRepositories.getObservableRepositoryIds(getPrincipal());
            // if not search the repo who is proxy for this remote
            if (!userRepos.contains(artifact.getContext())) {
                for (Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorAdmin.getProxyConnectorAsMap().entrySet()) {
                    for (ProxyConnector proxyConnector : entry.getValue()) {
                        if (StringUtils.equals("remote-" + proxyConnector.getTargetRepoId(), // 
                        artifact.getContext()) && userRepos.contains(entry.getKey())) {
                            sb.append('/').append(entry.getKey());
                        }
                    }
                }
            } else {
                sb.append('/').append(artifact.getContext());
            }
        } else {
            sb.append('/').append(repositoryId);
        }
        sb.append('/').append(StringUtils.replaceChars(artifact.getGroupId(), '.', '/'));
        sb.append('/').append(artifact.getArtifactId());
        if (VersionUtil.isSnapshot(artifact.getVersion())) {
            sb.append('/').append(VersionUtil.getBaseVersion(artifact.getVersion()));
        } else {
            sb.append('/').append(artifact.getVersion());
        }
        sb.append('/').append(artifact.getArtifactId());
        sb.append('-').append(artifact.getVersion());
        if (StringUtils.isNotBlank(artifact.getClassifier())) {
            sb.append('-').append(artifact.getClassifier());
        }
        // maven-plugin packaging is a jar
        if (StringUtils.equals("maven-plugin", artifact.getPackaging())) {
            sb.append("jar");
        } else {
            sb.append('.').append(artifact.getFileExtension());
        }
        return sb.toString();
    } catch (Exception e) {
        throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
    }
}
Also used : ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) List(java.util.List) ArrayList(java.util.ArrayList) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Map(java.util.Map) PropertyMap(org.modelmapper.PropertyMap) HashMap(java.util.HashMap) ArchivaSecurityException(org.apache.archiva.security.ArchivaSecurityException) TaskQueueException(org.apache.archiva.redback.components.taskqueue.TaskQueueException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) PrincipalNotFoundException(org.apache.archiva.security.PrincipalNotFoundException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) RepositoryException(org.apache.archiva.repository.RepositoryException) AccessDeniedException(org.apache.archiva.security.AccessDeniedException)

Example 12 with ProxyConnector

use of org.apache.archiva.admin.model.beans.ProxyConnector in project archiva by apache.

the class DownloadMergedIndexNonDefaultPathTest method downloadMergedIndexWithNonDefaultPath.

@Test
public void downloadMergedIndexWithNonDefaultPath() throws Exception {
    Path indexBaseDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve("archiva").resolve("remotedownloadtest");
    String indexBase = indexBaseDir.toString();
    FileUtils.deleteQuietly(indexBaseDir);
    if (!Files.exists(indexBaseDir)) {
        Files.createDirectories(indexBaseDir);
    }
    String id = Long.toString(System.currentTimeMillis());
    ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
    managedRepository.setId(id);
    managedRepository.setName("name of " + id);
    managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
    managedRepository.setIndexDirectory(indexBase + "/index-" + id);
    managedRepository.setPackedIndexDirectory(indexBase + "/indexPacked-" + id);
    ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
    if (managedRepositoriesService.getManagedRepository(id) != null) {
        managedRepositoriesService.deleteManagedRepository(id, false);
    }
    getManagedRepositoriesService().addManagedRepository(managedRepository);
    RepositoriesService repositoriesService = getRepositoriesService();
    repositoriesService.scanRepositoryNow(id, true);
    // wait a bit to ensure index is finished
    int timeout = 20000;
    while (timeout > 0 && repositoriesService.alreadyScanning(id)) {
        Thread.sleep(500);
        timeout -= 500;
    }
    RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
    String repoGroupId = "test-group";
    if (repositoryGroupService.getRepositoryGroup(repoGroupId) != null) {
        repositoryGroupService.deleteRepositoryGroup(repoGroupId);
    }
    RepositoryGroup repositoryGroup = new RepositoryGroup();
    repositoryGroup.setId(repoGroupId);
    String path = ".fooooo";
    repositoryGroup.setRepositories(Arrays.asList(id));
    repositoryGroup.setMergedIndexPath(path);
    repositoryGroupService.addRepositoryGroup(repositoryGroup);
    // create a repo with a remote on the one with index
    id = Long.toString(System.currentTimeMillis());
    managedRepository = new ManagedRepository(Locale.getDefault());
    managedRepository.setId(id);
    managedRepository.setName("name of " + id);
    managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
    managedRepository.setIndexDirectory(indexBaseDir + "/index-" + id);
    managedRepository.setPackedIndexDirectory(indexBase + "/tmpIndexPacked-" + id);
    if (managedRepositoriesService.getManagedRepository(id) != null) {
        managedRepositoriesService.deleteManagedRepository(id, false);
    }
    getManagedRepositoriesService().addManagedRepository(managedRepository);
    String remoteId = Long.toString(System.currentTimeMillis());
    RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
    remoteRepository.setId(remoteId);
    remoteRepository.setName(remoteId);
    remoteRepository.setDownloadRemoteIndex(true);
    remoteRepository.setUrl("http://localhost:" + port + "/repository/test-group");
    remoteRepository.setRemoteIndexUrl("http://localhost:" + port + "/repository/test-group/" + path);
    remoteRepository.setUserName(RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME);
    remoteRepository.setPassword(FakeCreateAdminService.ADMIN_TEST_PWD);
    getRemoteRepositoriesService().addRemoteRepository(remoteRepository);
    ProxyConnectorService proxyConnectorService = getProxyConnectorService();
    ProxyConnector proxyConnector = new ProxyConnector();
    proxyConnector.setProxyId("foo-bar2");
    proxyConnector.setSourceRepoId(id);
    proxyConnector.setTargetRepoId(remoteId);
    proxyConnectorService.addProxyConnector(proxyConnector);
    repositoriesService.scheduleDownloadRemoteIndex(remoteId, true, true);
    // wait the end
    while (!repositoriesService.getRunningRemoteDownloadIds().getStrings().isEmpty()) {
        Thread.sleep(500);
        log.debug("still running remote download");
    }
    SearchService searchService = getSearchService();
    SearchRequest request = new SearchRequest();
    request.setRepositories(Arrays.asList(id));
    request.setGroupId("org.apache.felix");
    List<Artifact> artifacts = searchService.searchArtifacts(request);
    assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
}
Also used : Path(java.nio.file.Path) SearchRequest(org.apache.archiva.rest.api.model.SearchRequest) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) ProxyConnectorService(org.apache.archiva.rest.api.services.ProxyConnectorService) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) Artifact(org.apache.archiva.maven2.model.Artifact) ManagedRepositoriesService(org.apache.archiva.rest.api.services.ManagedRepositoriesService) RepositoryGroup(org.apache.archiva.admin.model.beans.RepositoryGroup) RepositoriesService(org.apache.archiva.rest.api.services.RepositoriesService) ManagedRepositoriesService(org.apache.archiva.rest.api.services.ManagedRepositoriesService) SearchService(org.apache.archiva.rest.api.services.SearchService) RepositoryGroupService(org.apache.archiva.rest.api.services.RepositoryGroupService) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Test(org.junit.Test)

Example 13 with ProxyConnector

use of org.apache.archiva.admin.model.beans.ProxyConnector in project archiva by apache.

the class DownloadMergedIndexTest method downloadMergedIndex.

@Test
public void downloadMergedIndex() throws Exception {
    Path tmpIndexDir = Paths.get(System.getProperty("java.io.tmpdir"), "tmpIndex");
    if (Files.exists(tmpIndexDir)) {
        FileUtils.deleteDirectory(tmpIndexDir.toFile());
    }
    String id = Long.toString(System.currentTimeMillis());
    ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
    managedRepository.setId(id);
    managedRepository.setName("name of " + id);
    managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
    managedRepository.setIndexDirectory(System.getProperty("java.io.tmpdir") + "/tmpIndex/" + id);
    ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService();
    if (managedRepositoriesService.getManagedRepository(id) != null) {
        managedRepositoriesService.deleteManagedRepository(id, false);
    }
    getManagedRepositoriesService().addManagedRepository(managedRepository);
    RepositoriesService repositoriesService = getRepositoriesService();
    repositoriesService.scanRepositoryNow(id, true);
    // wait a bit to ensure index is finished
    int timeout = 20000;
    while (timeout > 0 && repositoriesService.alreadyScanning(id)) {
        Thread.sleep(500);
        timeout -= 500;
    }
    RepositoryGroupService repositoryGroupService = getRepositoryGroupService();
    String repoGroupId = "test-group";
    if (repositoryGroupService.getRepositoryGroup(repoGroupId) != null) {
        repositoryGroupService.deleteRepositoryGroup(repoGroupId);
    }
    RepositoryGroup repositoryGroup = new RepositoryGroup();
    repositoryGroup.setId(repoGroupId);
    repositoryGroup.setRepositories(Arrays.asList(id));
    repositoryGroupService.addRepositoryGroup(repositoryGroup);
    // create a repo with a remote on the one with index
    id = Long.toString(System.currentTimeMillis());
    managedRepository = new ManagedRepository(Locale.getDefault());
    managedRepository.setId(id);
    managedRepository.setName("name of " + id);
    managedRepository.setLocation(System.getProperty("basedir") + "/src/test/repositories/test-repo");
    managedRepository.setIndexDirectory(System.getProperty("java.io.tmpdir") + "/tmpIndex/" + id);
    if (managedRepositoriesService.getManagedRepository(id) != null) {
        managedRepositoriesService.deleteManagedRepository(id, false);
    }
    getManagedRepositoriesService().addManagedRepository(managedRepository);
    RemoteRepository remoteRepository = new RemoteRepository(Locale.getDefault());
    remoteRepository.setId("all-merged");
    remoteRepository.setName("all-merged");
    remoteRepository.setDownloadRemoteIndex(true);
    remoteRepository.setUrl("http://localhost:" + port + "/repository/test-group");
    remoteRepository.setRemoteIndexUrl("http://localhost:" + port + "/repository/test-group/.index");
    remoteRepository.setUserName(RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME);
    remoteRepository.setPassword(FakeCreateAdminService.ADMIN_TEST_PWD);
    if (getRemoteRepositoriesService().getRemoteRepository(remoteRepository.getId()) != null) {
        getRemoteRepositoriesService().deleteRemoteRepository(remoteRepository.getId());
    }
    getRemoteRepositoriesService().addRemoteRepository(remoteRepository);
    ProxyConnectorService proxyConnectorService = getProxyConnectorService();
    ProxyConnector proxyConnector = new ProxyConnector();
    proxyConnector.setProxyId("foo-bar1");
    proxyConnector.setSourceRepoId(id);
    proxyConnector.setTargetRepoId("all-merged");
    proxyConnectorService.addProxyConnector(proxyConnector);
    repositoriesService.scheduleDownloadRemoteIndex("all-merged", true, true);
    // wait a bit
    timeout = 20000;
    while (timeout > 0) {
        Thread.sleep(500);
        timeout -= 500;
    }
    SearchService searchService = getSearchService();
    SearchRequest request = new SearchRequest();
    request.setRepositories(Arrays.asList(id));
    request.setGroupId("org.apache.felix");
    List<Artifact> artifacts = searchService.searchArtifacts(request);
    assertThat(artifacts).isNotNull().isNotEmpty().hasSize(1);
}
Also used : Path(java.nio.file.Path) SearchRequest(org.apache.archiva.rest.api.model.SearchRequest) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) ProxyConnectorService(org.apache.archiva.rest.api.services.ProxyConnectorService) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) Artifact(org.apache.archiva.maven2.model.Artifact) ManagedRepositoriesService(org.apache.archiva.rest.api.services.ManagedRepositoriesService) RepositoryGroup(org.apache.archiva.admin.model.beans.RepositoryGroup) RepositoriesService(org.apache.archiva.rest.api.services.RepositoriesService) ManagedRepositoriesService(org.apache.archiva.rest.api.services.ManagedRepositoriesService) SearchService(org.apache.archiva.rest.api.services.SearchService) RepositoryGroupService(org.apache.archiva.rest.api.services.RepositoryGroupService) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) Test(org.junit.Test)

Example 14 with ProxyConnector

use of org.apache.archiva.admin.model.beans.ProxyConnector in project archiva by apache.

the class MavenRepositorySearch method getRemoteIndexingContextIds.

@Override
public Set<String> getRemoteIndexingContextIds(String managedRepoId) throws RepositorySearchException {
    Set<String> ids = new HashSet<>();
    List<ProxyConnector> proxyConnectors = null;
    try {
        proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get(managedRepoId);
    } catch (RepositoryAdminException e) {
        throw new RepositorySearchException(e.getMessage(), e);
    }
    if (proxyConnectors == null || proxyConnectors.isEmpty()) {
        return ids;
    }
    for (ProxyConnector proxyConnector : proxyConnectors) {
        String remoteId = "remote-" + proxyConnector.getTargetRepoId();
        RemoteRepository repo = repositoryRegistry.getRemoteRepository(proxyConnector.getTargetRepoId());
        if (repo.getType() == RepositoryType.MAVEN) {
            try {
                IndexingContext context = repo.getIndexingContext() != null ? repo.getIndexingContext().getBaseContext(IndexingContext.class) : null;
                if (context != null && context.isSearchable()) {
                    ids.add(remoteId);
                }
            } catch (UnsupportedBaseContextException e) {
            // Ignore this one
            }
        }
    }
    return ids;
}
Also used : UnsupportedBaseContextException(org.apache.archiva.indexer.UnsupportedBaseContextException) IndexingContext(org.apache.maven.index.context.IndexingContext) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector) RepositorySearchException(org.apache.archiva.indexer.search.RepositorySearchException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) HashSet(java.util.HashSet)

Example 15 with ProxyConnector

use of org.apache.archiva.admin.model.beans.ProxyConnector in project archiva by apache.

the class Maven3DependencyTreeBuilder method buildDependencyTree.

@Override
public void buildDependencyTree(List<String> repositoryIds, String groupId, String artifactId, String version, DependencyVisitor dependencyVisitor) throws DependencyTreeBuilderException {
    Artifact projectArtifact = factory.createProjectArtifact(groupId, artifactId, version);
    ManagedRepository repository = null;
    try {
        repository = findArtifactInRepositories(repositoryIds, projectArtifact);
    } catch (RepositoryAdminException e) {
        // FIXME better exception
        throw new DependencyTreeBuilderException("Cannot build project dependency tree " + e.getMessage(), e);
    }
    if (repository == null) {
        // metadata could not be resolved
        return;
    }
    List<RemoteRepository> remoteRepositories = new ArrayList<>();
    Map<String, NetworkProxy> networkProxies = new HashMap<>();
    try {
        // MRM-1411
        // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be
        // handled there. It doesn't cache anything locally!
        Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
        List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(repository.getId());
        if (proxyConnectors != null) {
            for (ProxyConnector proxyConnector : proxyConnectors) {
                remoteRepositories.add(remoteRepositoryAdmin.getRemoteRepository(proxyConnector.getTargetRepoId()));
                NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy(proxyConnector.getProxyId());
                if (networkProxyConfig != null) {
                    // key/value: remote repo ID/proxy info
                    networkProxies.put(proxyConnector.getTargetRepoId(), networkProxyConfig);
                }
            }
        }
    } catch (RepositoryAdminException e) {
        throw new DependencyTreeBuilderException(e.getMessage(), e);
    }
    // FIXME take care of relative path
    ResolveRequest resolveRequest = new ResolveRequest();
    resolveRequest.dependencyVisitor = dependencyVisitor;
    resolveRequest.localRepoDir = repository.getLocation();
    resolveRequest.groupId = groupId;
    resolveRequest.artifactId = artifactId;
    resolveRequest.version = version;
    resolveRequest.remoteRepositories = remoteRepositories;
    resolveRequest.networkProxies = networkProxies;
    resolve(resolveRequest);
}
Also used : ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RemoteRepository(org.apache.archiva.admin.model.beans.RemoteRepository) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) List(java.util.List) ArrayList(java.util.ArrayList) ProxyConnector(org.apache.archiva.admin.model.beans.ProxyConnector)

Aggregations

ProxyConnector (org.apache.archiva.admin.model.beans.ProxyConnector)20 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 AbstractRepositoryAdminTest (org.apache.archiva.admin.repository.AbstractRepositoryAdminTest)7 RemoteRepository (org.apache.archiva.admin.model.beans.RemoteRepository)6 HashMap (java.util.HashMap)5 List (java.util.List)5 ProxyConnectorRule (org.apache.archiva.admin.model.beans.ProxyConnectorRule)5 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)4 ProxyConnectorConfiguration (org.apache.archiva.configuration.ProxyConnectorConfiguration)4 Path (java.nio.file.Path)3 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)3 ProxyConnectorRuleConfiguration (org.apache.archiva.configuration.ProxyConnectorRuleConfiguration)3 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)2 RepositoryGroup (org.apache.archiva.admin.model.beans.RepositoryGroup)2 Configuration (org.apache.archiva.configuration.Configuration)2 Artifact (org.apache.archiva.maven2.model.Artifact)2 RemoteRepository (org.apache.archiva.repository.RemoteRepository)2 SearchRequest (org.apache.archiva.rest.api.model.SearchRequest)2 ManagedRepositoriesService (org.apache.archiva.rest.api.services.ManagedRepositoriesService)2