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