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.method.support;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.core.MethodParameter;
027import org.springframework.web.context.request.NativeWebRequest;
028
029/**
030 * Handles method return values by delegating to a list of registered {@link HandlerMethodReturnValueHandler}s.
031 * Previously resolved return types are cached for faster lookups.
032 *
033 * @author Rossen Stoyanchev
034 * @since 3.1
035 */
036public class HandlerMethodReturnValueHandlerComposite implements AsyncHandlerMethodReturnValueHandler {
037
038        protected final Log logger = LogFactory.getLog(getClass());
039
040        private final List<HandlerMethodReturnValueHandler> returnValueHandlers =
041                new ArrayList<HandlerMethodReturnValueHandler>();
042
043
044        /**
045         * Return a read-only list with the registered handlers, or an empty list.
046         */
047        public List<HandlerMethodReturnValueHandler> getHandlers() {
048                return Collections.unmodifiableList(this.returnValueHandlers);
049        }
050
051        /**
052         * Whether the given {@linkplain MethodParameter method return type} is supported by any registered
053         * {@link HandlerMethodReturnValueHandler}.
054         */
055        @Override
056        public boolean supportsReturnType(MethodParameter returnType) {
057                return getReturnValueHandler(returnType) != null;
058        }
059
060        private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) {
061                for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
062                        if (handler.supportsReturnType(returnType)) {
063                                return handler;
064                        }
065                }
066                return null;
067        }
068
069        /**
070         * Iterate over registered {@link HandlerMethodReturnValueHandler}s and invoke the one that supports it.
071         * @throws IllegalStateException if no suitable {@link HandlerMethodReturnValueHandler} is found.
072         */
073        @Override
074        public void handleReturnValue(Object returnValue, MethodParameter returnType,
075                        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
076
077                HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);
078                if (handler == null) {
079                        throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
080                }
081                handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
082        }
083
084        private HandlerMethodReturnValueHandler selectHandler(Object value, MethodParameter returnType) {
085                boolean isAsyncValue = isAsyncReturnValue(value, returnType);
086                for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
087                        if (isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler)) {
088                                continue;
089                        }
090                        if (handler.supportsReturnType(returnType)) {
091                                return handler;
092                        }
093                }
094                return null;
095        }
096
097        @Override
098        public boolean isAsyncReturnValue(Object value, MethodParameter returnType) {
099                for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
100                        if (handler instanceof AsyncHandlerMethodReturnValueHandler) {
101                                if (((AsyncHandlerMethodReturnValueHandler) handler).isAsyncReturnValue(value, returnType)) {
102                                        return true;
103                                }
104                        }
105                }
106                return false;
107        }
108
109        /**
110         * Add the given {@link HandlerMethodReturnValueHandler}.
111         */
112        public HandlerMethodReturnValueHandlerComposite addHandler(HandlerMethodReturnValueHandler handler) {
113                this.returnValueHandlers.add(handler);
114                return this;
115        }
116
117        /**
118         * Add the given {@link HandlerMethodReturnValueHandler}s.
119         */
120        public HandlerMethodReturnValueHandlerComposite addHandlers(List<? extends HandlerMethodReturnValueHandler> handlers) {
121                if (handlers != null) {
122                        for (HandlerMethodReturnValueHandler handler : handlers) {
123                                this.returnValueHandlers.add(handler);
124                        }
125                }
126                return this;
127        }
128
129}