Search in sources :

Example 16 with ForbiddenException

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

the class ConsumerResource method dryBind.

@ApiOperation(notes = "Retrieves a list of Pools and quantities that would be the " + "result of an auto-bind. This is a dry run of an autobind. It allows the client " + "to see what would be the result of an autobind without executing it. It can only" + " do this for the prevously established list of installed products for the consumer" + " If a service level is included in the request, then that level will override " + "the one stored on the consumer. If no service level is included then the existing " + "one will be used. The Response has a list of PoolQuantity objects", value = "dryBind")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{consumer_uuid}/entitlements/dry-run")
public List<PoolQuantityDTO> dryBind(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("service_level") String serviceLevel) {
    // Verify consumer exists:
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    if (owner.isAutobindDisabled()) {
        throw new BadRequestException(i18n.tr("Owner has autobind disabled."));
    }
    List<PoolQuantity> dryRunPools = new ArrayList<>();
    try {
        consumerBindUtil.validateServiceLevel(consumer.getOwnerId(), serviceLevel);
        dryRunPools = entitler.getDryRun(consumer, owner, serviceLevel);
    } catch (ForbiddenException fe) {
        return Collections.<PoolQuantityDTO>emptyList();
    } catch (BadRequestException bre) {
        throw bre;
    } catch (RuntimeException re) {
        return Collections.<PoolQuantityDTO>emptyList();
    }
    if (dryRunPools != null) {
        List<PoolQuantityDTO> dryRunPoolDtos = new ArrayList<>();
        for (PoolQuantity pq : dryRunPools) {
            dryRunPoolDtos.add(this.translator.translate(pq, PoolQuantityDTO.class));
        }
        return dryRunPoolDtos;
    } else {
        return Collections.<PoolQuantityDTO>emptyList();
    }
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) ArrayList(java.util.ArrayList) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PoolQuantityDTO(org.candlepin.dto.api.v1.PoolQuantityDTO) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 17 with ForbiddenException

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

the class OwnerContentResource method createContentImpl.

/**
 * Creates or merges the given Content object.
 *
 * @param owner
 *  The owner for which to create the new content
 *
 * @param content
 *  The content to create or merge
 *
 * @return
 *  the newly created and/or merged Content object.
 */
private Content createContentImpl(Owner owner, ContentDTO content) {
    // TODO: check if arches have changed ??
    Content entity = null;
    if (content.getId() == null || content.getId().trim().length() == 0) {
        content.setId(this.idGenerator.generateId());
        entity = this.contentManager.createContent(content, owner);
    } else {
        Content existing = this.ownerContentCurator.getContentById(owner, content.getId());
        if (existing != null) {
            if (existing.isLocked()) {
                throw new ForbiddenException(i18n.tr("content \"{0}\" is locked", existing.getId()));
            }
            entity = this.contentManager.updateContent(content, owner, true);
        } else {
            entity = this.contentManager.createContent(content, owner);
        }
    }
    return entity;
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content)

Example 18 with ForbiddenException

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

the class OwnerContentResource method remove.

@ApiOperation(notes = "Deletes a Content", value = "remove")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{content_id}")
public void remove(@PathParam("owner_key") String ownerKey, @PathParam("content_id") String contentId) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Content content = this.fetchContent(owner, contentId);
    if (content.isLocked()) {
        throw new ForbiddenException(i18n.tr("content \"{0}\" is locked", content.getId()));
    }
    this.contentManager.removeContent(owner, content, true);
    ownerManager.refreshOwnerForContentAccess(owner);
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 19 with ForbiddenException

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

the class Entitler method bindByPoolQuantity.

public List<Entitlement> bindByPoolQuantity(Consumer consumer, String poolId, Integer quantity) {
    Map<String, Integer> poolMap = new HashMap<>();
    poolMap.put(poolId, quantity);
    try {
        return bindByPoolQuantities(consumer, poolMap);
    } catch (EntitlementRefusedException e) {
        // TODO: Could be multiple errors, but we'll just report the first
        // one for now
        Pool pool = poolCurator.find(poolId);
        throw new ForbiddenException(messageTranslator.poolErrorToMessage(pool, e.getResults().get(poolId).getErrors().get(0)));
    }
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) Pool(org.candlepin.model.Pool)

Example 20 with ForbiddenException

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

the class Entitler method getDryRun.

/**
 * Entitles the given Consumer to the given Product. Will seek out pools
 * which provide access to this product, either directly or as a child, and
 * select the best one based on a call to the rules engine.
 *
 * @param consumer The consumer being entitled.
 * @return List of Entitlements
 */
public List<PoolQuantity> getDryRun(Consumer consumer, Owner owner, String serviceLevelOverride) {
    List<PoolQuantity> result = new ArrayList<>();
    try {
        if (consumer.isDev()) {
            if (config.getBoolean(ConfigProperties.STANDALONE) || !poolCurator.hasActiveEntitlementPools(consumer.getOwnerId(), null)) {
                throw new ForbiddenException(i18n.tr("Development units may only be used on hosted servers" + " and with orgs that have active subscriptions."));
            }
            // Look up the dev pool for this consumer, and if not found
            // create one. If a dev pool already exists, remove it and
            // create a new one.
            String sku = consumer.getFact("dev_sku");
            Pool devPool = poolCurator.findDevPool(consumer);
            if (devPool != null) {
                poolManager.deletePool(devPool);
            }
            devPool = poolManager.createPool(assembleDevPool(consumer, owner, sku));
            result.add(new PoolQuantity(devPool, 1));
        } else {
            result = poolManager.getBestPools(consumer, null, null, owner.getId(), serviceLevelOverride, null);
        }
        log.debug("Created Pool Quantity list: {}", result);
    } catch (EntitlementRefusedException e) {
        // to the process
        if (log.isDebugEnabled()) {
            log.debug("consumer {} dry-run errors:", consumer.getUuid());
            for (Entry<String, ValidationResult> entry : e.getResults().entrySet()) {
                log.debug("errors for pool id: {}", entry.getKey());
                for (ValidationError error : entry.getValue().getErrors()) {
                    log.debug(error.getResourceKey());
                }
            }
        }
    }
    return result;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Entry(java.util.Map.Entry) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError)

Aggregations

ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)37 Owner (org.candlepin.model.Owner)22 Consumer (org.candlepin.model.Consumer)19 Test (org.junit.Test)15 BadRequestException (org.candlepin.common.exceptions.BadRequestException)13 Product (org.candlepin.model.Product)13 ApiOperation (io.swagger.annotations.ApiOperation)12 Produces (javax.ws.rs.Produces)12 Pool (org.candlepin.model.Pool)12 ArrayList (java.util.ArrayList)11 Path (javax.ws.rs.Path)11 NotFoundException (org.candlepin.common.exceptions.NotFoundException)9 Transactional (com.google.inject.persist.Transactional)8 ApiResponses (io.swagger.annotations.ApiResponses)8 Content (org.candlepin.model.Content)7 HashMap (java.util.HashMap)6 Consumes (javax.ws.rs.Consumes)6 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)6 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)6 DELETE (javax.ws.rs.DELETE)5