/** * The dispatcher type {@code javax.servlet.DispatcherType.ASYNC} introduced * in Servlet 3.0 means a filter can be invoked in more than one thread over * the course of a single request. This method returns {@code true} if the * filter is currently executing within an asynchronous dispatch. * @param request the current request * @since 3.2 * @see WebAsyncManager#hasConcurrentResult() */ protected boolean isAsyncDispatch(HttpServletRequest request) { return WebAsyncUtils.getAsyncManager(request).hasConcurrentResult(); }
/** * Whether request processing is in asynchronous mode meaning that the * response will not be committed after the current thread is exited. * @param request the current request * @since 3.2 * @see WebAsyncManager#isConcurrentHandlingStarted() */ protected boolean isAsyncStarted(HttpServletRequest request) { return WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted(); }
public void triggerAfterTimeout(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception { for (DeferredResultProcessingInterceptor interceptor : this.interceptors) { if (deferredResult.isSetOrExpired()) { return; } if (!interceptor.handleTimeout(request, deferredResult)){ break; } } }
@Override public void run() { deferredResult.setResult(23); } });
@Override public void onFailure(Throwable ex) { result.setErrorResult(ex); } });
@Before public void setup() { this.servletRequest = new MockHttpServletRequest(); this.asyncManager = WebAsyncUtils.getAsyncManager(servletRequest); this.asyncManager.setTaskExecutor(new SyncTaskExecutor()); this.asyncWebRequest = mock(AsyncWebRequest.class); this.asyncManager.setAsyncWebRequest(this.asyncWebRequest); verify(this.asyncWebRequest).addCompletionHandler((Runnable) notNull()); reset(this.asyncWebRequest); }
@Override public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception { result.setErrorResult(new AsyncRequestTimeoutException()); return false; }
@Test(expected = IllegalArgumentException.class) public void setAsyncWebRequestAfterAsyncStarted() { this.asyncWebRequest.startAsync(); this.asyncManager.setAsyncWebRequest(null); }
@Override public void onCompletion(Runnable callback) { this.deferredResult.onCompletion(callback); } }
/** * Create an AsyncWebRequest instance. By default, an instance of * {@link StandardServletAsyncWebRequest} gets created. * @param request the current request * @param response the current response * @return an AsyncWebRequest instance (never {@code null}) */ public static AsyncWebRequest createAsyncWebRequest(HttpServletRequest request, HttpServletResponse response) { return new StandardServletAsyncWebRequest(request, response); }
public void applyBeforeConcurrentHandling(NativeWebRequest request, Callable<?> task) throws Exception { for (CallableProcessingInterceptor interceptor : this.interceptors) { interceptor.beforeConcurrentHandling(request, task); } }
public void applyPreProcess(NativeWebRequest request, Callable<?> task) throws Exception { for (CallableProcessingInterceptor interceptor : this.interceptors) { interceptor.preProcess(request, task); this.preProcessIndex++; } }
/** * Whether the selected handler for the current request chose to handle the * request asynchronously. A return value of "true" indicates concurrent * handling is under way and the response will remain open. A return value * of "false" means concurrent handling was either not started or possibly * that it has completed and the request was dispatched for further * processing of the concurrent result. */ public boolean isConcurrentHandlingStarted() { return (this.asyncWebRequest != null && this.asyncWebRequest.isAsyncStarted()); }
public void applyPreProcess(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception { for (DeferredResultProcessingInterceptor interceptor : this.interceptors) { interceptor.preProcess(request, deferredResult); this.preProcessingIndex++; } }
public void applyBeforeConcurrentHandling(NativeWebRequest request, DeferredResult<?> deferredResult) throws Exception { for (DeferredResultProcessingInterceptor interceptor : this.interceptors) { interceptor.beforeConcurrentHandling(request, deferredResult); } }
/** * Set the value for the DeferredResult and handle it. * @param result the value to set * @return {@code true} if the result was set and passed on for handling; * {@code false} if the result was already set or the async request expired * @see #isSetOrExpired() */ public boolean setResult(T result) { return setResultInternal(result); }
@Override public void accept(Throwable t) { deferredResult.setResult(t); } });
@Override public void completeWithError(Throwable failure) { this.deferredResult.setErrorResult(failure); }
@Override public <T> boolean handleError(NativeWebRequest request, DeferredResult<T> result, Throwable t) throws Exception { result.setErrorResult(t); return true; } };
@Override public <T> boolean handleTimeout(NativeWebRequest request, DeferredResult<T> result) throws Exception { result.setErrorResult(23); return true; } };