Example usage for javax.servlet.http HttpServletResponse isCommitted

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

Introduction

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

Prototype

public boolean isCommitted();

Source Link

Document

Returns a boolean indicating if the response has been committed.

Usage

From source file:ch.entwine.weblounge.common.impl.request.Http11ProtocolHandler.java

/**
 * Method generateResponse.//from ww  w  . jav  a 2  s.c  o m
 * 
 * @param resp
 * @param type
 * @param is
 * @return boolean
 * @throws IOException
 *           if generating the response fails
 */
public static boolean generateResponse(HttpServletResponse resp, Http11ResponseType type, InputStream is)
        throws IOException {

    /* first generate the response headers */
    generateHeaders(resp, type);

    /* adjust the statistics */
    ++stats[STATS_BODY_GENERATED];
    incResponseStats(type.type, bodyStats);

    /* generate the response body */
    try {
        if (resp.isCommitted())
            log.warn("Response is already committed!");
        switch (type.type) {
        case RESPONSE_OK:
            if (!type.isHeaderOnly() && is != null) {
                resp.setBufferSize(BUFFER_SIZE);
                OutputStream os = null;
                try {
                    os = resp.getOutputStream();
                    IOUtils.copy(is, os);
                } catch (IOException e) {
                    if (RequestUtils.isCausedByClient(e))
                        return true;
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
            break;

        case RESPONSE_PARTIAL_CONTENT:
            if (type.from < 0 || type.to < 0 || type.from > type.to || type.to > type.size) {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        "Invalid partial content parameters");
                log.warn("Invalid partial content parameters");
            } else if (!type.isHeaderOnly() && is != null) {
                resp.setBufferSize(BUFFER_SIZE);
                OutputStream os = resp.getOutputStream();
                if (is.skip(type.from) != type.from) {
                    resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                            "Premature end of input stream");
                    log.warn("Premature end of input stream");
                    break;
                }
                try {

                    /* get the temporary buffer for this thread */
                    byte[] tmp = buffer.get();
                    if (tmp == null) {
                        tmp = new byte[BUFFER_SIZE];
                        buffer.set(tmp);
                    }

                    int read = type.to - type.from;
                    int copy = read;
                    int write = 0;

                    read = is.read(tmp);
                    while (copy > 0 && read >= 0) {
                        copy -= read;
                        write = copy > 0 ? read : read + copy;
                        os.write(tmp, 0, write);
                        stats[STATS_BYTES_WRITTEN] += write;
                        read = is.read(tmp);
                    }
                    if (copy > 0) {
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                                "Premature end of input stream");
                        log.warn("Premature end of input stream");
                        break;
                    }
                    os.flush();
                    os.close();
                } catch (SocketException e) {
                    log.debug("Request cancelled by client");
                }
            }
            break;

        case RESPONSE_NOT_MODIFIED:
            /* NOTE: we MUST NOT return any content (RFC 2616)!!! */
            break;

        case RESPONSE_PRECONDITION_FAILED:
            if (type.err == null)
                resp.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            else
                resp.sendError(HttpServletResponse.SC_PRECONDITION_FAILED, type.err);
            break;

        case RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE:
            if (type.err == null)
                resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
            else
                resp.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE, type.err);
            break;

        case RESPONSE_METHOD_NOT_ALLOWED:
            if (type.err == null)
                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            else
                resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, type.err);
            break;

        case RESPONSE_INTERNAL_SERVER_ERROR:
        default:
            if (type.err == null)
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            else
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, type.err);
        }
    } catch (IOException e) {
        if (e instanceof EOFException) {
            log.debug("Request canceled by client");
            return true;
        }
        ++stats[STATS_ERRORS];
        String message = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        Throwable cause = e.getCause() != null ? e.getCause() : e;
        log.warn("I/O exception while sending response: {}", message, cause);
        throw e;
    }

    return true;
}

From source file:org.opentravel.pubs.servlet.DispatcherServlet.java

/**
 * @see org.springframework.web.servlet.DispatcherServlet#render(org.springframework.web.servlet.ModelAndView, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *//*from w ww  . j a  v  a  2s.c o  m*/
@Override
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    if (!response.isCommitted()) {
        super.render(mv, request, response);
    }
}

From source file:com.nec.harvest.servlet.interceptor.async.TimeoutCallableProcessingInterceptor.java

