Example usage for org.springframework.http HttpStatus UNPROCESSABLE_ENTITY

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

Introduction

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

Prototype

HttpStatus UNPROCESSABLE_ENTITY

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

Click Source Link

Document

422 Unprocessable Entity .

Usage

From source file:com.ge.predix.acs.service.policy.admin.PolicyManagementController.java

/**
 * @param name//from ww w.j av  a 2s.  c  om
 * @param policySetId
 */
private void validatePolicyIdOrFail(final PolicySet policySet, final String policySetId) {
    if (policySet == null) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY, "Policy Set cannot be empty or null");
    }

    String name = policySet.getName();
    if (!StringUtils.isEmpty(name) && !policySetId.equals(name)) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY,
                String.format(
                        "Policy Set name in the payload = %s, does not match the one provided in URI = %s",
                        name, policySetId));
    }
}

From source file:org.cloudfoundry.identity.uaa.login.AccountsController.java

private String handleUnprocessableEntity(Model model, HttpServletResponse response, String errorMessage) {
    model.addAttribute("error_message_code", errorMessage);
    response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
    return "accounts/new_activation_email";
}

From source file:com.ge.predix.acs.zone.management.ZoneController.java

/**
 * @param zone/* w w w  .  ja  v a2  s .  c  o  m*/
 */
private void validateAndSanitizeInputOrFail(final Zone zone, final String zoneName) {
    if (zone.getName() != null && !zoneName.equals(zone.getName())) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY,
                "Zone name in URI does not match the Zone Name in Payload");
    }
    if (zone.getName() == null) {
        zone.setName(zoneName);
    }
}

From source file:ch.heigvd.gamification.api.ApplicationsEndpoint.java

@Override
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> applicationsPost(
        @ApiParam(value = "Application to add", required = true) @RequestBody Registration body) {

    if (body != null) {

        AuthenKey apiKey = new AuthenKey();
        String password = "";

        if (apprepository.findByName(body.getApplicationName()) != null) {

            System.out.println(body.getApplicationName());
            return new ResponseEntity("name already use", HttpStatus.UNPROCESSABLE_ENTITY);
        }//from www  .j  a  v a2  s .c  om

        Application app = new Application(body.getApplicationName(), body.getPassword());

        apiKey.setApp(app);
        try {
            apprepository.save(app);
        } catch (javax.persistence.PersistenceException e) {
            return new ResponseEntity("name already use", HttpStatus.UNPROCESSABLE_ENTITY);
        }
        authenRepository.save(apiKey);
        return new ResponseEntity(HttpStatus.CREATED);

    } else {
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
}

From source file:ch.heigvd.gamification.api.PointScalesEndpoint.java

@RequestMapping(method = RequestMethod.POST)
@Override//from  w w  w. jav  a  2s .  c  o  m

public ResponseEntity<Void> pointScalesPost(
        @ApiParam(value = "token that identifies the app sending the request", required = true) @RequestHeader(value = "X-Gamification-Token", required = true) String xGamificationToken,
        @ApiParam(value = "PointScale to add", required = true) @RequestBody PointScaleDTO body) {

    AuthenKey apiKey = authenRepository.findByAppKey(xGamificationToken);
    if (apiKey == null) {
        return new ResponseEntity("apikey not exist", HttpStatus.UNAUTHORIZED);
    }

    PointScale pointScale = new PointScale();
    Application app = apiKey.getApp();

    if (pointscaleRepository.findByNameAndApp(body.getName(), apiKey.getApp()) != null) {

        return new ResponseEntity("poinscat exist already", HttpStatus.UNPROCESSABLE_ENTITY);
    }

    if (body != null) {

        pointScale.setDescription(body.getDescription());
        pointScale.setName(body.getName());
        pointScale.setMinpoint(body.getNbrDePoints());
        pointScale.setApplication(app);
        pointScale.setApplication(app);
        pointscaleRepository.save(pointScale);

        StringBuffer location = request.getRequestURL();
        if (!location.toString().endsWith("/")) {
            location.append("/");
        }
        location.append(pointScale.getId().toString());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Location", location.toString());
        return new ResponseEntity<>(headers, HttpStatus.CREATED);

    } else {
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
}

From source file:com.ge.predix.integration.test.PrivilegeManagementAccessControlServiceIT.java

public void testBatchCreateSubjectsEmptyList() {
    List<BaseSubject> subjects = new ArrayList<BaseSubject>();
    try {/*from  w  ww  .  ja v a 2  s . c o m*/
        this.acsAdminRestTemplate.postForEntity(this.acsUrl + PrivilegeHelper.ACS_SUBJECT_API_PATH,
                new HttpEntity<>(subjects, this.zone1Headers), BaseSubject[].class);
    } catch (HttpClientErrorException e) {
        Assert.assertEquals(e.getStatusCode(), HttpStatus.UNPROCESSABLE_ENTITY);
        return;
    }
    Assert.fail("Expected unprocessable entity http client error.");
}

From source file:com.ge.predix.acs.privilege.management.SubjectPrivilegeManagementController.java

@ApiOperation(value = "Creates/Updates a given Subject.", tags = { "Attribute Management" })
@RequestMapping(method = PUT, value = V1 + SUBJECT_URL, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<BaseSubject> putSubject(@RequestBody final BaseSubject subject,
        @PathVariable("subjectIdentifier") final String subjectIdentifier) {
    try {/*from   w w w. j a  v  a  2s  .c om*/
        this.failIfParentsSpecified(Collections.singletonList(subject));

        if (StringUtils.isEmpty(subject.getSubjectIdentifier())) {
            subject.setSubjectIdentifier(subjectIdentifier);
        }

        validSubjectIdentifierOrFail(subject, subjectIdentifier);

        boolean createdSubject = this.service.upsertSubject(subject);

        URI subjectUri = UriTemplateUtils.expand(SUBJECT_URL, "subjectIdentifier:" + subjectIdentifier);

        if (createdSubject) {
            return created(subjectUri.getRawPath(), false);
        }

        return created(subjectUri.getRawPath(), true);
    } catch (RestApiException e) {
        // NOTE: This block is necessary to avoid accidentally
        // converting the HTTP status code to an unintended one
        throw e;
    } catch (Exception e) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY, e);
    }
}

From source file:com.ge.predix.acs.privilege.management.ResourcePrivilegeManagementController.java

@ApiOperation(value = "Creates/Updates a given resource for a given zone. "
        + "The resourceIdentifier must be URL encoded in application/x-www-form-urlencoded format with "
        + "UTF-8.", tags = { "Attribute Management" })
@RequestMapping(method = PUT, value = {//from  www  .  jav  a 2s.c  o  m
        V1 + MANAGED_RESOURCE_URL }, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<BaseResource> putResourceV1(@RequestBody final BaseResource resource,
        @PathVariable("resourceIdentifier") final String resourceIdentifier) {
    try {
        this.failIfParentsSpecified(Collections.singletonList(resource));

        // resource identifier is optional, setting it to URI resource id if
        // missing from payload
        if (StringUtils.isEmpty(resource.getResourceIdentifier())) {
            resource.setResourceIdentifier(resourceIdentifier);
        }

        validResourceIdentifierOrFail(resource, resourceIdentifier);

        return doPutResource(resource, resourceIdentifier);
    } catch (RestApiException e) {
        // NOTE: This block is necessary to avoid accidentally
        // converting the HTTP status code to an unintended one
        throw e;
    } catch (Exception e) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY, e);
    }

}

