Search in sources :

Example 21 with ManagedRepositoryContent

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

the class ArchivaDavResourceFactory method getResourceFromGroup.

private DavResource getResourceFromGroup(DavServletRequest request, List<String> repositories, ArchivaDavResourceLocator locator, RepositoryGroupConfiguration repositoryGroupConfiguration) throws DavException {
    if (repositoryGroupConfiguration.getRepositories() == null || repositoryGroupConfiguration.getRepositories().isEmpty()) {
        Path file = Paths.get(System.getProperty("appserver.base"), "groups/" + repositoryGroupConfiguration.getId());
        return new ArchivaDavResource(file.toString(), "groups/" + repositoryGroupConfiguration.getId(), null, request.getDavSession(), locator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
    }
    List<Path> mergedRepositoryContents = new ArrayList<>();
    // multiple repo types so we guess they are all the same type
    // so use the first one
    // FIXME add a method with group in the repository storage
    String firstRepoId = repositoryGroupConfiguration.getRepositories().get(0);
    String path = getLogicalResource(locator, repositoryRegistry.getManagedRepository(firstRepoId), false);
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    LogicalResource logicalResource = new LogicalResource(path);
    // flow:
    // if the current user logged in has permission to any of the repositories, allow user to
    // browse the repo group but displaying only the repositories which the user has permission to access.
    // otherwise, prompt for authentication.
    String activePrincipal = getActivePrincipal(request);
    boolean allow = isAllowedToContinue(request, repositories, activePrincipal);
    // remove last /
    String pathInfo = StringUtils.removeEnd(request.getPathInfo(), "/");
    if (allow) {
        if (StringUtils.endsWith(pathInfo, repositoryGroupConfiguration.getMergedIndexPath())) {
            Path mergedRepoDir = buildMergedIndexDirectory(repositories, activePrincipal, request, repositoryGroupConfiguration);
            mergedRepositoryContents.add(mergedRepoDir);
        } else {
            if (StringUtils.equalsIgnoreCase(pathInfo, "/" + repositoryGroupConfiguration.getId())) {
                Path tmpDirectory = Paths.get(SystemUtils.getJavaIoTmpDir().toString(), repositoryGroupConfiguration.getId(), repositoryGroupConfiguration.getMergedIndexPath());
                if (!Files.exists(tmpDirectory)) {
                    synchronized (tmpDirectory.toAbsolutePath().toString()) {
                        if (!Files.exists(tmpDirectory)) {
                            try {
                                Files.createDirectories(tmpDirectory);
                            } catch (IOException e) {
                                throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create direcotory " + tmpDirectory);
                            }
                        }
                    }
                }
                mergedRepositoryContents.add(tmpDirectory.getParent());
            }
            for (String repository : repositories) {
                ManagedRepositoryContent managedRepository = null;
                ManagedRepository repo = repositoryRegistry.getManagedRepository(repository);
                if (repo == null) {
                    throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid managed repository <" + repository + ">");
                }
                managedRepository = repo.getContent();
                if (managedRepository == null) {
                    log.error("Inconsistency detected. Repository content not found for '{}'", repository);
                    throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid managed repository <" + repository + ">");
                }
                Path resourceFile = Paths.get(managedRepository.getRepoRoot(), logicalResource.getPath());
                if (Files.exists(resourceFile)) {
                    // in case of group displaying index directory doesn't have sense !!
                    IndexCreationFeature idf = managedRepository.getRepository().getFeature(IndexCreationFeature.class).get();
                    String repoIndexDirectory = idf.getIndexPath().toString();
                    if (StringUtils.isNotEmpty(repoIndexDirectory)) {
                        if (!Paths.get(repoIndexDirectory).isAbsolute()) {
                            repoIndexDirectory = Paths.get(managedRepository.getRepository().getLocation()).resolve(StringUtils.isEmpty(repoIndexDirectory) ? ".indexer" : repoIndexDirectory).toAbsolutePath().toString();
                        }
                    }
                    if (StringUtils.isEmpty(repoIndexDirectory)) {
                        repoIndexDirectory = Paths.get(managedRepository.getRepository().getLocation()).resolve(".indexer").toAbsolutePath().toString();
                    }
                    if (!StringUtils.equals(FilenameUtils.normalize(repoIndexDirectory), FilenameUtils.normalize(resourceFile.toAbsolutePath().toString()))) {
                        // for prompted authentication
                        if (httpAuth.getSecuritySession(request.getSession(true)) != null) {
                            try {
                                if (isAuthorized(request, repository)) {
                                    mergedRepositoryContents.add(resourceFile);
                                    log.debug("Repository '{}' accessed by '{}'", repository, activePrincipal);
                                }
                            } catch (DavException e) {
                                // TODO: review exception handling
                                log.debug("Skipping repository '{}' for user '{}': {}", managedRepository, activePrincipal, e.getMessage());
                            }
                        } else {
                            // for the current user logged in
                            try {
                                if (servletAuth.isAuthorized(activePrincipal, repository, WebdavMethodUtil.getMethodPermission(request.getMethod()))) {
                                    mergedRepositoryContents.add(resourceFile);
                                    log.debug("Repository '{}' accessed by '{}'", repository, activePrincipal);
                                }
                            } catch (UnauthorizedException e) {
                                // TODO: review exception handling
                                log.debug("Skipping repository '{}' for user '{}': {}", managedRepository, activePrincipal, e.getMessage());
                            }
                        }
                    }
                }
            }
        }
    } else {
        throw new UnauthorizedDavException(locator.getRepositoryId(), "User not authorized.");
    }
    ArchivaVirtualDavResource resource = new ArchivaVirtualDavResource(mergedRepositoryContents, logicalResource.getPath(), mimeTypes, locator, this);
    // compatibility with MRM-440 to ensure browsing the repository group works ok
    if (resource.isCollection() && !request.getRequestURI().endsWith("/")) {
        throw new BrowserRedirectException(resource.getHref());
    }
    return resource;
}
Also used : Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.repository.ManagedRepository) DavException(org.apache.jackrabbit.webdav.DavException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IndexCreationFeature(org.apache.archiva.repository.features.IndexCreationFeature) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) UnauthorizedException(org.apache.archiva.redback.authorization.UnauthorizedException)

