use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerProductResource method deleteProduct.
@ApiOperation(notes = "Removes a Product", value = "deleteProduct")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{product_id}")
@Transactional
public void deleteProduct(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId) {
Owner owner = this.getOwnerByKey(ownerKey);
Product product = this.fetchProduct(owner, productId);
if (product.isLocked()) {
throw new ForbiddenException(i18n.tr("product \"{0}\" is locked", product.getId()));
}
if (this.productCurator.productHasSubscriptions(owner, product)) {
throw new BadRequestException(i18n.tr("Product with ID \"{0}\" cannot be deleted while subscriptions exist.", productId));
}
this.productManager.removeProduct(owner, product);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method updateOwner.
/**
* Updates an Owner
* <p>
* To un-set the defaultServiceLevel for an owner, submit an empty string.
*
* @param key
* @param dto
* @return an Owner object
* @httpcode 404
* @httpcode 200
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{owner_key}")
@Transactional
@ApiOperation(notes = "To un-set the defaultServiceLevel for an owner, submit an empty string.", value = "Update Owner")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public OwnerDTO updateOwner(@PathParam("owner_key") @Verify(Owner.class) String key, @ApiParam(name = "owner", required = true) OwnerDTO dto) {
log.debug("Updating owner: {}", key);
Owner owner = findOwnerByKey(key);
// Note: We don't allow updating the content access mode list externally
// Reject changes to the content access mode in standalone mode
boolean refreshContentAccess = false;
String cam = dto.getContentAccessMode();
if (cam != null && !cam.equals(owner.getContentAccessMode())) {
if (config.getBoolean(ConfigProperties.STANDALONE)) {
throw new BadRequestException(i18n.tr("The owner content access mode cannot be set directly in standalone mode."));
}
if (!owner.isAllowedContentAccessMode(cam)) {
throw new BadRequestException(i18n.tr("The content access mode is not allowed for this owner."));
}
owner.setContentAccessMode(cam);
refreshContentAccess = true;
}
// Do the bulk of our entity population
this.populateEntity(owner, dto);
owner = ownerCurator.merge(owner);
ownerCurator.flush();
// Refresh content access mode if necessary
if (refreshContentAccess) {
this.ownerManager.refreshOwnerForContentAccess(owner);
}
// Build and queue the owner modified event
Event event = this.eventFactory.getEventBuilder(Target.OWNER, Type.MODIFIED).setEventData(owner).buildEvent();
this.sink.queueEvent(event);
// Output the updated owner
return this.translator.translate(owner, OwnerDTO.class);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method createPool.
/**
* Creates a custom pool for an Owner. Floating pools are not tied to any
* upstream subscription, and are most commonly used for custom content
* delivery in Satellite.
* Also helps in on-site deployment testing
*
* @return a Pool object
* @httpcode 404
* @httpcode 200
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/pools")
@ApiOperation(notes = "Creates a custom pool for an Owner. Floating pools are not tied to any " + "upstream subscription, and are most commonly used for custom content delivery " + "in Satellite. Also helps in on-site deployment testing", value = "Create Pool")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public PoolDTO createPool(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam(name = "pool", required = true) PoolDTO inputPoolDTO) {
log.info("Creating custom pool for owner {}: {}", ownerKey, inputPoolDTO);
Pool pool = new Pool();
// Correct owner
Owner owner = findOwnerByKey(ownerKey);
pool.setOwner(owner);
// Populate the rest of the pool
this.populateEntity(pool, inputPoolDTO);
pool.setContractNumber(inputPoolDTO.getContractNumber());
pool.setOrderNumber(inputPoolDTO.getOrderNumber());
pool.setAccountNumber(inputPoolDTO.getAccountNumber());
pool.setUpstreamPoolId(inputPoolDTO.getUpstreamPoolId());
pool.setSubscriptionSubKey(inputPoolDTO.getSubscriptionSubKey());
pool.setSubscriptionId(inputPoolDTO.getSubscriptionId());
if (inputPoolDTO.getProductId() == null || inputPoolDTO.getProductId().isEmpty()) {
throw new BadRequestException(i18n.tr("Pool product ID not specified"));
}
pool.setProduct(findProduct(pool.getOwner(), inputPoolDTO.getProductId()));
if (inputPoolDTO.getDerivedProductId() != null) {
pool.setDerivedProduct(findProduct(pool.getOwner(), inputPoolDTO.getDerivedProductId()));
}
if (inputPoolDTO.getSourceEntitlement() != null) {
pool.setSourceEntitlement(findEntitlement(inputPoolDTO.getSourceEntitlement().getId()));
}
pool = poolManager.createAndEnrichPools(pool);
return this.translator.translate(pool, PoolDTO.class);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method updatePool.
/**
* Updates a pool for an Owner.
* assumes this is a normal pool, and errors out otherwise cause we cannot
* create master pools from bonus pools
*
* @httpcode 404
* @httpcode 200
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{owner_key}/pools")
@ApiOperation(notes = "Updates a pool for an Owner. assumes this is a normal pool, and " + "errors out otherwise cause we cannot create master pools from bonus pools ", value = "Update Pool")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public void updatePool(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @ApiParam(name = "pool", required = true) PoolDTO newPoolDTO) {
Pool currentPool = this.poolManager.find(newPoolDTO.getId());
if (currentPool == null) {
throw new NotFoundException(i18n.tr("Unable to find a pool with the ID \"{0}\"", newPoolDTO.getId()));
}
// potential security concerns.
if (currentPool.getOwner() == null || !ownerKey.equals(currentPool.getOwner().getKey())) {
throw new NotFoundException(i18n.tr("Pool \"{0}\" does not belong to the specified owner \"{1}\"", currentPool.getId(), ownerKey));
}
// Verify the pool type is one that allows modifications
if (currentPool.getType() != PoolType.NORMAL) {
throw new BadRequestException(i18n.tr("Cannot update bonus pools, as they are auto generated"));
}
if (currentPool.isCreatedByShare()) {
throw new BadRequestException(i18n.tr("Cannot update shared pools, This should be triggered " + "by updating the share entitlement instead"));
}
Pool newPool = new Pool();
this.populateEntity(newPool, newPoolDTO);
// Owner & id have already been validated/resolved.
newPool.setOwner(currentPool.getOwner());
newPool.setId(newPoolDTO.getId());
/* These are @JsonIgnored. If a client creates a pool and subsequently
* wants to update it , we need to ensure Products are set appropriately.
*/
newPool.setProduct(currentPool.getProduct());
newPool.setDerivedProduct(currentPool.getDerivedProduct());
// Forcefully set fields we don't allow the client to change.
newPool.setCreatedByShare(currentPool.isCreatedByShare());
newPool.setHasSharedAncestor(currentPool.hasSharedAncestor());
newPool.setSourceSubscription(currentPool.getSourceSubscription());
newPool.setContractNumber(currentPool.getContractNumber());
newPool.setAccountNumber(currentPool.getAccountNumber());
newPool.setOrderNumber(currentPool.getOrderNumber());
newPool.setUpstreamPoolId(currentPool.getUpstreamPoolId());
newPool.setUpstreamEntitlementId(currentPool.getUpstreamEntitlementId());
newPool.setUpstreamConsumerId(currentPool.getUpstreamConsumerId());
newPool.setCertificate(currentPool.getCertificate());
newPool.setCdn(currentPool.getCdn());
// Update fields the client has changed, or otherwise set the current value.
if (newPoolDTO.getStartDate() == null) {
newPool.setStartDate(currentPool.getStartDate());
}
if (newPoolDTO.getEndDate() == null) {
newPool.setEndDate(currentPool.getEndDate());
}
if (newPoolDTO.getQuantity() == null) {
newPool.setQuantity(currentPool.getQuantity());
}
if (newPoolDTO.getAttributes() == null) {
newPool.setAttributes(currentPool.getAttributes());
}
if (newPoolDTO.getProvidedProducts() == null) {
newPool.setProvidedProducts(currentPool.getProvidedProducts());
}
if (newPoolDTO.getDerivedProvidedProducts() == null) {
newPool.setDerivedProvidedProducts(currentPool.getDerivedProvidedProducts());
}
if (newPoolDTO.getBranding() == null) {
newPool.setBranding(currentPool.getBranding());
}
// Apply changes to the pool and its derived pools
this.poolManager.updateMasterPool(newPool);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class OwnerResource method createOwner.
/**
* Creates an Owner
*
* @return an Owner object
* @httpcode 400
* @httpcode 200
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(notes = "Creates an Owner", value = "Create Owner")
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid owner specified in body") })
public OwnerDTO createOwner(@ApiParam(name = "owner", required = true) OwnerDTO dto) {
// Validate and set content access mode list & content access mode
if (StringUtils.isBlank(dto.getContentAccessModeList())) {
dto.setContentAccessModeList(ContentAccessCertServiceAdapter.DEFAULT_CONTENT_ACCESS_MODE);
}
if (StringUtils.isBlank(dto.getContentAccessMode())) {
dto.setContentAccessMode(ContentAccessCertServiceAdapter.DEFAULT_CONTENT_ACCESS_MODE);
}
if (!containsContentAccessMode(dto.getContentAccessModeList(), dto.getContentAccessMode())) {
throw new BadRequestException(i18n.tr("The content access mode \"{1}\" is not allowed for this owner.", dto.getContentAccessMode()));
}
// Translate the DTO to an entity Owner.
Owner owner = new Owner();
this.populateEntity(owner, dto);
owner.setKey(dto.getKey());
owner.setContentAccessModeList(dto.getContentAccessModeList());
owner.setContentAccessMode(dto.getContentAccessMode());
// Try to persist the owner
try {
owner = this.ownerCurator.create(owner);
if (owner == null) {
throw new BadRequestException(i18n.tr("Could not create the Owner: {0}", owner));
}
} catch (Exception e) {
log.debug("Unable to create owner: ", e);
throw new BadRequestException(i18n.tr("Could not create the Owner: {0}", owner));
}
log.info("Created owner: {}", owner);
sink.emitOwnerCreated(owner);
return this.translator.translate(owner, OwnerDTO.class);
}
Aggregations