private void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, Exception ex) throws Exception { if (mappedHandler != null) { mappedHandler.triggerAfterCompletion(request, response, ex); } throw ex; }
private void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, Exception ex) throws Exception { if (mappedHandler != null) { mappedHandler.triggerAfterCompletion(request, response, ex); } throw ex; }
/** * Apply preHandle methods of registered interceptors. * @return {@code true} if the execution chain should proceed with the * next interceptor or the handler itself. Else, DispatcherServlet assumes * that this interceptor has already dealt with the response itself. */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }
/** * Apply preHandle methods of registered interceptors. * @return {@code true} if the execution chain should proceed with the * next interceptor or the handler itself. Else, DispatcherServlet assumes * that this interceptor has already dealt with the response itself. */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }
@Test public void exceptionBeforePreHandle() throws Exception { this.chain.triggerAfterCompletion(this.request, this.response, null); verifyZeroInteractions(this.interceptor1, this.interceptor2, this.interceptor3); }
mappedHandler.triggerAfterCompletion(request, response, null);
mappedHandler.triggerAfterCompletion(request, response, null);
@Test public void exceptionDuringPreHandle() throws Exception { Exception ex = new Exception(""); given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willThrow(ex); try { this.chain.applyPreHandle(request, response); } catch (Exception actual) { assertSame(ex, actual); } this.chain.triggerAfterCompletion(this.request, this.response, ex); verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, ex); verify(this.interceptor3, never()).preHandle(this.request, this.response, this.handler); }
@Test public void exceptionAfterPreHandle() throws Exception { Exception ex = new Exception(""); given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true); this.chain.applyPreHandle(request, response); this.chain.triggerAfterCompletion(this.request, this.response, ex); verify(this.interceptor3).afterCompletion(this.request, this.response, this.handler, ex); verify(this.interceptor2).afterCompletion(this.request, this.response, this.handler, ex); verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, ex); }
@Test public void successAsyncScenario() throws Exception { given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true); this.chain.applyPreHandle(request, response); this.chain.applyAfterConcurrentHandlingStarted(request, response); this.chain.triggerAfterCompletion(this.request, this.response, null); verify(this.interceptor1).afterConcurrentHandlingStarted(request, response, this.handler); verify(this.interceptor2).afterConcurrentHandlingStarted(request, response, this.handler); verify(this.interceptor3).afterConcurrentHandlingStarted(request, response, this.handler); }
@Test public void successScenario() throws Exception { ModelAndView mav = new ModelAndView(); given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true); this.chain.applyPreHandle(request, response); this.chain.applyPostHandle(request, response, mav); this.chain.triggerAfterCompletion(this.request, this.response, null); verify(this.interceptor1).postHandle(this.request, this.response, this.handler, mav); verify(this.interceptor2).postHandle(this.request, this.response, this.handler, mav); verify(this.interceptor3).postHandle(this.request, this.response, this.handler, mav); verify(this.interceptor3).afterCompletion(this.request, this.response, this.handler, null); verify(this.interceptor2).afterCompletion(this.request, this.response, this.handler, null); verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, null); }
/** * Apply preHandle methods of registered interceptors. * @return {@code true} if the execution chain should proceed with the * next interceptor or the handler itself. Else, DispatcherServlet assumes * that this interceptor has already dealt with the response itself. */ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }