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.util.concurrent.ListenableFuture;
21  import org.springframework.util.concurrent.ListenableFutureCallback;
22  import org.springframework.web.context.request.NativeWebRequest;
23  import org.springframework.web.context.request.async.DeferredResult;
24  import org.springframework.web.context.request.async.WebAsyncUtils;
25  import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
26  import org.springframework.web.method.support.ModelAndViewContainer;
27  
28  /**
29   * Handles return values of type
30   * {@link org.springframework.util.concurrent.ListenableFuture}.
31   *
32   * @author Rossen Stoyanchev
33   * @since 4.1
34   */
35  public class ListenableFutureReturnValueHandler implements HandlerMethodReturnValueHandler {
36  
37  	@Override
38  	public boolean supportsReturnType(MethodParameter returnType) {
39  		return ListenableFuture.class.isAssignableFrom(returnType.getParameterType());
40  	}
41  
42  	@Override
43  	public void handleReturnValue(Object returnValue, MethodParameter returnType,
44  			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
45  
46  		if (returnValue == null) {
47  			mavContainer.setRequestHandled(true);
48  			return;
49  		}
50  
51  		final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
52  		WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
53  
54  		ListenableFuture<?> future = (ListenableFuture<?>) returnValue;
55  		future.addCallback(new ListenableFutureCallback<Object>() {
56  			@Override
57  			public void onSuccess(Object result) {
58  				deferredResult.setResult(result);
59  			}
60  			@Override
61  			public void onFailure(Throwable ex) {
62  				deferredResult.setErrorResult(ex);
63  			}
64  		});
65  	}
66  
67  }