Search in sources :

Example 11 with LayoutException

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

the class LegacyConverterArtifactConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    try {
        ArtifactReference reference = managedRepository.toArtifactReference(path);
        Artifact artifact = artifactFactory.createArtifact(reference.getGroupId(), reference.getArtifactId(), reference.getVersion(), reference.getClassifier(), reference.getType());
        artifactConverter.convert(artifact, destinationRepository);
    } catch (LayoutException e) {
        log.warn("Unable to convert artifact: {} : {}", path, e.getMessage(), e);
    } catch (ArtifactConversionException e) {
        log.warn("Unable to convert artifact: {} : {}", path, e.getMessage(), e);
    }
}
Also used : ArtifactConversionException(org.apache.archiva.converter.artifact.ArtifactConversionException) LayoutException(org.apache.archiva.repository.LayoutException) ArtifactReference(org.apache.archiva.model.ArtifactReference) Artifact(org.apache.maven.artifact.Artifact)

Example 12 with LayoutException

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

the class SimpleArtifactConsumer method processFile.

public void processFile(String path, boolean executeOnEntireRepo) throws ConsumerException {
    log.info("Processing entry [{}] from repository [{}]", path, this.repository.getId());
    try {
        ManagedRepositoryContent repositoryContent = repository.getContent();
        ArtifactReference artifact = repositoryContent.toArtifactReference(path);
        repositorySession.getRepository().getArtifacts(repository.getId(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
    } catch (LayoutException | MetadataResolutionException e) {
        throw new ConsumerException(e.getLocalizedMessage(), e);
    }
}
Also used : LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ConsumerException(org.apache.archiva.consumers.ConsumerException) ArtifactReference(org.apache.archiva.model.ArtifactReference) MetadataResolutionException(org.apache.archiva.metadata.repository.MetadataResolutionException)

Example 13 with LayoutException

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

the class MetadataTools method getFirstArtifact.

/**
 * Get the first Artifact found in the provided VersionedReference location.
 *
 * @param managedRepository the repository to search within.
 * @param reference         the reference to the versioned reference to search within
 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
 *         no artifact was found within the versioned reference.
 * @throws IOException     if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
 * @throws LayoutException
 */
public ArtifactReference getFirstArtifact(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, IOException {
    String path = toPath(reference);
    int idx = path.lastIndexOf('/');
    if (idx > 0) {
        path = path.substring(0, idx);
    }
    Path repoDir = Paths.get(managedRepository.getRepoRoot(), path);
    if (!Files.exists(repoDir)) {
        throw new IOException("Unable to gather the list of snapshot versions on a non-existant directory: " + repoDir.toAbsolutePath());
    }
    if (!Files.isDirectory(repoDir)) {
        throw new IOException("Unable to gather the list of snapshot versions on a non-directory: " + repoDir.toAbsolutePath());
    }
    try (Stream<Path> stream = Files.list(repoDir)) {
        String result = stream.filter(Files::isRegularFile).map(path1 -> PathUtil.getRelative(managedRepository.getRepoRoot(), path1)).filter(filetypes::matchesArtifactPattern).findFirst().orElse(null);
        if (result != null) {
            return managedRepository.toArtifactReference(result);
        }
    }
    // No artifact was found.
    return null;
}
Also used : Path(java.nio.file.Path) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) PathUtil(org.apache.archiva.common.utils.PathUtil) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) XMLException(org.apache.archiva.xml.XMLException) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) Registry(org.apache.archiva.redback.components.registry.Registry) NumberUtils(org.apache.commons.lang.math.NumberUtils) RegistryListener(org.apache.archiva.redback.components.registry.RegistryListener) Matcher(java.util.regex.Matcher) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) Map(java.util.Map) FileUtils(org.apache.archiva.common.utils.FileUtils) ParseException(java.text.ParseException) Path(java.nio.file.Path) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) ConfigurationNames(org.apache.archiva.configuration.ConfigurationNames) VersionedReference(org.apache.archiva.model.VersionedReference) Collection(java.util.Collection) Set(java.util.Set) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Plugin(org.apache.archiva.model.Plugin) List(java.util.List) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) ArtifactReference(org.apache.archiva.model.ArtifactReference) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ProjectReference(org.apache.archiva.model.ProjectReference) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) DateUtils(org.apache.commons.lang.time.DateUtils) Calendar(java.util.Calendar) Service(org.springframework.stereotype.Service) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) MavenMetadataReader(org.apache.archiva.maven2.metadata.MavenMetadataReader) Named(javax.inject.Named) LayoutException(org.apache.archiva.repository.LayoutException) VersionComparator(org.apache.archiva.common.utils.VersionComparator) LinkedHashSet(java.util.LinkedHashSet) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Files(java.nio.file.Files) VersionUtil(org.apache.archiva.common.utils.VersionUtil) FileTypes(org.apache.archiva.configuration.FileTypes) IOException(java.io.IOException) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) Paths(java.nio.file.Paths) Collections(java.util.Collections) IOException(java.io.IOException) Files(java.nio.file.Files)

