Example usage for org.springframework.http HttpStatus BAD_REQUEST

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

Introduction

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

Prototype

HttpStatus BAD_REQUEST

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

Click Source Link

Document

400 Bad Request .

Usage

From source file:ch.wisv.areafiftylan.exception.GlobalControllerExceptionHandler.java

@ExceptionHandler(InvalidTokenException.class)
public ResponseEntity<?> handleInvalidTokenException(InvalidTokenException ex) {
    return createResponseEntity(HttpStatus.BAD_REQUEST, ex.getMessage());
}

From source file:com.ge.predix.acs.commons.web.RestErrorHandler.java

/**
 * Handles the given exception and generates a response with error code and message description.
 *
 * @param e//from  ww w .j a  va2s  . c  o  m
 *            Given exception
 * @param request
 *            The http request
 * @param response
 *            The http response
 * @return The model view with the error response
 */
@SuppressWarnings("nls")
public ModelAndView createApiErrorResponse(final Exception e, final HttpServletRequest request,
        final HttpServletResponse response) {
    LOGGER.error(e.getMessage(), e);
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    RestApiErrorResponse restApiErrorResponse = new RestApiErrorResponse();

    if (e instanceof RestApiException) {
        RestApiException restEx = (RestApiException) e;
        response.setStatus(restEx.getHttpStatusCode().value());
        restApiErrorResponse.setErrorMessage(restEx.getMessage());
        restApiErrorResponse.setErrorCode(restEx.getAppErrorCode());
    } else if (IllegalArgumentException.class.isAssignableFrom(e.getClass())) {
        // Illegal argument exceptions mapped to 400 errors by default
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        restApiErrorResponse.setErrorMessage(e.getMessage());
    } else if (UntrustedIssuerException.class.isAssignableFrom(e.getClass())
            || SecurityException.class.isAssignableFrom(e.getClass())) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        restApiErrorResponse.setErrorMessage(e.getMessage());
    } else if (HttpMessageNotReadableException.class.isAssignableFrom(e.getClass())) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        restApiErrorResponse.setErrorMessage("Malformed JSON syntax. " + e.getLocalizedMessage());
    }

    return new ModelAndView(new MappingJackson2JsonView(), "ErrorDetails", restApiErrorResponse);
}

From source file:org.exoplatform.acceptance.rest.JsonErrorHandler.java

/**
 * Catch HttpMessageNotReadableException to log it (helps to diagnose errors and attacks on REST services).
 *
 * @param ex The exception trapped//from  w ww  .  j av  a2s . c o m
 * @return A standardized {@link org.exoplatform.acceptance.rest.JsonErrorResponse}
 * @throws java.io.IOException if any.
 */
@ExceptionHandler(HttpMessageNotReadableException.class)
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public JsonErrorResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException ex)
        throws IOException {
    LOGGER.warn("Http Message Not Readable : {}", ex.getMessage());
    return new JsonErrorResponse(ex);
}

From source file:io.getlime.security.powerauth.rest.api.spring.exception.PowerAuthExceptionHandler.java

/**
 * Handle PowerAuthActivationException exceptions.
 * @param ex Exception instance./*from ww  w .  j  a v a  2 s .c  o m*/
 * @return Error response.
 */
