Search in sources :

Example 1 with RepositoryRegistry

use of org.apache.archiva.repository.RepositoryRegistry in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurgeTest method testReleasedSnapshotsExistsInDifferentRepo.

@Test
public void testReleasedSnapshotsExistsInDifferentRepo() throws Exception {
    RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
    ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(TEST_REPO_ID);
    repositoryRegistry.removeRepository(TEST_REPO_ID);
    repositoryRegistry.putRepository(getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME));
    repositoryRegistry.putRepository(getRepoConfiguration(RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME));
    String repoRoot = prepareTestRepos();
    String projectNs = "org.apache.archiva";
    String projectPath = projectNs.replaceAll("\\.", "/");
    String projectName = "released-artifact-in-diff-repo";
    String projectVersion = "1.0-SNAPSHOT";
    String releaseVersion = "1.0";
    String projectRoot = repoRoot + "/" + projectPath + "/" + projectName;
    Path repo = getTestRepoRootPath();
    Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
    Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
    Set<String> deletedVersions = new HashSet<>();
    deletedVersions.add("1.0-SNAPSHOT");
    // test listeners for the correct artifacts
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.archiva", "released-artifact-in-diff-repo", "1.0-SNAPSHOT", "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar");
    listenerControl.replay();
    // Provide the metadata list
    List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
    when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
    List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID, projectName, repo.getParent(), releaseDir);
    when(metadataRepository.getArtifacts(RELEASES_TEST_REPO_ID, projectNs, projectName, releaseVersion)).thenReturn(ml2);
    repoPurge.process(PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO);
    listenerControl.verify();
    // Verify the metadataRepository invocations
    // Complete version removal for cleanup
    verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
    verify(metadataRepository, never()).removeProjectVersion(eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion));
    // check if the snapshot was removed
    assertDeleted(projectRoot + "/1.0-SNAPSHOT");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5");
    assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1");
    String releasesProjectRoot = AbstractRepositoryPurgeTest.fixPath(Paths.get("target/test-" + getName() + "/releases-test-repo-one").toAbsolutePath().toString() + "/org/apache/archiva/released-artifact-in-diff-repo");
    // check if the released version was not removed
    assertExists(releasesProjectRoot + "/1.0");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5");
    assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1");
    // remove RELEASES_TEST_REPO_ID so this test will be more independant
    applicationContext.getBean(ManagedRepositoryAdmin.class).deleteManagedRepository(RELEASES_TEST_REPO_ID, null, false);
}
Also used : Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.repository.ManagedRepository) ManagedRepositoryAdmin(org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RepositoryRegistry

use of org.apache.archiva.repository.RepositoryRegistry in project archiva by apache.

the class CleanupReleasedSnapshotsRepositoryPurgeTest method testReleasedSnapshotsExistsInSameRepo.

@Test
public void testReleasedSnapshotsExistsInSameRepo() throws Exception {
    RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
    repositoryRegistry.removeRepository(TEST_REPO_ID);
    repositoryRegistry.putRepository(getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME));
    String repoRoot = prepareTestRepos();
    String projectNs = "org.apache.maven.plugins";
    String projectPath = projectNs.replaceAll("\\.", "/");
    String projectName = "maven-plugin-plugin";
    String projectVersion = "2.3-SNAPSHOT";
    String projectRoot = repoRoot + "/" + projectPath + "/" + projectName;
    Path repo = getTestRepoRootPath();
    Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
    Set<String> deletedVersions = new HashSet<>();
    deletedVersions.add("2.3-SNAPSHOT");
    // test listeners for the correct artifacts
    listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins", "maven-plugin-plugin", "2.3-SNAPSHOT", "maven-plugin-plugin-2.3-SNAPSHOT.jar");
    listenerControl.replay();
    // Provide the metadata list
    List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
    when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
    repoPurge.process(PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO);
    listenerControl.verify();
    // Verify the metadataRepository invocations
    // complete snapshot version removal for released
    verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
    verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3"));
    // check if the snapshot was removed
    assertDeleted(projectRoot + "/2.3-SNAPSHOT");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5");
    assertDeleted(projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1");
    // check if the released version was not removed
    assertExists(projectRoot + "/2.3");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.md5");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3-sources.jar.sha1");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.jar");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.md5");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.jar.sha1");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.pom");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.md5");
    assertExists(projectRoot + "/2.3/maven-plugin-plugin-2.3.pom.sha1");
    // check if metadata file was updated
    Path artifactMetadataFile = Paths.get(projectRoot + "/maven-metadata.xml");
    String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString(artifactMetadataFile, Charset.defaultCharset());
    String expectedVersions = "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
    XMLAssert.assertXpathEvaluatesTo("2.3", "//metadata/versioning/release", metadataXml);
    XMLAssert.assertXpathEvaluatesTo("2.3", "//metadata/versioning/latest", metadataXml);
    System.out.println(metadataXml);
    XMLAssert.assertXpathsEqual("//expected/versions/version", expectedVersions, "//metadata/versioning/versions/version", metadataXml);
    XMLAssert.assertXpathEvaluatesTo("20070315032817", "//metadata/versioning/lastUpdated", metadataXml);
}
Also used : Path(java.nio.file.Path) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) ArtifactMetadata(org.apache.archiva.metadata.model.ArtifactMetadata) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with RepositoryRegistry

