Search in sources :

Example 26 with LayoutException

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

the class MetadataTools method gatherSnapshotVersions.

/**
 * Gather the set of snapshot versions found in a particular versioned reference.
 *
 * @return the Set of snapshot artifact versions found.
 * @throws LayoutException
 * @throws ContentNotFoundException
 */
public Set<String> gatherSnapshotVersions(ManagedRepositoryContent managedRepository, ItemSelector reference) throws LayoutException, IOException, ContentNotFoundException {
    Set<String> foundVersions = null;
    try {
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(reference.getNamespace()).withProjectId(reference.getArtifactId()).withArtifactId(reference.getArtifactId()).withVersion(reference.getVersion()).build();
        try (Stream<? extends Artifact> stream = managedRepository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
            foundVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
        }
    } catch (ContentAccessException e) {
        log.error("Error while accessing content {}", e.getMessage());
        throw new IOException("Could not access repository content: " + e.getMessage());
    }
    // Next gather up the referenced 'latest' versions found in any proxied repositories
    // maven-metadata-${proxyId}.xml files that may be present.
    // Does this repository have a set of remote proxied repositories?
    Set<String> proxiedRepoIds = this.proxies.get(managedRepository.getId());
    if (CollectionUtils.isNotEmpty(proxiedRepoIds)) {
        String baseVersion = VersionUtil.getBaseVersion(reference.getVersion());
        baseVersion = baseVersion.substring(0, baseVersion.indexOf(VersionUtil.SNAPSHOT) - 1);
        // Add in the proxied repo version ids too.
        Iterator<String> it = proxiedRepoIds.iterator();
        while (it.hasNext()) {
            String proxyId = it.next();
            ArchivaRepositoryMetadata proxyMetadata = readProxyMetadata(managedRepository, reference, proxyId);
            if (proxyMetadata == null) {
                // There is no proxy metadata, skip it.
                continue;
            }
            // Is there some snapshot info?
            SnapshotVersion snapshot = proxyMetadata.getSnapshotVersion();
            if (snapshot != null) {
                String timestamp = snapshot.getTimestamp();
                int buildNumber = snapshot.getBuildNumber();
                // Only interested in the timestamp + buildnumber.
                if (StringUtils.isNotBlank(timestamp) && (buildNumber > 0)) {
                    foundVersions.add(baseVersion + "-" + timestamp + "-" + buildNumber);
                }
            }
        }
    }
    return foundVersions;
}
Also used : Arrays(java.util.Arrays) ConfigurationListener(org.apache.archiva.configuration.provider.ConfigurationListener) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Date(java.util.Date) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) RegistryListener(org.apache.archiva.components.registry.RegistryListener) Matcher(java.util.regex.Matcher) Map(java.util.Map) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ConfigurationEvent(org.apache.archiva.configuration.provider.ConfigurationEvent) ParseException(java.text.ParseException) ContentItem(org.apache.archiva.repository.content.ContentItem) RemoteRepositoryContent(org.apache.archiva.repository.RemoteRepositoryContent) FileTypes(org.apache.archiva.configuration.provider.FileTypes) TimeZone(java.util.TimeZone) Collection(java.util.Collection) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) Set(java.util.Set) Collectors(java.util.stream.Collectors) Plugin(org.apache.archiva.model.Plugin) List(java.util.List) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Artifact(org.apache.archiva.repository.content.Artifact) ArchivaConfiguration(org.apache.archiva.configuration.provider.ArchivaConfiguration) ConfigurationNames(org.apache.archiva.configuration.model.ConfigurationNames) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HashSet(java.util.HashSet) ProxyConnectorConfiguration(org.apache.archiva.configuration.model.ProxyConnectorConfiguration) Calendar(java.util.Calendar) Service(org.springframework.stereotype.Service) ChecksumAlgorithm(org.apache.archiva.checksum.ChecksumAlgorithm) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) Named(javax.inject.Named) VersionComparator(org.apache.archiva.common.utils.VersionComparator) LinkedHashSet(java.util.LinkedHashSet) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException) Project(org.apache.archiva.repository.content.Project) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ContentNotFoundException(org.apache.archiva.repository.content.ContentNotFoundException) VersionUtil(org.apache.archiva.common.utils.VersionUtil) IOException(java.io.IOException) RepositoryType(org.apache.archiva.repository.RepositoryType) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) Registry(org.apache.archiva.components.registry.Registry) MetadataReader(org.apache.archiva.repository.metadata.MetadataReader) NumberUtils(org.apache.commons.lang3.math.NumberUtils) LayoutException(org.apache.archiva.repository.content.LayoutException) Collections(java.util.Collections) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) IOException(java.io.IOException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StringUtils(org.apache.commons.lang3.StringUtils) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Example 27 with LayoutException

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

the class Maven2RepositoryStorage method applyServerSideRelocation.

@Override
public ItemSelector applyServerSideRelocation(ManagedRepository managedRepository, ItemSelector artifactSelector) throws ProxyDownloadException {
    if ("pom".equals(artifactSelector.getType())) {
        return artifactSelector;
    }
    // Build the artifact POM reference
    BaseRepositoryContentLayout layout;
    try {
        layout = managedRepository.getContent().getLayout(BaseRepositoryContentLayout.class);
    } catch (LayoutException e) {
        throw new ProxyDownloadException("Could not set layout " + e.getMessage(), new HashMap<>());
    }
    RepositoryType repositoryType = managedRepository.getType();
    if (!proxyRegistry.hasHandler(repositoryType)) {
        throw new ProxyDownloadException("No proxy handler found for repository type " + repositoryType, new HashMap<>());
    }
    ItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifactSelector.getNamespace()).withProjectId(artifactSelector.getArtifactId()).withArtifactId(artifactSelector.getArtifactId()).withVersion(artifactSelector.getVersion()).withArtifactVersion(artifactSelector.getVersion()).withType("pom").build();
    Artifact pom = layout.getArtifact(selector);
    RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(repositoryType).get(0);
    // Get the artifact POM from proxied repositories if needed
    proxyHandler.fetchFromProxies(managedRepository, pom);
    if (!pom.exists()) {
        return artifactSelector;
    }
    try {
        // MavenXpp3Reader leaves the file open, so we need to close it ourselves.
        Model model;
        try (Reader reader = Channels.newReader(pom.getAsset().getReadChannel(), Charset.defaultCharset().name())) {
            model = MAVEN_XPP_3_READER.read(reader);
        }
        DistributionManagement dist = model.getDistributionManagement();
        if (dist != null) {
            Relocation relocation = dist.getRelocation();
            if (relocation != null) {
                ArchivaItemSelector.Builder relocatedBuilder = ArchivaItemSelector.builder();
                // artifact is relocated : update the repositoryPath
                if (relocation.getGroupId() != null) {
                    relocatedBuilder.withNamespace(relocation.getGroupId());
                } else {
                    relocatedBuilder.withNamespace(artifactSelector.getNamespace());
                }
                if (relocation.getArtifactId() != null) {
                    relocatedBuilder.withArtifactId(relocation.getArtifactId());
                } else {
                    relocatedBuilder.withArtifactId(artifactSelector.getArtifactId());
                }
                if (relocation.getVersion() != null) {
                    relocatedBuilder.withVersion(relocation.getVersion());
                } else {
                    relocatedBuilder.withVersion(artifactSelector.getVersion());
                }
                return relocatedBuilder.withArtifactVersion(artifactSelector.getArtifactVersion()).withClassifier(artifactSelector.getClassifier()).withType(artifactSelector.getType()).withProjectId(artifactSelector.getProjectId()).withExtension(artifactSelector.getExtension()).build();
            }
        }
    } catch (IOException e) {
    // Unable to read POM : ignore.
    } catch (XmlPullParserException e) {
    // Invalid POM : ignore
    }
    return artifactSelector;
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) HashMap(java.util.HashMap) RepositoryType(org.apache.archiva.repository.RepositoryType) MavenMetadataReader(org.apache.archiva.maven.metadata.MavenMetadataReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) Artifact(org.apache.archiva.repository.content.Artifact) ItemSelector(org.apache.archiva.repository.content.ItemSelector) ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) DistributionManagement(org.apache.maven.model.DistributionManagement) Relocation(org.apache.maven.model.Relocation) RepositoryProxyHandler(org.apache.archiva.proxy.model.RepositoryProxyHandler)

