Example usage for javax.servlet.http HttpServletResponse sendError

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

Introduction

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

Prototype

public void sendError(int sc) throws IOException;

Source Link

Document

Sends an error response to the client using the specified status code and clears the buffer.

Usage

From source file:com.mycompany.licit.custom.RESTAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest hsr, HttpServletResponse hsr1, AuthenticationException ae)
        throws IOException, ServletException {
    hsr1.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}

From source file:com.vigglet.servlet.PostJsonServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

From source file:cn.vlabs.umt.ui.servlet.PublicKeyServlet.java

private void notFound(HttpServletResponse response) throws IOException {
    response.sendError(404);
}

From source file:cn.vlabs.umt.ui.servlet.PublicKeyServlet.java

private void wrongParameter(HttpServletResponse response) throws IOException {
    response.sendError(500);
}

From source file:com.springsource.greenhouse.home.GlobalExceptionResolver.java

private ModelAndView handleEmptyResultDataAccessException(EmptyResultDataAccessException ex,
        HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return new ModelAndView();
}

From source file:com.octo.captcha.module.web.sound.SoundToWavHelper.java

/**
 * retrieve a new SoundCaptcha using SoundCaptchaService and flush it to the response. <br/> Captcha are localized
 * using request locale. <br/>This method returns a 404 to the client instead of the image if the request isn't
 * correct (missing parameters, etc...).. <br/>The log may be null. <br/>
 *
 * @param theRequest  the request//from w  w w .j  a  v a 2  s .c o  m
 * @param theResponse the response
 * @param log         a commons logger
 * @param service     an SoundCaptchaService instance
 *
 * @throws java.io.IOException if a problem occurs during the jpeg generation process
 */
public static void flushNewCaptchaToResponse(HttpServletRequest theRequest, HttpServletResponse theResponse,
        Log log, SoundCaptchaService service, String id, Locale locale) throws IOException {

    // call the ImageCaptchaService method to retrieve a captcha
    byte[] captchaChallengeAsWav = null;
    ByteArrayOutputStream wavOutputStream = new ByteArrayOutputStream();
    try {
        AudioInputStream stream = service.getSoundChallengeForID(id, locale);

        // call the ImageCaptchaService method to retrieve a captcha

        AudioSystem.write(stream, AudioFileFormat.Type.WAVE, wavOutputStream);
        //AudioSystem.(pAudioInputStream, AudioFileFormat.Type.WAVE, pFile);

    } catch (IllegalArgumentException e) {
        //    log a security warning and return a 404...
        if (log != null && log.isWarnEnabled()) {
            log.warn("There was a try from " + theRequest.getRemoteAddr()
                    + " to render an captcha with invalid ID :'" + id + "' or with a too long one");
            theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    } catch (CaptchaServiceException e) {
        // log and return a 404 instead of an image...
        if (log != null && log.isWarnEnabled()) {
            log.warn(

                    "Error trying to generate a captcha and " + "render its challenge as JPEG", e);
        }
        theResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    captchaChallengeAsWav = wavOutputStream.toByteArray();

    // render the captcha challenge as a JPEG image in the response
    theResponse.setHeader("Cache-Control", "no-store");
    theResponse.setHeader("Pragma", "no-cache");
    theResponse.setDateHeader("Expires", 0);

    theResponse.setContentType("audio/x-wav");
    ServletOutputStream responseOutputStream = theResponse.getOutputStream();
    responseOutputStream.write(captchaChallengeAsWav);
    responseOutputStream.flush();
    responseOutputStream.close();
}

From source file:com.vigglet.oei.standardinspection.CopyStandardinspectionToServiceServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
}

From source file:com.rockagen.gnext.service.spring.security.extension.ExAuthenticationEntryPoint.java

@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}

From source file:com.onyxscheduler.web.JobController.java

@ExceptionHandler(Scheduler.DuplicateJobKeyException.class)
void handleBadRequests(HttpServletResponse response) throws IOException {
    response.sendError(HttpStatus.BAD_REQUEST.value());
}

From source file:ru.mystamps.web.controller.ImageController.java

@GetMapping(Url.GET_IMAGE_PAGE)
public void getImage(@PathVariable("id") Integer imageId, HttpServletResponse response) throws IOException {

    if (imageId == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;/*w  w  w  .  jav  a  2s.c  o m*/
    }

    ImageDto image = imageService.get(imageId);
    if (image == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // TODO: set content disposition
    response.setContentType("image/" + image.getType().toLowerCase());
    response.setContentLength(image.getData().length);

    response.getOutputStream().write(image.getData());
}