use of org.apache.archiva.xml.XMLException in project archiva by apache.
the class Maven2RepositoryStorage method readProjectVersionMetadata.
@Override
public ProjectVersionMetadata readProjectVersionMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageMetadataNotFoundException, RepositoryStorageMetadataInvalidException, RepositoryStorageRuntimeException {
try {
ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(readMetadataRequest.getRepositoryId());
boolean isReleases = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE);
boolean isSnapshots = managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT);
String artifactVersion = readMetadataRequest.getProjectVersion();
// olamy: in case of browsing via the ui we can mix repos (parent of a SNAPSHOT can come from release repo)
if (!readMetadataRequest.isBrowsingRequest()) {
if (VersionUtil.isSnapshot(artifactVersion)) {
// skygo trying to improve speed by honoring managed configuration MRM-1658
if (isReleases && !isSnapshots) {
throw new RepositoryStorageRuntimeException("lookforsnaponreleaseonly", "managed repo is configured for release only");
}
} else {
if (!isReleases && isSnapshots) {
throw new RepositoryStorageRuntimeException("lookforsreleaseonsneponly", "managed repo is configured for snapshot only");
}
}
}
Path basedir = Paths.get(managedRepository.getLocation());
if (VersionUtil.isSnapshot(artifactVersion)) {
Path metadataFile = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), artifactVersion, METADATA_FILENAME);
try {
ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(metadataFile);
// re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
if (snapshotVersion != null) {
artifactVersion = // remove SNAPSHOT from end
artifactVersion.substring(0, artifactVersion.length() - 8);
artifactVersion = artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
}
} catch (XMLException e) {
// unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
LOGGER.warn("Invalid metadata: {} - {}", metadataFile, e.getMessage());
}
}
// TODO: won't work well with some other layouts, might need to convert artifact parts to ID by path translator
String id = readMetadataRequest.getProjectId() + "-" + artifactVersion + ".pom";
Path file = pathTranslator.toFile(basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), id);
if (!Files.exists(file)) {
// metadata could not be resolved
throw new RepositoryStorageMetadataNotFoundException("The artifact's POM file '" + file.toAbsolutePath() + "' was missing");
}
// TODO: this is a workaround until we can properly resolve using proxies as well - this doesn't cache
// anything locally!
List<RemoteRepository> remoteRepositories = new ArrayList<>();
Map<String, NetworkProxy> networkProxies = new HashMap<>();
Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get(readMetadataRequest.getRepositoryId());
if (proxyConnectors != null) {
for (ProxyConnector proxyConnector : proxyConnectors) {
RemoteRepository remoteRepoConfig = repositoryRegistry.getRemoteRepository(proxyConnector.getTargetRepoId());
if (remoteRepoConfig != null) {
remoteRepositories.add(remoteRepoConfig);
NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy(proxyConnector.getProxyId());
if (networkProxyConfig != null) {
// key/value: remote repo ID/proxy info
networkProxies.put(proxyConnector.getTargetRepoId(), networkProxyConfig);
}
}
}
}
// can have released parent pom
if (readMetadataRequest.isBrowsingRequest()) {
remoteRepositories.addAll(repositoryRegistry.getRemoteRepositories());
}
ModelBuildingRequest req = new DefaultModelBuildingRequest().setProcessPlugins(false).setPomFile(file.toFile()).setTwoPhaseBuilding(false).setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
// MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
req.setSystemProperties(System.getProperties());
// MRM-1411
req.setModelResolver(new RepositoryModelResolver(managedRepository, pathTranslator, wagonFactory, remoteRepositories, networkProxies, managedRepository));
Model model;
try {
model = builder.build(req).getEffectiveModel();
} catch (ModelBuildingException e) {
String msg = "The artifact's POM file '" + file + "' was invalid: " + e.getMessage();
List<ModelProblem> modelProblems = e.getProblems();
for (ModelProblem problem : modelProblems) {
// but setTwoPhaseBuilding(true) fix that
if (((problem.getException() instanceof FileNotFoundException || problem.getException() instanceof NoSuchFileException) && e.getModelId() != null && !e.getModelId().equals(problem.getModelId()))) {
LOGGER.warn("The artifact's parent POM file '{}' cannot be resolved. " + "Using defaults for project version metadata..", file);
ProjectVersionMetadata metadata = new ProjectVersionMetadata();
metadata.setId(readMetadataRequest.getProjectVersion());
MavenProjectFacet facet = new MavenProjectFacet();
facet.setGroupId(readMetadataRequest.getNamespace());
facet.setArtifactId(readMetadataRequest.getProjectId());
facet.setPackaging("jar");
metadata.addFacet(facet);
String errMsg = "Error in resolving artifact's parent POM file. " + (problem.getException() == null ? problem.getMessage() : problem.getException().getMessage());
RepositoryProblemFacet repoProblemFacet = new RepositoryProblemFacet();
repoProblemFacet.setRepositoryId(readMetadataRequest.getRepositoryId());
repoProblemFacet.setId(readMetadataRequest.getRepositoryId());
repoProblemFacet.setMessage(errMsg);
repoProblemFacet.setProblem(errMsg);
repoProblemFacet.setProject(readMetadataRequest.getProjectId());
repoProblemFacet.setVersion(readMetadataRequest.getProjectVersion());
repoProblemFacet.setNamespace(readMetadataRequest.getNamespace());
metadata.addFacet(repoProblemFacet);
return metadata;
}
}
throw new RepositoryStorageMetadataInvalidException("invalid-pom", msg, e);
}
// Check if the POM is in the correct location
boolean correctGroupId = readMetadataRequest.getNamespace().equals(model.getGroupId());
boolean correctArtifactId = readMetadataRequest.getProjectId().equals(model.getArtifactId());
boolean correctVersion = readMetadataRequest.getProjectVersion().equals(model.getVersion());
if (!correctGroupId || !correctArtifactId || !correctVersion) {
StringBuilder message = new StringBuilder("Incorrect POM coordinates in '" + file + "':");
if (!correctGroupId) {
message.append("\nIncorrect group ID: ").append(model.getGroupId());
}
if (!correctArtifactId) {
message.append("\nIncorrect artifact ID: ").append(model.getArtifactId());
}
if (!correctVersion) {
message.append("\nIncorrect version: ").append(model.getVersion());
}
throw new RepositoryStorageMetadataInvalidException("mislocated-pom", message.toString());
}
ProjectVersionMetadata metadata = new ProjectVersionMetadata();
metadata.setCiManagement(convertCiManagement(model.getCiManagement()));
metadata.setDescription(model.getDescription());
metadata.setId(readMetadataRequest.getProjectVersion());
metadata.setIssueManagement(convertIssueManagement(model.getIssueManagement()));
metadata.setLicenses(convertLicenses(model.getLicenses()));
metadata.setMailingLists(convertMailingLists(model.getMailingLists()));
metadata.setDependencies(convertDependencies(model.getDependencies()));
metadata.setName(model.getName());
metadata.setOrganization(convertOrganization(model.getOrganization()));
metadata.setScm(convertScm(model.getScm()));
metadata.setUrl(model.getUrl());
metadata.setProperties(model.getProperties());
MavenProjectFacet facet = new MavenProjectFacet();
facet.setGroupId(model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId());
facet.setArtifactId(model.getArtifactId());
facet.setPackaging(model.getPackaging());
if (model.getParent() != null) {
MavenProjectParent parent = new MavenProjectParent();
parent.setGroupId(model.getParent().getGroupId());
parent.setArtifactId(model.getParent().getArtifactId());
parent.setVersion(model.getParent().getVersion());
facet.setParent(parent);
}
metadata.addFacet(facet);
return metadata;
} catch (RepositoryAdminException e) {
throw new RepositoryStorageRuntimeException("repo-admin", e.getMessage(), e);
}
}
use of org.apache.archiva.xml.XMLException in project archiva by apache.
the class Maven3DependencyTreeBuilder method findArtifactInRepositories.
private ManagedRepository findArtifactInRepositories(List<String> repositoryIds, Artifact projectArtifact) throws RepositoryAdminException {
for (String repoId : repositoryIds) {
ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repoId);
Path repoDir = Paths.get(managedRepository.getLocation());
Path file = pathTranslator.toFile(repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(), projectArtifact.getBaseVersion(), projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion() + ".pom");
if (Files.exists(file)) {
return managedRepository;
}
// try with snapshot version
if (StringUtils.endsWith(projectArtifact.getBaseVersion(), VersionUtil.SNAPSHOT)) {
Path metadataFile = file.getParent().resolve(MetadataTools.MAVEN_METADATA);
if (Files.exists(metadataFile)) {
try {
ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.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(projectArtifact.getArtifactId()).append('-').append(StringUtils.remove(projectArtifact.getBaseVersion(), "-" + VersionUtil.SNAPSHOT)).append('-').append(timeStamp).append('-').append(Integer.toString(buildNumber)).append(".pom").toString();
Path timeStampFile = file.getParent().resolve(timeStampFileName);
log.debug("try to find timestamped snapshot version file: {}", timeStampFile);
if (Files.exists(timeStampFile)) {
return managedRepository;
}
} catch (XMLException e) {
log.warn("skip fail to find timestamped snapshot pom: {}", e.getMessage());
}
}
}
}
return null;
}
use of org.apache.archiva.xml.XMLException in project archiva by apache.
the class RepositoryModelResolver method findTimeStampedSnapshotPom.
protected Path findTimeStampedSnapshotPom(String groupId, String artifactId, String version, String parentDirectory) {
// reading metadata if there
Path mavenMetadata = Paths.get(parentDirectory, METADATA_FILENAME);
if (Files.exists(mavenMetadata)) {
try {
ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(mavenMetadata);
SnapshotVersion snapshotVersion = archivaRepositoryMetadata.getSnapshotVersion();
if (snapshotVersion != null) {
String lastVersion = snapshotVersion.getTimestamp();
int buildNumber = snapshotVersion.getBuildNumber();
String snapshotPath = StringUtils.replaceChars(groupId, '.', '/') + '/' + artifactId + '/' + version + '/' + artifactId + '-' + StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT) + '-' + lastVersion + '-' + buildNumber + ".pom";
log.debug("use snapshot path {} for maven coordinate {}:{}:{}", snapshotPath, groupId, artifactId, version);
Path model = basedir.resolve(snapshotPath);
// model = pathTranslator.toFile( basedir, groupId, artifactId, lastVersion, filename );
if (Files.exists(model)) {
return model;
}
}
} catch (XMLException e) {
log.warn("fail to read {}, {}", mavenMetadata.toAbsolutePath(), e.getCause());
}
}
return null;
}
use of org.apache.archiva.xml.XMLException in project archiva by apache.
the class DefaultBrowseService method artifactAvailable.
@Override
public Boolean 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) {
ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repoId);
if ((snapshot && !managedRepository.isSnapshots()) || (!snapshot && managedRepository.isSnapshots())) {
continue;
}
ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
// FIXME default to jar which can be wrong for war zip etc....
ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, StringUtils.isEmpty(classifier) ? "" : classifier, "jar", repoId);
Path file = managedRepositoryContent.toFile(archivaArtifact);
if (file != null && Files.exists(file)) {
return true;
}
// in case of SNAPSHOT we can have timestamped version locally !
if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) {
Path metadataFile = file.getParent().resolve(MetadataTools.MAVEN_METADATA);
if (Files.exists(metadataFile)) {
try {
ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.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();
Path timeStampFile = file.getParent().resolve(timeStampFileName);
log.debug("try to find timestamped snapshot version file: {}", timeStampFile.toAbsolutePath());
if (Files.exists(timeStampFile)) {
return true;
}
} catch (XMLException e) {
log.warn("skip fail to find timestamped snapshot file: {}", e.getMessage());
}
}
}
String path = managedRepositoryContent.toPath(archivaArtifact);
file = connectors.fetchFromProxies(managedRepositoryContent, path);
if (file != null && Files.exists(file)) {
// download pom now
String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom";
connectors.fetchFromProxies(managedRepositoryContent, pomPath);
return true;
}
}
} catch (RepositoryAdminException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
return false;
}
use of org.apache.archiva.xml.XMLException in project archiva by apache.
the class MetadataTools method readProxyMetadata.
public ArchivaRepositoryMetadata readProxyMetadata(ManagedRepositoryContent managedRepository, VersionedReference reference, String proxyId) {
String metadataPath = getRepositorySpecificName(proxyId, toPath(reference));
Path metadataFile = Paths.get(managedRepository.getRepoRoot(), metadataPath);
if (!Files.exists(metadataFile) || !Files.isRegularFile(metadataFile)) {
// Nothing to do. return null.
return null;
}
try {
return MavenMetadataReader.read(metadataFile);
} catch (XMLException e) {
// TODO: [monitor] consider a monitor for this event.
// TODO: consider a read-redo on monitor return code?
log.warn("Unable to read metadata: {}", metadataFile.toAbsolutePath(), e);
return null;
}
}
Aggregations