Example 28 with LayoutException

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

the class Maven2RepositoryStorage method getFilePathWithVersion.

@Override
public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent) throws RelocationException {
    if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) {
        return getFilePath(requestPath, managedRepositoryContent.getRepository());
    }
    String filePath = getFilePath(requestPath, managedRepositoryContent.getRepository());
    Artifact artifact;
    try {
        ContentItem item = managedRepositoryContent.toItem(filePath);
        artifact = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class).adaptItem(Artifact.class, item);
    } catch (LayoutException e) {
        return filePath;
    }
    if (VersionUtil.isGenericSnapshot(artifact.getArtifactVersion())) {
        // read maven metadata to get last timestamp
        StorageAsset metadataDir = managedRepositoryContent.getRepository().getAsset(filePath).getParent();
        if (!metadataDir.exists()) {
            return filePath;
        }
        StorageAsset metadataFile = metadataDir.resolve(METADATA_FILENAME);
        if (!metadataFile.exists()) {
            return filePath;
        }
        ArchivaRepositoryMetadata archivaRepositoryMetadata = null;
        try {
            archivaRepositoryMetadata = metadataReader.read(metadataFile);
        } catch (RepositoryMetadataException e) {
            log.error("Could not read metadata {}", e.getMessage(), e);
            return filePath;
        }
        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, // 
        artifact.getId() + "-" + // 
        artifact.getVersion().getId(), // 
        artifact.getId() + "-" + StringUtils.remove(artifact.getArtifactVersion(), // 
        "-" + VersionUtil.SNAPSHOT) + "-" + // 
        timestamp + "-" + buildNumber);
        throw new RelocationException("/repository/" + managedRepositoryContent.getRepository().getId() + (StringUtils.startsWith(filePath, "/") ? "" : "/") + filePath, RelocationException.RelocationType.TEMPORARY);
    }
    return filePath;
}
Also used : RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) RelocationException(org.apache.archiva.metadata.repository.storage.RelocationException) LayoutException(org.apache.archiva.repository.content.LayoutException) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata) Artifact(org.apache.archiva.repository.content.Artifact) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 29 with LayoutException

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

the class MavenRepositoryRequestInfoTest method testNativePathBadRequestBlank.

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

Aggregations

LayoutException (org.apache.archiva.repository.content.LayoutException)29 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)14 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)14 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)13 Artifact (org.apache.archiva.repository.content.Artifact)12 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)12 IOException (java.io.IOException)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 ContentItem (org.apache.archiva.repository.content.ContentItem)8 ItemSelector (org.apache.archiva.repository.content.ItemSelector)8 ContentAccessException (org.apache.archiva.repository.content.ContentAccessException)6 Version (org.apache.archiva.repository.content.Version)6 DavException (org.apache.jackrabbit.webdav.DavException)6 ArrayList (java.util.ArrayList)5 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)4 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)4 Project (org.apache.archiva.repository.content.Project)4 DavResource (org.apache.jackrabbit.webdav.DavResource)4 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3