Search in sources :

Example 21 with RepositoryGroup

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

the class ArchivaDavResourceFactory method createResource.

@Override
public DavResource createResource(final DavResourceLocator locator, final DavServletRequest request, final DavServletResponse response) throws DavException {
    final ArchivaDavResourceLocator archivaLocator = checkLocatorIsInstanceOfRepositoryLocator(locator);
    final String sRepoId = archivaLocator.getRepositoryId();
    RepositoryGroup repoGroup = repositoryRegistry.getRepositoryGroup(sRepoId);
    final boolean isGroupRepo = repoGroup != null;
    String activePrincipal = getActivePrincipal(request);
    List<String> resourcesInAbsolutePath = new ArrayList<>();
    boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
    RepositoryRequestInfo repositoryRequestInfo = null;
    DavResource resource;
    if (isGroupRepo) {
        if (!readMethod) {
            throw new DavException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Write method not allowed for repository groups.");
        }
        log.debug("Repository group '{}' accessed by '{}", repoGroup.getId(), activePrincipal);
        // handle browse requests for virtual repos
        if (getLogicalResource(archivaLocator, null, true).endsWith("/")) {
            DavResource davResource = getResourceFromGroup(request, archivaLocator, repoGroup);
            setHeaders(response, locator, davResource, true);
            return davResource;
        } else {
            // make a copy to avoid potential concurrent modifications (eg. by configuration)
            // TODO: ultimately, locking might be more efficient than copying in this fashion since updates are
            // infrequent
            resource = processRepositoryGroup(request, archivaLocator, activePrincipal, resourcesInAbsolutePath, repoGroup);
            for (ManagedRepository repo : repoGroup.getRepositories()) {
                if (repo != null) {
                    repositoryRequestInfo = repo.getRequestInfo();
                    break;
                }
            }
        }
    } else {
        // We do not provide folders for remote repositories
        ManagedRepository repo = repositoryRegistry.getManagedRepository(sRepoId);
        if (repo == null) {
            throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Invalid repository: " + archivaLocator.getRepositoryId());
        }
        ManagedRepositoryContent managedRepositoryContent = repo.getContent();
        if (managedRepositoryContent == null) {
            log.error("Inconsistency detected. Repository content not found for '{}'", archivaLocator.getRepositoryId());
            throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Invalid repository: " + archivaLocator.getRepositoryId());
        }
        log.debug("Managed repository '{}' accessed by '{}'", managedRepositoryContent.getId(), activePrincipal);
        resource = processRepository(request, archivaLocator, activePrincipal, managedRepositoryContent, repo);
        repositoryRequestInfo = repo.getRequestInfo();
        String logicalResource = getLogicalResource(archivaLocator, null, false);
        resourcesInAbsolutePath.add(managedRepositoryContent.getRepository().getRoot().getFilePath().resolve(logicalResource).toAbsolutePath().toString());
    }
    String requestedResource = request.getRequestURI();
    // merge metadata only when requested via the repo group
    if ((repositoryRequestInfo.isMetadata(requestedResource) || repositoryRequestInfo.isMetadataSupportFile(requestedResource)) && isGroupRepo) {
        // this should only be at the project level not version level!
        if (isProjectReference(requestedResource)) {
            ArchivaDavResource res = (ArchivaDavResource) resource;
            String newPath;
            if (res.getAsset().hasParent()) {
                newPath = res.getAsset().getParent().getPath() + "/maven-metadata-" + sRepoId + ".xml";
            } else {
                newPath = StringUtils.substringBeforeLast(res.getAsset().getPath(), "/") + "/maven-metadata-" + sRepoId + ".xml";
                ;
            }
            // for MRM-872 handle checksums of the merged metadata files
            if (repositoryRequestInfo.isSupportFile(requestedResource)) {
                String metadataChecksumPath = newPath + "." + StringUtils.substringAfterLast(requestedResource, ".");
                StorageAsset metadataChecksum = repoGroup.getAsset(metadataChecksumPath);
                if (repoGroup.getAsset(metadataChecksumPath).exists()) {
                    LogicalResource logicalResource = new LogicalResource(getLogicalResource(archivaLocator, null, false));
                    try {
                        resource = new ArchivaDavResource(metadataChecksum, logicalResource.getPath(), repoGroup, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
                    } catch (LayoutException e) {
                        log.error("Incompatible layout: {}", e.getMessage(), e);
                        throw new DavException(500, e);
                    }
                }
            } else {
                if (resourcesInAbsolutePath != null && resourcesInAbsolutePath.size() > 1) {
                    // merge the metadata of all repos under group
                    ArchivaRepositoryMetadata mergedMetadata = new ArchivaRepositoryMetadata();
                    for (String resourceAbsPath : resourcesInAbsolutePath) {
                        try {
                            Path metadataFile = Paths.get(resourceAbsPath);
                            FilesystemStorage storage = new FilesystemStorage(metadataFile.getParent(), new DefaultFileLockManager());
                            ArchivaRepositoryMetadata repoMetadata = repositoryRegistry.getMetadataReader(repoGroup.getType()).read(storage.getAsset(metadataFile.getFileName().toString()));
                            mergedMetadata = RepositoryMetadataMerge.merge(mergedMetadata, repoMetadata);
                        } catch (RepositoryMetadataException r) {
                            throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while merging metadata file.");
                        } catch (IOException e) {
                            throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while merging metadata file.");
                        }
                    }
                    try {
                        StorageAsset resourceFile = writeMergedMetadataToFile(repoGroup, mergedMetadata, newPath);
                        LogicalResource logicalResource = new LogicalResource(getLogicalResource(archivaLocator, null, false));
                        resource = new ArchivaDavResource(resourceFile, logicalResource.getPath(), repoGroup, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
                    } catch (RepositoryMetadataException r) {
                        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while writing metadata file.");
                    } catch (IOException ie) {
                        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred while generating checksum files.");
                    } catch (LayoutException e) {
                        log.error("Incompatible layout: {}", e.getMessage(), e);
                        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Incompatible layout for repository " + repoGroup.getId());
                    }
                }
            }
        }
    }
    setHeaders(response, locator, resource, false);
    // compatibility with MRM-440 to ensure browsing the repository works ok
    if (resource.isCollection() && !request.getRequestURI().endsWith("/")) {
        throw new BrowserRedirectException(resource.getHref());
    }
    resource.addLockManager(lockManager);
    return resource;
}
Also used : Path(java.nio.file.Path) DavResource(org.apache.jackrabbit.webdav.DavResource) ManagedRepository(org.apache.archiva.repository.ManagedRepository) DavException(org.apache.jackrabbit.webdav.DavException) FilesystemStorage(org.apache.archiva.repository.storage.fs.FilesystemStorage) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RepositoryRequestInfo(org.apache.archiva.repository.RepositoryRequestInfo) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) DefaultFileLockManager(org.apache.archiva.common.filelock.DefaultFileLockManager) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 22 with RepositoryGroup

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

the class RepositoryGroupHandler method put.

/**
 * Adds a new repository group or updates the repository with the same id, if it exists already.
 * The configuration is saved immediately.
 *
 * @param repositoryGroupConfiguration the repository configuration
 * @return the updated or created repository
 * @throws RepositoryException if an error occurs, or the configuration is not valid.
 */
@Override
public RepositoryGroup put(RepositoryGroupConfiguration repositoryGroupConfiguration) throws RepositoryException {
    final String id = repositoryGroupConfiguration.getId();
    final RepositoryType repositoryType = RepositoryType.valueOf(repositoryGroupConfiguration.getType());
    final RepositoryProvider provider = getProvider(repositoryType);
    RepositoryGroup currentRepository;
    ReentrantReadWriteLock.WriteLock configLock = this.getConfigurationHandler().getLock().writeLock();
    configLock.lock();
    try {
        Configuration configuration = this.getConfigurationHandler().getBaseConfiguration();
        currentRepository = getRepositories().get(id);
        RepositoryGroup oldRepository = currentRepository == null ? null : clone(currentRepository, id);
        try {
            boolean updated = false;
            if (currentRepository == null) {
                currentRepository = put(repositoryGroupConfiguration, configuration);
            } else {
                setRepositoryGroupDefaults(repositoryGroupConfiguration);
                provider.updateRepositoryGroupInstance((EditableRepositoryGroup) currentRepository, repositoryGroupConfiguration);
                updated = true;
                pushEvent(LifecycleEvent.UPDATED, currentRepository);
            }
            registerNewRepository(repositoryGroupConfiguration, currentRepository, configuration, updated);
        } catch (IndeterminateConfigurationException | RegistryException | RepositoryException e) {
            // Trying a rollback
            if (oldRepository != null) {
                RepositoryGroupConfiguration oldCfg = provider.getRepositoryGroupConfiguration(oldRepository);
                provider.updateRepositoryGroupInstance((EditableRepositoryGroup) currentRepository, oldCfg);
                rollback(configuration, oldRepository, e, oldCfg);
            } else {
                getRepositories().remove(id);
            }
            log.error("Could not save the configuration for repository group {}: {}", id, e.getMessage(), e);
            if (e instanceof RepositoryException) {
                throw (RepositoryException) e;
            } else {
                throw new RepositoryException("Could not save the configuration for repository group " + id + ": " + e.getMessage());
            }
        }
    } finally {
        configLock.unlock();
    }
    return currentRepository;
}
Also used : RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) ManagedRepositoryConfiguration(org.apache.archiva.configuration.model.ManagedRepositoryConfiguration) RepositoryGroupConfiguration(org.apache.archiva.configuration.model.RepositoryGroupConfiguration) Configuration(org.apache.archiva.configuration.model.Configuration) RepositoryType(org.apache.archiva.repository.RepositoryType) IndeterminateConfigurationException(org.apache.archiva.configuration.provider.IndeterminateConfigurationException) RepositoryException(org.apache.archiva.repository.RepositoryException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) RegistryException(org.apache.archiva.components.registry.RegistryException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) RepositoryProvider(org.apache.archiva.repository.RepositoryProvider) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup)

