Search in sources :

Example 1 with QuietServletException

use of org.eclipse.jetty.server.QuietServletException in project jetty.project by eclipse.

the class AsyncListenerTest method test_StartAsync_Throw_OnError.

private void test_StartAsync_Throw_OnError(IOConsumer<AsyncEvent> consumer) throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/ctx");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            asyncContext.addListener(new AsyncListenerAdapter() {

                @Override
                public void onError(AsyncEvent event) throws IOException {
                    consumer.accept(event);
                }
            });
            throw new QuietServletException(new TestRuntimeException());
        }
    }), "/path/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(HttpStatus.OK_200);
        }
    }), "/dispatch/*");
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().print("CUSTOM");
        }
    }), "/error/*");
    startServer(context);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) QuietServletException(org.eclipse.jetty.server.QuietServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent)

Aggregations

IOException (java.io.IOException)1 AsyncContext (javax.servlet.AsyncContext)1 AsyncEvent (javax.servlet.AsyncEvent)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 QuietServletException (org.eclipse.jetty.server.QuietServletException)1