use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class ManagedDefaultRepositoryContent method toItem.
@Override
public ContentItem toItem(String path) throws LayoutException {
StorageAsset asset = getRepository().getAsset(path);
ContentItem item = getItemFromPath(asset);
if (item instanceof DataItem) {
Artifact artifact = adaptItem(Artifact.class, item);
if (asset.getParent() == null) {
throw new LayoutException("Path too short for maven artifact " + path);
}
String version = asset.getParent().getName();
if (asset.getParent().getParent() == null) {
throw new LayoutException("Path too short for maven artifact " + path);
}
String project = item.getAsset().getParent().getParent().getName();
DataItem dataItem = (DataItem) item;
if (StringUtils.isEmpty(dataItem.getExtension())) {
throw new LayoutException("Missing type on maven artifact");
}
if (!project.equals(artifact.getId())) {
throw new LayoutException("The maven artifact id " + artifact.getId() + " does not match the project id: " + project);
}
boolean versionIsGenericSnapshot = VersionUtil.isGenericSnapshot(version);
boolean artifactVersionIsSnapshot = VersionUtil.isSnapshot(artifact.getArtifactVersion());
if (versionIsGenericSnapshot && !artifactVersionIsSnapshot) {
throw new LayoutException("The maven artifact has no snapshot version in snapshot directory " + dataItem);
}
if (!versionIsGenericSnapshot && artifactVersionIsSnapshot) {
throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " is a snapshot version but inside a non snapshot directory " + version);
}
if (!versionIsGenericSnapshot && !version.equals(artifact.getArtifactVersion())) {
throw new LayoutException("The maven artifact version " + artifact.getArtifactVersion() + " does not match the version directory " + version);
}
}
return item;
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class MetadataTransferTest method assertFetchVersionedFailed.
/**
* Transfer the metadata file, not expected to succeed.
*
* @param requestedResource the requested resource
* @throws Exception
*/
private void assertFetchVersionedFailed(String requestedResource) throws Exception {
Path expectedFile = managedDefaultDir.resolve(requestedResource);
ContentItem item = managedDefaultRepository.toItem(requestedResource);
assertNotNull(item);
BaseRepositoryContentLayout layout = managedDefaultRepository.getLayout(BaseRepositoryContentLayout.class);
Version version = layout.adaptItem(Version.class, item);
assertNotNull(version);
String metaPath = managedDefaultRepository.toPath(layout.getMetadataItem(version));
assertNotNull(metaPath);
StorageAsset downloadedFile = proxyHandler.fetchMetadataFromProxies(managedDefaultRepository.getRepository(), metaPath).getFile();
assertNull(downloadedFile);
assertNoTempFiles(expectedFile);
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class MetadataTransferTest method assertFetchProjectOrGroup.
/**
* Transfer the metadata file.
*
* @param requestedResource the requested resource
* @throws Exception
*/
private void assertFetchProjectOrGroup(String requestedResource) throws Exception {
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();
assertNotNull("Should have downloaded a file.", downloadedFile);
assertNoTempFiles(expectedFile);
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class DefaultMavenManagedRepositoryService method copyArtifact.
@Override
public Response copyArtifact(String srcRepositoryId, String dstRepositoryId, String path) throws ArchivaRestServiceException {
final AuditInformation auditInformation = getAuditInformation();
final String userName = auditInformation.getUser().getUsername();
if (StringUtils.isEmpty(userName)) {
httpServletResponse.setHeader("WWW-Authenticate", "Bearer realm=\"archiva\"");
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.NOT_AUTHENTICATED), 401);
}
ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(srcRepositoryId);
if (srcRepo == null) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, srcRepositoryId), 404);
}
ManagedRepository dstRepo = repositoryRegistry.getManagedRepository(dstRepositoryId);
if (dstRepo == null) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_NOT_FOUND, dstRepositoryId), 404);
}
checkAuthority(auditInformation.getUser().getUsername(), srcRepositoryId, dstRepositoryId);
try {
ContentItem srcItem = srcRepo.getContent().toItem(path);
ContentItem dstItem = dstRepo.getContent().toItem(path);
if (!srcItem.getAsset().exists()) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_NOT_FOUND, srcRepositoryId, path), 404);
}
if (dstItem.getAsset().exists()) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_EXISTS_AT_DEST, srcRepositoryId, path), 400);
}
FsStorageUtil.copyAsset(srcItem.getAsset(), dstItem.getAsset(), true);
} catch (LayoutException e) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_LAYOUT_ERROR, e.getMessage()));
} catch (IOException e) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.ARTIFACT_COPY_ERROR, e.getMessage()));
}
return Response.ok().build();
}
use of org.apache.archiva.repository.content.ContentItem in project archiva by apache.
the class DefaultRepositoriesService method deleteProject.
@Override
public ActionStatus deleteProject(String groupId, String projectId, 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);
}
if (StringUtils.isEmpty(projectId)) {
throw new ArchivaRestServiceException("artifactId 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).withProjectId(projectId).build();
ContentItem item = repository.getItem(itemSelector);
repository.deleteItem(item);
} catch (ContentNotFoundException e) {
log.warn("skip ContentNotFoundException: {}", e.getMessage());
} 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);
}
try {
MetadataRepository metadataRepository = repositorySession.getRepository();
metadataRepository.removeProject(repositorySession, repositoryId, groupId, projectId);
repositorySession.save();
} catch (MetadataRepositoryException | MetadataSessionException e) {
log.error(e.getMessage(), e);
throw new ArchivaRestServiceException("Repository exception: " + e.getMessage(), 500, e);
} finally {
repositorySession.close();
}
return ActionStatus.SUCCESS;
}
Aggregations