View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.handler.codec.http.cookie;
17  
18  import io.netty5.handler.codec.http.cookie.CookieHeaderNames.SameSite;
19  import io.netty5.util.internal.StringUtil;
20  
21  import static io.netty5.handler.codec.http.cookie.CookieUtil.validateAttributeValue;
22  import static io.netty5.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
23  import static java.util.Objects.requireNonNull;
24  
25  /**
26   * The default {@link Cookie} implementation.
27   */
28  public class DefaultCookie implements Cookie {
29  
30      private final String name;
31      private String value;
32      private boolean wrap;
33      private String domain;
34      private String path;
35      private long maxAge = UNDEFINED_MAX_AGE;
36      private boolean secure;
37      private boolean httpOnly;
38      private SameSite sameSite;
39  
40      /**
41       * Creates a new cookie with the specified name and value.
42       */
43      public DefaultCookie(String name, String value) {
44          this.name = checkNonEmptyAfterTrim(name, "name");
45          setValue(value);
46      }
47  
48      @Override
49      public String name() {
50          return name;
51      }
52  
53      @Override
54      public String value() {
55          return value;
56      }
57  
58      @Override
59      public void setValue(String value) {
60          this.value = requireNonNull(value, "value");
61      }
62  
63      @Override
64      public boolean wrap() {
65          return wrap;
66      }
67  
68      @Override
69      public void setWrap(boolean wrap) {
70          this.wrap = wrap;
71      }
72  
73      @Override
74      public String domain() {
75          return domain;
76      }
77  
78      @Override
79      public void setDomain(String domain) {
80          this.domain = validateAttributeValue("domain", domain);
81      }
82  
83      @Override
84      public String path() {
85          return path;
86      }
87  
88      @Override
89      public void setPath(String path) {
90          this.path = validateAttributeValue("path", path);
91      }
92  
93      @Override
94      public long maxAge() {
95          return maxAge;
96      }
97  
98      @Override
99      public void setMaxAge(long maxAge) {
100         this.maxAge = maxAge;
101     }
102 
103     @Override
104     public boolean isSecure() {
105         return secure;
106     }
107 
108     @Override
109     public void setSecure(boolean secure) {
110         this.secure = secure;
111     }
112 
113     @Override
114     public boolean isHttpOnly() {
115         return httpOnly;
116     }
117 
118     @Override
119     public void setHttpOnly(boolean httpOnly) {
120         this.httpOnly = httpOnly;
121     }
122 
123     /**
124      * Checks to see if this {@link Cookie} can be sent along cross-site requests.
125      * For more information, please look
126      * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
127      * @return <b>same-site-flag</b> value
128      */
129     public SameSite sameSite() {
130         return sameSite;
131     }
132 
133     /**
134      * Determines if this this {@link Cookie} can be sent along cross-site requests.
135      * For more information, please look
136      *  <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05">here</a>
137      * @param sameSite <b>same-site-flag</b> value
138      */
139     public void setSameSite(SameSite sameSite) {
140         this.sameSite = sameSite;
141     }
142 
143     @Override
144     public int hashCode() {
145         return name().hashCode();
146     }
147 
148     @Override
149     public boolean equals(Object o) {
150         if (this == o) {
151             return true;
152         }
153 
154         if (!(o instanceof Cookie)) {
155             return false;
156         }
157 
158         Cookie that = (Cookie) o;
159         if (!name().equals(that.name())) {
160             return false;
161         }
162 
163         if (path() == null) {
164             if (that.path() != null) {
165                 return false;
166             }
167         } else if (that.path() == null) {
168             return false;
169         } else if (!path().equals(that.path())) {
170             return false;
171         }
172 
173         if (domain() == null) {
174             return that.domain() == null;
175         } else {
176             return domain().equalsIgnoreCase(that.domain());
177         }
178     }
179 
180     @Override
181     public int compareTo(Cookie c) {
182         int v = name().compareTo(c.name());
183         if (v != 0) {
184             return v;
185         }
186 
187         if (path() == null) {
188             if (c.path() != null) {
189                 return -1;
190             }
191         } else if (c.path() == null) {
192             return 1;
193         } else {
194             v = path().compareTo(c.path());
195             if (v != 0) {
196                 return v;
197             }
198         }
199 
200         if (domain() == null) {
201             if (c.domain() != null) {
202                 return -1;
203             }
204         } else if (c.domain() == null) {
205             return 1;
206         } else {
207             v = domain().compareToIgnoreCase(c.domain());
208             return v;
209         }
210 
211         return 0;
212     }
213 
214     @Override
215     public String toString() {
216         StringBuilder buf = StringUtil.threadLocalStringBuilder()
217             .append(name())
218             .append('=')
219             .append(value());
220         if (domain() != null) {
221             buf.append(", domain=")
222                .append(domain());
223         }
224         if (path() != null) {
225             buf.append(", path=")
226                .append(path());
227         }
228         if (maxAge() >= 0) {
229             buf.append(", maxAge=")
230                .append(maxAge())
231                .append('s');
232         }
233         if (isSecure()) {
234             buf.append(", secure");
235         }
236         if (isHttpOnly()) {
237             buf.append(", HTTPOnly");
238         }
239         if (sameSite() != null) {
240             buf.append(", SameSite=").append(sameSite());
241         }
242         return buf.toString();
243     }
244 }