Search in sources :

Example 16 with LayoutException

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

the class Maven2RepositoryStorage method getFilePathWithVersion.

@Override
public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent) throws XMLException, RelocationException {
    if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) {
        return getFilePath(requestPath, managedRepositoryContent.getRepository());
    }
    String filePath = getFilePath(requestPath, managedRepositoryContent.getRepository());
    ArtifactReference artifactReference = null;
    try {
        artifactReference = pathParser.toArtifactReference(filePath);
    } catch (LayoutException e) {
        return filePath;
    }
    if (StringUtils.endsWith(artifactReference.getVersion(), VersionUtil.SNAPSHOT)) {
        // read maven metadata to get last timestamp
        Path metadataDir = Paths.get(managedRepositoryContent.getRepoRoot(), filePath).getParent();
        if (!Files.exists(metadataDir)) {
            return filePath;
        }
        Path metadataFile = metadataDir.resolve(METADATA_FILENAME);
        if (!Files.exists(metadataFile)) {
            return filePath;
        }
        ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(metadataFile);
        int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
        String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
        // MRM-1846
        if (buildNumber < 1 && timestamp == null) {
            return filePath;
        }
        // org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar
        // ->  archiva-checksum-1.4-M4-20130425.081822-1.jar
        filePath = // 
        StringUtils.replace(// 
        filePath, // 
        artifactReference.getArtifactId() + "-" + // 
        artifactReference.getVersion(), // 
        artifactReference.getArtifactId() + "-" + StringUtils.remove(artifactReference.getVersion(), // 
        "-" + VersionUtil.SNAPSHOT) + "-" + // 
        timestamp + "-" + buildNumber);
        throw new RelocationException("/repository/" + managedRepositoryContent.getRepository().getId() + (StringUtils.startsWith(filePath, "/") ? "" : "/") + filePath, RelocationException.RelocationType.TEMPORARY);
    }
    return filePath;
}
Also used : Path(java.nio.file.Path) RelocationException(org.apache.archiva.metadata.repository.storage.RelocationException) LayoutException(org.apache.archiva.repository.LayoutException) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 17 with LayoutException

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

the class MetadataUpdaterConsumer method processFile.

@Override
public void processFile(String path) throws ConsumerException {
    // Ignore paths like .index etc
    if (!path.startsWith(".")) {
        try {
            ArtifactReference artifact = repository.toArtifactReference(path);
            updateVersionMetadata(artifact, path);
            updateProjectMetadata(artifact, path);
        } catch (LayoutException e) {
            log.info("Not processing path that is not an artifact: {} ({})", path, e.getMessage());
        }
    }
}
Also used : LayoutException(org.apache.archiva.repository.LayoutException) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 18 with LayoutException

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