Example 23 with RepositoryGroup

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

the class RepositoryGroupHandler method createNewRepositoryGroup.

private RepositoryGroup createNewRepositoryGroup(RepositoryProvider provider, RepositoryGroupConfiguration config) throws RepositoryException {
    RepositoryGroup repositoryGroup = provider.createRepositoryGroup(config);
    updateReferences(repositoryGroup, config);
    if (repositoryGroup instanceof EditableRepository) {
        ((EditableRepository) repositoryGroup).setLastState(RepositoryState.REFERENCES_SET);
    }
    repositoryGroup.registerEventHandler(RepositoryEvent.ANY, repositoryRegistry);
    return repositoryGroup;
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) EditableRepository(org.apache.archiva.repository.EditableRepository)

Example 24 with RepositoryGroup

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

the class BasicRepositoryGroupValidatorTest method applyWithExisting.

@Test
void applyWithExisting() throws IOException, RepositoryException {
    BasicRepositoryGroupValidator validator = new BasicRepositoryGroupValidator(configurationHandler);
    validator.setRepositoryRegistry(repositoryRegistry);
    EditableRepositoryGroup group = createGroup("test", "test");
    group.setMergedIndexTTL(360);
    repositoryRegistry.putRepositoryGroup(group);
    EditableRepositoryGroup group2 = createGroup("test", "test2");
    group2.setMergedIndexTTL(360);
    ValidationResponse<RepositoryGroup> result = validator.apply(group2);
    assertNotNull(result);
    assertFalse(result.isValid());
    assertEquals("group_exists", result.getResult().get("id").get(0).getType());
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) Test(org.junit.jupiter.api.Test)

