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:org.apache.roller.weblogger.ui.core.filters.DebugFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    log.info("ENTERING " + request.getRequestURL());

    // some info about the request and response
    log.info("Response Object:");
    log.info("   isCommited = " + response.isCommitted());
    log.info("   bufferSize = " + response.getBufferSize());
    log.info("");

    chain.doFilter(request, response);/* ww w  . j  a v a2s.  c om*/

    log.info("EXITING " + request.getRequestURL());

    // some info about the request and response
    log.info("Response Object:");
    log.info("   isCommited = " + response.isCommitted());
    log.info("   bufferSize = " + response.getBufferSize());
    log.info("");
}

From source file:org.vaadin.spring.security.shared.AbstractVaadinAuthenticationTargetUrlRequestHandler.java

/**
 * Invokes the configured {@code RedirectStrategy} with the URL returned by the {@code determineTargetUrl} method.
 * <p/>/*from   w w  w .  java  2 s.  c o  m*/
 * The redirect will not be performed if the response has already been committed.
 */
protected void handle(Authentication authentication) throws IOException, ServletException {
    HttpServletRequest request = http.getCurrentRequest();
    HttpServletResponse response = http.getCurrentResponse();
    String targetUrl = determineTargetUrl(request, response);

    if (response.isCommitted()) {
        logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
        return;
    } else {
        logger.debug("Redirecting to " + targetUrl);
    }

    redirectStrategy.sendRedirect(targetUrl);
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ??JSP_PATH?/* w w w.j  a  v a 2  s.  c o  m*/
 * ?
 *
 * @throws ServletException
 * @throws IOException
 */
private static void forward() throws RuntimeException {
    try {
        HttpServletRequest request = ActionContext.getReq();
        HttpServletResponse response = ActionContext.getResp();
        if (response.isCommitted()) {
            return;
        }
        String jspPath = null;
        String jspPathParam = request.getParameter(JSP_PATH);
        if (StringUtils.isEmpty(jspPathParam)) {
            Object jspPathObjAttr = request.getAttribute(JSP_PATH);
            if (null == jspPathObjAttr) {
                Object jspPathObjSess = request.getSession().getAttribute(JSP_PATH);
                if (null != jspPathObjSess) {
                    jspPath = (String) jspPathObjSess;
                }
            } else {
                jspPath = (String) jspPathObjAttr;
            }
        } else {
            jspPath = jspPathParam;
        }
        if (null == jspPath) {
            throw new ServletException(
                    "????jspPath?");
        }
        request.getRequestDispatcher(jspPath).forward(request, response);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.grails.plugins.zkui.metaclass.RedirectDynamicMethod.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from w  w  w .jav a 2s.  c om*/
public Object invoke(Object target, String methodName, Object[] arguments) {
    if (arguments.length == 0) {
        throw new MissingMethodException(METHOD_SIGNATURE, target.getClass(), arguments);
    }

    Map argMap = arguments[0] instanceof Map ? (Map) arguments[0] : Collections.EMPTY_MAP;
    if (argMap.isEmpty()) {
        throw new MissingMethodException(METHOD_SIGNATURE, target.getClass(), arguments);
    }

    GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();

    HttpServletRequest request = webRequest.getCurrentRequest();
    if (request.getAttribute(GRAILS_REDIRECT_ISSUED) != null) {
        throw new CannotRedirectException(
                "Cannot issue a redirect(..) here. A previous call to redirect(..) has already redirected the response.");
    }

    HttpServletResponse response = webRequest.getCurrentResponse();
    if (response.isCommitted()) {
        throw new CannotRedirectException(
                "Cannot issue a redirect(..) here. The response has already been committed either by another redirect or by directly writing to the response.");
    }

    Object uri = argMap.get(ARGUMENT_URI);
    String url = argMap.containsKey(ARGUMENT_URL) ? argMap.get(ARGUMENT_URL).toString() : null;
    String actualUri;
    if (uri != null) {
        GrailsApplicationAttributes attrs = webRequest.getAttributes();
        actualUri = attrs.getApplicationUri(request) + uri.toString();
    } else if (url != null) {
        actualUri = url;
    } else {
        if (argMap.get(ARGUMENT_ACTION) == null || argMap.get(ARGUMENT_CONTROLLER) == null) {
            throw new CannotRedirectException(
                    "redirect required attribute [controller] or attribute [action] is missing");
        }
        String actionName = argMap.get(ARGUMENT_ACTION).toString();
        String controllerName = argMap.get(ARGUMENT_CONTROLLER).toString();
        controllerName = controllerName != null ? controllerName : webRequest.getControllerName();

        Map params = (Map) argMap.get(ARGUMENT_PARAMS);
        if (params == null) {
            params = new HashMap();
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("Dynamic method [redirect] looking up URL mapping for controller [" + controllerName
                    + "] and action [" + actionName + "] and params [" + params + "] with [" + urlMappingsHolder
                    + "]");
        }

        Object id = argMap.get(ARGUMENT_ID);
        try {
            if (id != null) {
                params.put(ARGUMENT_ID, id);
            }

            UrlCreator urlMapping = urlMappingsHolder.getReverseMapping(controllerName, actionName, params);
            if (urlMapping == null && LOG.isDebugEnabled()) {
                LOG.debug("Dynamic method [redirect] no URL mapping found for params [" + params + "]");
            }

            String frag = argMap.get(ARGUMENT_FRAGMENT) != null ? argMap.get(ARGUMENT_FRAGMENT).toString()
                    : null;
            actualUri = urlMapping.createURL(controllerName, actionName, params, request.getCharacterEncoding(),
                    frag);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Dynamic method [redirect] mapped to URL [" + actualUri + "]");
            }
        } finally {
            if (id != null) {
                params.remove(ARGUMENT_ID);
            }
        }
    }

    return redirectResponse(actualUri, request, response);
}

From source file:net.unicon.academus.apps.download.DownloadServiceServlet.java

private void renderErrorMsg(HttpServletResponse res, String errorMsg) {

    // Check if response is not already committed before
    // writing out the error page

    if (!res.isCommitted()) {

        StringBuffer errorPage = new StringBuffer(256);

        errorPage.append("<html>");
        errorPage.append("<head><title>");
        errorPage.append("Download failure");
        errorPage.append("</title></head>");
        errorPage.append("<body>");
        errorPage.append(errorMsg);//from   w w  w.j av a  2s .com
        errorPage.append("</body>");
        errorPage.append("</html>");

        try {
            res.getWriter().write(errorPage.toString());
        } catch (IOException ioe) {
            log.error("Failed to render error page", ioe);
        }
    }
}

From source file:org.codehaus.groovy.grails.web.metaclass.RedirectDynamicMethod.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from  w w  w . j a v  a 2s .co  m
public Object invoke(Object target, String methodName, Object[] arguments) {
    if (arguments.length == 0) {
        throw new MissingMethodException(METHOD_SIGNATURE, target.getClass(), arguments);
    }

    Map argMap = arguments[0] instanceof Map ? (Map) arguments[0] : Collections.EMPTY_MAP;
    if (argMap.isEmpty()) {
        throw new MissingMethodException(METHOD_SIGNATURE, target.getClass(), arguments);
    }

    GrailsWebRequest webRequest = (GrailsWebRequest) RequestContextHolder.currentRequestAttributes();
    LinkGenerator requestLinkGenerator = getLinkGenerator(webRequest);

    HttpServletRequest request = webRequest.getCurrentRequest();
    if (request.getAttribute(GRAILS_REDIRECT_ISSUED) != null) {
        throw new CannotRedirectException(
                "Cannot issue a redirect(..) here. A previous call to redirect(..) has already redirected the response.");
    }

    HttpServletResponse response = webRequest.getCurrentResponse();
    if (response.isCommitted()) {
        throw new CannotRedirectException(
                "Cannot issue a redirect(..) here. The response has already been committed either by another redirect or by directly writing to the response.");
    }

    if (target instanceof GroovyObject) {
        GroovyObject controller = (GroovyObject) target;

        // if there are errors add it to the list of errors
        Errors controllerErrors = (Errors) controller.getProperty(ControllerDynamicMethods.ERRORS_PROPERTY);
        Errors errors = (Errors) argMap.get(ARGUMENT_ERRORS);
        if (controllerErrors != null && errors != null) {
            controllerErrors.addAllErrors(errors);
        } else {
            controller.setProperty(ControllerDynamicMethods.ERRORS_PROPERTY, errors);
        }
        Object action = argMap.get(GrailsControllerClass.ACTION);
        if (action != null) {
            argMap.put(GrailsControllerClass.ACTION, establishActionName(action, controller));
        }
        if (!argMap.containsKey(GrailsControllerClass.NAMESPACE_PROPERTY)) {
            // this could be made more efficient if we had a reference to the GrailsControllerClass object, which
            // has the namespace property accessible without needing reflection
            argMap.put(GrailsControllerClass.NAMESPACE_PROPERTY, GrailsClassUtils
                    .getStaticFieldValue(controller.getClass(), GrailsControllerClass.NAMESPACE_PROPERTY));
        }
    }
    boolean permanent = DefaultGroovyMethods.asBoolean(argMap.get(ARGUMENT_PERMANENT));

    // we generate a relative link with no context path so that the absolute can be calculated by combining the serverBaseURL
    // which includes the contextPath
    argMap.put(LinkGenerator.ATTRIBUTE_CONTEXT_PATH, BLANK);

    return redirectResponse(requestLinkGenerator.getServerBaseURL(), requestLinkGenerator.link(argMap), request,
            response, permanent);
}

From source file:de.steilerdev.myVerein.server.security.rest.RestLogoutSuccessHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    User currentUser = (User) authentication.getPrincipal();
    logger.info("[{}] Successfully logged user out from IP {}", currentUser,
            SecurityHelper.getClientIpAddr(request));
    if (!response.isCommitted()) {
        response.sendError(HttpServletResponse.SC_OK, "Successfully logged out");
    }//from   w  w  w .  ja va2  s. c  om
}

From source file:org.jahia.bin.errors.ErrorServlet.java

protected void process(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (response.isCommitted()) {
        logger.warn("Response is already committed. Skipping error processing.");
        return;/*  w  w  w.j  a  v  a 2s. co m*/
    }

    int errorCode = getErrorCode(request);

    if (errorCode == HttpServletResponse.SC_UNAUTHORIZED) {
        String redirectUrl = LoginConfig.getInstance().getCustomLoginUrl(request);
        if (redirectUrl != null) {
            response.sendRedirect(response.encodeRedirectURL(redirectUrl));
            return;
        }
    }

    response.setStatus(errorCode);
    response.setContentType("text/html; charset=" + SettingsBean.getInstance().getCharacterEncoding());
    response.resetBuffer();

    String errorPagePath = getErrorPagePath(request, response);
    forwardToErrorPage(errorPagePath, request, response);
}

From source file:com.parakhcomputer.web.servlet.view.tiles2.DynamicTilesViewProcessor.java

/**
 * Renders output using Tiles./*  w ww  . j  av  a  2 s  . com*/
 */
protected void renderMergedOutputModel(String beanName, String url, ServletContext servletContext,
        HttpServletRequest request, HttpServletResponse response, TilesContainer container) throws Exception {
    JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext));

    if (!response.isCommitted()) {
        // Tiles is going to use a forward, but some web containers (e.g.
        // OC4J 10.1.3)
        // do not properly expose the Servlet 2.4 forward request
        // attributes... However,
        // must not do this on Servlet 2.5 or above, mainly for GlassFish
        // compatibility.
        if (servletContext.getMajorVersion() == 2 && servletContext.getMinorVersion() < 5) {
            WebUtils.exposeForwardRequestAttributes(request);
        }
    }

    String definitionName = startDynamicDefinition(beanName, url, request, response, container);

    container.render(definitionName, request, response);

    endDynamicDefinition(definitionName, beanName, request, response, container);
}

From source file:nl.tue.gale.ae.LoginServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    LoginManager login = (LoginManager) getApplicationContext().getBean("loginManager");
    ProcessorManager pm = (ProcessorManager) getApplicationContext().getBean("processorManager");
    login.doLoginPage(pm.createResource(req, resp));
    if (!resp.isCommitted())
        resp.sendError(501);/*  w w w .  j  av a2 s  .  c o m*/
}