the class DaysOldRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        Path artifactFile = Paths.get(repository.getRepoRoot(), path);
        if (!Files.exists(artifactFile)) {
            return;
        }
        ArtifactReference artifact = repository.toArtifactReference(path);
        Calendar olderThanThisDate = Calendar.getInstance(DateUtils.UTC_TIME_ZONE);
        olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
        // respect retention count
        VersionedReference reference = new VersionedReference();
        reference.setGroupId(artifact.getGroupId());
        reference.setArtifactId(artifact.getArtifactId());
        reference.setVersion(artifact.getVersion());
        List<String> versions = new ArrayList<>(repository.getVersions(reference));
        Collections.sort(versions, VersionComparator.getInstance());
        if (retentionCount > versions.size()) {
            // Done. nothing to do here. skip it.
            return;
        }
        int countToPurge = versions.size() - retentionCount;
        Set<ArtifactReference> artifactsToDelete = new HashSet<>();
        for (String version : versions) {
            if (countToPurge-- <= 0) {
                break;
            }
            ArtifactReference newArtifactReference = repository.toArtifactReference(artifactFile.toAbsolutePath().toString());
            newArtifactReference.setVersion(version);
            Path newArtifactFile = repository.toFile(newArtifactReference);
            // Is this a generic snapshot "1.0-SNAPSHOT" ?
            if (VersionUtil.isGenericSnapshot(newArtifactReference.getVersion())) {
                if (Files.getLastModifiedTime(newArtifactFile).toMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            } else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
            if (VersionUtil.isUniqueSnapshot(newArtifactReference.getVersion())) {
                Calendar timestampCal = uniqueSnapshotToCalendar(newArtifactReference.getVersion());
                if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
                    artifactsToDelete.addAll(repository.getRelatedArtifacts(newArtifactReference));
                }
            }
        }
        purge(artifactsToDelete);
    } catch (ContentNotFoundException | IOException e) {
        throw new RepositoryPurgeException(e.getMessage(), e);
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VersionedReference(org.apache.archiva.model.VersionedReference) LayoutException(org.apache.archiva.repository.LayoutException) ArtifactReference(org.apache.archiva.model.ArtifactReference) HashSet(java.util.HashSet)

Example 19 with LayoutException

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

the class RepositoryRequestTest method testNativePathBadRequestNull.

@Test
public void testNativePathBadRequestNull() throws Exception {
    ManagedRepositoryContent repository = createManagedRepo("default");
    // Test bad request path (too short)
    try {
        repoRequest.toNativePath(null, repository);
        fail("Should have thrown an exception about an null request.");
    } catch (LayoutException e) {
    // expected path.
    }
}
Also used : LayoutException(org.apache.archiva.repository.LayoutException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) Test(org.junit.Test)

Example 20 with LayoutException

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

the class ManagedDefaultRepositoryContent method getVersions.

@Override
public Set<String> getVersions(VersionedReference reference) throws ContentNotFoundException {
    String path = toMetadataPath(reference);
    int idx = path.lastIndexOf('/');
    if (idx > 0) {
        path = path.substring(0, idx);
    }
    Path repoBase = PathUtil.getPathFromUri(repository.getLocation());
    Path repoDir = repoBase.resolve(path);
    if (!Files.exists(repoDir)) {
        throw new ContentNotFoundException("Unable to get versions on a non-existant directory: " + repoDir.toAbsolutePath());
    }
    if (!Files.isDirectory(repoDir)) {
        throw new ContentNotFoundException("Unable to get versions on a non-directory: " + repoDir.toAbsolutePath());
    }
    Set<String> foundVersions = new HashSet<>();
    try (Stream<Path> stream = Files.list(repoDir)) {
        return stream.filter(Files::isRegularFile).map(p -> repoBase.relativize(p).toString()).filter(p -> !filetypes.matchesDefaultExclusions(p)).filter(filetypes::matchesArtifactPattern).map(path1 -> {
            try {
                return toArtifactReference(path1);
            } catch (LayoutException e) {
                log.debug("Not processing file that is not an artifact: {}", e.getMessage());
                return null;
            }
        }).filter(Objects::nonNull).map(ar -> ar.getVersion()).collect(Collectors.toSet());
    } catch (IOException e) {
        log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
    }
    return Collections.emptySet();
}
Also used : Path(java.nio.file.Path) StringUtils(org.apache.commons.lang.StringUtils) PathUtil(org.apache.archiva.common.utils.PathUtil) ArtifactMappingProvider(org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider) DefaultArtifactMappingProvider(org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider) ProjectReference(org.apache.archiva.model.ProjectReference) HashSet(java.util.HashSet) URI(java.net.URI) LayoutException(org.apache.archiva.repository.LayoutException) Path(java.nio.file.Path) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) VersionedReference(org.apache.archiva.model.VersionedReference) Files(java.nio.file.Files) RepositoryException(org.apache.archiva.repository.RepositoryException) Set(java.util.Set) FileTypes(org.apache.archiva.configuration.FileTypes) IOException(java.io.IOException) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) ArchivaArtifact(org.apache.archiva.model.ArchivaArtifact) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) ArtifactReference(org.apache.archiva.model.ArtifactReference) Collections(java.util.Collections) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) LayoutException(org.apache.archiva.repository.LayoutException) Objects(java.util.Objects) IOException(java.io.IOException) HashSet(java.util.HashSet)

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