Search in sources :

Example 1 with NotFoundException

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

the class ConsumerImporter method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO.
 * This method does not set the upstreamConsumer 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
 */
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.getId() != null) {
        entity.setId(dto.getId());
    }
    if (dto.getDisplayName() != null) {
        entity.setDisplayName(dto.getDisplayName());
    }
    if (dto.getKey() != null) {
        entity.setKey(dto.getKey());
    }
    if (dto.getLastRefreshed() != null) {
        entity.setLastRefreshed(dto.getLastRefreshed());
    }
    if (dto.getContentAccessMode() != null) {
        entity.setContentAccessMode(dto.getContentAccessMode());
    }
    if (dto.getContentAccessModeList() != null) {
        entity.setContentAccessModeList(dto.getContentAccessModeList());
    }
    if (dto.getCreated() != null) {
        entity.setCreated(dto.getCreated());
    }
    if (dto.getUpdated() != null) {
        entity.setUpdated(dto.getUpdated());
    }
    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.curator.find(pdto.getId());
        } else if (pdto.getKey() != null) {
            // look up by key
            parent = this.curator.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) {
        entity.setDefaultServiceLevel(dto.getDefaultServiceLevel());
    }
    if (dto.getLogLevel() != null) {
        entity.setLogLevel(dto.getLogLevel());
    }
    if (dto.isAutobindDisabled() != null) {
        entity.setAutobindDisabled(dto.isAutobindDisabled());
    }
}
Also used : Owner(org.candlepin.model.Owner) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) NotFoundException(org.candlepin.common.exceptions.NotFoundException)

Example 2 with NotFoundException

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

the class ResolverUtil method validateProductData.

public void validateProductData(ProductData dto, Owner owner, boolean allowNull) {
    if (dto != null) {
        if (dto.getUuid() != null) {
            // UUID is set. Verify that product exists and matches the ID provided, if any
            Product product = this.productCurator.find(dto.getUuid());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the UUID \"{0}\"", dto.getUuid()));
            }
            dto.setId(product.getId());
        } else if (dto.getId() != null) {
            Product product = this.ownerProductCurator.getProductById(owner, dto.getId());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the ID \"{0}\" for owner \"{1}\"", dto.getId(), owner.getKey()));
            }
        } else {
            throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
        }
    } else if (!allowNull) {
        throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
    }
}
Also used : ProvidedProduct(org.candlepin.model.ProvidedProduct) Product(org.candlepin.model.Product) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 3 with NotFoundException

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

the class VerifyAuthorizationFilter method getAccessedEntities.

@SuppressWarnings("unchecked")
protected List<Persisted> getAccessedEntities(Verify verify, Object requestValue) {
    // Nothing to access!
    if (verify.nullable() && null == requestValue) {
        return Collections.emptyList();
    }
    List<Persisted> entities = new ArrayList<>();
    Class<? extends Persisted> verifyType = verify.value();
    if (requestValue instanceof String) {
        String verifyParam = (String) requestValue;
        Persisted entity = null;
        entity = storeFactory.getFor(verifyType).lookup(verifyParam);
        // if it is not found.
        if (entity == null) {
            // This is bad, we're verifying a parameter with an ID which
            // doesn't seem to exist in the DB. Error will be thrown in
            // invoke though.
            String typeName = Util.getClassName(verifyType);
            if (typeName.equals("Owner")) {
                typeName = i18nProvider.get().tr("Organization");
            }
            String msg = i18nProvider.get().tr("{0} with id {1} could not be found.", typeName, verifyParam);
            log.info("No such entity: {}, id: {}", typeName, verifyParam);
            throw new NotFoundException(msg);
        }
        entities.add(entity);
    } else if (requestValue instanceof Collection) {
        Collection<String> verifyParams = (Collection<String>) requestValue;
        // up to the requester to determine if something is missing or not.
        if (verifyParams != null && !verifyParams.isEmpty()) {
            entities.addAll(storeFactory.getFor(verifyType).lookup(verifyParams));
        }
    }
    return entities;
}
Also used : ArrayList(java.util.ArrayList) Persisted(org.candlepin.model.Persisted) NotFoundException(org.candlepin.common.exceptions.NotFoundException) Collection(java.util.Collection)

