View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.servlet.mvc.method.annotation;
18  
19  import org.springframework.core.MethodParameter;
20  import org.springframework.web.context.request.NativeWebRequest;
21  import org.springframework.web.context.request.async.DeferredResult;
22  import org.springframework.web.context.request.async.WebAsyncUtils;
23  import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
24  import org.springframework.web.method.support.ModelAndViewContainer;
25  
26  /**
27   * Handles return values of type {@link DeferredResult}.
28   *
29   * @author Rossen Stoyanchev
30   * @since 3.2
31   */
32  public class DeferredResultMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
33  
34  	@Override
35  	public boolean supportsReturnType(MethodParameter returnType) {
36  		return DeferredResult.class.isAssignableFrom(returnType.getParameterType());
37  	}
38  
39  	@Override
40  	public void handleReturnValue(Object returnValue, MethodParameter returnType,
41  			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
42  
43  		if (returnValue == null) {
44  			mavContainer.setRequestHandled(true);
45  			return;
46  		}
47  
48  		DeferredResult<?> deferredResult = (DeferredResult<?>) returnValue;
49  		WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
50  	}
51  
52  }