org.springframework.mock.web

Class MockAsyncContext

  • java.lang.Object
    • org.springframework.mock.web.MockAsyncContext
  • All Implemented Interfaces:
    AsyncContext


    public class MockAsyncContext
    extends java.lang.Object
    implements AsyncContext
    Mock implementation of the AsyncContext interface.
    Since:
    3.2
    Author:
    Rossen Stoyanchev
    • Method Detail

      • addDispatchHandler

        public void addDispatchHandler(java.lang.Runnable handler)
      • hasOriginalRequestAndResponse

        public boolean hasOriginalRequestAndResponse()
        Description copied from interface: javax.servlet.AsyncContext
        Checks if this AsyncContext was initialized with the original or application-wrapped request and response objects.

        This information may be used by filters invoked in the outbound direction, after a request was put into asynchronous mode, to determine whether any request and/or response wrappers that they added during their inbound invocation need to be preserved for the duration of the asynchronous operation, or may be released.

        Specified by:
        hasOriginalRequestAndResponse in interface AsyncContext
        Returns:
        true if this AsyncContext was initialized with the original request and response objects by calling ServletRequest.startAsync(), or if it was initialized by calling ServletRequest.startAsync(ServletRequest, ServletResponse), and neither the ServletRequest nor ServletResponse arguments carried any application-provided wrappers; false otherwise
      • dispatch

        public void dispatch()
        Description copied from interface: javax.servlet.AsyncContext
        Dispatches the request and response objects of this AsyncContext to the servlet container.

        If the asynchronous cycle was started with ServletRequest.startAsync(ServletRequest, ServletResponse), and the request passed is an instance of HttpServletRequest, then the dispatch is to the URI returned by HttpServletRequest.getRequestURI(). Otherwise, the dispatch is to the URI of the request when it was last dispatched by the container.

        The following sequence illustrates how this will work:

        
         // REQUEST dispatch to /url/A
         AsyncContext ac = request.startAsync();
         ...
         ac.dispatch(); // ASYNC dispatch to /url/A
         
         // REQUEST to /url/A
         // FORWARD dispatch to /url/B
         request.getRequestDispatcher("/url/B").forward(request,response);
         // Start async operation from within the target of the FORWARD
         // dispatch
         ac = request.startAsync();
         ...
         ac.dispatch(); // ASYNC dispatch to /url/A
         
         // REQUEST to /url/A
         // FORWARD dispatch to /url/B
         request.getRequestDispatcher("/url/B").forward(request,response);
         // Start async operation from within the target of the FORWARD
         // dispatch
         ac = request.startAsync(request,response);
         ...
         ac.dispatch(); // ASYNC dispatch to /url/B
         

        This method returns immediately after passing the request and response objects to a container managed thread, on which the dispatch operation will be performed. If this method is called before the container-initiated dispatch that called startAsync has returned to the container, the dispatch operation will be delayed until after the container-initiated dispatch has returned to the container.

        The dispatcher type of the request is set to DispatcherType.ASYNC. Unlike forward dispatches, the response buffer and headers will not be reset, and it is legal to dispatch even if the response has already been committed.

        Control over the request and response is delegated to the dispatch target, and the response will be closed when the dispatch target has completed execution, unless ServletRequest.startAsync() or ServletRequest.startAsync(ServletRequest, ServletResponse) are called.

        Any errors or exceptions that may occur during the execution of this method must be caught and handled by the container, as follows:

        1. Invoke, at their onError method, all AsyncListener instances registered with the ServletRequest for which this AsyncContext was created, and make the caught Throwable available via AsyncEvent.getThrowable().
        2. If none of the listeners called AsyncContext.complete() or any of the AsyncContext.dispatch() methods, perform an error dispatch with a status code equal to HttpServletResponse.SC_INTERNAL_SERVER_ERROR, and make the above Throwable available as the value of the RequestDispatcher.ERROR_EXCEPTION request attribute.
        3. If no matching error page was found, or the error page did not call AsyncContext.complete() or any of the AsyncContext.dispatch() methods, call AsyncContext.complete().

        There can be at most one asynchronous dispatch operation per asynchronous cycle, which is started by a call to one of the ServletRequest.startAsync() methods. Any attempt to perform an additional asynchronous dispatch operation within the same asynchronous cycle will result in an IllegalStateException. If startAsync is subsequently called on the dispatched request, then any of the dispatch or AsyncContext.complete() methods may be called.

        Specified by:
        dispatch in interface AsyncContext
        See Also:
        ServletRequest.getDispatcherType()
      • getDispatchedPath

        @Nullable
        public java.lang.String getDispatchedPath()
      • complete

        public void complete()
        Description copied from interface: javax.servlet.AsyncContext
        Completes the asynchronous operation that was started on the request that was used to initialze this AsyncContext, closing the response that was used to initialize this AsyncContext.

        Any listeners of type AsyncListener that were registered with the ServletRequest for which this AsyncContext was created will be invoked at their onComplete method.

        It is legal to call this method any time after a call to ServletRequest.startAsync() or ServletRequest.startAsync(ServletRequest, ServletResponse), and before a call to one of the dispatch methods of this class. If this method is called before the container-initiated dispatch that called startAsync has returned to the container, then the call will not take effect (and any invocations of AsyncListener.onComplete(AsyncEvent) will be delayed) until after the container-initiated dispatch has returned to the container.

        Specified by:
        complete in interface AsyncContext
      • start

        public void start(java.lang.Runnable runnable)
        Description copied from interface: javax.servlet.AsyncContext
        Causes the container to dispatch a thread, possibly from a managed thread pool, to run the specified Runnable. The container may propagate appropriate contextual information to the Runnable.
        Specified by:
        start in interface AsyncContext
        Parameters:
        runnable - the asynchronous handler
      • addListener

        public void addListener(AsyncListener listener,
                                ServletRequest request,
                                ServletResponse response)
        Description copied from interface: javax.servlet.AsyncContext
        Registers the given AsyncListener with the most recent asynchronous cycle that was started by a call to one of the ServletRequest.startAsync() methods.

        The given AsyncListener will receive an AsyncEvent when the asynchronous cycle completes successfully, times out, results in an error, or a new asynchronous cycle is being initiated via one of the ServletRequest.startAsync() methods.

        AsyncListener instances will be notified in the order in which they were added.

        The given ServletRequest and ServletResponse objects will be made available to the given AsyncListener via the getSuppliedRequest and getSuppliedResponse methods, respectively, of the AsyncEvent delivered to it. These objects should not be read from or written to, respectively, at the time the AsyncEvent is delivered, because additional wrapping may have occurred since the given AsyncListener was registered, but may be used in order to release any resources associated with them.

        Specified by:
        addListener in interface AsyncContext
        Parameters:
        listener - the AsyncListener to be registered
        request - the ServletRequest that will be included in the AsyncEvent
        response - the ServletResponse that will be included in the AsyncEvent
      • getListeners

        public java.util.List<AsyncListener> getListeners()
      • createListener

        public <T extends AsyncListener> T createListener(java.lang.Class<T> clazz)
                                                   throws ServletException
        Description copied from interface: javax.servlet.AsyncContext
        Instantiates the given AsyncListener class.

        The returned AsyncListener instance may be further customized before it is registered with this AsyncContext via a call to one of the addListener methods.

        The given AsyncListener class must define a zero argument constructor, which is used to instantiate it.

        This method supports resource injection if the given clazz represents a Managed Bean. See the Java EE platform and JSR 299 specifications for additional details about Managed Beans and resource injection.

        This method supports any annotations applicable to AsyncListener.

        Specified by:
        createListener in interface AsyncContext
        Type Parameters:
        T - the class of the object to instantiate
        Parameters:
        clazz - the AsyncListener class to instantiate
        Returns:
        the new AsyncListener instance
        Throws:
        ServletException - if the given clazz fails to be instantiated
      • setTimeout

        public void setTimeout(long timeout)
        By default this is set to 10000 (10 seconds) even though the Servlet API specifies a default async request timeout of 30 seconds. Keep in mind the timeout could further be impacted by global configuration through the MVC Java config or the XML namespace, as well as be overridden per request on DeferredResult or on SseEmitter.
        Specified by:
        setTimeout in interface AsyncContext
        Parameters:
        timeout - the timeout value to use.
        See Also:
        AsyncContext.setTimeout(long)
      • getTimeout

        public long getTimeout()
        Description copied from interface: javax.servlet.AsyncContext
        Gets the timeout (in milliseconds) for this AsyncContext.

        This method returns the container's default timeout for asynchronous operations, or the timeout value passed to the most recent invocation of AsyncContext.setTimeout(long).

        A timeout value of zero or less indicates no timeout.

        Specified by:
        getTimeout in interface AsyncContext
        Returns:
        the timeout in milliseconds