use of org.apache.archiva.repository.ManagedRepository 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.repository.ManagedRepository in project archiva by apache.
the class MavenRepositoryProviderTest method createManagedInstance.
@Test
public void createManagedInstance() throws Exception {
ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
repo.setId("testm001");
repo.setName("Managed Test Repo 001");
repo.setDescription("This is a managed test");
repo.setRetentionPeriod(37);
repoLocation = Files.createTempDirectory("test-repo-001");
repo.setLocation(repoLocation.toAbsolutePath().toString());
repo.setSnapshots(true);
repo.setReleases(true);
repo.setRefreshCronExpression("4 0 0 ? * TUE");
repo.setScanned(true);
repo.setBlockRedeployments(true);
repo.setDeleteReleasedSnapshots(true);
repo.setRetentionCount(33);
repo.setSkipPackedIndexCreation(true);
repo.setStageRepoNeeded(true);
repo.setIndexDir("testmanaged/.index");
repo.setLayout("maven2");
repo.setType(RepositoryType.MAVEN.toString());
ManagedRepository mr = provider.createManagedInstance(repo);
assertNotNull(mr.getLocation());
String repoUri = repoLocation.toUri().toString();
assertTrue(Files.exists(repoLocation));
repoUri = repoUri.substring(0, repoUri.length() - 1);
assertEquals(repoUri, mr.getLocation().toString());
assertEquals("This is a managed test", mr.getDescription());
assertEquals("Managed Test Repo 001", mr.getName());
assertEquals(2, mr.getActiveReleaseSchemes().size());
assertTrue(mr.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE));
assertTrue(mr.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT));
assertEquals("testm001", mr.getId());
assertTrue(mr.blocksRedeployments());
assertEquals("4 0 0 ? * TUE", mr.getSchedulingDefinition());
assertTrue(mr.isScanned());
ArtifactCleanupFeature artifactCleanupFeature = mr.getFeature(ArtifactCleanupFeature.class).get();
assertEquals(Period.ofDays(37), artifactCleanupFeature.getRetentionPeriod());
assertTrue(artifactCleanupFeature.isDeleteReleasedSnapshots());
assertEquals(33, artifactCleanupFeature.getRetentionCount());
IndexCreationFeature indexCreationFeature = mr.getFeature(IndexCreationFeature.class).get();
assertNotNull(indexCreationFeature.getIndexPath());
assertEquals("testmanaged/.index", indexCreationFeature.getIndexPath().toString());
assertFalse(indexCreationFeature.getIndexPath().isAbsolute());
assertTrue(indexCreationFeature.isSkipPackedIndexCreation());
StagingRepositoryFeature stagingRepositoryFeature = mr.getFeature(StagingRepositoryFeature.class).get();
assertTrue(stagingRepositoryFeature.isStageRepoNeeded());
assertNull(stagingRepositoryFeature.getStagingRepository());
}
use of org.apache.archiva.repository.ManagedRepository in project archiva by apache.
the class CleanupReleasedSnapshotsRepositoryPurgeTest method testReleasedSnapshotsExistsInDifferentRepo.
@Test
public void testReleasedSnapshotsExistsInDifferentRepo() throws Exception {
RepositoryRegistry repositoryRegistry = applicationContext.getBean(RepositoryRegistry.class);
ManagedRepository managedRepository = repositoryRegistry.getManagedRepository(TEST_REPO_ID);
repositoryRegistry.removeRepository(TEST_REPO_ID);
repositoryRegistry.putRepository(getRepoConfiguration(TEST_REPO_ID, TEST_REPO_NAME));
repositoryRegistry.putRepository(getRepoConfiguration(RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME));
String repoRoot = prepareTestRepos();
String projectNs = "org.apache.archiva";
String projectPath = projectNs.replaceAll("\\.", "/");
String projectName = "released-artifact-in-diff-repo";
String projectVersion = "1.0-SNAPSHOT";
String releaseVersion = "1.0";
String projectRoot = repoRoot + "/" + projectPath + "/" + projectName;
Path repo = getTestRepoRootPath();
Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion);
Set<String> deletedVersions = new HashSet<>();
deletedVersions.add("1.0-SNAPSHOT");
// test listeners for the correct artifacts
listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.archiva", "released-artifact-in-diff-repo", "1.0-SNAPSHOT", "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar");
listenerControl.replay();
// Provide the metadata list
List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID, projectName, repo.getParent(), vDir);
when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs, projectName, projectVersion)).thenReturn(ml);
List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID, projectName, repo.getParent(), releaseDir);
when(metadataRepository.getArtifacts(RELEASES_TEST_REPO_ID, projectNs, projectName, releaseVersion)).thenReturn(ml2);
repoPurge.process(PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO);
listenerControl.verify();
// Verify the metadataRepository invocations
// Complete version removal for cleanup
verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
verify(metadataRepository, never()).removeProjectVersion(eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion));
// check if the snapshot was removed
assertDeleted(projectRoot + "/1.0-SNAPSHOT");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5");
assertDeleted(projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1");
String releasesProjectRoot = AbstractRepositoryPurgeTest.fixPath(Paths.get("target/test-" + getName() + "/releases-test-repo-one").toAbsolutePath().toString() + "/org/apache/archiva/released-artifact-in-diff-repo");
// check if the released version was not removed
assertExists(releasesProjectRoot + "/1.0");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5");
assertExists(releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1");
// remove RELEASES_TEST_REPO_ID so this test will be more independant
applicationContext.getBean(ManagedRepositoryAdmin.class).deleteManagedRepository(RELEASES_TEST_REPO_ID, null, false);
}
use of org.apache.archiva.repository.ManagedRepository in project archiva by apache.
the class ArchivaIndexManagerMock method createContext.
@Override
public ArchivaIndexingContext createContext(Repository repository) throws IndexCreationFailedException {
log.debug("Creating context for repo {}, type: {}", repository.getId(), repository.getType());
if (repository.getType() != RepositoryType.MAVEN) {
throw new UnsupportedRepositoryTypeException(repository.getType());
}
IndexingContext mvnCtx = null;
try {
if (repository instanceof RemoteRepository) {
mvnCtx = createRemoteContext((RemoteRepository) repository);
} else if (repository instanceof ManagedRepository) {
mvnCtx = createManagedContext((ManagedRepository) repository);
}
} catch (IOException e) {
log.error("IOException during context creation " + e.getMessage(), e);
throw new IndexCreationFailedException("Could not create index context for repository " + repository.getId() + (StringUtils.isNotEmpty(e.getMessage()) ? ": " + e.getMessage() : ""), e);
}
MavenIndexContextMock context = new MavenIndexContextMock(repository, mvnCtx);
return context;
}
use of org.apache.archiva.repository.ManagedRepository in project archiva by apache.
the class AbstractRestService method buildArtifacts.
protected List<Artifact> buildArtifacts(Collection<ArtifactMetadata> artifactMetadatas, String repositoryId) throws ArchivaRestServiceException {
try {
if (artifactMetadatas != null && !artifactMetadatas.isEmpty()) {
List<Artifact> artifacts = new ArrayList<>(artifactMetadatas.size());
for (ArtifactMetadata artifact : artifactMetadatas) {
String repoId = repositoryId != null ? repositoryId : artifact.getRepositoryId();
if (repoId == null) {
throw new IllegalStateException("Repository Id is null");
}
ManagedRepository repo = repositoryRegistry.getManagedRepository(repoId);
if (repo == null) {
throw new RepositoryException("Repository not found " + repoId);
}
ManagedRepositoryContent content = repo.getContent();
ArtifactBuilder builder = new ArtifactBuilder().forArtifactMetadata(artifact).withManagedRepositoryContent(content);
Artifact art = builder.build();
art.setUrl(getArtifactUrl(art, repositoryId));
artifacts.add(art);
}
return artifacts;
}
return Collections.emptyList();
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
}
Aggregations