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.annotation;
018
019import org.springframework.beans.factory.config.ConfigurableBeanFactory;
020import org.springframework.core.MethodParameter;
021import org.springframework.web.bind.ServletRequestBindingException;
022import org.springframework.web.bind.WebDataBinder;
023import org.springframework.web.bind.annotation.CookieValue;
024
025/**
026 * A base abstract class to resolve method arguments annotated with
027 * {@code @CookieValue}. Subclasses extract the cookie value from the request.
028 *
029 * <p>An {@code @CookieValue} is a named value that is resolved from a cookie.
030 * It has a required flag and a default value to fall back on when the cookie
031 * does not exist.
032 *
033 * <p>A {@link WebDataBinder} may be invoked to apply type conversion to the
034 * resolved cookie value.
035 *
036 * @author Arjen Poutsma
037 * @author Rossen Stoyanchev
038 * @since 3.1
039 */
040public abstract class AbstractCookieValueMethodArgumentResolver extends AbstractNamedValueMethodArgumentResolver {
041
042        /**
043         * @param beanFactory a bean factory to use for resolving  ${...}
044         * placeholder and #{...} SpEL expressions in default values;
045         * or {@code null} if default values are not expected to contain expressions
046         */
047        public AbstractCookieValueMethodArgumentResolver(ConfigurableBeanFactory beanFactory) {
048                super(beanFactory);
049        }
050
051
052        @Override
053        public boolean supportsParameter(MethodParameter parameter) {
054                return parameter.hasParameterAnnotation(CookieValue.class);
055        }
056
057        @Override
058        protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
059                CookieValue annotation = parameter.getParameterAnnotation(CookieValue.class);
060                return new CookieValueNamedValueInfo(annotation);
061        }
062
063        @Override
064        protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
065                throw new ServletRequestBindingException("Missing cookie '" + name +
066                                "' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
067        }
068
069
070        private static class CookieValueNamedValueInfo extends NamedValueInfo {
071
072                private CookieValueNamedValueInfo(CookieValue annotation) {
073                        super(annotation.name(), annotation.required(), annotation.defaultValue());
074                }
075        }
076
077}