use of org.apache.archiva.repository.metadata.MetadataReader in project archiva by apache.
the class DefaultBrowseService method artifactAvailable.
@Override
public AvailabilityStatus artifactAvailable(String groupId, String artifactId, String version, String classifier, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
boolean snapshot = VersionUtil.isSnapshot(version);
try {
for (String repoId : selectedRepos) {
org.apache.archiva.repository.ManagedRepository managedRepo = repositoryRegistry.getManagedRepository(repoId);
if (!proxyRegistry.hasHandler(managedRepo.getType())) {
throw new RepositoryException("No proxy handler found for repository type " + managedRepo.getType());
}
RepositoryProxyHandler proxyHandler = proxyRegistry.getHandler(managedRepo.getType()).get(0);
if ((snapshot && !managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT)) || (!snapshot && managedRepo.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT))) {
continue;
}
ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
// FIXME default to jar which can be wrong for war zip etc....
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(StringUtils.isEmpty(classifier) ? "" : classifier).withType("jar").withArtifactId(artifactId).build();
org.apache.archiva.repository.content.Artifact archivaArtifact = managedRepositoryContent.getItem(itemSelector).adapt(org.apache.archiva.repository.content.Artifact.class);
StorageAsset file = archivaArtifact.getAsset();
if (file != null && file.exists()) {
return new AvailabilityStatus(true);
}
// in case of SNAPSHOT we can have timestamped version locally !
if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
StorageAsset metadataFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + MetadataTools.MAVEN_METADATA);
if (metadataFile.exists()) {
MetadataReader metadataReader = repositoryRegistry.getMetadataReader(managedRepositoryContent.getRepository().getType());
ArchivaRepositoryMetadata archivaRepositoryMetadata = metadataReader.read(metadataFile);
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// rebuild file name with timestamped version and build number
String timeStampFileName = //
new StringBuilder(artifactId).append('-').append(//
StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT)).append('-').append(//
timeStamp).append('-').append(//
Integer.toString(buildNumber)).append(//
(StringUtils.isEmpty(classifier) ? "" : "-" + classifier)).append(".jar").toString();
StorageAsset timeStampFile = file.getStorage().getAsset(file.getParent().getPath() + "/" + timeStampFileName);
log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath());
if (timeStampFile.exists()) {
return new AvailabilityStatus(true);
}
}
}
String path = managedRepositoryContent.toPath(archivaArtifact);
file = proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), path);
if (file != null && file.exists()) {
// download pom now
String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
proxyHandler.fetchFromProxies(managedRepositoryContent.getRepository(), pomPath);
return new AvailabilityStatus(true);
}
}
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
return new AvailabilityStatus(false);
}
use of org.apache.archiva.repository.metadata.MetadataReader in project archiva by apache.
the class DefaultFileUploadService method getMetadata.
private ArchivaRepositoryMetadata getMetadata(StorageAsset metadataFile) throws RepositoryMetadataException {
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
if (metadataFile.exists()) {
Repository repo = repositoryRegistry.getRepositoryOfAsset(metadataFile);
RepositoryType type = repo == null ? RepositoryType.MAVEN : repo.getType();
MetadataReader metadataReader = repositoryRegistry.getMetadataReader(type);
metadata = metadataReader.read(metadataFile);
}
return metadata;
}
Aggregations