Search in sources :

Example 11 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTools method toVersionedReference.

/**
 * Take a path to a maven-metadata.xml, and attempt to translate it to a VersionedReference.
 *
 * @param path
 * @return
 */
public VersionedReference toVersionedReference(String path) throws RepositoryMetadataException {
    if (!path.endsWith("/" + MAVEN_METADATA)) {
        throw new RepositoryMetadataException("Cannot convert to versioned reference, not a metadata file. ");
    }
    VersionedReference reference = new VersionedReference();
    String normalizedPath = StringUtils.replace(path, "\\", "/");
    String[] pathParts = StringUtils.split(normalizedPath, '/');
    int versionOffset = pathParts.length - 2;
    int artifactIdOffset = versionOffset - 1;
    int groupIdEnd = artifactIdOffset - 1;
    reference.setVersion(pathParts[versionOffset]);
    if (!hasNumberAnywhere(reference.getVersion())) {
        // Scary check, but without it, all paths are version references;
        throw new RepositoryMetadataException("Not a versioned reference, as version id on path has no number in it.");
    }
    reference.setArtifactId(pathParts[artifactIdOffset]);
    StringBuilder gid = new StringBuilder();
    for (int i = 0; i <= groupIdEnd; i++) {
        if (i > 0) {
            gid.append(".");
        }
        gid.append(pathParts[i]);
    }
    reference.setGroupId(gid.toString());
    return reference;
}
Also used : VersionedReference(org.apache.archiva.model.VersionedReference)

Example 12 with VersionedReference

use of org.apache.archiva.model.VersionedReference 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 13 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTransferTest method assertReleaseMetadataContents.

/**
 * Test for the existance of the requestedResource in the default managed repository, and if it exists,
 * does it contain the expected release maven-metadata.xml contents?
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertReleaseMetadataContents(String requestedResource) throws Exception {
    Path actualFile = managedDefaultDir.resolve(requestedResource);
    assertTrue("Release Metadata should exist: " + requestedResource, Files.exists(actualFile));
    VersionedReference metadata = createVersionedReference(requestedResource);
    // Build expected metadata XML
    StringWriter expectedMetadataXml = new StringWriter();
    ArchivaRepositoryMetadata m = new ArchivaRepositoryMetadata();
    m.setGroupId(metadata.getGroupId());
    m.setArtifactId(metadata.getArtifactId());
    m.setVersion(metadata.getVersion());
    RepositoryMetadataWriter.write(m, expectedMetadataXml);
    // Compare the file to the actual contents.
    assertMetadataEquals(expectedMetadataXml.toString(), actualFile);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) StringWriter(java.io.StringWriter) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 14 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTransferTest method assertSnapshotMetadataContents.

/**
 * Test for the existance of the snapshot metadata in the default managed repository, and if it exists,
 * does it contain the expected release maven-metadata.xml contents?
 *
 * @param requestedResource   the requested resource
 * @param expectedDate        the date in "yyyyMMdd" format
 * @param expectedTime        the time in "hhmmss" format
 * @param expectedBuildnumber the build number
 * @throws Exception
 */
private void assertSnapshotMetadataContents(String requestedResource, String expectedDate, String expectedTime, int expectedBuildnumber) throws Exception {
    Path actualFile = managedDefaultDir.resolve(requestedResource);
    assertTrue("Snapshot Metadata should exist: " + requestedResource, Files.exists(actualFile));
    VersionedReference actualMetadata = createVersionedReference(requestedResource);
    assertSnapshotMetadata(actualFile, actualMetadata, expectedDate, expectedTime, expectedBuildnumber);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference)

Example 15 with VersionedReference

use of org.apache.archiva.model.VersionedReference in project archiva by apache.

the class MetadataTransferTest method assertFetchVersionedFailed.

/**
 * Transfer the metadata file, not expected to succeed.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchVersionedFailed(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    VersionedReference metadata = createVersionedReference(requestedResource);
    Path downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository, managedDefaultRepository.toMetadataPath(metadata)).getFile();
    assertNull(downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference)

Aggregations

VersionedReference (org.apache.archiva.model.VersionedReference)24 Path (java.nio.file.Path)15 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)8 ArtifactReference (org.apache.archiva.model.ArtifactReference)7 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)7 ArrayList (java.util.ArrayList)6 LayoutException (org.apache.archiva.repository.LayoutException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)4 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)4 ProjectReference (org.apache.archiva.model.ProjectReference)4 RepositoryException (org.apache.archiva.repository.RepositoryException)4 VersionComparator (org.apache.archiva.common.utils.VersionComparator)3 MetadataRepository (org.apache.archiva.metadata.repository.MetadataRepository)3 MetadataRepositoryException (org.apache.archiva.metadata.repository.MetadataRepositoryException)3 StringWriter (java.io.StringWriter)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2