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.adito.vfs.webdav.DAVServlet.java

/**
 * Add the headers required for the browser to popup an authentication
 * dialog for a specified realm, and send the
 * {@link HttpServletResponse.SC_UNAUTHORIZED} HTTP response code.
 * /*from w w  w  . ja v  a 2s  .co  m*/
 * @param request request
 * @param response response
 * @param realm realm.
 * @throws IOException
 */
public static void sendAuthorizationError(HttpServletRequest request, HttpServletResponse response,
        String realm) throws IOException {
    /*
     * If this is for the default realm (i.e Adito Authentication, we
     * need to set up an authentication scheme
     */
    if (realm.equals(WebDAVAuthenticationModule.DEFAULT_REALM)) {
        configureAuthenticationScheme(request, response);
    }

    // Configure the response

    if (log.isDebugEnabled())
        log.debug("Sending auth request for realm " + realm);
    response.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    request.getSession().setAttribute(DAVTransaction.ATTR_EXPECTING_REALM_AUTHENTICATION, realm);

}

From source file:com.octo.captcha.module.web.image.ImageToJpegHelper.java

/**
 * retrieve a new ImageCaptcha using ImageCaptchaService and flush it to the response.<br/> Captcha are localized
 * using request locale.<br/>//w  ww  .jav a  2s  . c  o m
 * <p/>
 * 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
 * @param theResponse the response
 * @param log         a commons logger
 * @param service     an ImageCaptchaService instance
 *
 * @throws java.io.IOException if a problem occurs during the jpeg generation process
 */
public static void flushNewCaptchaToResponse(HttpServletRequest theRequest, HttpServletResponse theResponse,
        Log log, ImageCaptchaService service, String id, Locale locale) throws IOException {

    // call the ImageCaptchaService method to retrieve a captcha
    byte[] captchaChallengeAsJpeg = null;
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    try {
        BufferedImage challenge = service.getImageChallengeForID(id, locale);
        // the output stream to render the captcha image as jpeg into

        ImageIO.write(challenge, "jpg", jpegOutputStream);

    } 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;
        }
    }

    captchaChallengeAsJpeg = jpegOutputStream.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("image/jpeg");
    ServletOutputStream responseOutputStream = theResponse.getOutputStream();
    responseOutputStream.write(captchaChallengeAsJpeg);
    responseOutputStream.flush();
    responseOutputStream.close();
}

From source file:com.sslexplorer.vfs.webdav.DAVServlet.java

/**
 * Add the headers required for the browser to popup an authentication
 * dialog for a specified realm, and send the
 * {@link HttpServletResponse.SC_UNAUTHORIZED} HTTP response code.
 * //from  w ww  .j  av  a 2 s . c  o  m
 * @param request request
 * @param response response
 * @param realm realm.
 * @throws IOException
 */
public static void sendAuthorizationError(HttpServletRequest request, HttpServletResponse response,
        String realm) throws IOException {
    /*
     * If this is for the default realm (i.e SSL-Explorer Authentication, we
     * need to set up an authentication scheme
     */
    if (realm.equals(WebDAVAuthenticationModule.DEFAULT_REALM)) {
        configureAuthenticationScheme(request, response);
    }

    // Configure the response

    if (log.isDebugEnabled())
        log.debug("Sending auth request for realm " + realm);
    response.setHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    request.getSession().setAttribute(DAVTransaction.ATTR_EXPECTING_REALM_AUTHENTICATION, realm);

}

From source file:com.armeniopinto.time.ApplicationStarter.java

@ExceptionHandler
public void handleUnkonwnDateFormat(final IllegalArgumentException iae, final HttpServletResponse response)
        throws IOException {
    response.sendError(HttpStatus.NOT_FOUND.value());
}

From source file:io.pivotal.strepsirrhini.chaosloris.web.ExceptionHandlers.java

@ExceptionHandler(DataIntegrityViolationException.class)
void handleDataIntegrityViolationException(HttpServletResponse response) throws IOException {
    response.sendError(SC_BAD_REQUEST);
}

From source file:io.pivotal.strepsirrhini.chaosloris.web.ExceptionHandlers.java

@ExceptionHandler(EmptyResultDataAccessException.class)
void handleEmptyResultDataAccessException(HttpServletResponse response) throws IOException {
    response.sendError(SC_NOT_FOUND);
}

From source file:io.pivotal.strepsirrhini.chaosloris.web.ExceptionHandlers.java

@ExceptionHandler(EntityNotFoundException.class)
void handleEntityNotFoundException(HttpServletResponse response) throws IOException {
    response.sendError(SC_NOT_FOUND);
}

From source file:net.swigg.talo.admin.TestControllerImpl.java

@Override
public String error(HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return "error";
}

From source file:org.codehaus.groovy.grails.plugins.remoting.DummyHttpExporter.java

/**
 * Always returns a 404 HTTP status./*from   www  . java 2s.c  o  m*/
 */
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
}

From source file:ru.mystamps.web.support.spring.security.Http401UnauthorizedEntryPoint.java

/**
 * Always returns a 401 error code to the client.
 **//*ww  w . j  a  va2  s . c  o m*/
@Override
public void commence(HttpServletRequest req, HttpServletResponse resp, AuthenticationException ex)
        throws IOException {

    resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}