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.http;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.Assert;
021
022/**
023 * Represents an HTTP cookie as a name-value pair consistent with the content of
024 * the "Cookie" request header. The {@link ResponseCookie} sub-class has the
025 * additional attributes expected in the "Set-Cookie" response header.
026 *
027 * @author Rossen Stoyanchev
028 * @since 5.0
029 * @see <a href="https://tools.ietf.org/html/rfc6265">RFC 6265</a>
030 */
031public class HttpCookie {
032
033        private final String name;
034
035        private final String value;
036
037
038        public HttpCookie(String name, @Nullable String value) {
039                Assert.hasLength(name, "'name' is required and must not be empty.");
040                this.name = name;
041                this.value = (value != null ? value : "");
042        }
043
044        /**
045         * Return the cookie name.
046         */
047        public String getName() {
048                return this.name;
049        }
050
051        /**
052         * Return the cookie value or an empty string (never {@code null}).
053         */
054        public String getValue() {
055                return this.value;
056        }
057
058
059        @Override
060        public int hashCode() {
061                return this.name.hashCode();
062        }
063
064        @Override
065        public boolean equals(@Nullable Object other) {
066                if (this == other) {
067                        return true;
068                }
069                if (!(other instanceof HttpCookie)) {
070                        return false;
071                }
072                HttpCookie otherCookie = (HttpCookie) other;
073                return (this.name.equalsIgnoreCase(otherCookie.getName()));
074        }
075
076        @Override
077        public String toString() {
078                return this.name + '=' + this.value;
079        }
080
081}