001/* 002 * Copyright 2002-2018 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.lang.Nullable; 028import org.springframework.web.context.request.NativeWebRequest; 029 030/** 031 * Handles method return values by delegating to a list of registered {@link HandlerMethodReturnValueHandler HandlerMethodReturnValueHandlers}. 032 * Previously resolved return types are cached for faster lookups. 033 * 034 * @author Rossen Stoyanchev 035 * @since 3.1 036 */ 037public class HandlerMethodReturnValueHandlerComposite implements HandlerMethodReturnValueHandler { 038 039 protected final Log logger = LogFactory.getLog(getClass()); 040 041 private final List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>(); 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 @Nullable 061 private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) { 062 for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) { 063 if (handler.supportsReturnType(returnType)) { 064 return handler; 065 } 066 } 067 return null; 068 } 069 070 /** 071 * Iterate over registered {@link HandlerMethodReturnValueHandler HandlerMethodReturnValueHandlers} and invoke the one that supports it. 072 * @throws IllegalStateException if no suitable {@link HandlerMethodReturnValueHandler} is found. 073 */ 074 @Override 075 public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, 076 ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { 077 078 HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType); 079 if (handler == null) { 080 throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName()); 081 } 082 handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest); 083 } 084 085 @Nullable 086 private HandlerMethodReturnValueHandler selectHandler(@Nullable Object value, MethodParameter returnType) { 087 boolean isAsyncValue = isAsyncReturnValue(value, returnType); 088 for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) { 089 if (isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler)) { 090 continue; 091 } 092 if (handler.supportsReturnType(returnType)) { 093 return handler; 094 } 095 } 096 return null; 097 } 098 099 private boolean isAsyncReturnValue(@Nullable Object value, MethodParameter returnType) { 100 for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) { 101 if (handler instanceof AsyncHandlerMethodReturnValueHandler && 102 ((AsyncHandlerMethodReturnValueHandler) handler).isAsyncReturnValue(value, returnType)) { 103 return true; 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 HandlerMethodReturnValueHandlers}. 119 */ 120 public HandlerMethodReturnValueHandlerComposite addHandlers( 121 @Nullable List<? extends HandlerMethodReturnValueHandler> handlers) { 122 123 if (handlers != null) { 124 this.returnValueHandlers.addAll(handlers); 125 } 126 return this; 127 } 128 129}