Java Code Examples for javax.servlet.AsyncContext#dispatch()

The following examples show how to use javax.servlet.AsyncContext#dispatch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestAsyncContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("AsyncErrorPageGet-");

    final AsyncContext ctxt = req.getAsyncContext();

    switch(mode) {
        case COMPLETE:
            TestAsyncContextImpl.track("Complete-");
            ctxt.complete();
            break;
        case DISPATCH:
            TestAsyncContextImpl.track("Dispatch-");
            ctxt.dispatch("/error/nonasync");
            break;
        case NO_COMPLETE:
            TestAsyncContextImpl.track("NoOp-");
            break;
        default:
            // Impossible
            break;
    }
}
 
Example 2
Source File: TestApplicationContextGetRequestDispatcher.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    AsyncContext ac = req.startAsync();
    // Quick and dirty. Sufficient for this test but ignores lots of
    // edge cases.
    String target = null;
    if (dispatchPath != null) {
        target = req.getServletPath();
        int lastSlash = target.lastIndexOf('/');
        target = target.substring(0, lastSlash + 1);
        if (encodePath) {
            target = URLEncoder.DEFAULT.encode(target, StandardCharsets.UTF_8);
        }
        target += dispatchPath;
    }
    try {
        ac.dispatch(target);
    } catch (UnsupportedOperationException uoe) {
        ac.complete();
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().print(NULL);
    }
}
 
Example 3
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String key = req.getParameter("key");
    if (key == null) {
        key = DEFAULT_KEY;
    }

    AsyncContext ac = (AsyncContext) req.getServletContext().getAttribute(key);
    if (ac == null) {
        TestAsyncContextImpl.track("FAIL:nullAsyncContext-");
    } else {
        TestAsyncContextImpl.track("AsyncRetrieveServletGet-");
        ac.dispatch("/target");
    }
}
 
Example 4
Source File: AwsAsyncContextTest.java    From aws-serverless-java-container with Apache License 2.0 6 votes vote down vote up
@Test
public void dispatch_sendsToCorrectServlet() {
    AwsProxyHttpServletRequest req = new AwsProxyHttpServletRequest(new AwsProxyRequestBuilder("/srv1/hello", "GET").build(), lambdaCtx, null);
    req.setResponse(handler.getContainerResponse(req, new CountDownLatch(1)));
    req.setServletContext(ctx);
    req.setContainerHandler(handler);

    AsyncContext asyncCtx = req.startAsync();
    handler.setDesiredStatus(201);
    asyncCtx.dispatch();
    assertNotNull(handler.getSelectedServlet());
    assertEquals(srv1, handler.getSelectedServlet());
    assertEquals(201, handler.getResponse().getStatus());

    req = new AwsProxyHttpServletRequest(new AwsProxyRequestBuilder("/srv5/hello", "GET").build(), lambdaCtx, null);
    req.setResponse(handler.getContainerResponse(req, new CountDownLatch(1)));
    req.setServletContext(ctx);
    req.setContainerHandler(handler);
    asyncCtx = req.startAsync();
    handler.setDesiredStatus(202);
    asyncCtx.dispatch();
    assertNotNull(handler.getSelectedServlet());
    assertEquals(srv2, handler.getSelectedServlet());
    assertEquals(202, handler.getResponse().getStatus());
}
 