From source file:com.ge.predix.integration.test.PrivilegeManagementAccessControlServiceIT.java

@Test
public void testBatchSubjectsDataConstraintViolationSubjectIdentifier() {
    List<BaseSubject> subjects = new ArrayList<BaseSubject>();
    subjects.add(this.privilegeHelper.createSubject("marissa"));
    subjects.add(this.privilegeHelper.createSubject("marissa"));

    try {/*w  w w .j  a v a  2s.c  o m*/
        this.acsAdminRestTemplate.postForEntity(this.acsUrl + PrivilegeHelper.ACS_SUBJECT_API_PATH,
                new HttpEntity<>(subjects, this.zone1Headers), ResponseEntity.class);
    } catch (HttpClientErrorException e) {
        Assert.assertEquals(e.getStatusCode(), HttpStatus.UNPROCESSABLE_ENTITY);
        return;
    }
    this.acsAdminRestTemplate.exchange(this.acsUrl + PrivilegeHelper.ACS_SUBJECT_API_PATH + "/marissa",
            HttpMethod.DELETE, new HttpEntity<>(this.zone1Headers), ResponseEntity.class);
    Assert.fail("Expected unprocessable entity http client error.");
}

From source file:com.ge.predix.acs.privilege.management.SubjectPrivilegeManagementController.java

private void validSubjectIdentifierOrFail(final BaseSubject subject, final String subjectIdentifier) {
    if (!subjectIdentifier.equals(subject.getSubjectIdentifier())) {
        throw new RestApiException(HttpStatus.UNPROCESSABLE_ENTITY,
                String.format("Subject identifier = %s, does not match the one provided in URI = %s",
                        subject.getSubjectIdentifier(), subjectIdentifier));
    }//w ww  .  j a  va  2 s.co  m
}