Search in sources :

Example 41 with BadRequestException

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

the class CdnResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO.
 * This method will not set the ID field.
 *
 * @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
 */
private void populateEntity(Cdn entity, CdnDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the Cdn model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the Cdn dto is null");
    }
    if (dto.getName() != null) {
        entity.setName(dto.getName());
    }
    if (dto.getUrl() != null) {
        entity.setUrl(dto.getUrl());
    }
    if (dto.getCertificate() != null) {
        CertificateDTO certDTO = dto.getCertificate();
        CdnCertificate cdnCert;
        if (certDTO.getKey() != null && certDTO.getCert() != null) {
            cdnCert = new CdnCertificate();
            cdnCert.setCert(certDTO.getCert());
            cdnCert.setKey(certDTO.getKey());
            if (certDTO.getSerial() != null) {
                CertificateSerialDTO certSerialDTO = certDTO.getSerial();
                CertificateSerial certSerial = new CertificateSerial();
                certSerial.setExpiration(certSerialDTO.getExpiration());
                if (certSerialDTO.getSerial() != null) {
                    certSerial.setSerial(certSerialDTO.getSerial().longValue());
                }
                if (certSerialDTO.isCollected() != null) {
                    certSerial.setCollected(certSerialDTO.isCollected());
                }
                if (certSerialDTO.isRevoked() != null) {
                    certSerial.setRevoked(certSerialDTO.isRevoked());
                }
                cdnCert.setSerial(certSerial);
            }
            entity.setCertificate(cdnCert);
        } else {
            throw new BadRequestException(i18n.tr("cdn certificate has null key or cert."));
        }
    }
}
Also used : CertificateDTO(org.candlepin.dto.api.v1.CertificateDTO) CdnCertificate(org.candlepin.model.CdnCertificate) CertificateSerialDTO(org.candlepin.dto.api.v1.CertificateSerialDTO) CertificateSerial(org.candlepin.model.CertificateSerial) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 42 with BadRequestException

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

the class CdnResource method create.

@ApiOperation(notes = "Creates a CDN @return a Cdn object", value = "create")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public CdnDTO create(@ApiParam(name = "cdn", required = true) CdnDTO cdnDTOInput, @Context Principal principal) {
    Cdn existing = curator.lookupByLabel(cdnDTOInput.getLabel());
    if (existing != null) {
        throw new BadRequestException(i18n.tr("A CDN with the label {0} already exists", cdnDTOInput.getLabel()));
    }
    Cdn cndToCreate = new Cdn();
    this.populateEntity(cndToCreate, cdnDTOInput);
    if (cdnDTOInput.getLabel() != null) {
        cndToCreate.setLabel(cdnDTOInput.getLabel());
    }
    return this.translator.translate(cdnManager.createCdn(cndToCreate), CdnDTO.class);
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) Cdn(org.candlepin.model.Cdn) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 43 with BadRequestException

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

the class Entitler method bindByPoolQuantities.

public List<Entitlement> bindByPoolQuantities(Consumer consumer, Map<String, Integer> poolIdAndQuantities) throws EntitlementRefusedException {
    try {
        List<Entitlement> entitlementList = poolManager.entitleByPools(consumer, poolIdAndQuantities);
        log.debug("Created {} entitlements.", entitlementList.size());
        return entitlementList;
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) Entitlement(org.candlepin.model.Entitlement)

Example 44 with BadRequestException

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

the class ConsumerResourceTest method testNoDryBindWhenAutobindDisabledForOwner.

@Test
public void testNoDryBindWhenAutobindDisabledForOwner() throws Exception {
    Owner owner = createOwner();
    owner.setId(TestUtil.randomString());
    Consumer consumer = createConsumer(owner);
    owner.setAutobindDisabled(true);
    when(mockConsumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
    ManifestManager manifestManager = mock(ManifestManager.class);
    when(mockOwnerCurator.findOwnerById(eq(owner.getId()))).thenReturn(owner);
    ConsumerResource consumerResource = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, null, null, null, null, null, i18n, null, null, null, null, null, null, null, mockOwnerCurator, null, null, null, null, null, null, this.config, null, null, null, null, manifestManager, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
    try {
        consumerResource.dryBind(consumer.getUuid(), "some-sla");
        fail("Should have thrown a BadRequestException.");
    } catch (BadRequestException e) {
        assertEquals("Owner has autobind disabled.", e.getMessage());
    }
}
Also used : Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ManifestManager(org.candlepin.controller.ManifestManager) Test(org.junit.Test)

Example 45 with BadRequestException

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

the class ServiceLevelValidator method validate.

public void validate(String ownerId, Collection<String> serviceLevels) {
    Set<String> invalidServiceLevels = new HashSet<>();
    for (String serviceLevel : serviceLevels) {
        if (!StringUtils.isBlank(serviceLevel)) {
            boolean found = false;
            for (String level : poolManager.retrieveServiceLevelsForOwner(ownerId, false)) {
                if (serviceLevel.equalsIgnoreCase(level)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                invalidServiceLevels.add(serviceLevel);
            }
        }
    }
    if (!invalidServiceLevels.isEmpty()) {
        Owner owner = ownerCurator.findOwnerById(ownerId);
        String error = i18n.tr("Service level \"{0}\" is not available to units of organization {1}.", StringUtils.join(invalidServiceLevels, ", "), owner.getKey());
        throw new BadRequestException(error);
    }
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HashSet(java.util.HashSet)

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