use of org.apache.archiva.repository.content.Artifact in project archiva by apache.
the class Maven2RepositoryStorage method getFilePathWithVersion.
@Override
public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent) throws RelocationException {
if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) {
return getFilePath(requestPath, managedRepositoryContent.getRepository());
}
String filePath = getFilePath(requestPath, managedRepositoryContent.getRepository());
Artifact artifact;
try {
ContentItem item = managedRepositoryContent.toItem(filePath);
artifact = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class).adaptItem(Artifact.class, item);
} catch (LayoutException e) {
return filePath;
}
if (VersionUtil.isGenericSnapshot(artifact.getArtifactVersion())) {
// read maven metadata to get last timestamp
StorageAsset metadataDir = managedRepositoryContent.getRepository().getAsset(filePath).getParent();
if (!metadataDir.exists()) {
return filePath;
}
StorageAsset metadataFile = metadataDir.resolve(METADATA_FILENAME);
if (!metadataFile.exists()) {
return filePath;
}
ArchivaRepositoryMetadata archivaRepositoryMetadata = null;
try {
archivaRepositoryMetadata = metadataReader.read(metadataFile);
} catch (RepositoryMetadataException e) {
log.error("Could not read metadata {}", e.getMessage(), e);
return filePath;
}
int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
// MRM-1846
if (buildNumber < 1 && timestamp == null) {
return filePath;
}
// org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar
// -> archiva-checksum-1.4-M4-20130425.081822-1.jar
filePath = //
StringUtils.replace(//
filePath, //
artifact.getId() + "-" + //
artifact.getVersion().getId(), //
artifact.getId() + "-" + StringUtils.remove(artifact.getArtifactVersion(), //
"-" + VersionUtil.SNAPSHOT) + "-" + //
timestamp + "-" + buildNumber);
throw new RelocationException("/repository/" + managedRepositoryContent.getRepository().getId() + (StringUtils.startsWith(filePath, "/") ? "" : "/") + filePath, RelocationException.RelocationType.TEMPORARY);
}
return filePath;
}
use of org.apache.archiva.repository.content.Artifact in project archiva by apache.
the class Maven2RepositoryStorage method readArtifactsMetadata.
@Override
public Collection<ArtifactMetadata> readArtifactsMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageRuntimeException {
StorageAsset dir = pathTranslator.toFile(getRepositoryBasedir(readMetadataRequest.getRepositoryId()), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion());
if (!(dir.exists() && dir.isContainer())) {
return Collections.emptyList();
}
// all files that are not metadata and not a checksum / signature are considered artifacts
final Predicate<StorageAsset> dFilter = new ArtifactDirectoryFilter(readMetadataRequest.getFilter());
// Returns a map TRUE -> (success values), FALSE -> (Exceptions)
Map<Boolean, List<Try<ArtifactMetadata>>> result = dir.list().stream().filter(dFilter).map(path -> {
try {
return Try.success(getArtifactFromFile(readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), path));
} catch (Exception e) {
log.debug("Could not create metadata for {}: {}", path, e.getMessage(), e);
return Try.<ArtifactMetadata>failure(e);
}
}).collect(Collectors.groupingBy(Try::isSuccess));
if (result.containsKey(Boolean.FALSE) && result.get(Boolean.FALSE).size() > 0 && (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE).size() == 0)) {
log.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
Try<ArtifactMetadata> failure = result.get(Boolean.FALSE).get(0);
log.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
} else {
if (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE) == null) {
return Collections.emptyList();
}
return result.get(Boolean.TRUE).stream().map(tr -> tr.get()).collect(Collectors.toList());
}
}
use of org.apache.archiva.repository.content.Artifact in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method deleteItemNotFound.
@Test
public void deleteItemNotFound() throws IOException, URISyntaxException, ItemNotFoundException {
ManagedRepository repo = createManagedRepoWithContent("delete-repository");
ManagedRepositoryContent myRepoContent = repo.getContent();
Path repoRoot = repo.getRoot().getFilePath();
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.test2").build();
ContentItem item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing namespace", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar2").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Project);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing project", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.1").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Version);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing version", false);
} catch (ItemNotFoundException e) {
}
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withExtension("jax").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
try {
myRepoContent.deleteItem(item);
assertTrue("ItemNotFoundException expected for non existing artifact", false);
} catch (ItemNotFoundException e) {
}
}
use of org.apache.archiva.repository.content.Artifact in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method checkArtifactListWithNamespaceSelectorRecursive.
private void checkArtifactListWithNamespaceSelectorRecursive(List<? extends Artifact> results) {
assertNotNull(results);
assertEquals(6, results.size());
Artifact artifact = results.stream().filter(a -> a.getFileName().equalsIgnoreCase("testproj2-1.0.pom")).findFirst().get();
assertNotNull(artifact);
assertEquals(6, artifact.getAsset().getParent().getPath().split("/").length);
artifact = results.stream().filter(a -> a.getFileName().equalsIgnoreCase("testproj1-1.0.pom")).findFirst().get();
assertNotNull(artifact);
assertEquals(5, artifact.getAsset().getParent().getPath().split("/").length);
}
use of org.apache.archiva.repository.content.Artifact in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method deleteArtifactItem.
@Test
public void deleteArtifactItem() throws IOException, URISyntaxException, ItemNotFoundException {
ManagedRepository repo = createManagedRepoWithContent("delete-repository");
ManagedRepositoryContent myRepoContent = repo.getContent();
Path repoRoot = repo.getRoot().getFilePath();
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven/A/1.0/A-1.0.pom")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven/A/1.0/A-1.0.war")));
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("A").withVersion("1.0").withArtifactId("A").withArtifactVersion("1.0").withExtension("pom").build();
ContentItem item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
myRepoContent.deleteItem(item);
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven/samplejar/1.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven/samplejar/2.0")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/maven/A/1.0/A-1.0.pom")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/maven/A/1.0/A-1.0.war")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.pom")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar")));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withExtension("jar").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.pom")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/2.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/1.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/2.0")));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withClassifier("source").withExtension("jar").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.pom")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/2.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/1.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/2.0")));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").withProjectId("samplejar").withVersion("1.0").withArtifactId("samplejar").withArtifactVersion("1.0").withExtension("jar.md5").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Artifact);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.md5")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0.pom")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/1.0/samplejar-1.0-source.jar.sha1")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar/2.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/1.0")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar/2.0")));
}
Aggregations