Search in sources :

Example 26 with NotFoundException

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

the class UserResourceTest method testUpdateUsersNoLogin.

@Test
public void testUpdateUsersNoLogin() {
    try {
        User user = new User();
        user.setUsername("henri");
        user.setPassword("password");
        userResource.updateUser("JarJarIsMyCopilot", user);
    } catch (NotFoundException e) {
        // this is exptected
        return;
    }
    fail("No exception was thrown");
}
Also used : User(org.candlepin.model.User) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Test(org.junit.Test)

Example 27 with NotFoundException

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

the class JobCurator method cancelNoReturn.

@Transactional
public void cancelNoReturn(String jobId) {
    String hql = "update JobStatus j " + "set j.state = :canceled " + "where j.id = :jobid";
    Query query = this.currentSession().createQuery(hql).setParameter("jobid", jobId).setInteger("canceled", JobState.CANCELED.ordinal());
    int updated = query.executeUpdate();
    if (updated == 0) {
        throw new NotFoundException("job not found");
    }
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(org.hibernate.Query) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Transactional(com.google.inject.persist.Transactional)

Example 28 with NotFoundException

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

the class ConsumerResource method validateShareConsumer.

/**
 * Ensure that certain fields remain unset when creating a share consumer.
 * @param consumer the consumer to validate
 * @param principal the principal performing the operation
 * @param keys any provided activation keys
 * @throws BadRequestException if any validations fail
 */
private void validateShareConsumer(ConsumerDTO consumer, Principal principal, List<ActivationKey> keys) throws BadRequestException {
    if (keys.size() > 0) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot be used with activation keys"));
    }
    if (StringUtils.isNotBlank(consumer.getServiceLevel())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a service level"));
    }
    if (StringUtils.isNotBlank(consumer.getReleaseVersion())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a release version"));
    }
    if (CollectionUtils.isNotEmpty(consumer.getInstalledProducts())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have installed products"));
    }
    if (StringUtils.isNotBlank(consumer.getContentAccessMode())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a content access mode"));
    }
    if (consumer.getGuestIds() != null && !consumer.getGuestIds().isEmpty()) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have guest IDs"));
    }
    if (consumer.getHypervisorId() != null) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a hypervisor ID"));
    }
    if (consumer.getRecipientOwnerKey() == null) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" must specify a recipient org key"));
    }
    if (consumer.isGuest()) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot be a virtual guest"));
    }
    String recipient = consumer.getRecipientOwnerKey();
    Owner recipientOwner = ownerCurator.lookupByKey(recipient);
    if (recipientOwner == null) {
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", recipient));
    }
    // Check permissions for current principal on the recipient owner
    if (!principal.canAccess(recipientOwner, SubResource.ENTITLEMENTS, Access.CREATE)) {
        log.warn("User {} does not have access to create shares to org {}", principal.getPrincipalName(), recipient);
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", recipient));
    }
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException)

Example 29 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException 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 30 with NotFoundException

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

the class ConsumerResource method unbindByPool.

@ApiOperation(notes = "Removes all Entitlements from a Consumer. By Pool Id", value = "unbindByPool")
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.WILDCARD)
@Path("/{consumer_uuid}/entitlements/pool/{pool_id}")
public void unbindByPool(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @PathParam("pool_id") String poolId) {
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    List<Entitlement> entitlementsToDelete = entitlementCurator.listByConsumerAndPoolId(consumer, poolId);
    if (!entitlementsToDelete.isEmpty()) {
        for (Entitlement toDelete : entitlementsToDelete) {
            poolManager.revokeEntitlement(toDelete);
        }
    } else {
        throw new NotFoundException(i18n.tr("No entitlements for consumer \"{0}\" with pool id \"{1}\"", consumerUuid, poolId));
    }
}
Also used : DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Entitlement(org.candlepin.model.Entitlement) 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)

Aggregations

NotFoundException (org.candlepin.common.exceptions.NotFoundException)44 ApiOperation (io.swagger.annotations.ApiOperation)25 Produces (javax.ws.rs.Produces)25 ApiResponses (io.swagger.annotations.ApiResponses)24 Path (javax.ws.rs.Path)22 BadRequestException (org.candlepin.common.exceptions.BadRequestException)16 Consumer (org.candlepin.model.Consumer)12 Owner (org.candlepin.model.Owner)12 DELETE (javax.ws.rs.DELETE)11 GET (javax.ws.rs.GET)10 Entitlement (org.candlepin.model.Entitlement)10 Pool (org.candlepin.model.Pool)9 ArrayList (java.util.ArrayList)7 Transactional (com.google.inject.persist.Transactional)6 Consumes (javax.ws.rs.Consumes)6 ConsumerType (org.candlepin.model.ConsumerType)6 PUT (javax.ws.rs.PUT)5 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)5 Environment (org.candlepin.model.Environment)5 Test (org.junit.Test)5