Search in sources :

Example 11 with BadRequestException

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

the class ContentOverrideValidatorTest method testValidateCollectionBothInvalid.

@Test
public void testValidateCollectionBothInvalid() {
    when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false);
    List<ContentOverride> overrides = new LinkedList<>();
    overrides.add(new ContentOverride("label", "baseurl", "value"));
    overrides.add(new ContentOverride("other label", "name", "other value"));
    try {
        validator.validate(overrides);
        fail("Expected exception \"BadRequestException\" was not thrown.");
    } catch (BadRequestException bre) {
        assertTrue(bre.getMessage().matches("^Not allowed to override values for: (?:baseurl, name|name, baseurl)"));
    }
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) ContentOverride(org.candlepin.model.ContentOverride) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with BadRequestException

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

the class EntitlementCurator method listFilteredPages.

private Page<List<Entitlement>> listFilteredPages(AbstractHibernateObject object, String objectType, String productId, EntitlementFilterBuilder filters, PageRequest pageRequest) {
    Page<List<Entitlement>> entitlementsPage;
    String ownerId = null;
    if (object != null) {
        ownerId = (object instanceof Owner) ? ((Owner) object).getId() : ((Consumer) object).getOwnerId();
    }
    // No need to add filters when matching by product.
    if (object != null && productId != null) {
        Product p = this.ownerProductCurator.getProductById(ownerId, productId);
        if (p == null) {
            throw new BadRequestException(i18n.tr("Product with ID \"{0}\" could not be found.", productId));
        }
        entitlementsPage = listByProduct(object, objectType, productId, pageRequest);
    } else {
        // Build up any provided entitlement filters from query params.
        Criteria criteria = this.createCriteriaFromFilters(filters);
        if (object != null) {
            criteria.add(Restrictions.eq(objectType, object));
        }
        List<String> entitlementIds = criteria.list();
        if (entitlementIds != null && !entitlementIds.isEmpty()) {
            criteria = this.currentSession().createCriteria(Entitlement.class).add(CPRestrictions.in("id", entitlementIds));
            entitlementsPage = listByCriteria(criteria, pageRequest);
        } else {
            entitlementsPage = new Page<>();
            entitlementsPage.setPageData(Collections.<Entitlement>emptyList());
            entitlementsPage.setMaxRecords(0);
        }
    }
    return entitlementsPage;
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 13 with BadRequestException

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

the class PageRequestFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    PageRequest p = null;
    MultivaluedMap<String, String> params = requestContext.getUriInfo().getQueryParameters();
    String page = params.getFirst(PageRequest.PAGE_PARAM);
    String perPage = params.getFirst(PageRequest.PER_PAGE_PARAM);
    String order = params.getFirst(PageRequest.ORDER_PARAM);
    String sortBy = params.getFirst(PageRequest.SORT_BY_PARAM);
    if (page != null || perPage != null || order != null || sortBy != null) {
        p = new PageRequest();
        if (order == null) {
            p.setOrder(PageRequest.DEFAULT_ORDER);
        } else {
            p.setOrder(readOrder(order));
        }
        /* We'll leave it to the curator layer to figure out what to sort by if
             * sortBy is null. */
        p.setSortBy(sortBy);
        try {
            if (page == null && perPage != null) {
                p.setPage(PageRequest.DEFAULT_PAGE);
                p.setPerPage(readInteger(perPage));
            } else if (page != null && perPage == null) {
                p.setPage(readInteger(page));
                p.setPerPage(PageRequest.DEFAULT_PER_PAGE);
            } else {
                p.setPage(readInteger(page));
                p.setPerPage(readInteger(perPage));
            }
        } catch (NumberFormatException nfe) {
            I18n i18n = this.i18nProvider.get();
            throw new BadRequestException(i18n.tr("offset and limit parameters" + " must be positive integers"), nfe);
        }
    }
    ResteasyProviderFactory.pushContext(PageRequest.class, p);
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) BadRequestException(org.candlepin.common.exceptions.BadRequestException) I18n(org.xnap.commons.i18n.I18n)

Example 14 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException 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)

Example 15 with BadRequestException

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

the class HypervisorResourceTest method ensureFailureWhenAutobindIsDisabledOnOwner.

@Test
public void ensureFailureWhenAutobindIsDisabledOnOwner() {
    Owner owner = new Owner("test_admin");
    owner.setAutobindDisabled(true);
    Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
    hostGuestMap.put("HYPERVISOR_A", new ArrayList());
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    try {
        hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
        fail("Exception should have been thrown since autobind was disabled for the owner.");
    } catch (BadRequestException bre) {
        assertEquals("Could not update host/guest mapping. Auto-attach is disabled for owner test_admin.", bre.getMessage());
    }
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BadRequestException(org.candlepin.common.exceptions.BadRequestException) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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