Search in sources :

Example 1 with ArchivaRestServiceException

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()));
    }
}
Also used : DownloadRemoteIndexException(org.apache.archiva.scheduler.indexing.DownloadRemoteIndexException) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) RemoteRepository(org.apache.archiva.repository.RemoteRepository)

Example 2 with ArchivaRestServiceException

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));
    }
}
Also used : Locale(java.util.Locale) Repository(org.apache.archiva.rest.api.v2.model.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) PagedResult(org.apache.archiva.components.rest.model.PagedResult)

Example 3 with ArchivaRestServiceException

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()));
    }
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenManagedRepository(org.apache.archiva.rest.api.v2.model.MavenManagedRepository) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException)

Example 4 with ArchivaRestServiceException

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();
}
Also used : AuditInformation(org.apache.archiva.admin.model.AuditInformation) ManagedRepository(org.apache.archiva.repository.ManagedRepository) MavenManagedRepository(org.apache.archiva.rest.api.v2.model.MavenManagedRepository) LayoutException(org.apache.archiva.repository.content.LayoutException) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) IOException(java.io.IOException) ContentItem(org.apache.archiva.repository.content.ContentItem)

Example 5 with ArchivaRestServiceException

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);
    }
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) User(org.apache.archiva.redback.users.User) UserManagerException(org.apache.archiva.redback.users.UserManagerException) AuthorizationException(org.apache.archiva.redback.authorization.AuthorizationException) SecuritySession(org.apache.archiva.redback.system.SecuritySession) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) ArchivaRestServiceException(org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) AuthenticationResult(org.apache.archiva.redback.authentication.AuthenticationResult)

Aggregations

ArchivaRestServiceException (org.apache.archiva.rest.api.v2.svc.ArchivaRestServiceException)23 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)9 RedbackRuntimeConfiguration (org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration)8 RepositoryException (org.apache.archiva.repository.RepositoryException)6 Map (java.util.Map)4 PagedResult (org.apache.archiva.components.rest.model.PagedResult)4 AuthenticationException (javax.naming.AuthenticationException)3 AuthenticationNotSupportedException (javax.naming.AuthenticationNotSupportedException)3 CommunicationException (javax.naming.CommunicationException)3 InvalidNameException (javax.naming.InvalidNameException)3 NamingException (javax.naming.NamingException)3 NoPermissionException (javax.naming.NoPermissionException)3 ServiceUnavailableException (javax.naming.ServiceUnavailableException)3 Response (javax.ws.rs.core.Response)3 LdapConnection (org.apache.archiva.redback.common.ldap.connection.LdapConnection)3 LdapConnectionConfiguration (org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration)3 LdapException (org.apache.archiva.redback.common.ldap.connection.LdapException)3 EditableRepositoryGroup (org.apache.archiva.repository.EditableRepositoryGroup)3 LdapConfiguration (org.apache.archiva.rest.api.v2.model.LdapConfiguration)3 ArrayList (java.util.ArrayList)2