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.bind;
018
019import org.springframework.core.MethodParameter;
020
021/**
022 * {@link ServletRequestBindingException} subclass that indicates
023 * that a request cookie expected in the method parameters of an
024 * {@code @RequestMapping} method is not present.
025 *
026 * @author Juergen Hoeller
027 * @since 5.1
028 * @see MissingRequestHeaderException
029 */
030@SuppressWarnings("serial")
031public class MissingRequestCookieException extends ServletRequestBindingException {
032
033        private final String cookieName;
034
035        private final MethodParameter parameter;
036
037
038        /**
039         * Constructor for MissingRequestCookieException.
040         * @param cookieName the name of the missing request cookie
041         * @param parameter the method parameter
042         */
043        public MissingRequestCookieException(String cookieName, MethodParameter parameter) {
044                super("");
045                this.cookieName = cookieName;
046                this.parameter = parameter;
047        }
048
049
050        @Override
051        public String getMessage() {
052                return "Missing cookie '" + this.cookieName +
053                                "' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
054        }
055
056        /**
057         * Return the expected name of the request cookie.
058         */
059        public final String getCookieName() {
060                return this.cookieName;
061        }
062
063        /**
064         * Return the method parameter bound to the request cookie.
065         */
066        public final MethodParameter getParameter() {
067                return this.parameter;
068        }
069
070}