001/*
002 * Copyright 2002-2016 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.core.MethodParameter;
020import org.springframework.util.concurrent.ListenableFuture;
021import org.springframework.util.concurrent.ListenableFutureCallback;
022import org.springframework.web.context.request.NativeWebRequest;
023import org.springframework.web.context.request.async.DeferredResult;
024import org.springframework.web.context.request.async.WebAsyncUtils;
025import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
026import org.springframework.web.method.support.ModelAndViewContainer;
027
028/**
029 * Handles return values of type
030 * {@link org.springframework.util.concurrent.ListenableFuture}.
031 *
032 * @author Rossen Stoyanchev
033 * @since 4.1
034 * @deprecated as of 4.3 {@link DeferredResultMethodReturnValueHandler} supports
035 * ListenableFuture return values via an adapter mechanism.
036 */
037@Deprecated
038public class ListenableFutureReturnValueHandler implements AsyncHandlerMethodReturnValueHandler {
039
040        @Override
041        public boolean supportsReturnType(MethodParameter returnType) {
042                return ListenableFuture.class.isAssignableFrom(returnType.getParameterType());
043        }
044
045        @Override
046        public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) {
047                return (returnValue != null && returnValue instanceof ListenableFuture);
048        }
049
050        @Override
051        public void handleReturnValue(Object returnValue, MethodParameter returnType,
052                        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
053
054                if (returnValue == null) {
055                        mavContainer.setRequestHandled(true);
056                        return;
057                }
058
059                final DeferredResult<Object> deferredResult = new DeferredResult<Object>();
060                WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
061
062                ListenableFuture<?> future = (ListenableFuture<?>) returnValue;
063                future.addCallback(new ListenableFutureCallback<Object>() {
064                        @Override
065                        public void onSuccess(Object result) {
066                                deferredResult.setResult(result);
067                        }
068                        @Override
069                        public void onFailure(Throwable ex) {
070                                deferredResult.setErrorResult(ex);
071                        }
072                });
073        }
074
075}