@ExceptionHandler(value = PowerAuthActivationException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public @ResponseBody PowerAuthApiResponse<ErrorModel> handleActivationException(Exception ex) {
    PowerAuthActivationException paex = (PowerAuthActivationException) ex;
    Logger.getLogger(PowerAuthExceptionHandler.class.getName()).log(Level.SEVERE, paex.getMessage(), paex);
    ErrorModel error = new ErrorModel(paex.getDefaultCode(), paex.getMessage());
    return new PowerAuthApiResponse<>(PowerAuthApiResponse.Status.ERROR, error);
}

From source file:com.ns.retailmgr.controller.ShopController.java

@ApiOperation(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, httpMethod = "POST", value = "", response = String.class, notes = "Save the shop details")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> saveShop(@RequestBody ShopDetails shopDetails) {
    LOGGER.info("Started endpoint method {}, params - {}", "saveShop");
    try {//  w w  w  .  j  av a2  s . c om
        ShopDetails newShopDetails = shopService.addShop(shopDetails);
        if (newShopDetails == null) {
            return new ResponseEntity<Object>(
                    "Unable to find latitude and logitude for shop details provided, please check and resubmit again",
                    HttpStatus.BAD_REQUEST);
        }
        if (newShopDetails.getStatus() != null)
            return new ResponseEntity<Object>(HttpStatus.CREATED);
        else
            return new ResponseEntity<Object>(newShopDetails, HttpStatus.OK);
    } catch (Exception e) {
        LOGGER.error("Exception {}", e);
        return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}

From source file:org.jrb.docasm.web.GlobalExceptionHandler.java

/**
 * Converts one of several client-based bad request exceptions into an HTTP
 * 400 response with an error body. The mapped exceptions are as follows:
 * <ul>//  w  w  w .  jav a 2  s. co  m
 * <li>{@link InvalidDocumentException}</li>
 * </ul>
 * 
 * @param e
 *            the client exception
 * @return the error body
 */
@ExceptionHandler({ InvalidDocumentException.class })
public ResponseEntity<MessageResponse> handleClientBadRequest(final Exception e) {
    if (LOG.isDebugEnabled()) {
        LOG.debug(e.getMessage(), e);
    }
    return utils.createMessageResponse(e.getMessage(), HttpStatus.BAD_REQUEST);
}

From source file:com.nebhale.buildmonitor.web.ProjectController.java

@Transactional
@RequestMapping(method = RequestMethod.POST, value = "", produces = MEDIA_TYPE)
ResponseEntity<?> create(@RequestBody Project project) {
    if (this.repository.exists(project.getKey())) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }/*from   w w  w  .j a v a  2s . c om*/

    this.repository.saveAndFlush(project);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(linkTo(ProjectController.class).slash(project.getKey()).toUri());

    this.projectsChangedNotifier.projectsChanged();
    return new ResponseEntity<>(headers, HttpStatus.CREATED);
}

From source file:org.venice.piazza.servicecontroller.messaging.handlers.SearchServiceHandler.java

/**
 * Handler for the RegisterServiceJob that was submitted. Stores the metadata in MongoDB (non-Javadoc)
 * /*from   www .  j  a  va  2s . c o  m*/
 * @see org.venice.piazza.servicecontroller.messaging.handlers.Handler#handle(model.job.PiazzaJobType)
 */
@Override
public ResponseEntity<String> handle(PiazzaJobType jobRequest) {
    SearchServiceJob job = (SearchServiceJob) jobRequest;
    ResponseEntity<String> responseEntity;
    if ((job != null) && (job.data != null)) {
        // Get the criteria to use for the search
        SearchCriteria criteria = job.data;

        coreLogger.log("search " + " " + criteria.field + "->" + criteria.pattern, Severity.INFORMATIONAL);

        ResponseEntity<String> response = handle(criteria);
        responseEntity = new ResponseEntity<>(response.getBody(), response.getStatusCode());
    } else {
        responseEntity = new ResponseEntity<>("Null request received.", HttpStatus.BAD_REQUEST);
    }
    return responseEntity;
}

From source file:org.openbaton.nfvo.api.exceptions.GlobalExceptionHandler.java

@ExceptionHandler({ BadRequestException.class, BadFormatException.class, NetworkServiceIntegrityException.class,
        WrongStatusException.class, UnrecognizedPropertyException.class, VimException.class,
        CyclicDependenciesException.class, WrongAction.class, PasswordWeakException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
protected ResponseEntity<Object> handleInvalidRequest(Exception e, WebRequest request) {
    if (log.isDebugEnabled()) {
        log.error("Exception was thrown -> Return message: " + e.getMessage(), e);
    } else {//ww w  .  j  a  v a  2  s  .  co m
        log.error("Exception was thrown -> Return message: " + e.getMessage());
    }
    ExceptionResource exc = new ExceptionResource("Bad Request", e.getMessage());
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(e, exc, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}

From source file:org.syncope.core.rest.data.RoleDataBinder.java

public SyncopeRole create(final RoleTO roleTO) throws SyncopeClientCompositeErrorException {

    SyncopeRole role = new SyncopeRole();
    role.setInheritAttributes(roleTO.isInheritAttributes());
    role.setInheritDerivedAttributes(roleTO.isInheritDerivedAttributes());
    role.setInheritVirtualAttributes(roleTO.isInheritVirtualAttributes());

    role.setInheritPasswordPolicy(roleTO.isInheritPasswordPolicy());
    role.setInheritAccountPolicy(roleTO.isInheritAccountPolicy());

    SyncopeClientCompositeErrorException scce = new SyncopeClientCompositeErrorException(
            HttpStatus.BAD_REQUEST);

    // name and parent
    SyncopeClientException invalidRoles = new SyncopeClientException(SyncopeClientExceptionType.InvalidRoles);
    if (roleTO.getName() == null) {
        LOG.error("No name specified for this role");

        invalidRoles.addElement("No name specified for this role");
    } else {/*from  w w w  . j  a  v a  2s  . co m*/
        role.setName(roleTO.getName());
    }
    Long parentRoleId = null;
    if (roleTO.getParent() != 0) {
        SyncopeRole parentRole = roleDAO.find(roleTO.getParent());
        if (parentRole == null) {
            LOG.error("Could not find role with id " + roleTO.getParent());

            invalidRoles.addElement(String.valueOf(roleTO.getParent()));
            scce.addException(invalidRoles);
        } else {
            role.setParent(parentRole);
            parentRoleId = role.getParent().getId();
        }
    }

    SyncopeRole otherRole = roleDAO.find(roleTO.getName(), parentRoleId);
    if (otherRole != null) {
        LOG.error("Another role exists with the same name " + "and the same parent role: " + otherRole);

        invalidRoles.addElement(roleTO.getName());
    }

    // attributes, derived attributes, virtual attributes and resources
    fill(role, roleTO, AttributableUtil.ROLE, scce);

    // entitlements
    Entitlement entitlement;
    for (String entitlementName : roleTO.getEntitlements()) {
        entitlement = entitlementDAO.find(entitlementName);
        if (entitlement == null) {
            LOG.warn("Ignoring invalid entitlement {}", entitlementName);
        } else {
            role.addEntitlement(entitlement);
        }
    }

    role.setPasswordPolicy(
            roleTO.getPasswordPolicy() != null ? (PasswordPolicy) policyDAO.find(roleTO.getPasswordPolicy())
                    : null);

    role.setAccountPolicy(
            roleTO.getAccountPolicy() != null ? (AccountPolicy) policyDAO.find(roleTO.getAccountPolicy())
                    : null);

    return role;
}