Search in sources :

Example 1 with FileTypes

use of org.apache.archiva.configuration.FileTypes 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 2 with FileTypes

use of org.apache.archiva.configuration.FileTypes in project archiva by apache.

the class NexusIndexerConsumerTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    scheduler = new ArchivaTaskSchedulerStub();
    ArchivaConfiguration configuration = applicationContext.getBean(ArchivaConfiguration.class);
    FileTypes filetypes = applicationContext.getBean(FileTypes.class);
    nexusIndexerConsumer = new NexusIndexerConsumer(scheduler, configuration, filetypes, indexCreators, managedRepositoryAdmin);
    // initialize to set the file types to be processed
    nexusIndexerConsumer.initialize();
    repositoryConfig = new BasicManagedRepository("test-repo", "Test Repository", Paths.get("target/test-classes"));
    repositoryConfig.setLocation(new URI("target/test-classes/test-repo"));
    repositoryConfig.setLayout("default");
    repositoryConfig.setScanned(true);
    repositoryConfig.addActiveReleaseScheme(ReleaseScheme.RELEASE);
    repositoryConfig.removeActiveReleaseScheme(ReleaseScheme.SNAPSHOT);
    repositoryRegistry.putRepository(repositoryConfig);
}
Also used : BasicManagedRepository(org.apache.archiva.repository.BasicManagedRepository) FileTypes(org.apache.archiva.configuration.FileTypes) URI(java.net.URI) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) Before(org.junit.Before)

Example 3 with FileTypes

use of org.apache.archiva.configuration.FileTypes in project archiva by apache.

the class RepositoryPurgeConsumerTest method assertNotConsumed.

@SuppressWarnings("deprecation")
private void assertNotConsumed(String path) throws Exception {
    ArchivaConfiguration archivaConfiguration = applicationContext.getBean("archivaConfiguration#default", ArchivaConfiguration.class);
    FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get(0);
    assertEquals(FileTypes.ARTIFACTS, fileType.getId());
    fileType.addPattern("**/*.xml");
    // FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
    for (FileTypes fileTypes : applicationContext.getBeansOfType(FileTypes.class).values()) {
        fileTypes.afterConfigurationChange(null, "repositoryScanning.fileTypes", null);
    }
    KnownRepositoryContentConsumer repoPurgeConsumer = applicationContext.getBean("knownRepositoryContentConsumer#repository-purge", KnownRepositoryContentConsumer.class);
    Path repoLocation = Paths.get("target/test-" + getName() + "/test-repo");
    Path localFile = repoLocation.resolve(path);
    ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
    BaseFile baseFile = new BaseFile(repoLocation.toFile(), localFile.toFile());
    predicate.setBasefile(baseFile);
    assertFalse(predicate.evaluate(repoPurgeConsumer));
}
Also used : Path(java.nio.file.Path) KnownRepositoryContentConsumer(org.apache.archiva.consumers.KnownRepositoryContentConsumer) FileType(org.apache.archiva.configuration.FileType) BaseFile(org.apache.archiva.common.utils.BaseFile) FileTypes(org.apache.archiva.configuration.FileTypes) ConsumerWantsFilePredicate(org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration)

Example 4 with FileTypes

use of org.apache.archiva.configuration.FileTypes 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

FileTypes (org.apache.archiva.configuration.FileTypes)4 Path (java.nio.file.Path)3 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)3 IOException (java.io.IOException)2 URI (java.net.URI)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 PathUtil (org.apache.archiva.common.utils.PathUtil)2 ArtifactReference (org.apache.archiva.model.ArtifactReference)2 ProjectReference (org.apache.archiva.model.ProjectReference)2 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Calendar (java.util.Calendar)1