Java Code Examples for javax.servlet.ServletResponse.reset()

The following are Jave code examples for showing how to use reset() of the javax.servlet.ServletResponse class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: simpleauth   File: SimpleAuthFilter.java   Source Code and License Vote up 6 votes
@Override
public void doFilter(final ServletRequest request, final ServletResponse response,
		final FilterChain chain) throws IOException, ServletException {
	if (request instanceof HttpServletRequest) {
		final HttpServletRequest req = ((HttpServletRequest) request);
		final HttpServletResponse res = ((HttpServletResponse) response);
		final String header = req.getHeader(SimpleAuth.HTTP_HEADER);
		if (header != null) {
			final String[] sp = header.split(" ");
			// Authentication: torch TOKEN
			if (sp.length == 2) {
				final String scheme = sp[0], param = sp[1];
				if (SimpleAuth.SCHEME.equals(scheme)) {
					if (sa.verify(param)) {
						chain.doFilter(request, response);
						return;
					}
				}
			}
		}
		if (!res.isCommitted()) {
			final String err = "FORBIDDEN\r\n";
			res.reset();
			res.setStatus(HttpServletResponse.SC_FORBIDDEN);
			res.setContentLength(err.length());
			res.setContentType("text/plain");
			res.setCharacterEncoding("ISO-8859-1");
			res.setHeader("Pragma", "no-cache");
			res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
			res.setHeader(getClass().getSimpleName(), "deny");
			res.getWriter().print(err);
			return;
		}
	}
	if (!response.isCommitted()) {
		response.reset();
	}
	throw new ServletException(new UnsupportedOperationException());
}
 
Example 2
Project: lams   File: ServletInitialHandler.java   Source Code and License Vote up 4 votes
public void handleFirstRequest(final HttpServerExchange exchange, final ServletChain servletChain, final ServletRequestContext servletRequestContext, final ServletRequest request, final ServletResponse response) throws Exception {

        ThreadSetupAction.Handle handle = setupAction.setup(exchange);
        try {
            servletRequestContext.setRunningInsideHandler(true);
            try {
                listeners.requestInitialized(request);
                next.handleRequest(exchange);
                //
                if(servletRequestContext.getErrorCode() > 0) {
                    servletRequestContext.getOriginalResponse().doErrorDispatch(servletRequestContext.getErrorCode(), servletRequestContext.getErrorMessage());
                }
            } catch (Throwable t) {

                //by default this will just log the exception
                boolean handled = exceptionHandler.handleThrowable(exchange, request, response, t);

                if(handled) {
                    exchange.endExchange();
                } else if (request.isAsyncStarted() || request.getDispatcherType() == DispatcherType.ASYNC) {
                    exchange.unDispatch();
                    servletRequestContext.getOriginalRequest().getAsyncContextInternal().handleError(t);
                } else {
                    if (!exchange.isResponseStarted()) {
                        response.reset();                       //reset the response
                        exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
                        exchange.getResponseHeaders().clear();
                        String location = servletContext.getDeployment().getErrorPages().getErrorLocation(t);
                        if (location == null) {
                            location = servletContext.getDeployment().getErrorPages().getErrorLocation(StatusCodes.INTERNAL_SERVER_ERROR);
                        }
                        if (location != null) {
                            RequestDispatcherImpl dispatcher = new RequestDispatcherImpl(location, servletContext);
                            try {
                                dispatcher.error(servletRequestContext, request, response, servletChain.getManagedServlet().getServletInfo().getName(), t);
                            } catch (Exception e) {
                                UndertowLogger.REQUEST_LOGGER.exceptionGeneratingErrorPage(e, location);
                            }
                        } else {
                            if (servletRequestContext.displayStackTraces()) {
                                ServletDebugPageHandler.handleRequest(exchange, servletRequestContext, t);
                            } else {
                                servletRequestContext.getOriginalResponse().doErrorDispatch(StatusCodes.INTERNAL_SERVER_ERROR, StatusCodes.INTERNAL_SERVER_ERROR_STRING);
                            }
                        }
                    }
                }

            } finally {
                servletRequestContext.setRunningInsideHandler(false);
                listeners.requestDestroyed(request);
            }
            //if it is not dispatched and is not a mock request
            if (!exchange.isDispatched() && !(exchange.getConnection() instanceof MockServerConnection)) {
                servletRequestContext.getOriginalResponse().responseDone();
                servletRequestContext.getOriginalRequest().clearAttributes();
            }
            if(!exchange.isDispatched()) {
                AsyncContextImpl ctx = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
                if(ctx != null) {
                    ctx.complete();
                }
            }
        } finally {
            handle.tearDown();
        }
    }
 
Example 3
Project: lams   File: RequestDispatcherImpl.java   Source Code and License Vote up 4 votes
public void forward( ServletRequest request, ServletResponse response ) throws ServletException, IOException {
    response.reset();
    _servletMetaData.getServlet().service( DispatchedRequestWrapper.createForwardRequestWrapper( (HttpServletRequest) request, this ), response );
}
 
Example 4
Project: jerrydog   File: ErrorReportValve.java   Source Code and License Vote up 4 votes
/**
 * Invoke the next Valve in the sequence. When the invoke returns, check 
 * the response state, and output an error report is necessary.
 *
 * @param request The servlet request to be processed
 * @param response The servlet response to be created
 * @param context The valve context used to invoke the next valve
 *  in the current processing pipeline
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
public void invoke(Request request, Response response,
                   ValveContext context)
    throws IOException, ServletException {

    // Perform the request
    context.invokeNext(request, response);

    ServletRequest sreq = (ServletRequest) request;
    Throwable throwable = 
        (Throwable) sreq.getAttribute(Globals.EXCEPTION_ATTR);

    ServletResponse sresp = (ServletResponse) response;
    if (sresp.isCommitted()) {
        return;
    }

    if (throwable != null) {

        // The response is an error
        response.setError();

        // Reset the response (if possible)
        try {
            sresp.reset();
        } catch (IllegalStateException e) {
            ;
        }

        ServletResponse sresponse = (ServletResponse) response;
        if (sresponse instanceof HttpServletResponse)
            ((HttpServletResponse) sresponse).sendError
                (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

    }

    response.setSuspended(false);

    try {
        report(request, response, throwable);
    } catch (Throwable tt) {
        tt.printStackTrace();
    }

}
 
Example 5
Project: parabuild-ci   File: RequestDispatcherImpl.java   Source Code and License Vote up 4 votes
public void forward( ServletRequest request, ServletResponse response ) throws ServletException, IOException {
    response.reset();
    _servletMetaData.getServlet().service( DispatchedRequestWrapper.createForwardRequestWrapper( (HttpServletRequest) request, this ), response );
}