Search in sources :

Example 1 with ConflictException

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

the class OwnerResource method deleteOwner.

/**
 * Removes an Owner
 *
 * @httpcode 404
 * @httpcode 200
 */
@DELETE
@Path("/{owner_key}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(notes = "Removes an Owner", value = "Delete Owner")
@ApiResponses({ @ApiResponse(code = 404, message = "Owner not found") })
public void deleteOwner(@PathParam("owner_key") String ownerKey, @QueryParam("revoke") @DefaultValue("true") boolean revoke, @QueryParam("force") @DefaultValue("false") boolean force) {
    Owner owner = findOwnerByKey(ownerKey);
    Event event = eventFactory.ownerDeleted(owner);
    if (!force && consumerCurator.doesShareConsumerExist(owner)) {
        throw new BadRequestException(i18n.tr("owner \"{0}\" cannot be deleted while there is a share consumer. " + "You may use \"force\" to bypass.", owner.getKey()));
    }
    try {
        ownerManager.cleanupAndDelete(owner, revoke);
    } catch (PersistenceException e) {
        if (e.getCause() instanceof ConstraintViolationException) {
            throw new ConflictException(e.getMessage(), e);
        } else {
            throw e;
        }
    }
    sink.queueEvent(event);
}
Also used : Owner(org.candlepin.model.Owner) ConflictException(org.candlepin.common.exceptions.ConflictException) PersistenceException(javax.persistence.PersistenceException) Event(org.candlepin.audit.Event) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) 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)

Example 2 with ConflictException

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

the class RuntimeExceptionMapperTest method candlepinExceptionWithChildNotCandleping.

@Test
public void candlepinExceptionWithChildNotCandleping() {
    CandlepinException ce = mock(CandlepinException.class);
    when(ce.httpReturnCode()).thenReturn(Status.CONFLICT);
    when(ce.message()).thenReturn(new ExceptionMessage().setDisplayMessage("you screwed up"));
    when(req.getHeader(HttpHeaderNames.ACCEPT)).thenReturn("application/json");
    when(ce.getCause()).thenReturn(new ConflictException("you screwed up"));
    Response r = rem.toResponse(ce);
    assertNotNull(r);
    assertEquals(Status.CONFLICT.getStatusCode(), r.getStatus());
    verifyMessage(r, "you screwed up");
}
Also used : Response(javax.ws.rs.core.Response) CandlepinException(org.candlepin.common.exceptions.CandlepinException) ExceptionMessage(org.candlepin.common.exceptions.ExceptionMessage) ConflictException(org.candlepin.common.exceptions.ConflictException) Test(org.junit.Test)

Example 3 with ConflictException

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

the class EnvironmentResource method promoteContent.

@ApiOperation(notes = "Promotes a Content into an Environment. This call accepts multiple " + "content sets to promote at once, after which all affected certificates for consumers" + " in the environment will be regenerated. Consumers registered to this environment " + "will now receive this content in their entitlement certificates. Because the" + " certificate regeneraiton can be quite time consuming, this is done as an " + "asynchronous job. The content will be promoted and immediately available for new " + "entitlements, but existing entitlements could take some time to be regenerated and " + "sent down to clients as they check in.", value = "promoteContent")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{env_id}/content")
public JobDetail promoteContent(@PathParam("env_id") @Verify(Environment.class) String envId, @ApiParam(name = "contentToPromote", required = true) List<org.candlepin.model.dto.EnvironmentContent> contentToPromote, @QueryParam("lazy_regen") @DefaultValue("true") Boolean lazyRegen) {
    Environment env = lookupEnvironment(envId);
    // there be a problem with the request.
    for (org.candlepin.model.dto.EnvironmentContent promoteMe : contentToPromote) {
        log.debug("EnvironmentContent to promote: {}:{}", promoteMe.getEnvironmentId(), promoteMe.getContentId());
        EnvironmentContent existing = this.envContentCurator.lookupByEnvironmentAndContent(env, promoteMe.getContentId());
        if (existing != null) {
            throw new ConflictException(i18n.tr("The content with id {0} has already been promoted in this environment.", promoteMe.getContentId()));
        }
    }
    Set<String> contentIds = new HashSet<>();
    try {
        contentIds = batchCreate(contentToPromote, env);
        clearContentAccessCerts(env);
    } catch (PersistenceException pe) {
        if (rdbmsExceptionTranslator.isConstraintViolationDuplicateEntry(pe)) {
            log.info("Concurrent content promotion will cause this request to fail.", pe);
            throw new ConflictException(i18n.tr("Some of the content is already associated with Environment: {0}", contentToPromote));
        } else {
            throw pe;
        }
    }
    JobDataMap map = new JobDataMap();
    map.put(RegenEnvEntitlementCertsJob.ENV, env);
    map.put(RegenEnvEntitlementCertsJob.CONTENT, contentIds);
    map.put(RegenEnvEntitlementCertsJob.LAZY_REGEN, lazyRegen);
    JobDetail detail = newJob(RegenEnvEntitlementCertsJob.class).withIdentity("regen_entitlement_cert_of_env" + Util.generateUUID()).usingJobData(map).build();
    return detail;
}
Also used : JobDataMap(org.quartz.JobDataMap) ConflictException(org.candlepin.common.exceptions.ConflictException) EnvironmentContent(org.candlepin.model.EnvironmentContent) JobDetail(org.quartz.JobDetail) RegenEnvEntitlementCertsJob(org.candlepin.pinsetter.tasks.RegenEnvEntitlementCertsJob) PersistenceException(javax.persistence.PersistenceException) Environment(org.candlepin.model.Environment) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

ConflictException (org.candlepin.common.exceptions.ConflictException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 PersistenceException (javax.persistence.PersistenceException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 HashSet (java.util.HashSet)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1 Response (javax.ws.rs.core.Response)1 Event (org.candlepin.audit.Event)1 BadRequestException (org.candlepin.common.exceptions.BadRequestException)1 CandlepinException (org.candlepin.common.exceptions.CandlepinException)1 ExceptionMessage (org.candlepin.common.exceptions.ExceptionMessage)1 Environment (org.candlepin.model.Environment)1 EnvironmentContent (org.candlepin.model.EnvironmentContent)1 Owner (org.candlepin.model.Owner)1 RegenEnvEntitlementCertsJob (org.candlepin.pinsetter.tasks.RegenEnvEntitlementCertsJob)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1