Search in sources :

Example 16 with BadRequestException

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);
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Product(org.candlepin.model.Product) BadRequestException(org.candlepin.common.exceptions.BadRequestException) 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 17 with BadRequestException

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);
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Event(org.candlepin.audit.Event) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses) Transactional(com.google.inject.persist.Transactional)

Example 18 with BadRequestException

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);
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Pool(org.candlepin.model.Pool) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 19 with BadRequestException

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);
}
Also used : NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Pool(org.candlepin.model.Pool) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 20 with BadRequestException

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);
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ImporterException(org.candlepin.sync.ImporterException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) SyncDataFormatException(org.candlepin.sync.SyncDataFormatException) ResourceMovedException(org.candlepin.common.exceptions.ResourceMovedException) IseException(org.candlepin.common.exceptions.IseException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) ConflictException(org.candlepin.common.exceptions.ConflictException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) PersistenceException(javax.persistence.PersistenceException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

BadRequestException (org.candlepin.common.exceptions.BadRequestException)69 ApiOperation (io.swagger.annotations.ApiOperation)38 Produces (javax.ws.rs.Produces)38 ApiResponses (io.swagger.annotations.ApiResponses)36 Owner (org.candlepin.model.Owner)33 Path (javax.ws.rs.Path)28 Consumer (org.candlepin.model.Consumer)27 Consumes (javax.ws.rs.Consumes)24 NotFoundException (org.candlepin.common.exceptions.NotFoundException)21 POST (javax.ws.rs.POST)15 ConsumerType (org.candlepin.model.ConsumerType)15 Transactional (com.google.inject.persist.Transactional)14 DeletedConsumer (org.candlepin.model.DeletedConsumer)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 GET (javax.ws.rs.GET)13 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)11 PUT (javax.ws.rs.PUT)9 IseException (org.candlepin.common.exceptions.IseException)9 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)9