001/* 002 * Copyright 2002-2012 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.util.Assert; 023import org.springframework.web.context.request.AsyncWebRequestInterceptor; 024import org.springframework.web.context.request.WebRequestInterceptor; 025import org.springframework.web.servlet.AsyncHandlerInterceptor; 026import org.springframework.web.servlet.ModelAndView; 027 028/** 029 * Adapter that implements the Servlet HandlerInterceptor interface 030 * and wraps an underlying WebRequestInterceptor. 031 * 032 * @author Juergen Hoeller 033 * @since 2.0 034 * @see org.springframework.web.context.request.WebRequestInterceptor 035 * @see org.springframework.web.servlet.HandlerInterceptor 036 */ 037public class WebRequestHandlerInterceptorAdapter implements AsyncHandlerInterceptor { 038 039 private final WebRequestInterceptor requestInterceptor; 040 041 042 /** 043 * Create a new WebRequestHandlerInterceptorAdapter for the given WebRequestInterceptor. 044 * @param requestInterceptor the WebRequestInterceptor to wrap 045 */ 046 public WebRequestHandlerInterceptorAdapter(WebRequestInterceptor requestInterceptor) { 047 Assert.notNull(requestInterceptor, "WebRequestInterceptor must not be null"); 048 this.requestInterceptor = requestInterceptor; 049 } 050 051 052 @Override 053 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 054 throws Exception { 055 056 this.requestInterceptor.preHandle(new DispatcherServletWebRequest(request, response)); 057 return true; 058 } 059 060 @Override 061 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) 062 throws Exception { 063 064 this.requestInterceptor.postHandle(new DispatcherServletWebRequest(request, response), 065 (modelAndView != null && !modelAndView.wasCleared() ? modelAndView.getModelMap() : null)); 066 } 067 068 @Override 069 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 070 throws Exception { 071 072 this.requestInterceptor.afterCompletion(new DispatcherServletWebRequest(request, response), ex); 073 } 074 075 @Override 076 public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) { 077 if (this.requestInterceptor instanceof AsyncWebRequestInterceptor) { 078 AsyncWebRequestInterceptor asyncInterceptor = (AsyncWebRequestInterceptor) this.requestInterceptor; 079 DispatcherServletWebRequest webRequest = new DispatcherServletWebRequest(request, response); 080 asyncInterceptor.afterConcurrentHandlingStarted(webRequest); 081 } 082 } 083 084}