use of org.candlepin.common.exceptions.NotFoundException 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.NotFoundException in project candlepin by candlepin.
the class ConsumerResource method removeDeletionRecord.
@ApiOperation(notes = "Removes the Deletion Record for a Consumer Allowed for a superadmin." + " The main use case for this would be if a user accidently deleted a " + "non-RHEL hypervisor, causing it to no longer be auto-detected via virt-who.", value = "removeDeletionRecord")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@DELETE
@Path("{consumer_uuid}/deletionrecord")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public void removeDeletionRecord(@PathParam("consumer_uuid") String uuid) {
DeletedConsumer dc = deletedConsumerCurator.findByConsumerUuid(uuid);
if (dc == null) {
throw new NotFoundException(i18n.tr("Deletion record for hypervisor \"{0}\" not found.", uuid));
}
deletedConsumerCurator.delete(dc);
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class ConsumerResource method unbind.
@ApiOperation(notes = "Removes an Entitlement from a Consumer By the Entitlement ID", value = "unbind")
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.WILDCARD)
@Path("/{consumer_uuid}/entitlements/{dbid}")
public void unbind(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @PathParam("dbid") @Verify(Entitlement.class) String dbid, @Context Principal principal) {
consumerCurator.verifyAndLookupConsumer(consumerUuid);
Entitlement toDelete = entitlementCurator.find(dbid);
if (toDelete != null) {
poolManager.revokeEntitlement(toDelete);
return;
}
throw new NotFoundException(i18n.tr("Entitlement with ID \"{0}\" could not be found.", dbid));
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class OwnerResource method populateEntity.
/**
* Populates the specified entity with data from the provided DTO. This method will not set the
* ID, key, upstream consumer, content access mode list or content access mode fields.
*
* @param entity
* The entity instance to populate
*
* @param dto
* The DTO containing the data with which to populate the entity
*
* @throws IllegalArgumentException
* if either entity or dto are null
*/
protected void populateEntity(Owner entity, OwnerDTO dto) {
if (entity == null) {
throw new IllegalArgumentException("the owner model entity is null");
}
if (dto == null) {
throw new IllegalArgumentException("the owner dto is null");
}
if (dto.getDisplayName() != null) {
entity.setDisplayName(dto.getDisplayName());
}
if (dto.getParentOwner() != null) {
// Impl note:
// We do not allow modifying a parent owner through its children, so all we'll do here
// is set the parent owner and ignore everything else; including further nested owners.
OwnerDTO pdto = dto.getParentOwner();
Owner parent = null;
if (pdto.getId() != null) {
// look up by ID
parent = this.ownerCurator.find(pdto.getId());
} else if (pdto.getKey() != null) {
// look up by key
parent = this.ownerCurator.lookupByKey(pdto.getKey());
}
if (parent == null) {
throw new NotFoundException(i18n.tr("Unable to find parent owner: {0}", pdto));
}
entity.setParentOwner(parent);
}
if (dto.getContentPrefix() != null) {
entity.setContentPrefix(dto.getContentPrefix());
}
if (dto.getDefaultServiceLevel() != null) {
if (dto.getDefaultServiceLevel().isEmpty()) {
entity.setDefaultServiceLevel(null);
} else {
this.serviceLevelValidator.validate(entity.getId(), dto.getDefaultServiceLevel());
entity.setDefaultServiceLevel(dto.getDefaultServiceLevel());
}
}
if (dto.getLogLevel() != null) {
entity.setLogLevel(dto.getLogLevel());
}
if (dto.isAutobindDisabled() != null) {
entity.setAutobindDisabled(dto.isAutobindDisabled());
}
}
use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.
the class PoolResource method list.
/**
* @deprecated Use the method on /owners
* @return List of pools
*/
@ApiOperation(notes = "Retrieves a list of Pools @deprecated Use the method on /owners", value = "")
@ApiResponses({ @ApiResponse(code = 400, message = "if both consumer(unit) and owner are given, or if a" + " product id is specified without a consumer(unit) or owner"), @ApiResponse(code = 404, message = "if a specified consumer(unit) or owner is not found"), @ApiResponse(code = 403, message = "") })
@GET
@Produces(MediaType.APPLICATION_JSON)
@Wrapped(element = "pools")
@Deprecated
@SecurityHole
public List<PoolDTO> list(@QueryParam("owner") String ownerId, @QueryParam("consumer") String consumerUuid, @QueryParam("product") String productId, @ApiParam("Use with consumerUuid to list all pools available to the consumer. " + "This will include pools which would otherwise be omitted due to a rules" + " warning. (i.e. not recommended) Pools that trigger an error however will" + " still be omitted. (no entitlements available, consumer type mismatch, etc)") @QueryParam("listall") @DefaultValue("false") boolean listAll, @ApiParam("Uses ISO 8601 format") @QueryParam("activeon") String activeOn, @Context Principal principal, @Context PageRequest pageRequest) {
// Make sure we were given sane query parameters:
if (consumerUuid != null && ownerId != null) {
throw new BadRequestException(i18n.tr("Cannot filter on both owner and unit"));
}
if (consumerUuid == null && ownerId == null && productId != null) {
throw new BadRequestException(i18n.tr("A unit or owner is needed to filter on product"));
}
Date activeOnDate = activeOn != null ? ResourceDateParser.parseDateString(activeOn) : new Date();
Consumer c = null;
String oId = null;
if (consumerUuid != null) {
c = consumerCurator.findByUuid(consumerUuid);
if (c == null) {
throw new NotFoundException(i18n.tr("Unit: {0} not found", consumerUuid));
}
// Now that we have a consumer, check that this principal can access it:
if (!principal.canAccess(c, SubResource.NONE, Access.READ_ONLY)) {
throw new ForbiddenException(i18n.tr("User {0} cannot access unit {1}", principal.getPrincipalName(), consumerUuid));
}
if (listAll) {
oId = c.getOwnerId();
}
}
if (ownerId != null) {
Owner o = ownerCurator.secureFind(ownerId);
if (o == null) {
throw new NotFoundException(i18n.tr("owner: {0}", ownerId));
}
oId = o.getId();
// Now that we have an owner, check that this principal can access it:
if (!principal.canAccess(o, SubResource.POOLS, Access.READ_ONLY)) {
throw new ForbiddenException(i18n.tr("User {0} cannot access owner {1}", principal.getPrincipalName(), o.getKey()));
}
}
// the system).
if (consumerUuid == null && ownerId == null && !principal.hasFullAccess()) {
throw new ForbiddenException(i18n.tr("User {0} cannot access all pools.", principal.getPrincipalName()));
}
Page<List<Pool>> page = poolManager.listAvailableEntitlementPools(c, null, oId, productId, null, activeOnDate, listAll, new PoolFilterBuilder(), pageRequest, false, false, null);
List<Pool> poolList = page.getPageData();
calculatedAttributesUtil.setCalculatedAttributes(poolList, activeOnDate);
calculatedAttributesUtil.setQuantityAttributes(poolList, c, activeOnDate);
// Store the page for the LinkHeaderResponseFilter
ResteasyProviderFactory.pushContext(Page.class, page);
List<PoolDTO> poolDTOs = new ArrayList<>();
for (Pool pool : poolList) {
poolDTOs.add(translator.translate(pool, PoolDTO.class));
}
return poolDTOs;
}
Aggregations