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;
}
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());
}
}
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);
}
}
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"));
}
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);
}
Aggregations