001/*
002 * Copyright 2002-2020 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 javax.servlet.http.Cookie;
020import javax.servlet.http.HttpServletRequest;
021
022import org.springframework.beans.factory.config.ConfigurableBeanFactory;
023import org.springframework.core.MethodParameter;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026import org.springframework.web.context.request.NativeWebRequest;
027import org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver;
028import org.springframework.web.util.UrlPathHelper;
029import org.springframework.web.util.WebUtils;
030
031/**
032 * An {@link org.springframework.web.method.annotation.AbstractCookieValueMethodArgumentResolver}
033 * that resolves cookie values from an {@link HttpServletRequest}.
034 *
035 * @author Rossen Stoyanchev
036 * @since 3.1
037 */
038public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
039
040        private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
041
042
043        public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {
044                super(beanFactory);
045        }
046
047
048        public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
049                this.urlPathHelper = urlPathHelper;
050        }
051
052
053        @Override
054        @Nullable
055        protected Object resolveName(String cookieName, MethodParameter parameter,
056                        NativeWebRequest webRequest) throws Exception {
057
058                HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
059                Assert.state(servletRequest != null, "No HttpServletRequest");
060
061                Cookie cookieValue = WebUtils.getCookie(servletRequest, cookieName);
062                if (Cookie.class.isAssignableFrom(parameter.getNestedParameterType())) {
063                        return cookieValue;
064                }
065                else if (cookieValue != null) {
066                        return this.urlPathHelper.decodeRequestString(servletRequest, cookieValue.getValue());
067                }
068                else {
069                        return null;
070                }
071        }
072
073}