Search in sources :

Example 16 with ContentItem

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

the class DaysOldRepositoryPurge method process.

@Override
public void process(String path) throws RepositoryPurgeException {
    try {
        ContentItem item = repository.toItem(path);
        Artifact artifactItem = repository.getLayout(BaseRepositoryContentLayout.class).adaptItem(Artifact.class, item);
        if (!artifactItem.exists()) {
            return;
        }
        // ArtifactReference artifact = repository.toArtifactReference( path );
        Calendar olderThanThisDate = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        olderThanThisDate.add(Calendar.DATE, -retentionPeriod);
        ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withClassifier("*").includeRelatedArtifacts().build();
        List<String> artifactVersions;
        try (Stream<? extends Artifact> stream = repository.getLayout(BaseRepositoryContentLayout.class).newArtifactStream(selector)) {
            artifactVersions = stream.map(a -> a.getArtifactVersion()).filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
        }
        Collections.sort(artifactVersions, VersionComparator.getInstance());
        if (retentionCount > artifactVersions.size()) {
            // Done. nothing to do here. skip it.
            return;
        }
        int countToPurge = artifactVersions.size() - retentionCount;
        ArchivaItemSelector.Builder artifactSelectorBuilder = ArchivaItemSelector.builder().withNamespace(artifactItem.getVersion().getProject().getNamespace().getId()).withProjectId(artifactItem.getVersion().getProject().getId()).withVersion(artifactItem.getVersion().getId()).withArtifactId(artifactItem.getId()).withClassifier("*").includeRelatedArtifacts();
        Set<Artifact> artifactsToDelete = new HashSet<>();
        for (String version : artifactVersions) {
            if (countToPurge-- <= 0) {
                break;
            }
            ArchivaItemSelector artifactSelector = artifactSelectorBuilder.withArtifactVersion(version).build();
            try {
                // Is this a generic snapshot "1.0-SNAPSHOT" ?
                if (VersionUtil.isGenericSnapshot(version)) {
                    List<? extends Artifact> artifactList = repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector);
                    if (artifactList.size() > 0 && artifactList.get(0).getAsset().getModificationTime().toEpochMilli() < olderThanThisDate.getTimeInMillis()) {
                        artifactsToDelete.addAll(artifactList);
                    }
                } else // Is this a timestamp snapshot "1.0-20070822.123456-42" ?
                if (VersionUtil.isUniqueSnapshot(version)) {
                    Calendar timestampCal = uniqueSnapshotToCalendar(version);
                    if (timestampCal.getTimeInMillis() < olderThanThisDate.getTimeInMillis()) {
                        artifactsToDelete.addAll(repository.getLayout(BaseRepositoryContentLayout.class).getArtifacts(artifactSelector));
                    }
                }
            } catch (IllegalArgumentException e) {
                log.error("Bad selector for artifact: {}", e.getMessage(), e);
            // continue
            }
        }
        purge(artifactsToDelete);
    } catch (LayoutException e) {
        log.debug("Not processing file that is not an artifact: {}", e.getMessage());
    } catch (ContentAccessException e) {
        e.printStackTrace();
    }
}
Also used : ArchivaItemSelector(org.apache.archiva.repository.content.base.ArchivaItemSelector) Calendar(java.util.Calendar) Artifact(org.apache.archiva.repository.content.Artifact) LayoutException(org.apache.archiva.repository.content.LayoutException) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) ContentItem(org.apache.archiva.repository.content.ContentItem) HashSet(java.util.HashSet) ContentAccessException(org.apache.archiva.repository.content.ContentAccessException)

Example 17 with ContentItem

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

the class DefaultRepositoryProxyHandler method fetchFromProxies.

