001/* 002 * Copyright 2002-2015 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.mvc.method.annotation; 018 019import org.springframework.beans.factory.BeanFactory; 020import org.springframework.core.MethodParameter; 021import org.springframework.web.context.request.NativeWebRequest; 022import org.springframework.web.context.request.async.WebAsyncTask; 023import org.springframework.web.context.request.async.WebAsyncUtils; 024import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler; 025import org.springframework.web.method.support.ModelAndViewContainer; 026 027/** 028 * Handles return values of type {@link WebAsyncTask}. 029 * 030 * @author Rossen Stoyanchev 031 * @since 3.2 032 */ 033public class AsyncTaskMethodReturnValueHandler implements AsyncHandlerMethodReturnValueHandler { 034 035 private final BeanFactory beanFactory; 036 037 038 public AsyncTaskMethodReturnValueHandler(BeanFactory beanFactory) { 039 this.beanFactory = beanFactory; 040 } 041 042 043 @Override 044 public boolean supportsReturnType(MethodParameter returnType) { 045 return WebAsyncTask.class.isAssignableFrom(returnType.getParameterType()); 046 } 047 048 @Override 049 public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) { 050 return (returnValue != null && returnValue instanceof WebAsyncTask); 051 } 052 053 @Override 054 public void handleReturnValue(Object returnValue, MethodParameter returnType, 055 ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { 056 057 if (returnValue == null) { 058 mavContainer.setRequestHandled(true); 059 return; 060 } 061 062 WebAsyncTask<?> webAsyncTask = (WebAsyncTask<?>) returnValue; 063 webAsyncTask.setBeanFactory(this.beanFactory); 064 WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(webAsyncTask, mavContainer); 065 } 066 067}