@Override
public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception {
    final HttpServletResponse servletResponse = request.getNativeResponse(HttpServletResponse.class);
    if (!servletResponse.isCommitted()) {
        servletResponse.sendError(HttpStatus.SERVICE_UNAVAILABLE.value());
    }//from   ww  w  .  java 2 s  .  com

    return CallableProcessingInterceptor.RESPONSE_HANDLED;
}

From source file:de.itsvs.cwtrpc.security.DefaultRpcLogoutFailureHandler.java

public void onLogoutFailure(HttpServletRequest request, HttpServletResponse response, Throwable exception)
        throws IOException, ServletException {
    if (response.isCommitted()) {
        log.debug("Response has already been sent to client");
        return;//w  w w  . ja  v  a  2s  .com
    }

    writeUnexpectedException(request, response, exception);
}

From source file:org.esigate.servlet.impl.ResponseSender.java

public void sendResponse(HttpResponse httpResponse, IncomingRequest httpRequest, HttpServletResponse response)
        throws IOException {
    if (response.isCommitted()) {
        return; // Response already sent
    }//from w  w  w.  jav  a2 s .co  m
    sendHeaders(httpResponse, httpRequest, response);
    HttpEntity httpEntity = httpResponse.getEntity();
    if (httpEntity != null) {
        HttpResponseUtils.writeTo(httpEntity, response.getOutputStream());
    } else {
        response.sendError(httpResponse.getStatusLine().getStatusCode(),
                httpResponse.getStatusLine().getReasonPhrase());
    }
}

From source file:de.itsvs.cwtrpc.security.DefaultRpcLogoutSuccessHandler.java

public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    if (response.isCommitted()) {
        log.debug("Response has already been sent to client");
        return;//from   ww  w. j a  v  a2  s  .  c  om
    }

    sendResponse(request, response, authentication);
}

From source file:de.itsvs.cwtrpc.security.DefaultRpcAuthenticationSuccessHandler.java

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    if (response.isCommitted()) {
        log.debug("Response has already been sent to client");
        return;// w ww.  ja v a  2 s  .  c  o m
    }

    sendResponse(request, response, authentication);
}

From source file:info.magnolia.cms.security.auth.callback.FormClientCallback.java

@Override
public void handle(HttpServletRequest request, HttpServletResponse response) {
    try {/*from www .  java2s.  c o m*/
        if (!response.isCommitted()) {
            response.setContentType("text/html");
            if (null == request.getCharacterEncoding()) {
                response.setCharacterEncoding(MIMEMapping.getContentEncodingOrDefault("text/html"));
            }
        }
        FreemarkerUtil.process(getLoginForm(), getMessages(), response.getWriter());
    } catch (Throwable t) {
        log.error("exception while writing login template", t);
    }
}

From source file:org.entando.entando.aps.system.services.controller.executor.PageExecutorService.java

@Override
public void service(RequestContext reqCtx) {
    HttpServletRequest request = reqCtx.getRequest();
    HttpServletResponse response = reqCtx.getResponse();
    try {//w  w w. jav  a 2s  .co  m
        if (response.isCommitted()) {
            return;
        }
        IPage page = (IPage) reqCtx.getExtraParam(SystemConstants.EXTRAPAR_CURRENT_PAGE);
        PageModel model = page.getModel();
        if (StringUtils.isBlank(model.getTemplate())) {
            String jspPath = model.getPageModelJspPath();
            RequestDispatcher dispatcher = request.getSession().getServletContext()
                    .getRequestDispatcher(jspPath);
            dispatcher.forward(request, response);
        } else {
            ExecutorBeanContainer ebc = (ExecutorBeanContainer) reqCtx
                    .getExtraParam(SystemConstants.EXTRAPAR_EXECUTOR_BEAN_CONTAINER);
            Template template = new Template(page.getCode(), new StringReader(model.getTemplate()),
                    ebc.getConfiguration());
            try {
                template.process(ebc.getTemplateModel(), response.getWriter());
            } catch (Throwable t) {
                String msg = "Error detected while including a page model " + model.getCode();
                _logger.error(msg, t);
                throw new RuntimeException(msg, t);
            }
        }
    } catch (ServletException e) {
        String msg = "Error detected while including a page model";
        _logger.error(msg, e);
        throw new RuntimeException(msg, e);
    } catch (IOException e) {
        String msg = "IO error detected while including the page model";
        _logger.error(msg, e);
        throw new RuntimeException(msg, e);
    }
}

From source file:com.haulmont.cuba.web.controllers.FileDownloadController.java

protected void error(HttpServletResponse response) throws IOException {
    if (!response.isCommitted())
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
}