Example usage for org.springframework.http HttpStatus PRECONDITION_FAILED

List of usage examples for org.springframework.http HttpStatus PRECONDITION_FAILED

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus PRECONDITION_FAILED.

Prototype

HttpStatus PRECONDITION_FAILED

To view the source code for org.springframework.http HttpStatus PRECONDITION_FAILED.

Click Source Link

Document

412 Precondition failed .

Usage

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractTenantController.java

/**
 * Add credit method.// w  ww . j  a v a 2  s  .c o m
 * 
 * @param tenantParam the tenant param.
 * @param credit the Credit Amount
 * @param comment
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @return
 */
@RequestMapping(value = "/{tenantParam}/add_credit", method = RequestMethod.POST)
@ResponseBody
public String issueCredit(@PathVariable String tenantParam,
        @RequestParam(value = "credit", required = true) String credit,
        @RequestParam(value = "comment", required = true) String comment, HttpServletRequest request,
        HttpServletResponse response) {
    logger.debug("###Entering in issueCredit(tenantId,credit,comment,response) method @POST");
    try {
        Tenant tenant = tenantService.get(tenantParam);
        JSONObject result = new JSONObject();
        BigDecimal creditDecimal = billingPostProcessor.setScaleByCurrency(new BigDecimal(credit),
                tenant.getCurrency());
        if (creditDecimal.compareTo(new BigDecimal(0)) < 0) {// negative credit
            response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
        } else {

            SalesLedgerRecord creditSLR = billingAdminService.addPaymentOrCredit(tenant, creditDecimal,
                    Type.SERVICE_CREDIT, comment, null);

            AccountStatement accountStatement = creditSLR.getAccountStatement();
            response.setStatus(HttpStatus.OK.value());
            String message = messageSource.getMessage("tenant.credit.confirmation", null,
                    getSessionLocale(request));
            result.put("creditBalance", accountStatement.getFinalCharges().negate()) // toConfirm
                    .put("message", StringEscapeUtils.escapeHtml(message))
                    .put("creditamount", accountStatement.getCredits());
        }
        logger.debug(result.toString());
        logger.debug("###Exiting issueCredit(tenantId,credit,comment,response) method @POST");
        return result.toString();
    } catch (Exception ex) {
        response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
        logger.error(ex);
        return null;
    }
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractTenantController.java

/**
 * Cancel credit method.//from w  ww .  jav a  2s.  c om
 * 
 * @param tenantParam
 * @param slrUuid
 * @param comment
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 * @return
 */
@RequestMapping(value = "/{tenantParam}/cancel_credit", method = RequestMethod.POST)
@ResponseBody
public String cancelCredit(@PathVariable String tenantParam,
        @RequestParam(value = "slrUuid", required = true) String slrUuid,
        @RequestParam(value = "comment", required = true) String comment, HttpServletRequest request,
        HttpServletResponse response) {
    logger.debug("###Entering in cancelCredit(tenantId,credit,comment,response) method @POST");
    try {
        Tenant tenant = tenantService.get(tenantParam);
        JSONObject result = new JSONObject();
        SalesLedgerRecord slr = billingAdminService.getSalesLedgerRecord(slrUuid);

        // Check if it is already cancelled, if cancelled then return error
        if (slr != null && slr.getCancellationReferenceId() != null) {
            throw new BillingAdminServiceException(" This payment has already been cancelled by slr:"
                    + slr.getCancellationReferenceId().getParam());
        }

        if (slr != null && !slr.getType().equals(SalesLedgerRecord.Type.INVOICE)) {
            SalesLedgerRecord slr1 = billingAdminService.cancelCredit(tenant, slr, comment);
            if (slr1 == null) {// null is returned if any precondition fails
                response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
                return "failure";
            } else {
                response.setStatus(HttpStatus.OK.value());
                String message = messageSource.getMessage("tenant.credit.confirmation", null,
                        getSessionLocale(request));
                result.put("creditBalance", slr1.getAccountStatement().getFinalCharges().negate()) // toConfirm
                        .put("message", StringEscapeUtils.escapeHtml(message));
            }
        } else {
            response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
            return "failure";
        }
        logger.debug(result.toString());
        logger.debug("###Exiting cancelCredit(tenantId,credit,comment,response) method @POST");
        return "success";
    } catch (Exception ex) {
        response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
        logger.error(ex);
        return "failure";
    }
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractBillingController.java

@RequestMapping(value = "/recordpayment", method = RequestMethod.POST)
@ResponseBody//from  w  w  w.j ava 2  s .c  om
public String recordPayment(@RequestParam BigDecimal amount, @RequestParam String memo,
        @RequestParam String tenantParam, HttpServletResponse response, ModelMap map) {
    logger.debug("###Entering in recordPaymentInvoice(tenantId,form, amount,response) method @POST");
    Tenant tenant = tenantService.get(tenantParam);
    try {
        if (amount.compareTo(BigDecimal.ZERO) <= 0) {
            return "failed";
        }
        BigDecimal roundedAmount = billingPostProcessor.setScaleByCurrency(amount, tenant.getCurrency());
        billingService.addPaymentOrCredit(tenant, roundedAmount, Type.RECORD, memo, null);

        String message = "payment.received";
        String messageArgs = tenant.getName();
        eventService.createEvent(new Date(), tenant, message, messageArgs, Source.PORTAL, Scope.ACCOUNT,
                Category.ACCOUNT, Severity.INFORMATION, true);
        logger.debug("###Exiting recordPaymentInvoice(tenantId,amount,form ,response) method @POST");
    } catch (BillingServiceException e) {
        response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
        return "failed";
    }
    map.addAttribute("tenant", tenant);
    return "success";
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractTenantController.java

/**
 * Exception Handler for {@link TenantStateChangeFailedException}
 * /*  w w w .j av  a2 s  .  c  om*/
 * @param exception
 * @param request {@link HttpServletRequest}
 * @return
 */
@ExceptionHandler(TenantStateChangeFailedException.class)
@ResponseStatus(value = HttpStatus.PRECONDITION_FAILED)
public ModelAndView handleTenantStateChangeFailedException(TenantStateChangeFailedException ex,
        HttpServletRequest request) {
    logger.error("handleTenantStateChangeFailedException####" + ex.getMessage(), ex);
    ModelAndView viewData = new ModelAndView();
    viewData.setViewName("errors.messagepage");
    viewData.addObject("errorMessage", ex.getMessage());
    return viewData;
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test to make sure command args are limitted to 10,000 characters.
 *
 * @throws Exception On error//from   w w  w . ja  v a  2  s. c  o  m
 */
@Test
public void testForTooManyCommandArgs() throws Exception {
    final JobRequest tooManyCommandArguments = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION,
            Lists.newArrayList(new ClusterCriteria(Sets.newHashSet(LOCALHOST_CLUSTER_TAG))),
            Sets.newHashSet(BASH_COMMAND_TAG)).withCommandArgs(StringUtils.leftPad("bad", 10_001)).build();

    RestAssured.given(this.getRequestSpecification()).contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(tooManyCommandArguments)).when()
            .port(this.port).post(JOBS_API).then()
            .statusCode(Matchers.is(HttpStatus.PRECONDITION_FAILED.value()));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test the job submit method for incorrect cluster resolved.
 *
 * @throws Exception If there is a problem.
 */// w ww.  j av  a2  s . c o  m
@Test
public void testSubmitJobMethodMissingCluster() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final List<String> commandArgs = Lists.newArrayList("-c", "'echo hello world'");

    final List<ClusterCriteria> clusterCriteriaList = new ArrayList<>();
    final Set<String> clusterTags = Sets.newHashSet("undefined");
    final ClusterCriteria clusterCriteria = new ClusterCriteria(clusterTags);
    clusterCriteriaList.add(clusterCriteria);

    final String jobId = UUID.randomUUID().toString();

    final Set<String> commandCriteria = Sets.newHashSet(BASH_COMMAND_TAG);
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, clusterCriteriaList,
            commandCriteria).withId(jobId).withCommandArgs(commandArgs).withDisableLogArchival(true).build();

    RestAssured.given(this.getRequestSpecification()).contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(jobRequest)).when().port(this.port)
            .post(JOBS_API).then().statusCode(Matchers.is(HttpStatus.PRECONDITION_FAILED.value()));

    Assert.assertThat(this.getStatus(jobId), Matchers.is("{\"status\":\"FAILED\"}"));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test the job submit method for incorrect cluster criteria.
 *
 * @throws Exception If there is a problem.
 *//*  w  w  w .ja v  a  2  s .c o m*/
@Test
public void testSubmitJobMethodInvalidClusterCriteria() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final List<String> commandArgs = Lists.newArrayList("-c", "'echo hello world'");

    final List<ClusterCriteria> clusterCriteriaList = Lists
            .newArrayList(new ClusterCriteria(Sets.newHashSet(" ", "", null)));

    final String jobId = UUID.randomUUID().toString();

    final Set<String> commandCriteria = Sets.newHashSet("bash");
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, clusterCriteriaList,
            commandCriteria).withId(jobId).withCommandArgs(commandArgs).withDisableLogArchival(true).build();

    RestAssured.given(this.getRequestSpecification()).contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(jobRequest)).when().port(this.port)
            .post(JOBS_API).then().statusCode(Matchers.is(HttpStatus.PRECONDITION_FAILED.value()));

    RestAssured.given(this.getRequestSpecification()).when().port(this.port)
            .get(JOBS_API + "/{id}/status", jobId).then().statusCode(Matchers.is(HttpStatus.NOT_FOUND.value()));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test the job submit method for incorrect cluster criteria.
 *
 * @throws Exception If there is a problem.
 *//*from w ww  .  j  a v a 2 s.c  om*/
