Example usage for javax.servlet.http HttpServletResponse SC_BAD_REQUEST

List of usage examples for javax.servlet.http HttpServletResponse SC_BAD_REQUEST

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_BAD_REQUEST.

Prototype

int SC_BAD_REQUEST

To view the source code for javax.servlet.http HttpServletResponse SC_BAD_REQUEST.

Click Source Link

Document

Status code (400) indicating the request sent by the client was syntactically incorrect.

Usage

From source file:com.sg.dvdlibrary.controller.HomeController.java

@RequestMapping(value = "/addDvd", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)//  w ww.  j av a  2s . co  m
@ResponseBody
public Dvd createEntry(@Valid @RequestBody Dvd dvd, HttpServletResponse response) {
    if (!dvd.getTitle().equals(".")) {
        dao.addEntry(dvd);
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    return dvd;
}

From source file:cn.org.once.cstack.config.UserAjaxAuthenticationFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    logger.warn("Authentication failed");
    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad credentials");
}

From source file:fr.epsi.controllers.rest.OrderController.java

/**
 * Methode pour les commandes/* w  ww .j  a v a2 s  .c  o m*/
 * @param reference la reference du produit
 * @param quantity la quantite voulue
 * @param guid l'identifiant de l'utilisateur
 * @return Un message selon le statut de la commande
 */
@RequestMapping(value = "/order", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Product order(@RequestParam("reference") String reference,
        @RequestParam("quantity") int quantity, @RequestParam("token") String token, HttpServletResponse resp) {

    Users userModel = Users.getInstance(); //Recupere l'instance de user

    try {
        // Si on trouve un user correspondant au guid
        if (userModel.findByGUID(token) != null) {
            Products productModel = Products.getInstance();

            // On recupere le produit par reference
            Product product = productModel.findByRef(reference);

            if (product == null || quantity <= 0 || product.getQuantity() == 0
                    || product.getQuantity() < quantity) {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            } else {
                //On decremente la quantite du produit
                product.setQuantity(product.getQuantity() - quantity);

                return new Product(product.getReference(), product.getName(), quantity);
            }
        } else {
            resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return null;
    }
}

From source file:org.openmrs.module.clinicalsummary.web.controller.response.MedicationAcceptedController.java

@RequestMapping(method = RequestMethod.POST, value = "/module/clinicalsummary/response/acceptMedication")
public void processAccept(final @RequestParam(required = true, value = "id") Integer responseId,
        final @RequestParam(required = true, value = "comment") String comment,
        final HttpServletResponse response) throws IOException {

    if (Context.isAuthenticated()) {
        if (StringUtils.isBlank(comment))
            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
                    Context.getMessageSourceService().getMessage("clinicalsummary.response.required"));
        else {/*from  www  .  ja v  a2 s  . c  o m*/
            UtilService service = Context.getService(UtilService.class);
            MedicationResponse medicationResponse = service.getResponse(MedicationResponse.class, responseId);
            medicationResponse.setReviewer(Context.getAuthenticatedUser().getPerson());
            medicationResponse.setDateReviewed(new Date());
            medicationResponse.setReviewComment(comment);
            medicationResponse.setActionType(ActionType.ACTION_ACCEPT);
            service.saveResponse(medicationResponse);
        }
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.FileDownload.java

@Override
public ActionForward execute(final ActionMapping mapping, final ActionForm actionForm,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String oid = request.getParameter("oid");
    final File file = FenixFramework.getDomainObject(oid);
    if (file == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();//w w w.  j  a  v a  2  s .  c  o m
    } else {
        final Person person = AccessControl.getPerson();
        if (!file.isPrivate() || file.isPersonAllowedToAccess(person)) {
            response.setContentType(file.getContentType());
            response.addHeader("Content-Disposition", "attachment; filename=" + file.getFilename());
            response.setContentLength(file.getSize().intValue());
            final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
            dos.write(file.getContents());
            dos.close();
        } else if (file.isPrivate() && person == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_UNAUTHORIZED));
            response.getWriter().close();
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_FORBIDDEN));
            response.getWriter().close();
        }
    }
    return null;
}