use of org.apache.archiva.repository.RepositoryRegistry in project archiva by apache.

the class RepositoryPurgeConsumerTest method removeRepoFromConfiguration.

private void removeRepoFromConfiguration(String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration) throws Exception {
    RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
    repositoryRegistry.removeRepository(repoConfiguration);
}
Also used : RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry)

Example 4 with RepositoryRegistry

use of org.apache.archiva.repository.RepositoryRegistry in project archiva by apache.

the class DefaultManagedRepositoryAdmin method deleteManagedRepository.

private Boolean deleteManagedRepository(ManagedRepositoryConfiguration repository, boolean deleteContent, Configuration config, boolean stagedOne) throws RepositoryAdminException {
    if (!stagedOne) {
        RepositorySession repositorySession = getRepositorySessionFactory().createSession();
        try {
            MetadataRepository metadataRepository = repositorySession.getRepository();
            metadataRepository.removeRepository(repository.getId());
            // invalidate cache
            namespacesCache.remove(repository.getId());
            log.debug("call repositoryStatisticsManager.deleteStatistics");
            getRepositoryStatisticsManager().deleteStatistics(metadataRepository, repository.getId());
            repositorySession.save();
        } catch (MetadataRepositoryException e) {
            // throw new RepositoryAdminException( e.getMessage(), e );
            log.warn("skip error during removing repository from MetadataRepository:{}", e.getMessage(), e);
        } finally {
            repositorySession.close();
        }
    }
    if (deleteContent) {
        // TODO could be async ? as directory can be huge
        Path dir = Paths.get(repository.getLocation());
        org.apache.archiva.common.utils.FileUtils.deleteQuietly(dir);
    }
    // olamy: copy list for reading as a unit test in webapp fail with ConcurrentModificationException
    List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>(config.getProxyConnectors());
    for (ProxyConnectorConfiguration proxyConnector : proxyConnectors) {
        if (StringUtils.equals(proxyConnector.getSourceRepoId(), repository.getId())) {
            config.removeProxyConnector(proxyConnector);
        }
    }
    Map<String, List<String>> repoToGroupMap = config.getRepositoryToGroupMap();
    if (repoToGroupMap != null) {
        if (repoToGroupMap.containsKey(repository.getId())) {
            List<String> repoGroups = repoToGroupMap.get(repository.getId());
            for (String repoGroup : repoGroups) {
                // copy to prevent UnsupportedOperationException
                RepositoryGroupConfiguration repositoryGroupConfiguration = config.findRepositoryGroupById(repoGroup);
                List<String> repos = new ArrayList<>(repositoryGroupConfiguration.getRepositories());
                config.removeRepositoryGroup(repositoryGroupConfiguration);
                repos.remove(repository.getId());
                repositoryGroupConfiguration.setRepositories(repos);
                config.addRepositoryGroup(repositoryGroupConfiguration);
            }
        }
    }
    try {
        removeRepositoryRoles(repository);
    } catch (RoleManagerException e) {
        throw new RepositoryAdminException("fail to remove repository roles for repository " + repository.getId() + " : " + e.getMessage(), e);
    }
    try {
        final RepositoryRegistry reg = getRepositoryRegistry();
        if (reg.getManagedRepository(repository.getId()) != null) {
            reg.removeRepository(reg.getManagedRepository(repository.getId()));
        }
    } catch (RepositoryException e) {
        throw new RepositoryAdminException("Removal of repository " + repository.getId() + " failed: " + e.getMessage());
    }
    saveConfiguration(config);
    return Boolean.TRUE;
}
Also used : MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) Path(java.nio.file.Path) RepositoryGroupConfiguration(org.apache.archiva.configuration.RepositoryGroupConfiguration) ArrayList(java.util.ArrayList) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MetadataRepository(org.apache.archiva.metadata.repository.MetadataRepository) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) List(java.util.List) ArrayList(java.util.ArrayList) RoleManagerException(org.apache.archiva.redback.role.RoleManagerException)

