Search in sources :

Example 31 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class ConsumerResource method verifyPersonConsumer.

private void verifyPersonConsumer(ConsumerDTO consumer, ConsumerType type, Owner owner, String username, Principal principal) {
    User user = null;
    try {
        user = userService.findByLogin(username);
    } catch (UnsupportedOperationException e) {
        log.warn("User service does not allow user lookups, cannot verify person consumer.");
    }
    if (user == null) {
        throw new NotFoundException(i18n.tr("User with ID \"{0}\" could not be found."));
    }
    // has some association with the owner the consumer is destined for:
    if (!principal.canAccess(owner, SubResource.NONE, Access.ALL) && !principal.hasFullAccess()) {
        throw new ForbiddenException(i18n.tr("User \"{0}\" has no roles for organization \"{1}\"", user.getUsername(), owner.getKey()));
    }
    // TODO: Refactor out type specific checks?
    if (type.isType(ConsumerTypeEnum.PERSON)) {
        Consumer existing = consumerCurator.findByUser(user);
        if (existing != null && this.consumerTypeCurator.getConsumerType(existing).isType(ConsumerTypeEnum.PERSON)) {
            // TODO: This is not the correct error code for this situation!
            throw new BadRequestException(i18n.tr("User \"{0}\" has already registered a personal consumer", user.getUsername()));
        }
        consumer.setName(user.getUsername());
    }
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) User(org.candlepin.model.User) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 32 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class ConsumerResource method removeDeletionRecord.

@ApiOperation(notes = "Removes the Deletion Record for a Consumer Allowed for a superadmin." + " The main use case for this would be if a user accidently deleted a " + "non-RHEL hypervisor, causing it to no longer be auto-detected via virt-who.", value = "removeDeletionRecord")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@DELETE
@Path("{consumer_uuid}/deletionrecord")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public void removeDeletionRecord(@PathParam("consumer_uuid") String uuid) {
    DeletedConsumer dc = deletedConsumerCurator.findByConsumerUuid(uuid);
    if (dc == null) {
        throw new NotFoundException(i18n.tr("Deletion record for hypervisor \"{0}\" not found.", uuid));
    }
    deletedConsumerCurator.delete(dc);
}
Also used : DeletedConsumer(org.candlepin.model.DeletedConsumer) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) Transactional(com.google.inject.persist.Transactional)

Example 33 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class ConsumerResource method unbind.

@ApiOperation(notes = "Removes an Entitlement from a Consumer By the Entitlement ID", value = "unbind")
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.WILDCARD)
@Path("/{consumer_uuid}/entitlements/{dbid}")
public void unbind(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @PathParam("dbid") @Verify(Entitlement.class) String dbid, @Context Principal principal) {
    consumerCurator.verifyAndLookupConsumer(consumerUuid);
    Entitlement toDelete = entitlementCurator.find(dbid);
    if (toDelete != null) {
        poolManager.revokeEntitlement(toDelete);
        return;
    }
    throw new NotFoundException(i18n.tr("Entitlement with ID \"{0}\" could not be found.", dbid));
}
Also used : NotFoundException(org.candlepin.common.exceptions.NotFoundException) Entitlement(org.candlepin.model.Entitlement) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 34 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class OwnerResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO. This method will not set the
 * ID, key, upstream consumer, content access mode list or content access mode fields.
 *
 * @param entity
 *  The entity instance to populate
 *
 * @param dto
 *  The DTO containing the data with which to populate the entity
 *
 * @throws IllegalArgumentException
 *  if either entity or dto are null
 */
protected void populateEntity(Owner entity, OwnerDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the owner model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the owner dto is null");
    }
    if (dto.getDisplayName() != null) {
        entity.setDisplayName(dto.getDisplayName());
    }
    if (dto.getParentOwner() != null) {
        // Impl note:
        // We do not allow modifying a parent owner through its children, so all we'll do here
        // is set the parent owner and ignore everything else; including further nested owners.
        OwnerDTO pdto = dto.getParentOwner();
        Owner parent = null;
        if (pdto.getId() != null) {
            // look up by ID
            parent = this.ownerCurator.find(pdto.getId());
        } else if (pdto.getKey() != null) {
            // look up by key
            parent = this.ownerCurator.lookupByKey(pdto.getKey());
        }
        if (parent == null) {
            throw new NotFoundException(i18n.tr("Unable to find parent owner: {0}", pdto));
        }
        entity.setParentOwner(parent);
    }
    if (dto.getContentPrefix() != null) {
        entity.setContentPrefix(dto.getContentPrefix());
    }
    if (dto.getDefaultServiceLevel() != null) {
        if (dto.getDefaultServiceLevel().isEmpty()) {
            entity.setDefaultServiceLevel(null);
        } else {
            this.serviceLevelValidator.validate(entity.getId(), dto.getDefaultServiceLevel());
            entity.setDefaultServiceLevel(dto.getDefaultServiceLevel());
        }
    }
    if (dto.getLogLevel() != null) {
        entity.setLogLevel(dto.getLogLevel());
    }
    if (dto.isAutobindDisabled() != null) {
        entity.setAutobindDisabled(dto.isAutobindDisabled());
    }
}
Also used : Owner(org.candlepin.model.Owner) OwnerDTO(org.candlepin.dto.api.v1.OwnerDTO) NotFoundException(org.candlepin.common.exceptions.NotFoundException)

