Search in sources :

Example 51 with BadRequestException

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

the class ConsumerResource method checkActivationKeys.

private List<ActivationKey> checkActivationKeys(Principal principal, Owner owner, Set<String> keyStrings) throws BadRequestException {
    List<ActivationKey> keys = new ArrayList<>();
    for (String keyString : keyStrings) {
        ActivationKey key = null;
        try {
            key = findKey(keyString, owner);
            keys.add(key);
        } catch (NotFoundException e) {
            log.warn(e.getMessage());
        }
    }
    if ((principal instanceof NoAuthPrincipal) && keys.isEmpty()) {
        throw new BadRequestException(i18n.tr("None of the activation keys specified exist for this org."));
    }
    return keys;
}
Also used : NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ActivationKey(org.candlepin.model.activationkeys.ActivationKey)

Example 52 with BadRequestException

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

the class ConsumerResource method verifyPersonConsumer.

private void verifyPersonConsumer(ConsumerDTO consumer, ConsumerType type, Owner owner, String username, Principal principal) {
    User user = null;
    try {
        user = userService.findByLogin(username);
    } catch (UnsupportedOperationException e) {
        log.warn("User service does not allow user lookups, cannot verify person consumer.");
    }
    if (user == null) {
        throw new NotFoundException(i18n.tr("User with ID \"{0}\" could not be found."));
    }
    // has some association with the owner the consumer is destined for:
    if (!principal.canAccess(owner, SubResource.NONE, Access.ALL) && !principal.hasFullAccess()) {
        throw new ForbiddenException(i18n.tr("User \"{0}\" has no roles for organization \"{1}\"", user.getUsername(), owner.getKey()));
    }
    // TODO: Refactor out type specific checks?
    if (type.isType(ConsumerTypeEnum.PERSON)) {
        Consumer existing = consumerCurator.findByUser(user);
        if (existing != null && this.consumerTypeCurator.getConsumerType(existing).isType(ConsumerTypeEnum.PERSON)) {
            // TODO: This is not the correct error code for this situation!
            throw new BadRequestException(i18n.tr("User \"{0}\" has already registered a personal consumer", user.getUsername()));
        }
        consumer.setName(user.getUsername());
    }
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) User(org.candlepin.model.User) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 53 with BadRequestException

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

the class ConsumerResource method exportData.

/**
 * Retrieves a compressed file representation of a Consumer (manifest).
 *
 * @deprecated use GET /consumers/:consumer_uuid/export/async
 * @param response
 * @param consumerUuid
 * @param cdnLabel
 * @param webAppPrefix
 * @param apiUrl
 * @return the generated file archive.
 */
@Deprecated
@ApiOperation(notes = "Retrieves a Compressed File representation of a Consumer (manifest).", value = "Consumer Export (manifest)", response = File.class)
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 500, message = ""), @ApiResponse(code = 404, message = "") })
@Produces("application/zip")
@GET
@Path("{consumer_uuid}/export")
public File exportData(@Context HttpServletResponse response, @PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("cdn_label") String cdnLabel, @QueryParam("webapp_prefix") String webAppPrefix, @QueryParam("api_url") String apiUrl, @QueryParam("ext") @CandlepinParam(type = KeyValueParameter.class) @ApiParam(value = "Key/Value pairs to be passed to the extension adapter when generating a manifest", required = false, example = "ext=version:1.2.3&ext=extension_key:EXT1") List<KeyValueParameter> extensionArgs) {
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    if (ctype.isType(ConsumerTypeEnum.SHARE)) {
        throw new BadRequestException(i18n.tr("Can not export manifest of a share consumer"));
    }
    try {
        File archive = manifestManager.generateManifest(consumerUuid, cdnLabel, webAppPrefix, apiUrl, getExtensionParamMap(extensionArgs));
        response.addHeader("Content-Disposition", "attachment; filename=" + archive.getName());
        return archive;
    } catch (ExportCreationException e) {
        throw new IseException(i18n.tr("Unable to create export archive"), e);
    }
}
Also used : DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) IseException(org.candlepin.common.exceptions.IseException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 54 with BadRequestException

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

the class ConsumerResource method getHost.

@ApiOperation(notes = "Retrieves a Host Consumer of a Consumer", value = "getHost")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{consumer_uuid}/host")
public ConsumerDTO getHost(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @Context Principal principal) {
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    if (consumer.getFact("virt.uuid") == null || consumer.getFact("virt.uuid").trim().equals("")) {
        throw new BadRequestException(i18n.tr("The system with UUID {0} is not a virtual guest.", consumer.getUuid()));
    }
    Consumer host = consumerCurator.getHost(consumer);
    Owner hostOwner = ownerCurator.findOwnerById(host.getOwnerId());
    // current organization.
    if (host == null || principal.canAccess(hostOwner, SubResource.CONSUMERS, Access.READ_ONLY)) {
        return translator.translate(host, ConsumerDTO.class);
    }
    throw new ForbiddenException(i18n.tr("This host is under a different organization that you do not have access to"));
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 55 with BadRequestException

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

the class OwnerProductResource method getProductCertificate.

@ApiOperation(notes = "Retrieves a Certificate for a Product", value = "getProductCertificate")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@GET
@Path("/{product_id}/certificate")
@Produces(MediaType.APPLICATION_JSON)
@SecurityHole
@Transactional
public ProductCertificateDTO getProductCertificate(@PathParam("owner_key") String ownerKey, @ApiParam(name = "productId", required = true, value = "Numeric product identifier") @PathParam("product_id") String productId) {
    if (!productId.matches("\\d+")) {
        throw new BadRequestException(i18n.tr("Only numeric product IDs are allowed."));
    }
    Owner owner = this.getOwnerByKey(ownerKey);
    Product product = this.fetchProduct(owner, productId);
    ProductCertificate productCertificate = this.productCertCurator.getCertForProduct(product);
    return this.translator.translate(productCertificate, ProductCertificateDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) ProductCertificate(org.candlepin.model.ProductCertificate) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Product(org.candlepin.model.Product) Path(javax.ws.rs.Path) SecurityHole(org.candlepin.common.auth.SecurityHole) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) Transactional(com.google.inject.persist.Transactional)

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