use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.
the class Maven2RepositoryMetadataResolverTest method testGetArtifactMetadataSnapshotsMRM1859.
@Test
public void testGetArtifactMetadataSnapshotsMRM1859() throws Exception {
Path repoDir = Paths.get("target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT");
URL url = Thread.currentThread().getContextClassLoader().getResource("resolver-status.properties");
Path resFile = Paths.get(url.toURI());
Path destFile = repoDir.resolve(resFile.getFileName());
Files.copy(resFile, destFile, StandardCopyOption.REPLACE_EXISTING);
URL url2 = Thread.currentThread().getContextClassLoader().getResource("test01.properties");
Path resFile2 = Paths.get(url2.toURI());
Path destFile2 = repoDir.resolve(resFile2.getFileName());
Files.copy(resFile2, destFile2, StandardCopyOption.REPLACE_EXISTING);
try {
Collection<ArtifactMetadata> testArtifacts = storage.readArtifactsMetadata(new ReadMetadataRequest(TEST_REPO_ID, "com.example.test", "test-artifact", "1.0-SNAPSHOT", ALL));
List<ArtifactMetadata> artifacts = new ArrayList<>(testArtifacts);
Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {
@Override
public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
return o1.getId().compareTo(o2.getId());
}
});
assertEquals(6, artifacts.size());
ArtifactMetadata artifactMetadata = artifacts.get(0);
assertEquals("test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId());
MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(1, facet.getBuildNumber());
assertEquals("20100308.230825", facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("jar", facet.getType());
artifactMetadata = artifacts.get(1);
assertEquals("test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(1, facet.getBuildNumber());
assertEquals("20100308.230825", facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("pom", facet.getType());
artifactMetadata = artifacts.get(2);
assertEquals("test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(2, facet.getBuildNumber());
assertEquals("20100310.014828", facet.getTimestamp());
assertEquals("javadoc", facet.getClassifier());
assertEquals("javadoc", facet.getType());
artifactMetadata = artifacts.get(3);
assertEquals("test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(2, facet.getBuildNumber());
assertEquals("20100310.014828", facet.getTimestamp());
assertEquals("sources", facet.getClassifier());
assertEquals("java-source", facet.getType());
artifactMetadata = artifacts.get(4);
assertEquals("test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(2, facet.getBuildNumber());
assertEquals("20100310.014828", facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("jar", facet.getType());
artifactMetadata = artifacts.get(5);
assertEquals("test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(2, facet.getBuildNumber());
assertEquals("20100310.014828", facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("pom", facet.getType());
} finally {
Files.delete(destFile);
Files.delete(destFile2);
}
}
use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.
the class Maven2RepositoryMetadataResolverTest method testGetArtifactMetadata.
@Test
public void testGetArtifactMetadata() throws Exception {
Collection<ArtifactMetadata> springArtifacts = storage.readArtifactsMetadata(new ReadMetadataRequest(TEST_REPO_ID, "org.codehaus.plexus", "plexus-spring", "1.2", ALL));
List<ArtifactMetadata> artifacts = new ArrayList<>(springArtifacts);
Collections.sort(artifacts, new Comparator<ArtifactMetadata>() {
@Override
public int compare(ArtifactMetadata o1, ArtifactMetadata o2) {
return o1.getId().compareTo(o2.getId());
}
});
assertEquals(3, artifacts.size());
ArtifactMetadata artifactMetadata = artifacts.get(0);
assertEquals("plexus-spring-1.2-sources.jar", artifactMetadata.getId());
MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(0, facet.getBuildNumber());
assertNull(facet.getTimestamp());
assertEquals("sources", facet.getClassifier());
assertEquals("java-source", facet.getType());
artifactMetadata = artifacts.get(1);
assertEquals("plexus-spring-1.2.jar", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(0, facet.getBuildNumber());
assertNull(facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("jar", facet.getType());
artifactMetadata = artifacts.get(2);
assertEquals("plexus-spring-1.2.pom", artifactMetadata.getId());
facet = (MavenArtifactFacet) artifactMetadata.getFacet(MavenArtifactFacet.FACET_ID);
assertEquals(0, facet.getBuildNumber());
assertNull(facet.getTimestamp());
assertNull(facet.getClassifier());
assertEquals("pom", facet.getType());
}
use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.
the class RepositoryStatisticsManagerTest method createArtifact.
private ArtifactMetadata createArtifact(String namespace, String projectId, String projectVersion, String type) {
ArtifactMetadata metadata = new ArtifactMetadata();
metadata.setRepositoryId(TEST_REPO_ID);
metadata.setId(projectId + "-" + projectVersion + "." + type);
metadata.setProject(projectId);
metadata.setSize(12345L);
metadata.setProjectVersion(projectVersion);
metadata.setVersion(projectVersion);
metadata.setNamespace(namespace);
MavenArtifactFacet facet = new MavenArtifactFacet();
facet.setType(type);
metadata.addFacet(facet);
return metadata;
}
use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet in project archiva by apache.
the class ManagedRepositoryContentMock method getArtifactFromId.
public ArtifactMetadata getArtifactFromId(String repoId, String namespace, String projectId, String projectVersion, String id) {
if (!id.startsWith(projectId + "-")) {
throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't start with artifact ID '" + projectId + "'");
}
MavenArtifactFacet facet = new MavenArtifactFacet();
int index = projectId.length() + 1;
String version;
String idSubStrFromVersion = id.substring(index);
if (idSubStrFromVersion.startsWith(projectVersion) && !VersionUtil.isUniqueSnapshot(projectVersion)) {
// non-snapshot versions, or non-timestamped snapshot versions
version = projectVersion;
} else if (VersionUtil.isGenericSnapshot(projectVersion)) {
// timestamped snapshots
try {
// 8 is length of "SNAPSHOT"
int mainVersionLength = projectVersion.length() - 8;
if (mainVersionLength == 0) {
throw new IllegalArgumentException("Timestamped snapshots must contain the main version, filename was '" + id + "'");
}
Matcher m = TIMESTAMP_PATTERN.matcher(idSubStrFromVersion.substring(mainVersionLength));
m.matches();
String timestamp = m.group(1);
String buildNumber = m.group(2);
facet.setTimestamp(timestamp);
facet.setBuildNumber(Integer.parseInt(buildNumber));
version = idSubStrFromVersion.substring(0, mainVersionLength) + timestamp + "-" + buildNumber;
} catch (IllegalStateException e) {
throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain a timestamped version matching snapshot '" + projectVersion + "'", e);
}
} else {
// invalid
throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '" + projectVersion + "'");
}
String classifier;
String ext;
index += version.length();
if (index == id.length()) {
// no classifier or extension
classifier = null;
ext = null;
} else {
char c = id.charAt(index);
if (c == '-') {
// classifier up until '.'
int extIndex = id.indexOf('.', index);
if (extIndex >= 0) {
classifier = id.substring(index + 1, extIndex);
ext = id.substring(extIndex + 1);
} else {
classifier = id.substring(index + 1);
ext = null;
}
} else if (c == '.') {
// rest is the extension
classifier = null;
ext = id.substring(index + 1);
} else {
throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' expected classifier or extension but got '" + id.substring(index) + "'");
}
}
ArtifactMetadata metadata = new ArtifactMetadata();
metadata.setId(id);
metadata.setNamespace(namespace);
metadata.setProject(projectId);
metadata.setRepositoryId(repoId);
metadata.setProjectVersion(projectVersion);
metadata.setVersion(version);
facet.setClassifier(classifier);
// we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
// to select the correct order to apply multiple extensions mappings to a preferred type
// TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
// perhaps the plugins could register missing entries in configuration, then we just use configuration
// here?
String type = null;
// use extension as default
if (type == null) {
type = ext;
}
// TODO: should we allow this instead?
if (type == null) {
throw new IllegalArgumentException("Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type");
}
facet.setType(type);
metadata.addFacet(facet);
return metadata;
}
use of org.apache.archiva.maven.metadata.model.MavenArtifactFacet 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;
}
Aggregations