@Test
public void testSubmitJobMethodInvalidCommandCriteria() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final List<String> commandArgs = Lists.newArrayList("-c", "'echo hello world'");

    final List<ClusterCriteria> clusterCriteriaList = Lists
            .newArrayList(new ClusterCriteria(Sets.newHashSet("ok")));

    final String jobId = UUID.randomUUID().toString();

    final Set<String> commandCriteria = Sets.newHashSet(" ", "", null);
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, clusterCriteriaList,
            commandCriteria).withId(jobId).withCommandArgs(commandArgs).withDisableLogArchival(true).build();

    RestAssured.given(this.getRequestSpecification()).contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(jobRequest)).when().port(this.port)
            .post(JOBS_API).then().statusCode(Matchers.is(HttpStatus.PRECONDITION_FAILED.value()));

    RestAssured.given(this.getRequestSpecification()).when().port(this.port)
            .get(JOBS_API + "/{id}/status", jobId).then().statusCode(Matchers.is(HttpStatus.NOT_FOUND.value()));
}

From source file:com.netflix.genie.web.controllers.JobRestControllerIntegrationTest.java

/**
 * Test the job submit method for incorrect command resolved.
 *
 * @throws Exception If there is a problem.
 *//*from ww  w .ja v a2s . c o  m*/
