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 if (that.domain() == null) {
157             return false;
158         } else {
159             return domain().equalsIgnoreCase(that.domain());
160         }
161 
162         return true;
163     }
164 
165     @Override
166     public int compareTo(Cookie c) {
167         int v = name().compareTo(c.name());
168         if (v != 0) {
169             return v;
170         }
171 
172         if (path() == null) {
173             if (c.path() != null) {
174                 return -1;
175             }
176         } else if (c.path() == null) {
177             return 1;
178         } else {
179             v = path().compareTo(c.path());
180             if (v != 0) {
181                 return v;
182             }
183         }
184 
185         if (domain() == null) {
186             if (c.domain() != null) {
187                 return -1;
188             }
189         } else if (c.domain() == null) {
190             return 1;
191         } else {
192             v = domain().compareToIgnoreCase(c.domain());
193             return v;
194         }
195 
196         return 0;
197     }
198 
199     /**
200      * Validate a cookie attribute value, throws a {@link IllegalArgumentException} otherwise.
201      * Only intended to be used by {@link io.netty.handler.codec.http.DefaultCookie}.
202      * @param name attribute name
203      * @param value attribute value
204      * @return the trimmed, validated attribute value
205      * @deprecated CookieUtil is package private, will be removed once old Cookie API is dropped
206      */
207     @Deprecated
208     protected String validateValue(String name, String value) {
209         return validateAttributeValue(name, value);
210     }
211 
212     @Override
213     public String toString() {
214         StringBuilder buf = stringBuilder()
215             .append(name())
216             .append('=')
217             .append(value());
218         if (domain() != null) {
219             buf.append(", domain=")
220                .append(domain());
221         }
222         if (path() != null) {
223             buf.append(", path=")
224                .append(path());
225         }
226         if (maxAge() >= 0) {
227             buf.append(", maxAge=")
228                .append(maxAge())
229                .append('s');
230         }
231         if (isSecure()) {
232             buf.append(", secure");
233         }
234         if (isHttpOnly()) {
235             buf.append(", HTTPOnly");
236         }
237         return buf.toString();
238     }
239 }