use of org.apache.archiva.repository.LayoutException in project archiva by apache.
the class ManagedDefaultRepositoryContent method getVersions.
/**
* Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
* information.
*
* @return the Set of available versions, based on the project reference.
* @throws LayoutException
* @throws LayoutException
*/
@Override
public Set<String> getVersions(ProjectReference reference) throws ContentNotFoundException, LayoutException {
String path = toMetadataPath(reference);
int idx = path.lastIndexOf('/');
if (idx > 0) {
path = path.substring(0, idx);
}
Path repoDir = PathUtil.getPathFromUri(repository.getLocation()).resolve(path);
if (!Files.exists(repoDir)) {
throw new ContentNotFoundException("Unable to get Versions on a non-existant directory: " + repoDir.toAbsolutePath());
}
if (!Files.isDirectory(repoDir)) {
throw new ContentNotFoundException("Unable to get Versions on a non-directory: " + repoDir.toAbsolutePath());
}
final String groupId = reference.getGroupId();
final String artifactId = reference.getArtifactId();
try (Stream<Path> stream = Files.list(repoDir)) {
return stream.filter(Files::isDirectory).map(p -> newVersionedRef(groupId, artifactId, p.getFileName().toString())).filter(this::hasArtifact).map(ref -> ref.getVersion()).collect(Collectors.toSet());
} catch (IOException e) {
log.error("Could not read directory {}: {}", repoDir, e.getMessage(), e);
} catch (RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof LayoutException) {
throw (LayoutException) e.getCause();
} else {
throw e;
}
}
return Collections.emptySet();
}
use of org.apache.archiva.repository.LayoutException in project archiva by apache.
the class RepositoryRequest method toNativePath.
/**
* Adjust the requestedPath to conform to the native layout of the provided {@link org.apache.archiva.repository.ManagedRepositoryContent}.
*
* @param requestedPath the incoming requested path.
* @param repository the repository to adjust to.
* @return the adjusted (to native) path.
* @throws LayoutException if the path cannot be parsed.
*/
public String toNativePath(String requestedPath, ManagedRepositoryContent repository) throws LayoutException {
if (StringUtils.isBlank(requestedPath)) {
throw new LayoutException("Request Path is blank.");
}
String referencedResource = requestedPath;
// No checksum by default.
String supportfile = "";
// Figure out support file, and actual referencedResource.
if (isSupportFile(requestedPath)) {
int idx = requestedPath.lastIndexOf('.');
referencedResource = requestedPath.substring(0, idx);
supportfile = requestedPath.substring(idx);
}
if (isMetadata(referencedResource)) {
/* Nothing to translate.
* Default layout is the only layout that can contain maven-metadata.xml files, and
* if the managedRepository is layout legacy, this request would never occur.
*/
return requestedPath;
}
// Treat as an artifact reference.
ArtifactReference ref = toArtifactReference(referencedResource);
String adjustedPath = repository.toPath(ref);
return adjustedPath + supportfile;
}
use of org.apache.archiva.repository.LayoutException in project archiva by apache.
the class CleanupReleasedSnapshotsRepositoryPurge method updateMetadata.
/*
* TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
* -> not sure what needs to be changed here.
*/
@SuppressWarnings("deprecation")
private void updateMetadata(ArtifactReference artifact) {
VersionedReference versionRef = new VersionedReference();
versionRef.setGroupId(artifact.getGroupId());
versionRef.setArtifactId(artifact.getArtifactId());
versionRef.setVersion(artifact.getVersion());
ProjectReference projectRef = new ProjectReference();
projectRef.setGroupId(artifact.getGroupId());
projectRef.setArtifactId(artifact.getArtifactId());
try {
metadataTools.updateMetadata(repository, versionRef);
} catch (ContentNotFoundException e) {
// Ignore. (Just means we have no snapshot versions left to reference).
} catch (RepositoryMetadataException e) {
// Ignore.
} catch (IOException e) {
// Ignore.
} catch (LayoutException e) {
// Ignore.
}
try {
metadataTools.updateMetadata(repository, projectRef);
} catch (ContentNotFoundException e) {
// Ignore. (Just means we have no snapshot versions left to reference).
} catch (RepositoryMetadataException e) {
// Ignore.
} catch (IOException e) {
// Ignore.
} catch (LayoutException e) {
// Ignore.
}
}
use of org.apache.archiva.repository.LayoutException in project archiva by apache.
the class CleanupReleasedSnapshotsRepositoryPurge method process.
@Override
public void process(String path) throws RepositoryPurgeException {
try {
Path artifactFile = Paths.get(repository.getRepoRoot(), path);
if (!Files.exists(artifactFile)) {
// Nothing to do here, file doesn't exist, skip it.
return;
}
ArtifactReference artifactRef = repository.toArtifactReference(path);
if (!VersionUtil.isSnapshot(artifactRef.getVersion())) {
// Nothing to do here, not a snapshot, skip it.
return;
}
ProjectReference reference = new ProjectReference();
reference.setGroupId(artifactRef.getGroupId());
reference.setArtifactId(artifactRef.getArtifactId());
// Gether the released versions
List<String> releasedVersions = new ArrayList<>();
Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories();
for (org.apache.archiva.repository.ManagedRepository repo : repos) {
if (repo.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE)) {
try {
ManagedRepositoryContent repoContent = repo.getContent();
for (String version : repoContent.getVersions(reference)) {
if (!VersionUtil.isSnapshot(version)) {
releasedVersions.add(version);
}
}
} catch (RepositoryException e) {
// swallow
}
}
}
Collections.sort(releasedVersions, VersionComparator.getInstance());
// Now clean out any version that is earlier than the highest released version.
boolean needsMetadataUpdate = false;
VersionedReference versionRef = new VersionedReference();
versionRef.setGroupId(artifactRef.getGroupId());
versionRef.setArtifactId(artifactRef.getArtifactId());
MetadataRepository metadataRepository = repositorySession.getRepository();
if (releasedVersions.contains(VersionUtil.getReleaseVersion(artifactRef.getVersion()))) {
versionRef.setVersion(artifactRef.getVersion());
repository.deleteVersion(versionRef);
for (RepositoryListener listener : listeners) {
listener.deleteArtifact(metadataRepository, repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(), artifactFile.getFileName().toString());
}
metadataRepository.removeProjectVersion(repository.getId(), artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion());
needsMetadataUpdate = true;
}
if (needsMetadataUpdate) {
updateMetadata(artifactRef);
}
} catch (LayoutException e) {
log.debug("Not processing file that is not an artifact: {}", e.getMessage());
} catch (ContentNotFoundException e) {
throw new RepositoryPurgeException(e.getMessage(), e);
} catch (MetadataRepositoryException e) {
log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
}
}
use of org.apache.archiva.repository.LayoutException in project archiva by apache.
the class RetentionCountRepositoryPurge method process.
@Override
public void process(String path) throws RepositoryPurgeException {
try {
Path artifactFile = Paths.get(repository.getRepoRoot(), path);
if (!Files.exists(artifactFile)) {
return;
}
ArtifactReference artifact = repository.toArtifactReference(path);
if (VersionUtil.isSnapshot(artifact.getVersion())) {
VersionedReference reference = new VersionedReference();
reference.setGroupId(artifact.getGroupId());
reference.setArtifactId(artifact.getArtifactId());
reference.setVersion(artifact.getVersion());
List<String> versions = new ArrayList<>(repository.getVersions(reference));
Collections.sort(versions, VersionComparator.getInstance());
if (retentionCount > versions.size()) {
log.trace("No deletion, because retention count is higher than actual number of artifacts.");
// Done. nothing to do here. skip it.
return;
}
int countToPurge = versions.size() - retentionCount;
Set<ArtifactReference> artifactsToDelete = new HashSet<>();
for (String version : versions) {
if (countToPurge-- <= 0) {
break;
}
artifactsToDelete.addAll(repository.getRelatedArtifacts(getNewArtifactReference(artifact, version)));
}
purge(artifactsToDelete);
}
} catch (LayoutException le) {
throw new RepositoryPurgeException(le.getMessage(), le);
} catch (ContentNotFoundException e) {
log.error("Repostory artifact not found {}", path);
}
}
Aggregations