use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method testAddArtifact.
@Test
public void testAddArtifact() throws IOException, URISyntaxException, LayoutException {
ManagedRepository repo = createManagedRepoWithContent("delete-repository");
ManagedRepositoryContent myRepoContent = repo.getContent();
BaseRepositoryContentLayout layout = myRepoContent.getLayout(BaseRepositoryContentLayout.class);
Path repoRoot = repo.getRoot().getFilePath();
Path tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
for (int i = 0; i < 255; i++) {
outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
}
}
Path file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.jar");
FileTime lmt = Files.getLastModifiedTime(file);
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("jar").build();
Artifact artifact = layout.getArtifact(selector);
layout.addArtifact(tmpFile, artifact);
FileTime lmtAfter = Files.getLastModifiedTime(file);
assertNotEquals(lmtAfter, lmt);
Reader ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
char[] content = new char[50];
ln.read(content);
assertTrue(new String(content).startsWith("test.test.test"));
tmpFile = Files.createTempFile("archiva-mvn-repotest", "jar");
try (OutputStream outputStream = Files.newOutputStream(tmpFile)) {
for (int i = 0; i < 255; i++) {
outputStream.write("test.test.test\n".getBytes(Charset.forName("UTF-8")));
}
}
file = repoRoot.resolve("org/apache/maven/samplejar/2.0/samplejar-2.0.test");
assertFalse(Files.exists(file));
assertTrue(Files.exists(tmpFile));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").withProjectId("samplejar").withVersion("2.0").withArtifactId("samplejar").withArtifactVersion("2.0").withExtension("test").build();
artifact = layout.getArtifact(selector);
layout.addArtifact(tmpFile, artifact);
ln = Files.newBufferedReader(file, Charset.forName("UTF-8"));
ln.read(content);
assertTrue(new String(content).startsWith("test.test.test"));
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class ManagedDefaultRepositoryContentTest method deleteNamespaceItem.
@Test
public void deleteNamespaceItem() 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")));
ArchivaItemSelector selector = ArchivaItemSelector.builder().withNamespace("org.apache.maven").build();
ContentItem item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/maven")));
assertTrue(Files.exists(repoRoot.resolve("org/apache")));
// Sub namespaces are deleted too
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
assertTrue(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
selector = ArchivaItemSelector.builder().withNamespace("org.apache.test").build();
item = myRepoContent.getItem(selector);
assertTrue(item instanceof Namespace);
myRepoContent.deleteItem(item);
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/samplejar")));
assertFalse(Files.exists(repoRoot.resolve("org/apache/test/sub/samplejar")));
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class DefaultBrowseService method getArtifactContentText.
@Override
public ArtifactContent getArtifactContentText(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
try {
for (String repoId : selectedRepos) {
ManagedRepositoryContent managedRepositoryContent = null;
try {
managedRepositoryContent = getManagedRepositoryContent(repoId);
} catch (RepositoryException e) {
log.error("No repository content found for " + repoId);
continue;
}
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(classifier).withType(StringUtils.isEmpty(type) ? "jar" : type).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.exists()) {
log.debug("file: {} not exists for repository: {} try next repository", file, repoId);
continue;
}
if (StringUtils.isNotBlank(path)) {
// zip entry of the path -> path must a real file entry of the archive
FsStorageUtil.PathInformation pathInfo = FsStorageUtil.getAssetDataAsPath(file);
JarFile jarFile = new JarFile(pathInfo.getPath().toFile());
ZipEntry zipEntry = jarFile.getEntry(path);
try (InputStream inputStream = jarFile.getInputStream(zipEntry)) {
return new ArtifactContent(IOUtils.toString(inputStream, ARTIFACT_CONTENT_ENCODING), repoId);
} finally {
closeQuietly(jarFile);
if (pathInfo.isTmpFile()) {
Files.deleteIfExists(pathInfo.getPath());
}
}
}
try (InputStream readStream = file.getReadStream()) {
return new ArtifactContent(IOUtils.toString(readStream, ARTIFACT_CONTENT_ENCODING), repoId);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
log.debug("artifact: {}:{}:{}:{}:{} not found", groupId, artifactId, version, classifier, type);
// 404 ?
return new ArtifactContent();
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class DefaultBrowseService method getArtifactContentEntries.
@Override
public List<ArtifactContentEntry> getArtifactContentEntries(String groupId, String artifactId, String version, String classifier, String type, String path, String repositoryId) throws ArchivaRestServiceException {
List<String> selectedRepos = getSelectedRepos(repositoryId);
try {
for (String repoId : selectedRepos) {
ManagedRepositoryContent managedRepositoryContent = getManagedRepositoryContent(repoId);
ArchivaItemSelector itemSelector = ArchivaItemSelector.builder().withNamespace(groupId).withProjectId(artifactId).withVersion(version).withClassifier(classifier).withType(StringUtils.isEmpty(type) ? "jar" : type).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.exists()) {
return readFileEntries(file, path, repoId);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} catch (RepositoryNotFoundException 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 Collections.emptyList();
}
use of org.apache.archiva.repository.content.base.ArchivaItemSelector in project archiva by apache.
the class DefaultRepositoriesService method deleteGroupId.
@Override
public ActionStatus deleteGroupId(String groupId, String repositoryId) throws ArchivaRestServiceException {
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 (StringUtils.isEmpty(groupId)) {
throw new ArchivaRestServiceException("groupId cannot be null", 400, null);
}
RepositorySession repositorySession = null;
try {
repositorySession = repositorySessionFactory.createSession();
} catch (MetadataRepositoryException e) {
e.printStackTrace();
}
try {
ManagedRepositoryContent repository = getManagedRepositoryContent(repositoryId);
ArchivaItemSelector itemselector = ArchivaItemSelector.builder().withNamespace(groupId).build();
ContentItem item = repository.getItem(itemselector);
repository.deleteItem(item);
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeNamespace(repositorySession, repositoryId, groupId);
// just invalidate cache entry
String cacheKey = repositoryId + "-" + groupId;
namespacesCache.remove(cacheKey);
namespacesCache.remove(repositoryId);
repositorySession.save();
} catch (MetadataRepositoryException | MetadataSessionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (RepositoryException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} catch (ItemNotFoundException e) {
log.error("Item not found {}", e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
Aggregations