Example 5 with RepositoryRegistry

use of org.apache.archiva.repository.RepositoryRegistry in project archiva by apache.

the class HttpProxyTransferTest method setUp.

@Before
public void setUp() throws Exception {
    proxyHandler = applicationContext.getBean("repositoryProxyConnectors#test", RepositoryProxyConnectors.class);
    config = applicationContext.getBean("archivaConfiguration#mock", ArchivaConfiguration.class);
    // clear from previous tests - TODO the spring context should be initialised per test instead, or the config
    // made a complete mock
    config.getConfiguration().getProxyConnectors().clear();
    // Setup source repository (using default layout)
    String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
    Path destRepoDir = Paths.get(repoPath);
    // Cleanout destination dirs.
    if (Files.exists(destRepoDir)) {
        FileUtils.deleteDirectory(destRepoDir.toFile());
    }
    // Make the destination dir.
    Files.createDirectories(destRepoDir);
    MavenManagedRepository repo = new MavenManagedRepository(MANAGED_ID, "Default Managed Repository", Paths.get(repoPath).getParent());
    repo.setLocation(new URI(repoPath));
    repo.setLayout("default");
    RepositoryContentProvider provider = applicationContext.getBean("repositoryContentProvider#maven", RepositoryContentProvider.class);
    ManagedRepositoryContent repoContent = provider.createManagedContent(repo);
    managedDefaultRepository = repoContent;
    ((DefaultManagedRepositoryAdmin) applicationContext.getBean(ManagedRepositoryAdmin.class)).setArchivaConfiguration(config);
    RepositoryRegistry managedRepositoryAdmin = applicationContext.getBean(RepositoryRegistry.class);
    if (managedRepositoryAdmin.getManagedRepository(repo.getId()) == null) {
        managedRepositoryAdmin.putRepository(repo);
    }
    // config.getConfiguration().addManagedRepository( repo );
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
            response.setContentType("text/plain");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print("get-default-layout-1.0.jar\n\n");
            assertNotNull(request.getHeader("Proxy-Connection"));
            ((Request) request).setHandled(true);
        }

        public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
            response.setContentType("text/plain");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print("get-default-layout-1.0.jar\n\n");
            assertNotNull(request.getHeader("Proxy-Connection"));
            ((Request) request).setHandled(true);
        }
    };
    server = new Server();
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory());
    server.addConnector(serverConnector);
    server.setHandler(handler);
    server.start();
    int port = serverConnector.getLocalPort();
    NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
    proxyConfig.setHost("localhost");
    proxyConfig.setPort(port);
    proxyConfig.setProtocol("http");
    proxyConfig.setId(PROXY_ID);
    config.getConfiguration().addNetworkProxy(proxyConfig);
    // Setup target (proxied to) repository.
    RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
    repoConfig.setId(PROXIED_ID);
    repoConfig.setName("Proxied Repository 1");
    repoConfig.setLayout("default");
    repoConfig.setUrl("http://www.example.com/");
    config.getConfiguration().addRemoteRepository(repoConfig);
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MavenManagedRepository(org.apache.archiva.repository.maven2.MavenManagedRepository) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) RepositoryProxyConnectors(org.apache.archiva.proxy.model.RepositoryProxyConnectors) URI(java.net.URI) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConnector(org.eclipse.jetty.server.ServerConnector) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ManagedRepositoryAdmin(org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) Before(org.junit.Before)

Aggregations

RepositoryRegistry (org.apache.archiva.repository.RepositoryRegistry)8 Path (java.nio.file.Path)6 Test (org.junit.Test)4 ArtifactMetadata (org.apache.archiva.metadata.model.ArtifactMetadata)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 HashSet (java.util.HashSet)2 ManagedRepositoryAdmin (org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin)2 DefaultManagedRepositoryAdmin (org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)1 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)1 NetworkProxyConfiguration (org.apache.archiva.configuration.NetworkProxyConfiguration)1 ProxyConnectorConfiguration (org.apache.archiva.configuration.ProxyConnectorConfiguration)1 RemoteRepositoryConfiguration (org.apache.archiva.configuration.RemoteRepositoryConfiguration)1 RepositoryGroupConfiguration (org.apache.archiva.configuration.RepositoryGroupConfiguration)1 MetadataFacet (org.apache.archiva.metadata.model.MetadataFacet)1