From source file:com.cloudera.oryx.als.serving.web.EstimateForAnonymousServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    CharSequence pathInfo = request.getPathInfo();
    if (pathInfo == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No path");
        return;/*from  w w  w .  j  av a 2 s  .c  o  m*/
    }
    Iterator<String> pathComponents = SLASH.split(pathInfo).iterator();
    String toItemID;
    Pair<String[], float[]> itemIDsAndValue;
    try {
        toItemID = pathComponents.next();
        itemIDsAndValue = RecommendToAnonymousServlet.parseItemValuePairs(pathComponents);
    } catch (NoSuchElementException nsee) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());
        return;
    }

    if (itemIDsAndValue.getFirst().length == 0) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No items");
        return;
    }

    String[] itemIDs = itemIDsAndValue.getFirst();
    unescapeSlashHack(itemIDs);
    float[] values = itemIDsAndValue.getSecond();

    OryxRecommender recommender = getRecommender();
    try {
        float estimate = recommender.estimateForAnonymous(toItemID, itemIDs, values);
        Writer out = response.getWriter();
        out.write(Float.toString(estimate));
        out.write('\n');
    } catch (NotReadyException nre) {
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, nre.toString());
    } catch (NoSuchItemException nsie) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, nsie.toString());
    }
}

From source file:jp.ambrosoli.http.server.action.IndexAction.java

@Execute(validator = false)
public String helloWorld() {
    String name = this.request.getParameter("name");
    String queryString = this.requestScope.get("javax.servlet.forward.query_string");
    String[] query = StringUtils.split(queryString, "=");
    if (ArrayUtils.isNotEmpty(query)) {
        try {/*from  w w w .j  a va  2 s  .  c  o  m*/
            name = URLDecoder.decode(query[1], this.request.getCharacterEncoding());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    if (StringUtils.isEmpty(name)) {
        this.response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
    ResponseUtil.write("Hello, " + name + "!", "UTF-8");
    return null;
}

From source file:com.kixeye.chassis.transport.http.HttpExceptionHandler.java

@ExceptionHandler(Exception.class)
@ResponseBody//from   w  w  w  .  j  a  v  a2  s .  c om
public ServiceError defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
        throws Exception {
    ServiceError error = ExceptionServiceErrorMapper.mapException(ex);

    switch (error.code) {
    case ExceptionServiceErrorMapper.UNKNOWN_ERROR_CODE:
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

        logger.error("Unexpected error", ex);
        break;
    case ExceptionServiceErrorMapper.VALIDATION_ERROR_CODE:
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

        if (logger.isDebugEnabled()) {
            logger.debug("Validation exception", ex);
        }
        break;
    case ExceptionServiceErrorMapper.SECURITY_ERROR_CODE:
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

        if (logger.isDebugEnabled()) {
            logger.debug("Security exception", ex);
        }
        break;
    default:
        if (ex instanceof HttpServiceException) {
            HttpServiceException httpEx = (HttpServiceException) ex;

            response.setStatus(httpEx.httpResponseCode);
        }

        logger.warn("Service exception", ex);
        break;
    }

    return error;
}

From source file:fr.epsi.controllers.rest.ProductController.java

@RequestMapping(value = "/product", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Product[] order(HttpServletResponse resp) {

    try {//from ww w .jav a  2  s.com
        Products productModel = Products.getInstance();

        // On recupere le produit par reference
        Product[] product = productModel.getAllProduct();

        if (product != null) {
            return product;
        } else {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
        return null;
    } catch (Exception e) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return null;
    }
}

From source file:com.toptal.conf.ConstraintViolationExceptionHandler.java

/**
 * Handles ConstraintViolationException.
 * @param exception Exception./* w  ww  . jav a2s  . c om*/
 * @param resp Http response.
 * @throws IOException If smth goes wrong.
 * @checkstyle IndentationCheck (10 lines)
 */
@ExceptionHandler(ConstraintViolationException.class)
public void handle(final ConstraintViolationException exception, final HttpServletResponse resp)
        throws IOException {
    resp.sendError(HttpServletResponse.SC_BAD_REQUEST, exception.getMessage());
}