Example 5
Source File: TestAsyncContextImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if ("y".equals(req.getParameter(DISPATCH_CHECK))) {
        if (req.getDispatcherType() != DispatcherType.ASYNC) {
            track("WrongDispatcherType-");
        }
    }
    track("DispatchingServletGet-");
    final int iter = Integer.parseInt(req.getParameter(ITER_PARAM)) - 1;
    final AsyncContext ctxt = req.startAsync();
    if (addTrackingListener) {
        TrackingListener listener =
            new TrackingListener(completeOnError, true, null);
        ctxt.addListener(listener);
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (iter > 0) {
                ctxt.dispatch("/stage1?" + ITER_PARAM + "=" + iter +
                        "&" + DISPATCH_CHECK + "=y");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 6
Source File: TestAsyncContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("DispatchingServletGet-");
    resp.flushBuffer();

    final boolean first = TrackingServlet.first;
    TrackingServlet.first = false;

    final AsyncContext ctxt = req.startAsync();
    TrackingListener listener = new TrackingListener(false, true, null);
    ctxt.addListener(listener);
    ctxt.setTimeout(3000);

    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (first) {
                ctxt.dispatch("/stage1");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 7
Source File: TestAsyncContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void service(ServletRequest req, ServletResponse resp)
        throws ServletException, IOException {
    if (DispatcherType.ASYNC != req.getDispatcherType()) {
        AsyncContext asyncContext;
        if ("y".equals(req.getParameter(CUSTOM_REQ_RESP))) {
            asyncContext = req.startAsync(
                    new ServletRequestWrapper(req),
                    new ServletResponseWrapper(resp));
        } else {
            asyncContext = req.startAsync();
        }
        if ("y".equals(req.getParameter(EMPTY_DISPATCH))) {
            asyncContext.dispatch();
        } else {
            asyncContext.dispatch("/target");
        }
        try {
            asyncContext.dispatch("/nonExistingServlet");
            TestAsyncContextImpl.track("FAIL");
        } catch (IllegalStateException e) {
            TestAsyncContextImpl.track("OK");
        }
    } else {
        TestAsyncContextImpl.track("DispatchingGenericServletGet-");
    }
}
 
Example 8
Source File: TestErrorReportValve.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // Only set the status on the first call (the dispatch will trigger
    // another call to this Servlet)
    if (resp.getStatus() != HttpServletResponse.SC_BAD_REQUEST) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        AsyncContext ac = req.startAsync();
        ac.dispatch();
    }
}
 
Example 9
Source File: TestErrorReportValve.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // Only set the status on the first call (the dispatch will trigger
    // another call to this Servlet)
    if (resp.getStatus() != HttpServletResponse.SC_BAD_REQUEST) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        AsyncContext ac = req.startAsync();
        ac.dispatch();
    }
}
 
Example 10
Source File: AwsAsyncContextTest.java    From aws-serverless-java-container with Apache License 2.0 5 votes vote down vote up
@Test
public void dispatchNewPath_sendsToCorrectServlet() throws InvalidRequestEventException {
    AwsProxyHttpServletRequest req = (AwsProxyHttpServletRequest) reader.readRequest(new AwsProxyRequestBuilder("/srv1/hello", "GET").build(), null, lambdaCtx, LambdaContainerHandler.getContainerConfig());
    req.setResponse(handler.getContainerResponse(req, new CountDownLatch(1)));
    req.setServletContext(ctx);
    req.setContainerHandler(handler);

    AsyncContext asyncCtx = req.startAsync();
    handler.setDesiredStatus(301);
    asyncCtx.dispatch("/srv4/hello");
    assertNotNull(handler.getSelectedServlet());
    assertEquals(srv2, handler.getSelectedServlet());
    assertNotNull(handler.getResponse());
    assertEquals(301, handler.getResponse().getStatus());
}
 
Example 11
Source File: TestAsyncContextImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("DispatchingServletGet-");
    resp.flushBuffer();

    final boolean first = TrackingServlet.first;
    TrackingServlet.first = false;

    final AsyncContext ctxt = req.startAsync();
    TrackingListener listener = new TrackingListener(false, true, null);
    ctxt.addListener(listener);
    ctxt.setTimeout(3000);

    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (first) {
                ctxt.dispatch("/stage1");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 12
Source File: TestAsyncContextImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    Integer countObj = (Integer) req.getAttribute("count");
    int count = 0;
    if (countObj != null) {
        count = countObj.intValue();
    }
    count++;
    req.setAttribute("count", Integer.valueOf(count));

    String encodedUri = req.getRequestURI();
    UDecoder uDecoder = new UDecoder();
    String decodedUri = uDecoder.convert(encodedUri, false);

    try {
        // Just here to trigger the error
        @SuppressWarnings("unused")
        URI u = new URI(encodedUri);
    } catch (URISyntaxException e) {
        throw new ServletException(e);
    }

    if (count > 3) {
        resp.setContentType("text/plain");
        resp.getWriter().print("OK");
    } else {
        AsyncContext ac = req.startAsync();
        ac.dispatch(decodedUri);
    }
}
 
