Example usage for javax.servlet.http HttpServletResponse resetBuffer

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

Introduction

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

Prototype


public void resetBuffer();

Source Link

Document

Clears the content of the underlying buffer in the response without clearing headers or status code.

Usage

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;//from ww  w  .  ja  va 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:org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisAtomPubServlet.java

@Override
protected void printError(Exception ex, HttpServletResponse response) {
    ErrorInfo errorInfo = extractError(ex);
    if (response.isCommitted()) {
        LOG.warn("Failed to send error message to client. " + "Response is already committed.", ex);
        return;/*w w w .  j av a 2 s.com*/
    }

    try {
        response.resetBuffer();
        response.setStatus(errorInfo.statusCode);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        PrintWriter pw = response.getWriter();

        pw.print("<html><head><title>Apache Chemistry OpenCMIS - " + errorInfo.exceptionName + " error</title>"
                + "<style><!--H1 {font-size:24px;line-height:normal;font-weight:bold;background-color:#f0f0f0;color:#003366;border-bottom:1px solid #3c78b5;padding:2px;} "
                + "BODY {font-family:Verdana,arial,sans-serif;color:black;font-size:14px;} "
                + "HR {color:#3c78b5;height:1px;}--></style></head><body>");
        pw.print("<h1>HTTP Status " + errorInfo.statusCode + " - <!--exception-->" + errorInfo.exceptionName
                + "<!--/exception--></h1>");
        pw.print("<p><!--message-->" + StringEscapeUtils.escapeHtml(errorInfo.message) + "<!--/message--></p>");

        String st = ExceptionHelper.getStacktraceAsString(ex);
        if (st != null) {
            pw.print("<hr noshade='noshade'/><!--stacktrace--><pre>\n" + st
                    + "\n</pre><!--/stacktrace--><hr noshade='noshade'/>");
        }

        pw.print("</body></html>");
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        try {
            response.sendError(errorInfo.statusCode, errorInfo.message);
        } catch (Exception en) {
            // there is nothing else we can do
        }
    }
}

From source file:org.nuxeo.ecm.core.opencmis.bindings.NuxeoCmisBrowserBindingServlet.java

@Override
public void printError(CallContext context, Exception ex, HttpServletRequest request,
        HttpServletResponse response) {
    ErrorInfo errorInfo = extractError(ex);
    if (response.isCommitted()) {
        LOG.warn("Failed to send error message to client. " + "Response is already committed.", ex);
        return;// w w w  .j av  a 2 s.  c o m
    }

    String token = (context instanceof BrowserCallContextImpl ? ((BrowserCallContextImpl) context).getToken()
            : null);

    if (token == null) {
        response.resetBuffer();
        CALL.setStatus(request, response, errorInfo.statusCode);

        String message = ex.getMessage();
        if (!(ex instanceof CmisBaseException)) {
            message = "An error occurred!";
        }

        JSONObject jsonResponse = new JSONObject();
        jsonResponse.put(ERROR_EXCEPTION, errorInfo.exceptionName);
        jsonResponse.put(ERROR_MESSAGE, errorInfo.message);

        String st = ExceptionHelper.getStacktraceAsString(ex);
        if (st != null) {
            jsonResponse.put(ERROR_STACKTRACE, st);
        }

        try {
            CALL.writeJSON(jsonResponse, request, response);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            try {
                response.sendError(errorInfo.statusCode, message);
            } catch (Exception en) {
                // there is nothing else we can do
            }
        }
    } else {
        CALL.setStatus(request, response, SC_OK);
        response.setContentType(AbstractBrowserServiceCall.HTML_MIME_TYPE);
        response.setContentLength(0);

        if (context != null) {
            CALL.setCookie(request, response, context.getRepositoryId(), token, CALL
                    .createCookieValue(errorInfo.statusCode, null, errorInfo.exceptionName, ex.getMessage()));
        }
    }
}

From source file:org.nuxeo.ecm.platform.io.client.ImportExportActionBean.java

private static void handleRedirect(HttpServletResponse response, String url) throws IOException {
    response.resetBuffer();
    response.sendRedirect(url);/*  ww  w  .j a  v  a  2 s  .co m*/
    response.flushBuffer();
    getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, true);
    FacesContext.getCurrentInstance().responseComplete();
}

From source file:org.nuxeo.ecm.platform.ui.web.auth.cas2.SecurityExceptionHandler.java

