View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  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,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.constructs.web;
18  
19  import javax.servlet.http.Cookie;
20  import java.io.Serializable;
21  
22  /**
23   * A serializable cookie which wraps Cookie.
24   * This gets around a NotSerializable error with Cookie when non memory stores are used.
25   *
26   * @version $Id: SerializableCookie.java 512 2007-07-10 09:18:45Z gregluck $
27   * @author <a href="mailto:amurdoch@thoughtworks.com">Adam Murdoch</a>
28   */
29  public class SerializableCookie implements Serializable {
30  
31      private static final long serialVersionUID = 8628587700329421486L;
32  
33      private String name;
34      private String value;
35      private String comment;
36      private String domain;
37      private int maxAge;
38      private String path;
39      private boolean secure;
40      private int version;
41  
42      /** Creates a cookie. */
43      public SerializableCookie(final Cookie cookie) {
44          name = cookie.getName();
45          value = cookie.getValue();
46          comment = cookie.getComment();
47          domain = cookie.getDomain();
48          maxAge = cookie.getMaxAge();
49          path = cookie.getPath();
50          secure = cookie.getSecure();
51          version = cookie.getVersion();
52      }
53  
54      /** Builds a Cookie object from this object. */
55      public Cookie toCookie() {
56          final Cookie cookie = new Cookie(name, value);
57          cookie.setComment(comment);
58          //Otherwise null pointer exception
59          if (domain != null) {
60              cookie.setDomain(domain);
61          }
62          cookie.setMaxAge(maxAge);
63          cookie.setPath(path);
64          cookie.setSecure(secure);
65          cookie.setVersion(version);
66          return cookie;
67      }
68  }