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    *   http://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.netty.handler.codec.http.cookie;
17  
18  import static io.netty.handler.codec.http.cookie.CookieUtil.*;
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  
21  /**
22   * The default {@link Cookie} implementation.
23   */
24  public class DefaultCookie implements Cookie {
25  
26      private final String name;
27      private String value;
28      private boolean wrap;
29      private String domain;
30      private String path;
31      private long maxAge = UNDEFINED_MAX_AGE;
32      private boolean secure;
33      private boolean httpOnly;
34  
35      /**
36       * Creates a new cookie with the specified name and value.
37       */
38      public DefaultCookie(String name, String value) {
39          name = checkNotNull(name, "name").trim();
40          if (name.isEmpty()) {
41              throw new IllegalArgumentException("empty name");
42          }
43          this.name = name;
44          setValue(value);
45      }
46  
47      @Override
48      public String name() {
49          return name;
50      }
51  
52      @Override
53      public String value() {
54          return value;
55      }
56  
57      @Override
58      public void setValue(String value) {
59          this.value = checkNotNull(value, "value");
60      }
61  
62      @Override
63      public boolean wrap() {
64          return wrap;
65      }
66  
67      @Override
68      public void setWrap(boolean wrap) {
69          this.wrap = wrap;
70      }
71  
72      @Override
73      public String domain() {
74          return domain;
75      }
76  
77      @Override
78      public void setDomain(String domain) {
79          this.domain = validateAttributeValue("domain", domain);
80      }
81  
82      @Override
83      public String path() {
84          return path;
85      }
86  
87      @Override
88      public void setPath(String path) {
89          this.path = validateAttributeValue("path", path);
90      }
91  
92      @Override
93      public long maxAge() {
94          return maxAge;
95      }
96  
97      @Override
98      public void setMaxAge(long maxAge) {
99          this.maxAge = maxAge;
100     }
101 
102     @Override
103     public boolean isSecure() {
104         return secure;
105     }
106 
107     @Override
108     public void setSecure(boolean secure) {
109         this.secure = secure;
110     }
111 
112     @Override
113     public boolean isHttpOnly() {
114         return httpOnly;
115     }
116 
117     @Override
118     public void setHttpOnly(boolean httpOnly) {
119         this.httpOnly = httpOnly;
120     }
121 
122     @Override
123     public int hashCode() {
124         return name().hashCode();
125     }
126 
127     @Override
128     public boolean equals(Object o) {
129         if (this == o) {
130             return true;
131         }
132 
133         if (!(o instanceof Cookie)) {
134             return false;
135         }
136 
137         Cookie that = (Cookie) o;
138         if (!name().equals(that.name())) {
139             return false;
140         }
141 
142         if (path() == null) {
143             if (that.path() != null) {
144                 return false;
145             }
146         } else if (that.path() == null) {
147             return false;
148         } else if (!path().equals(that.path())) {
149             return false;
150         }
151 
152         if (domain() == null) {
153             if (that.domain() != null) {
154                 return false;
155             }
156         } else {
157             return domain().equalsIgnoreCase(that.domain());
158         }
159 
160         return true;
161     }
162 
163     @Override
164     public int compareTo(Cookie c) {
165         int v = name().compareTo(c.name());
166         if (v != 0) {
167             return v;
168         }
169 
170         if (path() == null) {
171             if (c.path() != null) {
172                 return -1;
173             }
174         } else if (c.path() == null) {
175             return 1;
176         } else {
177             v = path().compareTo(c.path());
178             if (v != 0) {
179                 return v;
180             }
181         }
182 
183         if (domain() == null) {
184             if (c.domain() != null) {
185                 return -1;
186             }
187         } else if (c.domain() == null) {
188             return 1;
189         } else {
190             v = domain().compareToIgnoreCase(c.domain());
191             return v;
192         }
193 
194         return 0;
195     }
196 
197     /**
198      * Validate a cookie attribute value, throws a {@link IllegalArgumentException} otherwise.
199      * Only intended to be used by {@link io.netty.handler.codec.http.DefaultCookie}.
200      * @param name attribute name
201      * @param value attribute value
202      * @return the trimmed, validated attribute value
203      * @deprecated CookieUtil is package private, will be removed once old Cookie API is dropped
204      */
205     @Deprecated
206     protected String validateValue(String name, String value) {
207         return validateAttributeValue(name, value);
208     }
209 
210     @Override
211     public String toString() {
212         StringBuilder buf = stringBuilder()
213             .append(name())
214             .append('=')
215             .append(value());
216         if (domain() != null) {
217             buf.append(", domain=")
218                .append(domain());
219         }
220         if (path() != null) {
221             buf.append(", path=")
222                .append(path());
223         }
224         if (maxAge() >= 0) {
225             buf.append(", maxAge=")
226                .append(maxAge())
227                .append('s');
228         }
229         if (isSecure()) {
230             buf.append(", secure");
231         }
232         if (isHttpOnly()) {
233             buf.append(", HTTPOnly");
234         }
235         return buf.toString();
236     }
237 }