Example 13
Source File: TestAsyncContextImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void service(ServletRequest req, ServletResponse resp)
        throws ServletException, IOException {
    if (DispatcherType.ASYNC != req.getDispatcherType()) {
        AsyncContext asyncContext;
        if ("y".equals(req.getParameter(CUSTOM_REQ_RESP))) {
            asyncContext = req.startAsync(
                    new ServletRequestWrapper(req),
                    new ServletResponseWrapper(resp));
        } else {
            asyncContext = req.startAsync();
        }
        if ("y".equals(req.getParameter(EMPTY_DISPATCH))) {
            asyncContext.dispatch();
        } else {
            asyncContext.dispatch("/target");
        }
        try {
            asyncContext.dispatch("/nonExistingServlet");
            TestAsyncContextImpl.track("FAIL");
        } catch (IllegalStateException e) {
            TestAsyncContextImpl.track("OK");
        }
    } else {
        TestAsyncContextImpl.track("DispatchingGenericServletGet-");
    }
}
 
Example 14
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("DispatchingServletGet-");
    resp.flushBuffer();

    final boolean first = TrackingServlet.first;
    TrackingServlet.first = false;

    final AsyncContext ctxt = req.startAsync();
    TrackingListener listener = new TrackingListener(false, true, null);
    ctxt.addListener(listener);
    ctxt.setTimeout(3000);

    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (first) {
                ctxt.dispatch("/stage1");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 15
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    if ("y".equals(req.getParameter(DISPATCH_CHECK))) {
        if (req.getDispatcherType() != DispatcherType.ASYNC) {
            track("WrongDispatcherType-");
        }
    }
    track("DispatchingServletGet-");
    final int iter = Integer.parseInt(req.getParameter(ITER_PARAM)) - 1;
    final AsyncContext ctxt = req.startAsync();
    if (trackingListener != null) {
        ctxt.addListener(trackingListener);
    }
    Runnable run = new Runnable() {
        @Override
        public void run() {
            if (iter > 0) {
                ctxt.dispatch("/stage1?" + ITER_PARAM + "=" + iter +
                        "&" + DISPATCH_CHECK + "=y");
            } else {
                ctxt.dispatch("/stage2");
            }
        }
    };
    if ("y".equals(req.getParameter("useThread"))) {
        new Thread(run).start();
    } else {
        run.run();
    }
}
 
Example 16
Source File: TestErrorReportValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // Only set the status on the first call (the dispatch will trigger
    // another call to this Servlet)
    if (resp.getStatus() != HttpServletResponse.SC_BAD_REQUEST) {
        resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        AsyncContext ac = req.startAsync();
        ac.dispatch();
    }
}
 
Example 17
Source File: TestCoyoteOutputStream.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doAsyncWrite(AsyncContext asyncCtxt,
        ServletOutputStream sos) throws IOException {
    while (sos.isReady()) {
        int next = asyncWriteCount.getAndIncrement();
        if (next < asyncWriteTarget) {
            sos.write(
                    ("OK - " + next + System.lineSeparator()).getBytes(
                            StandardCharsets.UTF_8));
            sos.flush();
        } else {
            asyncCtxt.dispatch("/write");
            break;
        }
    }
}
 
Example 18
Source File: Async3.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30*1000);
    actx.dispatch("/jsp/async/async3.jsp");
}
 
Example 19
Source File: Async3.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30*1000);
    actx.dispatch("/jsp/async/async3.jsp");
}
 
Example 20
Source File: Async3.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30*1000);
    actx.dispatch("/jsp/async/async3.jsp");
}