001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.handler; 018 019import javax.servlet.http.HttpServletRequest; 020import javax.servlet.http.HttpServletResponse; 021 022import org.springframework.lang.Nullable; 023import org.springframework.util.Assert; 024import org.springframework.web.context.request.AsyncWebRequestInterceptor; 025import org.springframework.web.context.request.WebRequestInterceptor; 026import org.springframework.web.servlet.AsyncHandlerInterceptor; 027import org.springframework.web.servlet.ModelAndView; 028 029/** 030 * Adapter that implements the Servlet HandlerInterceptor interface 031 * and wraps an underlying WebRequestInterceptor. 032 * 033 * @author Juergen Hoeller 034 * @since 2.0 035 * @see org.springframework.web.context.request.WebRequestInterceptor 036 * @see org.springframework.web.servlet.HandlerInterceptor 037 */ 038public class WebRequestHandlerInterceptorAdapter implements AsyncHandlerInterceptor { 039 040 private final WebRequestInterceptor requestInterceptor; 041 042 043 /** 044 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor. 045 * @param requestInterceptor the WebRequestInterceptor to wrap 046 */ 047 public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor) { 048 Assert.notNull(requestInterceptor, "WebRequestInterceptor must not be null"); 049 this.requestInterceptor = requestInterceptor; 050 } 051 052 053 @Override 054 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 055 throws Exception { 056 057 this.requestInterceptor.preHandle(new DispatcherServletWebRequest(request, response)); 058 return true; 059 } 060 061 @Override 062 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 063 @Nullable ModelAndView modelAndView) throws Exception { 064 065 this.requestInterceptor.postHandle(new DispatcherServletWebRequest(request, response), 066 (modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null)); 067 } 068 069 @Override 070 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, 071 @Nullable Exception ex) throws Exception { 072 073 this.requestInterceptor.afterCompletion(new DispatcherServletWebRequest(request, response), ex); 074 } 075 076 @Override 077 public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) { 078 if (this.requestInterceptor instanceof AsyncWebRequestInterceptor) { 079 AsyncWebRequestInterceptor asyncInterceptor = (AsyncWebRequestInterceptor) this.requestInterceptor; 080 DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, response); 081 asyncInterceptor.afterConcurrentHandlingStarted(webRequest); 082 } 083 } 084 085}