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.annotation;
018
019import java.util.Map;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.web.bind.support.WebDataBinderFactory;
023import org.springframework.web.context.request.NativeWebRequest;
024import org.springframework.web.method.support.HandlerMethodArgumentResolver;
025import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
026import org.springframework.web.method.support.ModelAndViewContainer;
027
028/**
029 * Resolves {@link Map} method arguments and handles {@link Map} return values.
030 *
031 * <p>A Map return value can be interpreted in more than one ways depending
032 * on the presence of annotations like {@code @ModelAttribute} or
033 * {@code @ResponseBody}. Therefore this handler should be configured after
034 * the handlers that support these annotations.
035 *
036 * @author Rossen Stoyanchev
037 * @since 3.1
038 */
039public class MapMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
040
041        @Override
042        public boolean supportsParameter(MethodParameter parameter) {
043                return Map.class.isAssignableFrom(parameter.getParameterType());
044        }
045
046        @Override
047        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
048                        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
049
050                return mavContainer.getModel();
051        }
052
053        @Override
054        public boolean supportsReturnType(MethodParameter returnType) {
055                return Map.class.isAssignableFrom(returnType.getParameterType());
056        }
057
058        @Override
059        @SuppressWarnings({"unchecked", "rawtypes"})
060        public void handleReturnValue(Object returnValue, MethodParameter returnType,
061                        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
062
063                if (returnValue == null) {
064                        return;
065                }
066                else if (returnValue instanceof Map){
067                        mavContainer.addAllAttributes((Map) returnValue);
068                }
069                else {
070                        // should not happen
071                        throw new UnsupportedOperationException("Unexpected return type: " +
072                                        returnType.getParameterType().getName() + " in method: " + returnType.getMethod());
073                }
074        }
075
076}