use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ContentItemTest method testWithAttribute.
@Test
void testWithAttribute() {
ContentItem test = getBuilder().withAttribute("testkey1", "testvalue1").withAttribute("testkey2", "testvalue2").build();
assertNotNull(test.getAttributes());
assertEquals(2, test.getAttributes().size());
assertEquals("testvalue1", test.getAttribute("testkey1"));
assertEquals("testvalue2", test.getAttribute("testkey2"));
assertNull(test.getAttribute("key123"));
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContent method newItemStream.
@Override
public Stream<? extends ContentItem> newItemStream(ItemSelector selector, boolean parallel) throws ContentAccessException, IllegalArgumentException {
final Predicate<StorageAsset> filter = getItemFileFilterFromSelector(selector);
StorageAsset startDir;
if (selector.getNamespace().contains("*")) {
startDir = getAsset("");
} else if (selector.hasProjectId() && selector.getProjectId().contains("*")) {
startDir = getAsset(selector.getNamespace());
} else if (selector.hasProjectId() && selector.hasVersion() && selector.getVersion().contains("*")) {
startDir = getAsset(selector.getNamespace(), selector.getProjectId());
} else if (selector.hasProjectId() && selector.hasVersion()) {
startDir = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
} else if (selector.hasProjectId()) {
startDir = getAsset(selector.getNamespace(), selector.getProjectId());
} else {
startDir = getAsset(selector.getNamespace());
if (!selector.recurse()) {
// We descend into 2 subdirectories (project and version)
return startDir.list().stream().flatMap(a -> getChildrenDF(a, 1)).map(this::getItemFromPath);
}
}
return StorageUtil.newAssetStream(startDir, parallel).filter(filter).map(this::getItemFromPath);
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class MetadataTransferTest method testGetProjectMetadataProxiedNotLocalOnRemoteConnectoDisabled.
@Test
public void testGetProjectMetadataProxiedNotLocalOnRemoteConnectoDisabled() throws Exception {
// New project metadata that does not exist locally but exists on remote.
String requestedResource = "org/apache/maven/test/get-found-in-proxy/maven-metadata.xml";
setupTestableManagedRepository(requestedResource);
// Configure Connector (usually done within archiva.xml configuration)
saveConnector(ID_DEFAULT_MANAGED, ID_PROXIED1, ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO, true);
assertResourceNotFound(requestedResource);
assertNoRepoMetadata(ID_PROXIED1, requestedResource);
Path expectedFile = managedDefaultDir.resolve(requestedResource);
BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
ContentItem metaItem = managedDefaultRepository.toItem(requestedResource);
Project project = layout.adaptItem(Project.class, managedDefaultRepository.getParent(metaItem));
assertNotNull(project);
String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(project));
StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
assertNull("Should not have downloaded a file.", downloadedFile);
assertNoTempFiles(expectedFile);
}
use of org.apache.archiva.repository.content.ContentItem 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;
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ArchivaDavResourceFactory method processRepository.
private DavResource processRepository(final DavServletRequest request, ArchivaDavResourceLocator archivaLocator, String activePrincipal, ManagedRepositoryContent managedRepositoryContent, org.apache.archiva.repository.ManagedRepository managedRepository) throws DavException {
DavResource resource = null;
if (isAuthorized(request, managedRepositoryContent.getId())) {
boolean readMethod = WebdavMethodUtil.isReadMethod(request.getMethod());
// Maven Centric part ask evaluation if -SNAPSHOT
// MRM-1846 test if read method to prevent issue with maven 2.2.1 and uniqueVersion false
String path = readMethod ? evaluatePathWithVersion(archivaLocator, managedRepositoryContent, request.getContextPath()) : getLogicalResource(archivaLocator, managedRepository, false);
if (path.startsWith("/")) {
path = path.substring(1);
}
LogicalResource logicalResource = new LogicalResource(path);
StorageAsset repoAsset = managedRepository.getAsset(path);
// Path resourceFile = Paths.get( managedRepositoryContent.getRepoRoot(), path );
try {
resource = new ArchivaDavResource(repoAsset, path, managedRepository, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
} catch (LayoutException e) {
log.error("Incompatible layout: {}", e.getMessage(), e);
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
if (WebdavMethodUtil.isReadMethod(request.getMethod())) {
if (archivaLocator.getHref(false).endsWith("/") && !repoAsset.isContainer()) {
// force a resource not found
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
} else {
if (!resource.isCollection()) {
boolean previouslyExisted = repoAsset.exists();
boolean fromProxy = fetchContentFromProxies(managedRepository, request, logicalResource);
StorageAsset resourceAsset = null;
// legacy layout format.
try {
// Perform an adjustment of the resource to the managed
// repository expected path.
// String localResourcePath = managedRepository.getRequestInfo().toNativePath( logicalResource.getPath() );
resourceAsset = managedRepository.getAsset(logicalResource.getPath());
resource = new ArchivaDavResource(resourceAsset, logicalResource.getPath(), managedRepository, request.getRemoteAddr(), activePrincipal, request.getDavSession(), archivaLocator, this, mimeTypes, auditListeners, scheduler);
} catch (LayoutException e) {
if (resourceAsset == null || !resourceAsset.exists()) {
throw new DavException(HttpServletResponse.SC_NOT_FOUND, e);
}
}
if (fromProxy) {
String action = (previouslyExisted ? AuditEvent.MODIFY_FILE : AuditEvent.CREATE_FILE) + PROXIED_SUFFIX;
log.debug("Proxied artifact '{}' in repository '{}' (current user '{}')", resourceAsset.getName(), managedRepositoryContent.getId(), activePrincipal);
triggerAuditEvent(request.getRemoteAddr(), archivaLocator.getRepositoryId(), logicalResource.getPath(), action, activePrincipal);
}
if (!resourceAsset.exists()) {
throw new DavException(HttpServletResponse.SC_NOT_FOUND, "Resource does not exist");
}
}
}
}
if (request.getMethod().equals(HTTP_PUT_METHOD)) {
String resourcePath = logicalResource.getPath();
RepositoryRequestInfo repositoryRequestInfo = managedRepository.getRequestInfo();
// we suppose that release-artifacts can be deployed only to repos enabled for releases
if (managedRepositoryContent.getRepository().getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) && !repositoryRequestInfo.isMetadata(resourcePath) && !repositoryRequestInfo.isSupportFile(resourcePath)) {
// ArtifactReference artifact = null;
Artifact artifact = null;
try {
BaseRepositoryContentLayout layout = managedRepositoryContent.getLayout(BaseRepositoryContentLayout.class);
ContentItem artifactItem = managedRepositoryContent.toItem(resourcePath);
artifact = layout.adaptItem(Artifact.class, artifactItem);
if (!VersionUtil.isSnapshot(artifact.getVersion().getId())) {
// check if artifact already exists and if artifact re-deployment to the repository is allowed
if (artifactItem.exists() && managedRepositoryContent.getRepository().blocksRedeployments()) {
log.warn("Overwriting released artifacts in repository '{}' is not allowed.", managedRepositoryContent.getId());
throw new DavException(HttpServletResponse.SC_CONFLICT, "Overwriting released artifacts is not allowed.");
}
}
} catch (LayoutException e) {
log.warn("Artifact path '{}' is invalid.", resourcePath);
} catch (ContentAccessException e) {
e.printStackTrace();
}
}
/*
* Create parent directories that don't exist when writing a file This actually makes this
* implementation not compliant to the WebDAV RFC - but we have enough knowledge about how the
* collection is being used to do this reasonably and some versions of Maven's WebDAV don't correctly
* create the collections themselves.
*/
StorageAsset rootDirectory = managedRepositoryContent.getRepository().getRoot();
StorageAsset destDir = rootDirectory.resolve(logicalResource.getPath()).getParent();
if (!destDir.exists()) {
try {
destDir.create();
} catch (IOException e) {
log.error("Could not create directory {}: {}", destDir, e.getMessage(), e);
throw new DavException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not create directory " + destDir);
}
String relPath = PathUtil.getRelative(rootDirectory.getPath(), destDir.getPath());
log.debug("Creating destination directory '{}' (current user '{}')", destDir.getName(), activePrincipal);
triggerAuditEvent(request.getRemoteAddr(), managedRepositoryContent.getId(), relPath, AuditEvent.CREATE_DIR, activePrincipal);
}
}
}
return resource;
}
Aggregations