Example 14 with LayoutException

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

the class ArchivaDavResourceFactory method fetchContentFromProxies.

private boolean fetchContentFromProxies(ManagedRepositoryContent managedRepository, DavServletRequest request, LogicalResource resource) throws DavException {
    String path = resource.getPath();
    if (repositoryRequest.isSupportFile(path)) {
        Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Is it a Metadata resource?
    if (repositoryRequest.isDefault(path) && repositoryRequest.isMetadata(path)) {
        return connectors.fetchMetadataFromProxies(managedRepository, path).isModified();
    }
    // Is it an Archetype Catalog?
    if (repositoryRequest.isArchetypeCatalog(path)) {
        // FIXME we must implement a merge of remote archetype catalog from remote servers.
        Path proxiedFile = connectors.fetchFromProxies(managedRepository, path);
        return (proxiedFile != null);
    }
    // Not any of the above? Then it's gotta be an artifact reference.
    try {
        // Get the artifact reference in a layout neutral way.
        ArtifactReference artifact = repositoryRequest.toArtifactReference(path);
        if (artifact != null) {
            String repositoryLayout = managedRepository.getRepository().getLayout();
            RepositoryStorage repositoryStorage = this.applicationContext.getBean("repositoryStorage#" + repositoryLayout, RepositoryStorage.class);
            repositoryStorage.applyServerSideRelocation(managedRepository, artifact);
            Path proxiedFile = connectors.fetchFromProxies(managedRepository, artifact);
            resource.setPath(managedRepository.toPath(artifact));
            log.debug("Proxied artifact '{}:{}:{}'", artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
            return (proxiedFile != null);
        }
    } catch (LayoutException e) {
    /* eat it */
    } catch (ProxyDownloadException e) {
        log.error(e.getMessage(), e);
        throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Unable to fetch artifact resource.");
    }
    return false;
}
Also used : Path(java.nio.file.Path) RepositoryStorage(org.apache.archiva.metadata.repository.storage.RepositoryStorage) LayoutException(org.apache.archiva.repository.LayoutException) DavException(org.apache.jackrabbit.webdav.DavException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 15 with LayoutException

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

the class ArchivaDavResourceFactory method processRepository.

private DavResource processRepository(final DavServletRequest request, ArchivaDavResourceLocator archivaLocator, String activePrincipal, ManagedRepositoryContent managedRepositoryContent, org.apache.archiva.repository.ManagedRepository managedRepository) throws DavException {
    DavResource resource = null;
    if (isAuthorized(request, managedRepositoryContent.getId())) {
        boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
        // Maven Centric part ask evaluation if -SNAPSHOT
        // MRM-1846 test if read method to prevent issue with maven 2.2.1 and uniqueVersion false
        String path = readMethod ? evaluatePathWithVersion(archivaLocator, managedRepositoryContent, request.getContextPath()) : getLogicalResource(archivaLocator, managedRepository, false);
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        LogicalResource logicalResource = new LogicalResource(path);
        Path resourceFile = Paths.get(managedRepositoryContent.getRepoRoot(), path);
        resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), path, managedRepositoryContent.getRepository(), request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
        if (WebdavMethodUtil.isReadMethod(request.getMethod())) {
            if (archivaLocator.getHref(false).endsWith("/") && !Files.isDirectory(resourceFile)) {
                // force a resource not found
                throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
            } else {
                if (!resource.isCollection()) {
                    boolean previouslyExisted = Files.exists(resourceFile);
                    boolean fromProxy = fetchContentFromProxies(managedRepositoryContent, request, logicalResource);
                    // legacy layout format.
                    try {
                        // Perform an adjustment of the resource to the managed
                        // repository expected path.
                        String localResourcePath = repositoryRequest.toNativePath(logicalResource.getPath(), managedRepositoryContent);
                        resourceFile = Paths.get(managedRepositoryContent.getRepoRoot(), localResourcePath);
                        resource = new ArchivaDavResource(resourceFile.toAbsolutePath().toString(), logicalResource.getPath(), managedRepositoryContent.getRepository(), request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler, fileLockManager);
                    } catch (LayoutException e) {
                        if (!Files.exists(resourceFile)) {
                            throw new DavException(HttpServletResponse.SC_NOT_FOUND, e);
                        }
                    }
                    if (fromProxy) {
                        String action = (previouslyExisted ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE) + PROXIED_SUFFIX;
                        log.debug("Proxied artifact '{}' in repository '{}' (current user '{}')", resourceFile.getFileName(), managedRepositoryContent.getId(), activePrincipal);
                        triggerAuditEvent(request.getRemoteAddr(), archivaLocator.getRepositoryId(), logicalResource.getPath(), action, activePrincipal);
                    }
                    if (!Files.exists(resourceFile)) {
                        throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
                    }
                }
            }
        }
        if (request.getMethod().equals(HTTP_PUT_METHOD)) {
            String resourcePath = logicalResource.getPath();
            // we suppose that release-artifacts can be deployed only to repos enabled for releases
            if (managedRepositoryContent.getRepository().getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !repositoryRequest.isMetadata(resourcePath) && !repositoryRequest.isSupportFile(resourcePath)) {
                ArtifactReference artifact = null;
                try {
                    artifact = managedRepositoryContent.toArtifactReference(resourcePath);
                    if (!VersionUtil.isSnapshot(artifact.getVersion())) {
                        // check if artifact already exists and if artifact re-deployment to the repository is allowed
                        if (managedRepositoryContent.hasContent(artifact) && managedRepositoryContent.getRepository().blocksRedeployments()) {
                            log.warn("Overwriting released artifacts in repository '{}' is not allowed.", managedRepositoryContent.getId());
                            throw new DavException(HttpServletResponse.SC_CONFLICT, "Overwriting released artifacts is not allowed.");
                        }
                    }
                } catch (LayoutException e) {
                    log.warn("Artifact path '{}' is invalid.", resourcePath);
                }
            }
            /*
                 * Create parent directories that don't exist when writing a file This actually makes this
                 * implementation not compliant to the WebDAV RFC - but we have enough knowledge about how the
                 * collection is being used to do this reasonably and some versions of Maven's WebDAV don't correctly
                 * create the collections themselves.
                 */
            Path rootDirectory = Paths.get(managedRepositoryContent.getRepoRoot());
            Path destDir = rootDirectory.resolve(logicalResource.getPath()).getParent();
            if (!Files.exists(destDir)) {
                try {
                    Files.createDirectories(destDir);
                } catch (IOException e) {
                    log.error("Could not create directory {}: {}", destDir, e.getMessage(), e);
                    throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory " + destDir);
                }
                String relPath = PathUtil.getRelative(rootDirectory.toAbsolutePath().toString(), destDir);
                log.debug("Creating destination directory '{}' (current user '{}')", destDir.getFileName(), activePrincipal);
                triggerAuditEvent(request.getRemoteAddr(), managedRepositoryContent.getId(), relPath, AuditEvent.CREATE_DIR, activePrincipal);
            }
        }
    }
    return resource;
}
Also used : Path(java.nio.file.Path) DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) LayoutException(org.apache.archiva.repository.LayoutException) IOException(java.io.IOException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Aggregations

LayoutException (org.apache.archiva.repository.LayoutException)21 ArtifactReference (org.apache.archiva.model.ArtifactReference)15 Path (java.nio.file.Path)11 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)10 IOException (java.io.IOException)8 VersionedReference (org.apache.archiva.model.VersionedReference)8 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)8 HashSet (java.util.HashSet)6 ProjectReference (org.apache.archiva.model.ProjectReference)6 Files (java.nio.file.Files)4 Paths (java.nio.file.Paths)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 List (java.util.List)4 Set (java.util.Set)4 Stream (java.util.stream.Stream)4 PathUtil (org.apache.archiva.common.utils.PathUtil)4 FileTypes (org.apache.archiva.configuration.FileTypes)4 RepositoryException (org.apache.archiva.repository.RepositoryException)4 StringUtils (org.apache.commons.lang.StringUtils)4