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();
}
}
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;
}
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);
}
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);
}
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;
}
Aggregations