@Override
public void handleException(HttpServletRequest request, HttpServletResponse response, Throwable t)
        throws IOException, ServletException {

    Throwable unwrappedException = unwrapException(t);

    if (!ExceptionHelper.isSecurityError(unwrappedException)
            && !response.containsHeader(SSO_INITIAL_URL_REQUEST_KEY)) {
        super.handleException(request, response, t);
        return;/*from   w  ww.j av a  2 s .c om*/
    }

    Principal principal = request.getUserPrincipal();
    NuxeoPrincipal nuxeoPrincipal = null;
    if (principal instanceof NuxeoPrincipal) {
        nuxeoPrincipal = (NuxeoPrincipal) principal;
        // redirect to login than to requested page
        if (nuxeoPrincipal.isAnonymous()) {
            response.resetBuffer();

            String urlToReach = getURLToReach(request);
            Cookie cookieUrlToReach = new Cookie(NXAuthConstants.SSO_INITIAL_URL_REQUEST_KEY, urlToReach);
            cookieUrlToReach.setPath("/");
            cookieUrlToReach.setMaxAge(60);
            response.addCookie(cookieUrlToReach);

            if (!response.isCommitted()) {
                request.getRequestDispatcher(CAS_REDIRECTION_URL).forward(request, response);
            }
            FacesContext.getCurrentInstance().responseComplete();
        }
    }
    // go back to default handler
    super.handleException(request, response, t);
}

From source file:org.nuxeo.ecm.platform.ui.web.rest.RestHelper.java

public static void handleRedirect(HttpServletResponse response, String url) throws IOException {
    response.resetBuffer();
    response.sendRedirect(url);/* w  w w . j a  va 2s .  c o  m*/
    response.flushBuffer();
    getHttpServletRequest().setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, Boolean.TRUE);
    FacesContext.getCurrentInstance().responseComplete();
}

From source file:org.nuxeo.ecm.webapp.seam.NuxeoSeamHotReloader.java

/**
 * Triggers a full reload of Seam context and components.
 * <p>//from w w  w  . java  2  s .c o  m
 * Needs the Seam debug jar to be present and Seam debug mode to be enabled.
 */
public String doReload() {
    final FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext == null) {
        return null;
    }

    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();

    String url = BaseURL.getBaseURL(request);
    url += "restAPI/seamReload";

    try {
        response.resetBuffer();
        response.sendRedirect(url);
        response.flushBuffer();
        request.setAttribute(NXAuthConstants.DISABLE_REDIRECT_REQUEST_KEY, Boolean.TRUE);
        facesContext.responseComplete();
    } catch (IOException e) {
        log.error("Error during redirect", e);
    }
    return null;
}

From source file:org.nuxeo.io.NuxeoIOStartupHelper.java

public void redirect(String url) {
    HttpServletResponse response = getHttpServletResponse();
    try {//w w  w . j a v  a  2  s. c o m
        response.resetBuffer();
        response.sendRedirect(url);
        response.flushBuffer();

        FacesContext.getCurrentInstance().responseComplete();
    } catch (IOException e) {
        log.warn(e);
        log.debug(e, e);
    }
}

From source file:org.openlegacy.db.mvc.rest.DefaultDbRestController.java

private static void sendError(int errorCode, String message, HttpServletResponse response) throws IOException {
    response.resetBuffer();
    response.setStatus(errorCode);/*  ww w. ja va 2s .  c  om*/
    response.setHeader("Content-Type", "application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(String.format("{\"error\":\"%s\"}", message));
    response.flushBuffer();
}

From source file:org.openlegacy.mvc.AbstractRestController.java

public Object login(String user, String password, HttpServletResponse response)
        throws IOException, LoginException {
    if (!enableLogin || !enableGetLogin) {
        throw (new UnsupportedOperationException("/login is not support"));
    }//from  w w w. ja v a 2s .c  o m

    try {
        org.openlegacy.modules.login.Login loginModule = getSession()
                .getModule(org.openlegacy.modules.login.Login.class);
        if (loginModule != null) {
            loginModule.login(user, password);
        } else {
            logger.warn("No login module defined. Skipping login");
        }
    } catch (RegistryException e) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    } catch (LoginException e) {
        getSession().disconnect();
        response.resetBuffer();
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.setContentType("text/plain; charset=UTF-8");
        response.getOutputStream().print(e.getMessage());
        throw e;
    }
    response.setStatus(HttpServletResponse.SC_OK);
    return getMenu(response);

}