Example usage for javax.servlet ServletResponse resetBuffer

List of usage examples for javax.servlet ServletResponse resetBuffer

Introduction

In this page you can find the example usage for javax.servlet ServletResponse 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:net.lightbody.bmp.proxy.jetty.jetty.servlet.Dispatcher.java

void dispatch(ServletRequest servletRequest, ServletResponse servletResponse, int type)
        throws ServletException, IOException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

    HttpConnection httpConnection = _servletHandler.getHttpContext().getHttpConnection();
    ServletHttpRequest servletHttpRequest = (ServletHttpRequest) httpConnection.getRequest().getWrapper();

    // wrap the request and response
    DispatcherRequest request = new DispatcherRequest(httpServletRequest, servletHttpRequest, type);
    DispatcherResponse response = new DispatcherResponse(request, httpServletResponse);

    if (type == Dispatcher.__FORWARD)
        servletResponse.resetBuffer();

    // Merge parameters
    String query = _query;//  w ww .ja  v a 2  s  . co m
    MultiMap parameters = null;
    if (query != null) {
        Map old_params = httpServletRequest.getParameterMap();

        // Add the parameters
        parameters = new MultiMap();
        UrlEncoded.decodeTo(query, parameters, request.getCharacterEncoding());

        if (old_params != null && old_params.size() > 0) {
            // Merge old parameters.
            Iterator iter = old_params.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String name = (String) entry.getKey();
                String[] values = (String[]) entry.getValue();
                for (int i = 0; i < values.length; i++)
                    parameters.add(name, values[i]);
            }
        }

        request.setParameters(parameters);

        String old_query = httpServletRequest.getQueryString();
        if (old_query != null)
            request.setQuery(query + "&" + old_query);
        else
            request.setQuery(query);
    }

    Object old_scope = null;
    try {
        if (request.crossContext()) {
            // Setup new _context
            old_scope = _servletHandler.getHttpContext().enterContextScope(httpConnection.getRequest(),
                    httpConnection.getResponse());
        }

        if (isNamed()) {
            // No further modifications required.
            if (_servletHandler instanceof WebApplicationHandler) {
                JSR154Filter filter = ((WebApplicationHandler) _servletHandler).getJsr154Filter();
                if (filter != null && filter.isUnwrappedDispatchSupported()) {
                    filter.setDispatch(request, response);
                    _servletHandler.dispatch(null, httpServletRequest, httpServletResponse, _holder, type);
                } else
                    _servletHandler.dispatch(null, request, response, _holder, type);
            } else
                _servletHandler.dispatch(null, request, response, _holder, type);
        } else {
            // Adjust servlet paths
            request.setPaths(_servletHandler.getHttpContext().getContextPath(),
                    PathMap.pathMatch(_pathSpec, _pathInContext), PathMap.pathInfo(_pathSpec, _pathInContext));

            // are we wrap over or wrap under
            if (_servletHandler instanceof WebApplicationHandler) {
                JSR154Filter filter = ((WebApplicationHandler) _servletHandler).getJsr154Filter();
                if (filter != null && filter.isUnwrappedDispatchSupported()) {
                    filter.setDispatch(request, response);
                    _servletHandler.dispatch(_pathInContext, httpServletRequest, httpServletResponse, _holder,
                            type);
                } else
                    _servletHandler.dispatch(_pathInContext, request, response, _holder, type);
            } else
                _servletHandler.dispatch(_pathInContext, request, response, _holder, type);

            if (type != Dispatcher.__INCLUDE)
                response.close();
            else if (response.isFlushNeeded())
                response.flushBuffer();
        }
    } finally {
        // restore _context
        if (request.crossContext())
            _servletHandler.getHttpContext().leaveContextScope(httpConnection.getRequest(),
                    httpConnection.getResponse(), old_scope);
    }
}

From source file:org.apache.catalina.core.ApplicationDispatcher.java

private void doForward(ServletRequest request, ServletResponse response) throws ServletException, IOException {

    // Reset any output that has been buffered, but keep headers/cookies
    if (response.isCommitted()) {
        if (log.isDebugEnabled())
            log.debug("  Forward on committed response --> ISE");
        throw new IllegalStateException(sm.getString("applicationDispatcher.forward.ise"));
    }/*from  w  w w.  j  av  a 2 s .c  o  m*/
    try {
        response.resetBuffer();
    } catch (IllegalStateException e) {
        if (log.isDebugEnabled())
            log.debug("  Forward resetBuffer() returned ISE: " + e);
        throw e;
    }

    // Set up to handle the specified request and response
    setup(request, response, false);

    // Identify the HTTP-specific request and response objects (if any)
    HttpServletRequest hrequest = null;
    if (request instanceof HttpServletRequest)
        hrequest = (HttpServletRequest) request;
    HttpServletResponse hresponse = null;
    if (response instanceof HttpServletResponse)
        hresponse = (HttpServletResponse) response;

    // Handle a non-HTTP forward by passing the existing request/response
    if ((hrequest == null) || (hresponse == null)) {

        if (log.isDebugEnabled())
            log.debug(" Non-HTTP Forward");

        processRequest(hrequest, hresponse);

    }

    // Handle an HTTP named dispatcher forward
    else if ((servletPath == null) && (pathInfo == null)) {

        if (log.isDebugEnabled())
            log.debug(" Named Dispatcher Forward");

        processRequest(request, response);

    }

    // Handle an HTTP path-based forward
    else {

        if (log.isDebugEnabled())
            log.debug(" Path Based Forward");

        ApplicationHttpRequest wrequest = (ApplicationHttpRequest) wrapRequest();
        String contextPath = context.getPath();
        wrequest.setContextPath(contextPath);
        wrequest.setRequestURI(requestURI);
        wrequest.setServletPath(servletPath);
        wrequest.setPathInfo(pathInfo);

        wrequest.setAttribute(Globals.FORWARD_REQUEST_URI_ATTR, hrequest.getRequestURI());
        wrequest.setAttribute(Globals.FORWARD_CONTEXT_PATH_ATTR, hrequest.getContextPath());
        wrequest.setAttribute(Globals.FORWARD_SERVLET_PATH_ATTR, hrequest.getServletPath());
        wrequest.setAttribute(Globals.FORWARD_PATH_INFO_ATTR, hrequest.getPathInfo());
        wrequest.setAttribute(Globals.FORWARD_QUERY_STRING_ATTR, hrequest.getQueryString());

        if (queryString != null) {
            wrequest.setQueryString(queryString);
            wrequest.setQueryParams(queryString);
        }

        processRequest(request, response);
        unwrapRequest();

    }

    // This is not a real close in order to support error processing
    if (log.isDebugEnabled())
        log.debug(" Disabling the response for futher output");

    if (response instanceof ResponseFacade) {
        ((ResponseFacade) response).finish();
    } else {
        // Servlet SRV.6.2.2. The Resquest/Response may have been wrapped
        // and may no longer be instance of RequestFacade 
        if (log.isDebugEnabled()) {
            log.debug(" The Response is vehiculed using a wrapper: " + response.getClass().getName());
        }

        // Close anyway
        try {
            PrintWriter writer = response.getWriter();
            writer.close();
        } catch (IllegalStateException e) {
            try {
                ServletOutputStream stream = response.getOutputStream();
                stream.close();
            } catch (IllegalStateException f) {
                ;
            } catch (IOException f) {
                ;
            }
        } catch (IOException e) {
            ;
        }
    }

}