Search in sources :

Example 36 with BadRequestException

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

the class GuestIdResource method updateGuest.

@ApiOperation(notes = "Updates a single Guest on a Consumer. Allows virt-who to avoid uploading" + " an entire list of guests", value = "updateGuest")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{guest_id}")
public void updateGuest(@ApiParam("consumer who owns or hosts the guest in question") @PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @ApiParam("guest virtual uuid") @PathParam("guest_id") String guestId, @ApiParam(name = "updated", required = true, value = "updated guest data to use") GuestIdDTO updatedDTO) {
    // I'm not sure this can happen
    if (guestId == null || guestId.isEmpty()) {
        throw new BadRequestException(i18n.tr("Please supply a valid guest id"));
    }
    if (updatedDTO == null) {
        // If they're not sending attributes, we can get the guestId from the url
        updatedDTO = new GuestIdDTO().setGuestId(guestId);
    }
    // Allow the id to be left out in this case, we can use the path param
    if (updatedDTO.getGuestId() == null) {
        updatedDTO.setGuestId(guestId);
    }
    // If the guest uuids do not match, something is wrong
    if (!guestId.equalsIgnoreCase(updatedDTO.getGuestId())) {
        throw new BadRequestException(i18n.tr("Guest ID in json \"{0}\" does not match path guest ID \"{1}\"", updatedDTO.getGuestId(), guestId));
    }
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    GuestId guestIdEntity = new GuestId();
    populateEntity(guestIdEntity, updatedDTO);
    guestIdEntity.setConsumer(consumer);
    GuestId toUpdate = guestIdCurator.findByGuestIdAndOrg(guestId, consumer.getOwnerId());
    if (toUpdate != null) {
        guestIdEntity.setId(toUpdate.getId());
    }
    guestIdCurator.merge(guestIdEntity);
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) Consumer(org.candlepin.model.Consumer) GuestId(org.candlepin.model.GuestId) BadRequestException(org.candlepin.common.exceptions.BadRequestException) 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)

Example 37 with BadRequestException

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

the class ConsumerTypeResource method create.

@ApiOperation(notes = "Creates a Consumer Type", value = "create")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ConsumerTypeDTO create(@ApiParam(name = "consumerType", required = true) ConsumerTypeDTO dto) throws BadRequestException {
    try {
        ConsumerType type = new ConsumerType();
        this.populateEntity(type, dto);
        type = consumerTypeCurator.create(type);
        return this.translator.translate(type, ConsumerTypeDTO.class);
    } catch (Exception e) {
        log.error("Problem creating unit type: ", e);
        throw new BadRequestException(i18n.tr("Problem creating unit type: {0}", dto));
    }
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) 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 38 with BadRequestException

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

the class EntitlementResource method updateEntitlement.

@ApiOperation(notes = "Updates an Entitlement. This only works for the quantity.", value = "updateEntitlement")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{entitlement_id}")
public void updateEntitlement(@PathParam("entitlement_id") @Verify(Entitlement.class) String id, @ApiParam(name = "update", required = true) EntitlementDTO update) {
    // Check that quantity param was set and is not 0:
    if (update.getQuantity() <= 0) {
        throw new BadRequestException(i18n.tr("Quantity value must be greater than 0."));
    }
    // Verify entitlement exists:
    Entitlement entitlement = entitlementCurator.find(id);
    if (entitlement != null) {
        // make sure that this will be a change
        if (!entitlement.getQuantity().equals(update.getQuantity())) {
            Consumer consumer = entitlement.getConsumer();
            entitler.adjustEntitlementQuantity(consumer, entitlement, update.getQuantity());
        }
    } else {
        throw new NotFoundException(i18n.tr("Entitlement with ID \"{0}\" could not be found.", id));
    }
}
Also used : Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Entitlement(org.candlepin.model.Entitlement) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT)

Example 39 with BadRequestException

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

the class HypervisorResource method hypervisorUpdateAsync.

@ApiOperation(notes = "Creates or Updates the list of Hypervisor hosts Allows agents such" + " as virt-who to update hosts' information . This is typically used when a host is" + " unable to register to candlepin via subscription manager. In situations where " + "consumers already exist it is probably best not to allow creation of new hypervisor" + " consumers.  Most consumers do not have a hypervisorId attribute, so that should be" + " added manually when necessary by the management environment. Default is true.  " + "If false is specified, hypervisorIds that are not found will result in a failed " + "state of the job.", value = "hypervisorUpdateAsync")
@ApiResponses({ @ApiResponse(code = 202, message = "") })
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@Path("/{owner}")
@UpdateConsumerCheckIn
@SuppressWarnings("checkstyle:indentation")
public JobDetail hypervisorUpdateAsync(String hypervisorJson, @Context Principal principal, @PathParam("owner") @Verify(value = Owner.class, require = Access.READ_ONLY, subResource = SubResource.HYPERVISOR) String ownerKey, @ApiParam("specify whether or not to create missing hypervisors." + "Default is true.  If false is specified, hypervisorIds that are not found" + "will result in failed entries in the resulting HypervisorCheckInResult") @QueryParam("create_missing") @DefaultValue("true") boolean createMissing, @QueryParam("reporter_id") String reporterId) {
    if (hypervisorJson == null || hypervisorJson.isEmpty()) {
        log.debug("Host/Guest mapping provided during hypervisor update was null.");
        throw new BadRequestException(i18n.tr("Host to guest mapping was not provided for hypervisor update."));
    }
    log.info("Hypervisor update by principal: " + principal);
    Owner owner = this.getOwner(ownerKey);
    return HypervisorUpdateJob.forOwner(owner, hypervisorJson, createMissing, principal, reporterId);
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Path(javax.ws.rs.Path) UpdateConsumerCheckIn(org.candlepin.auth.UpdateConsumerCheckIn) 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) Transactional(com.google.inject.persist.Transactional)

Example 40 with BadRequestException

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

the class ActivationKeyResource method addProductIdToKey.

@ApiOperation(notes = "Adds an Product ID to an Activation Key", value = "Add Product ID to key")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Path("{activation_key_id}/product/{product_id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public ActivationKeyDTO addProductIdToKey(@PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId, @PathParam("product_id") String productId) {
    ActivationKey key = activationKeyCurator.verifyAndLookupKey(activationKeyId);
    Product product = confirmProduct(key.getOwner(), productId);
    // Make sure we don't try to register the product ID twice.
    if (key.hasProduct(product)) {
        throw new BadRequestException(i18n.tr("Product ID \"{0}\" has already been registered with this activation key", productId));
    }
    key.addProduct(product);
    activationKeyCurator.update(key);
    return this.translator.translate(key, ActivationKeyDTO.class);
}
Also used : Product(org.candlepin.model.Product) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Path(javax.ws.rs.Path) 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