@Override
public StorageAsset fetchFromProxies(ManagedRepository repository, ItemSelector artifactSelector) throws ProxyDownloadException {
    Map<String, Exception> previousExceptions = new LinkedHashMap<>();
    ContentItem item = repository.getContent().getItem(artifactSelector);
    StorageAsset localFile = item.getAsset();
    Properties requestProperties = new Properties();
    requestProperties.setProperty("filetype", "artifact");
    requestProperties.setProperty("version", artifactSelector.getVersion());
    requestProperties.setProperty("managedRepositoryId", repository.getId());
    List<ProxyConnector> connectors = getProxyConnectors(repository);
    for (ProxyConnector connector : connectors) {
        if (!connector.isEnabled()) {
            continue;
        }
        RemoteRepository targetRepository = connector.getTargetRepository();
        requestProperties.setProperty("remoteRepositoryId", targetRepository.getId());
        StorageAsset targetFile = targetRepository.getAsset(localFile.getPath());
        // Removing the leading '/' from the path
        String targetPath = targetFile.getPath().substring(1);
        try {
            StorageAsset downloadedFile = transferFile(connector, targetRepository, targetPath, repository, localFile, requestProperties, true);
            if (fileExists(downloadedFile)) {
                log.debug("Successfully transferred: {}", downloadedFile.getPath());
                return downloadedFile;
            }
        } catch (NotFoundException e) {
            log.debug("Artifact {} not found on repository \"{}\".", item, targetRepository.getId());
        } catch (NotModifiedException e) {
            log.debug("Artifact {} not updated on repository \"{}\".", item, targetRepository.getId());
        } catch (ProxyException e) {
            validatePolicies(this.downloadErrorPolicies, connector.getPolicies(), requestProperties, item, targetRepository.getContent(), localFile, e, previousExceptions);
        }
    }
    if (!previousExceptions.isEmpty()) {
        throw new ProxyDownloadException("Failures occurred downloading from some remote repositories", previousExceptions);
    }
    log.debug("Exhausted all target repositories, artifact {} not found.", item);
    return null;
}
Also used : RemoteRepository(org.apache.archiva.repository.RemoteRepository) Properties(java.util.Properties) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) PolicyConfigurationException(org.apache.archiva.policies.PolicyConfigurationException) ProxyDownloadException(org.apache.archiva.policies.ProxyDownloadException) RepositoryMetadataException(org.apache.archiva.repository.metadata.RepositoryMetadataException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PolicyViolationException(org.apache.archiva.policies.PolicyViolationException) TaskQueueException(org.apache.archiva.components.taskqueue.TaskQueueException) LinkedHashMap(java.util.LinkedHashMap) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 18 with ContentItem

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

the class MetadataTransferTest method assertFetchVersioned.

/**
 * Transfer the metadata file.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchVersioned(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    ContentItem item = managedDefaultRepository.toItem(requestedResource);
    if (item instanceof DataItem) {
        item = managedDefaultRepository.getParent(item);
    }
    assertNotNull(item);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    Version version = layout.adaptItem(Version.class, item);
    assertNotNull(version);
    String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(version));
    StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
    assertNotNull("Should have downloaded a file.", downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) DataItem(org.apache.archiva.repository.content.DataItem) Version(org.apache.archiva.repository.content.Version) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 19 with ContentItem

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

the class MetadataTransferTest method assertFetchProjectOrGroupFailed.

/**
 * Transfer the metadata file, not expected to succeed.
 *
 * @param requestedResource the requested resource
 * @throws Exception
 */
private void assertFetchProjectOrGroupFailed(String requestedResource) throws Exception {
    Path expectedFile = managedDefaultDir.resolve(requestedResource);
    BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
    ContentItem metaItem = managedDefaultRepository.toItem(requestedResource);
    Project project = layout.adaptItem(Project.class, managedDefaultRepository.getParent(metaItem));
    assertNotNull(project);
    String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(project));
    StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
    assertNull(downloadedFile);
    assertNoTempFiles(expectedFile);
}
Also used : Path(java.nio.file.Path) Project(org.apache.archiva.repository.content.Project) BaseRepositoryContentLayout(org.apache.archiva.repository.content.BaseRepositoryContentLayout) StorageAsset(org.apache.archiva.repository.storage.StorageAsset) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 20 with ContentItem

use of org.apache.archiva.repository.content.ContentItem 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)

Aggregations

ContentItem (org.apache.archiva.repository.content.ContentItem)29 ArchivaItemSelector (org.apache.archiva.repository.content.base.ArchivaItemSelector)16 ArchivaContentItem (org.apache.archiva.repository.content.base.ArchivaContentItem)15 Test (org.junit.Test)14 Path (java.nio.file.Path)12 StorageAsset (org.apache.archiva.repository.storage.StorageAsset)12 Artifact (org.apache.archiva.repository.content.Artifact)10 BaseRepositoryContentLayout (org.apache.archiva.repository.content.BaseRepositoryContentLayout)10 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)9 Project (org.apache.archiva.repository.content.Project)9 ManagedRepository (org.apache.archiva.repository.ManagedRepository)8 LayoutException (org.apache.archiva.repository.content.LayoutException)8 Version (org.apache.archiva.repository.content.Version)8 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)7 ItemSelector (org.apache.archiva.repository.content.ItemSelector)7 MavenManagedRepository (org.apache.archiva.maven.repository.MavenManagedRepository)6 DataItem (org.apache.archiva.repository.content.DataItem)6 Namespace (org.apache.archiva.repository.content.Namespace)6 IOException (java.io.IOException)5 ItemNotFoundException (org.apache.archiva.repository.content.ItemNotFoundException)5