Example 25 with RepositoryGroup

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

the class RepositoryGroupHandler method put.

@Override
public RepositoryGroup put(RepositoryGroupConfiguration repositoryGroupConfiguration, Configuration configuration) throws RepositoryException {
    final String id = repositoryGroupConfiguration.getId();
    final RepositoryType repoType = RepositoryType.valueOf(repositoryGroupConfiguration.getType());
    RepositoryGroup repo;
    setRepositoryGroupDefaults(repositoryGroupConfiguration);
    if (getRepositories().containsKey(id)) {
        repo = clone(getRepositories().get(id), id);
        if (repo instanceof EditableRepositoryGroup) {
            getProvider(repoType).updateRepositoryGroupInstance((EditableRepositoryGroup) repo, repositoryGroupConfiguration);
        } else {
            throw new RepositoryException("The repository is not editable " + id);
        }
    } else {
        repo = getProvider(repoType).createRepositoryGroup(repositoryGroupConfiguration);
        setLastState(repo, RepositoryState.CREATED);
    }
    replaceOrAddRepositoryConfig(repositoryGroupConfiguration, configuration);
    updateReferences(repo, repositoryGroupConfiguration);
    setLastState(repo, RepositoryState.REFERENCES_SET);
    return repo;
}
Also used : EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup) RepositoryGroup(org.apache.archiva.repository.RepositoryGroup) RepositoryType(org.apache.archiva.repository.RepositoryType) RepositoryException(org.apache.archiva.repository.RepositoryException) EditableRepositoryGroup(org.apache.archiva.repository.EditableRepositoryGroup)

Aggregations

RepositoryGroup (org.apache.archiva.repository.RepositoryGroup)25 EditableRepositoryGroup (org.apache.archiva.repository.EditableRepositoryGroup)23 Test (org.junit.jupiter.api.Test)15 RepositoryGroupConfiguration (org.apache.archiva.configuration.model.RepositoryGroupConfiguration)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 RepositoryException (org.apache.archiva.repository.RepositoryException)5 ValidationError (org.apache.archiva.repository.validation.ValidationError)5 IOException (java.io.IOException)4 RepositoryType (org.apache.archiva.repository.RepositoryType)4 Path (java.nio.file.Path)3 RegistryException (org.apache.archiva.components.registry.RegistryException)3 Configuration (org.apache.archiva.configuration.model.Configuration)3 ManagedRepositoryConfiguration (org.apache.archiva.configuration.model.ManagedRepositoryConfiguration)3 IndeterminateConfigurationException (org.apache.archiva.configuration.provider.IndeterminateConfigurationException)3 ManagedRepository (org.apache.archiva.repository.ManagedRepository)3 List (java.util.List)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2