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();
}
}
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;
}
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);
}
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)));
}
}
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;
}
Aggregations