Example 22 with ManagedRepositoryContent

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

the class ArchivaDavResourceFactoryTest method createManagedRepositoryContent.

private ManagedRepositoryContent createManagedRepositoryContent(String repoId) throws RepositoryAdminException {
    ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(artifactMappingProviders, fileTypes);
    org.apache.archiva.repository.ManagedRepository repo = repositoryRegistry.getManagedRepository(repoId);
    repoContent.setRepository(repo);
    if (repo != null && repo instanceof EditableManagedRepository) {
        ((EditableManagedRepository) repo).setContent(repoContent);
    }
    return repoContent;
}
Also used : EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ManagedDefaultRepositoryContent(org.apache.archiva.repository.content.maven2.ManagedDefaultRepositoryContent)

Example 23 with ManagedRepositoryContent

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

the class ArchivaDavResourceFactoryTest method testRepositoryGroupFirstRepositoryRequiresAuthentication.

// MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
@Test
public void testRepositoryGroupFirstRepositoryRequiresAuthentication() throws Exception {
    DavResourceLocator locator = new ArchivaDavResourceLocator("", "/repository/" + LOCAL_REPO_GROUP + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory());
    ManagedRepositoryContent internalRepo = createManagedRepositoryContent(INTERNAL_REPO);
    ManagedRepositoryContent releasesRepo = createManagedRepositoryContent(RELEASES_REPO);
    try {
        archivaConfigurationControl.reset();
        expect(archivaConfiguration.getConfiguration()).andReturn(config).times(3);
        expect(request.getMethod()).andReturn("GET").times(3);
        expect(request.getPathInfo()).andReturn("org/apache/archiva").times(0, 2);
        expect(repoFactory.getManagedRepositoryContent(RELEASES_REPO)).andReturn(releasesRepo);
        expect(request.getRemoteAddr()).andReturn("http://localhost:8080").times(2);
        expect(request.getDavSession()).andReturn(new ArchivaDavSession()).times(2);
        expect(request.getContextPath()).andReturn("").times(2);
        expect(repoRequest.isSupportFile("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(true);
        expect(repoRequest.isDefault("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false);
        expect(repoRequest.toArtifactReference("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(null);
        expect(repoRequest.toNativePath("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", internalRepo)).andReturn(Paths.get(config.findManagedRepositoryById(INTERNAL_REPO).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar").toString());
        expect(repoFactory.getManagedRepositoryContent(INTERNAL_REPO)).andReturn(internalRepo);
        expect(repoRequest.isArchetypeCatalog("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false);
        archivaConfigurationControl.replay();
        requestControl.replay();
        repoContentFactoryControl.replay();
        repoRequestControl.replay();
        resourceFactory.createResource(locator, request, response);
        archivaConfigurationControl.verify();
        requestControl.verify();
        repoContentFactoryControl.verify();
        repoRequestControl.verify();
        fail("A DavException with 401 error code should have been thrown.");
    } catch (DavException e) {
        assertEquals(401, e.getErrorCode());
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator) Test(org.junit.Test)

Example 24 with ManagedRepositoryContent

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

the class ArchivaDavResourceFactoryTest method testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled.

@Test
public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled() throws Exception {
    DavResourceLocator locator = new ArchivaDavResourceLocator("", "/repository/" + LOCAL_REPO_GROUP + "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory());
    defaultManagedRepositoryAdmin.addManagedRepository(createManagedRepository(LOCAL_MIRROR_REPO, Paths.get("target/test-classes/local-mirror").toString(), "default"), false, null);
    List<RepositoryGroupConfiguration> repoGroups = new ArrayList<>();
    RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
    repoGroup.setId(LOCAL_REPO_GROUP);
    repoGroup.addRepository(INTERNAL_REPO);
    repoGroup.addRepository(LOCAL_MIRROR_REPO);
    repoGroups.add(repoGroup);
    config.setRepositoryGroups(repoGroups);
    ManagedRepositoryContent internalRepo = createManagedRepositoryContent(INTERNAL_REPO);
    ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent(LOCAL_MIRROR_REPO);
    try {
        archivaConfigurationControl.reset();
        expect(archivaConfiguration.getConfiguration()).andReturn(config).times(3);
        expect(request.getMethod()).andReturn("GET").times(5);
        expect(request.getPathInfo()).andReturn("org/apache/archiva").times(0, 2);
        expect(repoFactory.getManagedRepositoryContent(INTERNAL_REPO)).andReturn(internalRepo);
        expect(repoFactory.getManagedRepositoryContent(LOCAL_MIRROR_REPO)).andReturn(localMirrorRepo);
        expect(request.getRemoteAddr()).andReturn("http://localhost:8080").times(4);
        expect(request.getDavSession()).andReturn(new ArchivaDavSession()).times(4);
        expect(request.getContextPath()).andReturn("").times(2);
        expect(repoRequest.isSupportFile("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false).times(2);
        expect(repoRequest.isDefault("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false).times(2);
        expect(repoRequest.toArtifactReference("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(null).times(2);
        expect(repoRequest.toNativePath("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", internalRepo)).andReturn(Paths.get(config.findManagedRepositoryById(INTERNAL_REPO).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar").toString());
        expect(repoRequest.toNativePath("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar", localMirrorRepo)).andReturn(Paths.get(config.findManagedRepositoryById(LOCAL_MIRROR_REPO).getLocation(), "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar").toString());
        expect(repoRequest.isArchetypeCatalog("org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar")).andReturn(false).times(2);
        archivaConfigurationControl.replay();
        requestControl.replay();
        repoContentFactoryControl.replay();
        repoRequestControl.replay();
        resourceFactory.createResource(locator, request, response);
        archivaConfigurationControl.verify();
        requestControl.verify();
        repoContentFactoryControl.verify();
        repoRequestControl.verify();
        fail("A DavException with 404 error code should have been thrown.");
    } catch (DavException e) {
        assertEquals(404, e.getErrorCode());
    }
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.RepositoryGroupConfiguration) DavException(org.apache.jackrabbit.webdav.DavException) ArrayList(java.util.ArrayList) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator) Test(org.junit.Test)

Example 25 with ManagedRepositoryContent

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

the class ArchivaDavResourceFactoryTest method testRequestMetadataRepoIsLegacy.

@Test
public void testRequestMetadataRepoIsLegacy() throws Exception {
    ManagedRepositoryContent legacyRepo = createManagedRepositoryContent(LEGACY_REPO);
    ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    RepositoryContentProvider provider = createRepositoryContentProvider(legacyRepo);
    beanFactory.registerSingleton("repositoryContentProvider#legacy", provider);
    RepositoryContentFactory repoContentFactory = applicationContext.getBean("repositoryContentFactory#default", RepositoryContentFactory.class);
    repoContentFactory.getRepositoryContentProviders().add(provider);
    defaultManagedRepositoryAdmin.addManagedRepository(createManagedRepository(LEGACY_REPO, Paths.get("target/test-classes/" + LEGACY_REPO).toString(), "legacy"), false, null);
    DavResourceLocator locator = new ArchivaDavResourceLocator("", "/repository/" + LEGACY_REPO + "/eclipse/maven-metadata.xml", LEGACY_REPO, new ArchivaDavLocatorFactory());
    // use actual object (this performs the isMetadata, isDefault and isSupportFile check!)
    RepositoryRequest repoRequest = new RepositoryRequest();
    resourceFactory.setRepositoryRequest(repoRequest);
    try {
        archivaConfigurationControl.reset();
        expect(archivaConfiguration.getConfiguration()).andReturn(config).times(2);
        expect(repoFactory.getManagedRepositoryContent(LEGACY_REPO)).andReturn(legacyRepo);
        expect(request.getMethod()).andReturn("GET").times(3);
        expect(request.getRemoteAddr()).andReturn("http://localhost:8080").times(3);
        expect(request.getDavSession()).andReturn(new ArchivaDavSession()).times(2);
        expect(request.getContextPath()).andReturn("").times(2);
        archivaConfigurationControl.replay();
        repoContentFactoryControl.replay();
        requestControl.replay();
        resourceFactory.createResource(locator, request, response);
        archivaConfigurationControl.verify();
        repoContentFactoryControl.verify();
        requestControl.verify();
        fail("A 404 error should have been thrown!");
    } catch (DavException e) {
        assertEquals(404, e.getErrorCode());
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) DavException(org.apache.jackrabbit.webdav.DavException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) RepositoryRequest(org.apache.archiva.repository.content.maven2.RepositoryRequest) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) RepositoryContentFactory(org.apache.archiva.repository.RepositoryContentFactory) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator) Test(org.junit.Test)

Aggregations

ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)41 Path (java.nio.file.Path)16 Test (org.junit.Test)13 RepositoryException (org.apache.archiva.repository.RepositoryException)12 ArchivaRestServiceException (org.apache.archiva.rest.api.services.ArchivaRestServiceException)11 DavException (org.apache.jackrabbit.webdav.DavException)10 ArrayList (java.util.ArrayList)9 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)9 IOException (java.io.IOException)8 ArtifactReference (org.apache.archiva.model.ArtifactReference)8 VersionedReference (org.apache.archiva.model.VersionedReference)7 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)6 LayoutException (org.apache.archiva.repository.LayoutException)6 RepositoryContentProvider (org.apache.archiva.repository.RepositoryContentProvider)6 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)6 ManagedRepository (org.apache.archiva.admin.model.beans.ManagedRepository)5 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)5 ManagedRepository (org.apache.archiva.repository.ManagedRepository)5 MavenManagedRepository (org.apache.archiva.repository.maven2.MavenManagedRepository)5