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.pentaho.platform.web.http.api.resources.XactionUtil.java

public static String postExecute(IRuntimeContext runtime, boolean debugMessages, boolean doWrapper,
        IOutputHandler outputHandler, Map<String, IParameterProvider> parameterProviders,
        HttpServletRequest request, HttpServletResponse response, List<?> messages) throws Exception {
    StringBuffer buffer = new StringBuffer();

    boolean hasResponse = outputHandler.isResponseExpected();
    IContentItem responseContentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE,
            IOutputHandler.CONTENT, null, null);

    boolean success = (runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS);
    boolean printSuccess = (runtime != null) && success && (!hasResponse || debugMessages);
    boolean printError = (runtime != null) && !success && !response.isCommitted();

    if (printSuccess || printError) {
        final String htmlMimeType = "text/html"; //$NON-NLS-1$
        responseContentItem.setMimeType(htmlMimeType);
        response.setContentType(htmlMimeType);
        IMessageFormatter formatter = PentahoSystem.get(IMessageFormatter.class,
                PentahoSessionHolder.getSession());

        if (printSuccess) {
            formatter.formatSuccessMessage(htmlMimeType, runtime, buffer, debugMessages, doWrapper);
        } else {// w  w  w . j  a  va2  s .  com
            response.resetBuffer();
            formatter.formatFailureMessage(htmlMimeType, runtime, buffer, messages);
        }
    }
    return buffer.toString();
}

From source file:org.pentaho.platform.web.servlet.ViewAction.java

@SuppressWarnings("unchecked")
protected void handleActionRequest(final HttpServletRequest request, final HttpServletResponse response,
        final IOutputHandler outputHandler, final HttpServletRequestHandler requestHandler,
        OutputStream outputStream, final IContentItem contentItem) throws ServletException, IOException {
    IRuntimeContext runtime = null;//  w w  w .j a v  a  2  s  .  c  om
    try {
        runtime = requestHandler.handleActionRequest(0, 0);

        if (runtime == null) {
            StringBuffer buffer = new StringBuffer();
            for (String message : (List<String>) requestHandler.getMessages()) {
                buffer.append(message);
            }
            outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
            return;
        }

        /*
         * the flag "hasResponse" should be set if the outputHandler is expected to serve a response back via either the
         * "response.content" output (a final content output), or an intermediate response such as a form to request
         * parameters such as from a SecureFilterComponent.
         */
        boolean hasResponse = outputHandler.isResponseExpected();
        IContentItem responseContentItem = outputHandler.getOutputContentItem(IOutputHandler.RESPONSE,
                IOutputHandler.CONTENT, null, null);

        boolean success = (runtime != null && runtime.getStatus() == IRuntimeContext.RUNTIME_STATUS_SUCCESS);
        boolean debugMessages = doMessages(request);
        boolean printSuccess = (runtime != null) && success && (!hasResponse || debugMessages);
        boolean printError = (runtime != null) && !success && !response.isCommitted();

        if (printSuccess || printError) {
            final String htmlMimeType = "text/html"; //$NON-NLS-1$
            responseContentItem.setMimeType(htmlMimeType);
            // this is going to be the response output stream unless you are in debug mode
            outputStream = responseContentItem.getOutputStream(null);

            response.setContentType(htmlMimeType);
            StringBuffer buffer = new StringBuffer();

            IMessageFormatter formatter = PentahoSystem.get(IMessageFormatter.class,
                    PentahoSessionHolder.getSession());

            if (printSuccess) {
                boolean doWrapper = !("false".equals(request.getParameter("wrapper"))); //$NON-NLS-1$ //$NON-NLS-2$
                formatter.formatSuccessMessage(htmlMimeType, runtime, buffer, debugMessages, doWrapper);
            } else {
                response.resetBuffer();
                formatter.formatFailureMessage(htmlMimeType, runtime, buffer, requestHandler.getMessages());
            }

            outputStream.write(buffer.toString().getBytes(LocaleHelper.getSystemEncoding()));
            responseContentItem.closeOutputStream();
        }
    } finally {
        if (runtime != null) {
            runtime.dispose();
        }
    }
}