Example 4 with NotFoundException

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

the class ManifestManager method writeStoredExportToResponse.

/**
 * Write the stored manifest file to the specified response output stream and update
 * the appropriate response data.
 *
 * @param exportId the id of the manifest file to find.
 * @param exportedConsumerUuid the UUID of the consumer the export was generated for.
 * @param response the response to write the file to.
 * @throws ManifestFileServiceException if there was an issue getting the file from the service
 * @throws NotFoundException if the manifest file is not found
 * @throws BadRequestException if the manifests target consumer does not match the specified
 *                             consumer.
 * @throws IseException if there was an issue writing the file to the response.
 */
@Transactional
public void writeStoredExportToResponse(String exportId, String exportedConsumerUuid, HttpServletResponse response) throws ManifestFileServiceException, NotFoundException, BadRequestException, IseException {
    Consumer exportedConsumer = consumerCurator.verifyAndLookupConsumer(exportedConsumerUuid);
    // In order to stream the results from the DB to the client
    // we write the file contents directly to the response output stream.
    // 
    // NOTE: Passing the database input stream to the response builder seems
    // like it would be a correct approach here, but large object streaming
    // can only be done inside a single transaction, so we have to stream it
    // manually.
    ManifestFile manifest = manifestFileService.get(exportId);
    if (manifest == null) {
        throw new NotFoundException(i18n.tr("Unable to find specified manifest by id: {0}", exportId));
    }
    // The specified consumer must match that of the manifest.
    if (!exportedConsumer.getUuid().equals(manifest.getTargetId())) {
        throw new BadRequestException(i18n.tr("Could not validate export against specifed consumer: {0}", exportedConsumer.getUuid()));
    }
    BufferedOutputStream output = null;
    InputStream input = null;
    try {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=" + manifest.getName());
        // NOTE: Input and output streams are expected to be closed by their creators.
        input = manifest.getInputStream();
        output = new BufferedOutputStream(response.getOutputStream());
        int data = input.read();
        while (data != -1) {
            output.write(data);
            data = input.read();
        }
        output.flush();
    } catch (Exception e) {
        // Reset the response data so that a json response can be returned,
        // by RestEasy.
        response.setContentType("text/json");
        response.setHeader("Content-Disposition", "");
        throw new IseException(i18n.tr("Unable to download manifest: {0}", exportId), e);
    }
}
Also used : Consumer(org.candlepin.model.Consumer) IseException(org.candlepin.common.exceptions.IseException) InputStream(java.io.InputStream) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) BufferedOutputStream(java.io.BufferedOutputStream) ManifestFile(org.candlepin.sync.file.ManifestFile) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ImporterException(org.candlepin.sync.ImporterException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) ExportCreationException(org.candlepin.sync.ExportCreationException) IOException(java.io.IOException) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) IseException(org.candlepin.common.exceptions.IseException) Transactional(com.google.inject.persist.Transactional)

Example 5 with NotFoundException

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

the class UeberCertificateGenerator method generate.

@Transactional
public UeberCertificate generate(String ownerKey, Principal principal) {
    Owner owner = this.ownerCurator.lookupByKey(ownerKey);
    if (owner == null) {
        throw new NotFoundException(i18n.tr("Unable to find an owner with key: {0}", ownerKey));
    }
    this.ownerCurator.lock(owner);
    try {
        // There can only be one ueber certificate per owner, so delete the existing and regenerate it.
        this.ueberCertCurator.deleteForOwner(owner);
        return this.generateUeberCert(owner, principal.getUsername());
    } catch (Exception e) {
        log.error("Problem generating ueber cert for owner: {}", ownerKey, e);
        throw new BadRequestException(i18n.tr("Problem generating ueber cert for owner {0}", ownerKey), e);
    }
}
Also used : NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) GeneralSecurityException(java.security.GeneralSecurityException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) IOException(java.io.IOException) Transactional(com.google.inject.persist.Transactional)

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