Example 35 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class PoolResource method list.

/**
 * @deprecated Use the method on /owners
 * @return List of pools
 */
@ApiOperation(notes = "Retrieves a list of Pools @deprecated Use the method on /owners", value = "")
@ApiResponses({ @ApiResponse(code = 400, message = "if both consumer(unit) and owner are given, or if a" + " product id is specified without a consumer(unit) or owner"), @ApiResponse(code = 404, message = "if a specified consumer(unit) or owner is not found"), @ApiResponse(code = 403, message = "") })
@GET
@Produces(MediaType.APPLICATION_JSON)
@Wrapped(element = "pools")
@Deprecated
@SecurityHole
public List<PoolDTO> list(@QueryParam("owner") String ownerId, @QueryParam("consumer") String consumerUuid, @QueryParam("product") String productId, @ApiParam("Use with consumerUuid to list all pools available to the consumer. " + "This will include pools which would otherwise be omitted due to a rules" + " warning. (i.e. not recommended) Pools that trigger an error however will" + " still be omitted. (no entitlements available, consumer type mismatch, etc)") @QueryParam("listall") @DefaultValue("false") boolean listAll, @ApiParam("Uses ISO 8601 format") @QueryParam("activeon") String activeOn, @Context Principal principal, @Context PageRequest pageRequest) {
    // Make sure we were given sane query parameters:
    if (consumerUuid != null && ownerId != null) {
        throw new BadRequestException(i18n.tr("Cannot filter on both owner and unit"));
    }
    if (consumerUuid == null && ownerId == null && productId != null) {
        throw new BadRequestException(i18n.tr("A unit or owner is needed to filter on product"));
    }
    Date activeOnDate = activeOn != null ? ResourceDateParser.parseDateString(activeOn) : new Date();
    Consumer c = null;
    String oId = null;
    if (consumerUuid != null) {
        c = consumerCurator.findByUuid(consumerUuid);
        if (c == null) {
            throw new NotFoundException(i18n.tr("Unit: {0} not found", consumerUuid));
        }
        // Now that we have a consumer, check that this principal can access it:
        if (!principal.canAccess(c, SubResource.NONE, Access.READ_ONLY)) {
            throw new ForbiddenException(i18n.tr("User {0} cannot access unit {1}", principal.getPrincipalName(), consumerUuid));
        }
        if (listAll) {
            oId = c.getOwnerId();
        }
    }
    if (ownerId != null) {
        Owner o = ownerCurator.secureFind(ownerId);
        if (o == null) {
            throw new NotFoundException(i18n.tr("owner: {0}", ownerId));
        }
        oId = o.getId();
        // Now that we have an owner, check that this principal can access it:
        if (!principal.canAccess(o, SubResource.POOLS, Access.READ_ONLY)) {
            throw new ForbiddenException(i18n.tr("User {0} cannot access owner {1}", principal.getPrincipalName(), o.getKey()));
        }
    }
    // the system).
    if (consumerUuid == null && ownerId == null && !principal.hasFullAccess()) {
        throw new ForbiddenException(i18n.tr("User {0} cannot access all pools.", principal.getPrincipalName()));
    }
    Page<List<Pool>> page = poolManager.listAvailableEntitlementPools(c, null, oId, productId, null, activeOnDate, listAll, new PoolFilterBuilder(), pageRequest, false, false, null);
    List<Pool> poolList = page.getPageData();
    calculatedAttributesUtil.setCalculatedAttributes(poolList, activeOnDate);
    calculatedAttributesUtil.setQuantityAttributes(poolList, c, activeOnDate);
    // Store the page for the LinkHeaderResponseFilter
    ResteasyProviderFactory.pushContext(Page.class, page);
    List<PoolDTO> poolDTOs = new ArrayList<>();
    for (Pool pool : poolList) {
        poolDTOs.add(translator.translate(pool, PoolDTO.class));
    }
    return poolDTOs;
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) PoolDTO(org.candlepin.dto.api.v1.PoolDTO) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) List(java.util.List) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) SecurityHole(org.candlepin.common.auth.SecurityHole) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Wrapped(org.jboss.resteasy.annotations.providers.jaxb.Wrapped) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

NotFoundException (org.candlepin.common.exceptions.NotFoundException)44 ApiOperation (io.swagger.annotations.ApiOperation)25 Produces (javax.ws.rs.Produces)25 ApiResponses (io.swagger.annotations.ApiResponses)24 Path (javax.ws.rs.Path)22 BadRequestException (org.candlepin.common.exceptions.BadRequestException)16 Consumer (org.candlepin.model.Consumer)12 Owner (org.candlepin.model.Owner)12 DELETE (javax.ws.rs.DELETE)11 GET (javax.ws.rs.GET)10 Entitlement (org.candlepin.model.Entitlement)10 Pool (org.candlepin.model.Pool)9 ArrayList (java.util.ArrayList)7 Transactional (com.google.inject.persist.Transactional)6 Consumes (javax.ws.rs.Consumes)6 ConsumerType (org.candlepin.model.ConsumerType)6 PUT (javax.ws.rs.PUT)5 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)5 Environment (org.candlepin.model.Environment)5 Test (org.junit.Test)5