use of org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException in project archiva by apache.
the class DefaultRepositoryService method scheduleDownloadRemoteIndex.
@Override
public Response scheduleDownloadRemoteIndex(String repositoryId, boolean immediately, boolean full, UriInfo uriInfo) throws ArchivaRestServiceException {
boolean immediateSet = RestUtil.isFlagSet(uriInfo, "immediate");
boolean fullSet = RestUtil.isFlagSet(uriInfo, "full");
RemoteRepository repo = repositoryRegistry.getRemoteRepository(repositoryId);
if (repo == null) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_REMOTE_NOT_FOUND, repositoryId), 404);
}
try {
downloadRemoteIndexScheduler.scheduleDownloadRemote(repositoryId, immediateSet, fullSet);
return Response.ok().build();
} catch (DownloadRemoteIndexException e) {
log.error("Could not schedule index download for repository {}: {}", repositoryId, e.getMessage(), e);
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_REMOTE_INDEX_DOWNLOAD_FAILED, e.getMessage()));
}
}
use of org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException in project archiva by apache.
the class DefaultRepositoryService method getRepositories.
@Override
public PagedResult<Repository> getRepositories(String searchTerm, Integer offset, Integer limit, List<String> orderBy, String order, String localeString) throws ArchivaRestServiceException {
final Locale locale = StringUtils.isNotEmpty(localeString) ? Locale.forLanguageTag(localeString) : Locale.getDefault();
boolean isAscending = QUERY_HELPER.isAscending(order);
Predicate<org.apache.archiva.repository.Repository> filter = QUERY_HELPER.getQueryFilter(searchTerm);
Comparator<org.apache.archiva.repository.Repository> comparator = QUERY_HELPER.getComparator(orderBy, isAscending);
try {
int totalCount = Math.toIntExact(repositoryRegistry.getRepositories().stream().filter(filter).count());
return new PagedResult<>(totalCount, offset, limit, repositoryRegistry.getRepositories().stream().filter(filter).skip(offset).limit(limit).sorted(comparator).map(repo -> Repository.of(repo, locale)).collect(Collectors.toList()));
} catch (ArithmeticException e) {
log.error("Invalid integer conversion for totalCount");
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.INVALID_RESULT_SET_ERROR));
}
}
use of org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException in project archiva by apache.
the class DefaultMavenManagedRepositoryService method updateManagedRepository.
@Override
public MavenManagedRepository updateManagedRepository(final String repositoryId, final MavenManagedRepositoryUpdate managedRepository) throws ArchivaRestServiceException {
org.apache.archiva.admin.model.beans.ManagedRepository repo = convert(managedRepository);
try {
managedRepositoryAdmin.updateManagedRepository(repo, managedRepository.hasStagingRepository(), getAuditInformation(), managedRepository.isResetStats());
ManagedRepository newRepo = repositoryRegistry.getManagedRepository(managedRepository.getId());
if (newRepo == null) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_UPDATE_FAILED, repositoryId));
}
return mapper.reverseMap(newRepo);
} catch (RepositoryAdminException e) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.REPOSITORY_ADMIN_ERROR, e.getMessage()));
}
}
use of org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException 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.rest.api.v2.svc.ArchivaRestServiceException in project archiva by apache.
the class DefaultMavenManagedRepositoryService method checkAuthority.
private void checkAuthority(final String userName, final String srcRepositoryId, final String dstRepositoryId) throws ArchivaRestServiceException {
User user;
try {
user = securitySystem.getUserManager().findUser(userName);
} catch (UserNotFoundException e) {
httpServletResponse.setHeader("WWW-Authenticate", "Bearer realm=\"archiva\"");
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.USER_NOT_FOUND, userName), 401);
} catch (UserManagerException e) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.USER_MANAGER_ERROR, e.getMessage()));
}
// check karma on source : read
AuthenticationResult authn = new AuthenticationResult(true, userName, null);
SecuritySession securitySession = new DefaultSecuritySession(authn, user);
try {
boolean authz = securitySystem.isAuthorized(securitySession, OPERATION_READ_REPOSITORY, srcRepositoryId);
if (!authz) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.PERMISSION_REPOSITORY_DENIED, srcRepositoryId, OPERATION_READ_REPOSITORY), 403);
}
} catch (AuthorizationException e) {
log.error("Error reading permission: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.AUTHORIZATION_ERROR, e.getMessage()), 403);
}
// check karma on target: write
try {
boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_ADD_ARTIFACT, dstRepositoryId);
if (!authz) {
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.PERMISSION_REPOSITORY_DENIED, dstRepositoryId, OPERATION_ADD_ARTIFACT));
}
} catch (AuthorizationException e) {
log.error("Error reading permission: {}", e.getMessage(), e);
throw new ArchivaRestServiceException(ErrorMessage.of(ErrorKeys.AUTHORIZATION_ERROR, e.getMessage()), 403);
}
}
Aggregations