use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultRepositoryTaskAdministration method scheduleIndexScan.
@Override
public void scheduleIndexScan(String repositoryId, String relativePath) throws RepositoryAdminException {
if (StringUtils.isEmpty(repositoryId)) {
throw RepositoryAdminException.ofKey("repository.id.invalid", "");
}
try {
org.apache.archiva.repository.ManagedRepository repository = repositoryRegistry.getManagedRepository(repositoryId);
if (repository == null) {
throw RepositoryAdminException.ofKey("repository.not_found", repositoryId);
}
StorageAsset asset = repository.getAsset(relativePath);
if (!asset.exists()) {
throw RepositoryAdminException.ofKey("repository.file.not_found", repositoryId, relativePath);
}
ArtifactIndexingTask task = new ArtifactIndexingTask(repository, asset.getFilePath(), ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext());
task.setExecuteOnEntireRepo(false);
task.setOnlyUpdate(true);
indexingArchivaTaskScheduler.queueTask(task);
} catch (TaskQueueException e) {
log.error("Could not queue the task: {}", e.getMessage(), e);
throw RepositoryAdminException.ofKey("repository.scan.task_queue_error", e, e.getMessage());
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultRepositoriesService method deleteArtifact.
@Override
public ActionStatus deleteArtifact(Artifact artifact) throws ArchivaRestServiceException {
String repositoryId = artifact.getContext();
// so try both!!
if (StringUtils.isEmpty(repositoryId)) {
repositoryId = artifact.getRepositoryId();
}
if (StringUtils.isEmpty(repositoryId)) {
throw new ArchivaRestServiceException("repositoryId cannot be null", 400, null);
}
if (!getPermissionStatus(repositoryId).isAuthorizedToDeleteArtifacts()) {
throw new ArchivaRestServiceException("not authorized to delete artifacts", 403, null);
}
if (artifact == null) {
throw new ArchivaRestServiceException("artifact cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getGroupId())) {
throw new ArchivaRestServiceException("artifact.groupId cannot be null", 400, null);
}
if (StringUtils.isEmpty(artifact.getArtifactId())) {
throw new ArchivaRestServiceException("artifact.artifactId cannot be null", 400, null);
}
// TODO more control on artifact fields
boolean snapshotVersion = VersionUtil.isSnapshot(artifact.getVersion()) | VersionUtil.isGenericSnapshot(artifact.getVersion());
String baseVersion = VersionUtil.getBaseVersion(artifact.getVersion());
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
TimeZone timezone = TimeZone.getTimeZone("UTC");
DateFormat fmt = new SimpleDateFormat("yyyyMMdd.HHmmss");
fmt.setTimeZone(timezone);
ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryId);
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
BaseRepositoryContentLayout layout = repository.getLayout(BaseRepositoryContentLayout.class);
ArchivaItemSelector versionSelector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).build();
Version version1 = layout.getVersion(versionSelector);
String path = repository.toPath(version1);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace(artifact.getGroupId()).withProjectId(artifact.getArtifactId()).withVersion(baseVersion).withClassifier(artifact.getClassifier()).withArtifactId(artifact.getArtifactId()).withType(artifact.getType()).includeRelatedArtifacts().build();
MetadataRepository metadataRepository = repositorySession.getRepository();
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
List<? extends org.apache.archiva.repository.content.Artifact> artifactItems = layout.getArtifacts(selector);
for (org.apache.archiva.repository.content.Artifact aRef : artifactItems) {
try {
repository.deleteItem(aRef);
} catch (ItemNotFoundException e) {
log.error("Could not delete item, seems to be deleted by other thread. {}, {} ", aRef, e.getMessage());
}
}
} else {
int index = path.lastIndexOf('/');
path = path.substring(0, index);
StorageAsset targetPath = repo.getAsset(path);
if (!targetPath.exists()) {
// throw new ContentNotFoundException(
// artifact.getNamespace() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() );
log.warn("targetPath {} not found skip file deletion", targetPath);
return ActionStatus.FAIL;
}
// delete from file system
if (!snapshotVersion && version1.exists()) {
try {
repository.deleteItem(version1);
} catch (ItemNotFoundException e) {
log.error("Could not delete version item {}", e.getMessage());
}
} else {
// We are deleting all version related artifacts for a snapshot version
for (org.apache.archiva.repository.content.Artifact delArtifact : layout.getArtifacts(selector)) {
try {
repository.deleteItem(delArtifact);
} catch (ItemNotFoundException e) {
log.warn("Artifact that should be deleted, was not found: {}", delArtifact);
}
}
StorageAsset metadataFile = getMetadata(repo, targetPath.getPath());
ArchivaRepositoryMetadata metadata = getMetadata(repository.getRepository().getType(), metadataFile);
updateMetadata(metadata, metadataFile, lastUpdatedTimestamp, artifact);
}
}
Collection<ArtifactMetadata> artifacts = Collections.emptyList();
if (snapshotVersion) {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), baseVersion);
} else {
artifacts = metadataRepository.getArtifacts(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
log.debug("artifacts: {}", artifacts);
if (artifacts.isEmpty()) {
if (!snapshotVersion) {
// verify metata repository doesn't contains anymore the version
Collection<String> projectVersions = metadataRepository.getProjectVersions(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId());
if (projectVersions.contains(artifact.getVersion())) {
log.warn("artifact not found when deleted but version still here ! so force cleanup");
metadataRepository.removeProjectVersion(repositorySession, repositoryId, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}
}
}
for (ArtifactMetadata artifactMetadata : artifacts) {
// TODO: mismatch between artifact (snapshot) version and project (base) version here
if (artifactMetadata.getVersion().equals(artifact.getVersion())) {
if (StringUtils.isNotBlank(artifact.getClassifier())) {
if (StringUtils.isBlank(artifact.getPackaging())) {
throw new ArchivaRestServiceException("You must configure a type/packaging when using classifier", 400, null);
}
// cleanup facet which contains classifier information
MavenArtifactFacet mavenArtifactFacet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
if (StringUtils.equals(artifact.getClassifier(), mavenArtifactFacet.getClassifier())) {
artifactMetadata.removeFacet(MavenArtifactFacet.FACET_ID);
String groupId = artifact.getGroupId(), artifactId = artifact.getArtifactId(), version = artifact.getVersion();
MavenArtifactFacet mavenArtifactFacetToCompare = new MavenArtifactFacet();
mavenArtifactFacetToCompare.setClassifier(artifact.getClassifier());
metadataRepository.removeFacetFromArtifact(repositorySession, repositoryId, groupId, artifactId, version, mavenArtifactFacetToCompare);
repositorySession.save();
}
} else {
if (snapshotVersion) {
metadataRepository.removeTimestampedArtifact(repositorySession, artifactMetadata, VersionUtil.getBaseVersion(artifact.getVersion()));
} else {
metadataRepository.removeArtifact(repositorySession, artifactMetadata.getRepositoryId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifact.getVersion(), artifactMetadata.getId());
}
}
// repository metadata to an artifact
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), artifactMetadata.getNamespace(), artifactMetadata.getProject(), artifactMetadata.getVersion(), artifactMetadata.getId());
}
triggerAuditEvent(repositoryId, path, AuditEvent.REMOVE_FILE);
}
}
} catch (ContentNotFoundException e) {
throw new ArchivaRestServiceException("Artifact does not exist: " + e.getMessage(), 400, e);
} catch (RepositoryNotFoundException e) {
throw new ArchivaRestServiceException("Target repository cannot be found: " + e.getMessage(), 400, e);
} catch (RepositoryException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (MetadataResolutionException | MetadataSessionException | MetadataRepositoryException | LayoutException e) {
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
try {
repositorySession.save();
} catch (MetadataSessionException e) {
log.error("Could not save sesion {}", e.getMessage());
}
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultFileUploadService method updateProjectMetadata.
/**
* Update artifact level metadata. If it does not exist, create the metadata and fix checksums if necessary.
*/
private void updateProjectMetadata(StorageAsset targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber, boolean fixChecksums, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws RepositoryMetadataException {
List<String> availableVersions = new ArrayList<>();
String latestVersion = version;
StorageAsset projectDir = targetPath.getParent();
StorageAsset projectMetadataFile = projectDir.resolve(MetadataTools.MAVEN_METADATA);
ArchivaRepositoryMetadata projectMetadata = getMetadata(projectMetadataFile);
if (projectMetadataFile.exists()) {
availableVersions = projectMetadata.getAvailableVersions();
Collections.sort(availableVersions, VersionComparator.getInstance());
if (!availableVersions.contains(version)) {
availableVersions.add(version);
}
latestVersion = availableVersions.get(availableVersions.size() - 1);
} else {
availableVersions.add(version);
projectMetadata.setGroupId(groupId);
projectMetadata.setArtifactId(artifactId);
}
if (projectMetadata.getGroupId() == null) {
projectMetadata.setGroupId(groupId);
}
if (projectMetadata.getArtifactId() == null) {
projectMetadata.setArtifactId(artifactId);
}
projectMetadata.setLatestVersion(latestVersion);
projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
projectMetadata.setAvailableVersions(availableVersions);
if (!VersionUtil.isSnapshot(version)) {
projectMetadata.setReleasedVersion(latestVersion);
}
RepositoryMetadataWriter.write(projectMetadata, projectMetadataFile);
if (fixChecksums) {
fixChecksums(projectMetadataFile);
}
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class DefaultFileUploadService method createPom.
private StorageAsset createPom(StorageAsset targetPath, String filename, FileMetadata fileMetadata, String groupId, String artifactId, String version, String packaging) throws IOException {
Model projectModel = new Model();
projectModel.setModelVersion("4.0.0");
projectModel.setGroupId(groupId);
projectModel.setArtifactId(artifactId);
projectModel.setVersion(version);
projectModel.setPackaging(packaging);
StorageAsset pomFile = targetPath.resolve(filename);
MavenXpp3Writer writer = new MavenXpp3Writer();
try (Writer w = new OutputStreamWriter(pomFile.getWriteStream(true))) {
writer.write(w, projectModel);
}
return pomFile;
}
use of org.apache.archiva.repository.storage.StorageAsset in project archiva by apache.
the class Maven2RepositoryStorage method isProject.
private boolean isProject(StorageAsset dir, Filter<String> filter) {
// scan directories for a valid project version subdirectory, meaning this must be a project directory
final Predicate<StorageAsset> dFilter = new DirectoryFilter(filter);
boolean projFound = dir.list().stream().filter(dFilter).anyMatch(path -> isProjectVersion(path));
if (projFound) {
return true;
}
// if a metadata file is present, check if this is the "artifactId" directory, marking it as a project
ArchivaRepositoryMetadata metadata = readMetadata(dir);
if (metadata != null && dir.getName().toString().equals(metadata.getArtifactId())) {
return true;
}
return false;
}
Aggregations