@Test
public void testSubmitJobMethodMissingCommand() throws Exception {
    Assume.assumeTrue(SystemUtils.IS_OS_UNIX);
    final List<String> commandArgs = Lists.newArrayList("-c", "'echo hello world'");

    final List<ClusterCriteria> clusterCriteriaList = new ArrayList<>();
    final Set<String> clusterTags = Sets.newHashSet(LOCALHOST_CLUSTER_TAG);
    final ClusterCriteria clusterCriteria = new ClusterCriteria(clusterTags);
    clusterCriteriaList.add(clusterCriteria);

    final String jobId = UUID.randomUUID().toString();

    final Set<String> commandCriteria = Sets.newHashSet("undefined");
    final JobRequest jobRequest = new JobRequest.Builder(JOB_NAME, JOB_USER, JOB_VERSION, clusterCriteriaList,
            commandCriteria).withId(jobId).withCommandArgs(commandArgs).withDisableLogArchival(true).build();

    RestAssured.given(this.getRequestSpecification()).contentType(MediaType.APPLICATION_JSON_VALUE)
            .body(GenieObjectMapper.getMapper().writeValueAsBytes(jobRequest)).when().port(this.port)
            .post(JOBS_API).then().statusCode(Matchers.is(HttpStatus.PRECONDITION_FAILED.value()));

    Assert.assertThat(this.getStatus(jobId), Matchers.is("{\"status\":\"FAILED\"}"));
}