use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerProductResource method updateProduct.
@ApiOperation(notes = "Updates a Product", value = "updateProduct")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@PUT
@Path("/{product_id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
public ProductDTO updateProduct(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, @ApiParam(name = "update", required = true) ProductDTO update) {
if (StringUtils.isEmpty(update.getId())) {
update.setId(productId);
} else if (!StringUtils.equals(update.getId(), productId)) {
throw new BadRequestException(i18n.tr("Contradictory ids in update request: {0}, {1}", productId, update.getId()));
}
Owner owner = this.getOwnerByKey(ownerKey);
Product existing = this.fetchProduct(owner, productId);
if (existing.isLocked()) {
throw new ForbiddenException(i18n.tr("product \"{0}\" is locked", existing.getId()));
}
this.productCurator.lock(existing);
Product updated = this.productManager.updateProduct(update, owner, true);
return this.translator.translate(updated, ProductDTO.class);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method setLogLevel.
/**
* Sets the Log Level for an Owner
*
* @param ownerKey
* @param level
* @return an Owner object
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
@Path("{owner_key}/log")
@ApiOperation(notes = "Sets the Log Level for an Owner", value = "Set Log Level")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public OwnerDTO setLogLevel(@PathParam("owner_key") String ownerKey, @QueryParam("level") @DefaultValue("DEBUG") @ApiParam(allowableValues = "ALL, TRACE, DEBUG, INFO, WARN, ERROR, OFF") String level) {
Owner owner = findOwnerByKey(ownerKey);
Level logLevel = Level.toLevel(level, null);
if (logLevel == null) {
throw new BadRequestException(i18n.tr("{0} is not a valid log level", level));
}
owner.setLogLevel(logLevel.toString());
owner = ownerCurator.merge(owner);
return this.translator.translate(owner, OwnerDTO.class);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method createActivationKey.
/**
* Creates an Activation Key for the Owner
*
* @param ownerKey id of the owner whose keys are sought.
* @return an Activation Key object
* @httpcode 400
* @httpcode 404
* @httpcode 200
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{owner_key}/activation_keys")
@ApiOperation(notes = "Creates an Activation Key for the Owner", value = "Create Activation Key")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 400, message = "Invalid activation key") })
public ActivationKeyDTO createActivationKey(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam(name = "activation_key", required = true) ActivationKeyDTO dto) {
if (StringUtils.isBlank(dto.getName())) {
throw new BadRequestException(i18n.tr("Must provide a name for activation key."));
}
String testName = dto.getName().replace("-", "0").replace("_", "0");
if (!testName.matches("[a-zA-Z0-9]*")) {
throw new BadRequestException(i18n.tr("The activation key name \"{0}\" must be alphanumeric or " + "include the characters \"-\" or \"_\"", dto.getName()));
}
if (dto.getContentOverrides() != null) {
contentOverrideValidator.validateDTOs(dto.getContentOverrides());
}
Owner owner = findOwnerByKey(ownerKey);
if (activationKeyCurator.lookupForOwner(dto.getName(), owner) != null) {
throw new BadRequestException(i18n.tr("The activation key name \"{0}\" is already in use for owner {1}", dto.getName(), ownerKey));
}
serviceLevelValidator.validate(owner.getId(), dto.getServiceLevel());
ActivationKey key = new ActivationKey();
this.populateEntity(key, dto);
key.setOwner(owner);
ActivationKey newKey = activationKeyCurator.create(key);
sink.emitActivationKeyCreated(newKey);
return translator.translate(newKey, ActivationKeyDTO.class);
}
use of org.candlepin.common.exceptions.BadRequestException 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;
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class RulesResource method upload.
@ApiOperation(notes = "Uploads the Rules Returns a copy of the uploaded rules.", value = "upload")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public String upload(String rulesBuffer) {
if (rulesBuffer == null || rulesBuffer.isEmpty()) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}
Rules rules = null;
try {
String decoded = new String(Base64.decodeBase64(rulesBuffer));
rules = new Rules(decoded);
} catch (Throwable t) {
log.error("Exception in rules upload", t);
throw new BadRequestException(i18n.tr("Error decoding the rules. The text should be base 64 encoded"));
}
Rules oldRules = rulesCurator.getRules();
rulesCurator.update(rules);
sink.emitRulesModified(oldRules, rules);
// Trigger a recompile of the JS rules so version/source are set correctly:
jsProvider.compileRules(true);
return rulesBuffer;
}
Aggregations