001/*
002 * Copyright 2002-2017 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 java.util.Map;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.lang.Nullable;
023import org.springframework.ui.Model;
024import org.springframework.ui.ModelMap;
025import org.springframework.util.Assert;
026import org.springframework.validation.DataBinder;
027import org.springframework.web.bind.support.WebDataBinderFactory;
028import org.springframework.web.context.request.NativeWebRequest;
029import org.springframework.web.method.support.HandlerMethodArgumentResolver;
030import org.springframework.web.method.support.ModelAndViewContainer;
031import org.springframework.web.servlet.mvc.support.RedirectAttributes;
032import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
033
034/**
035 * Resolves method arguments of type {@link RedirectAttributes}.
036 *
037 * <p>This resolver must be listed ahead of
038 * {@link org.springframework.web.method.annotation.ModelMethodProcessor} and
039 * {@link org.springframework.web.method.annotation.MapMethodProcessor},
040 * which support {@link Map} and {@link Model} arguments both of which are
041 * "super" types of {@code RedirectAttributes} and would also attempt to
042 * resolve a {@code RedirectAttributes} argument.
043 *
044 * @author Rossen Stoyanchev
045 * @since 3.1
046 */
047public class RedirectAttributesMethodArgumentResolver implements HandlerMethodArgumentResolver {
048
049        @Override
050        public boolean supportsParameter(MethodParameter parameter) {
051                return RedirectAttributes.class.isAssignableFrom(parameter.getParameterType());
052        }
053
054        @Override
055        public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
056                        NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
057
058                Assert.state(mavContainer != null, "RedirectAttributes argument only supported on regular handler methods");
059
060                ModelMap redirectAttributes;
061                if (binderFactory != null) {
062                        DataBinder dataBinder = binderFactory.createBinder(webRequest, null, DataBinder.DEFAULT_OBJECT_NAME);
063                        redirectAttributes = new RedirectAttributesModelMap(dataBinder);
064                }
065                else {
066                        redirectAttributes  = new RedirectAttributesModelMap();
067                }
068                mavContainer.setRedirectModel(redirectAttributes);
069                return redirectAttributes;
070        }
071
072}