View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.http.cookie;
14  
15  import java.text.ParseException;
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  /**
20   * Definition of a cookie
21   *
22   * @author Daniel Sendula
23   */
24  public class Cookie {
25  
26      /** Format of the cookie date */
27      public static final SimpleDateFormat cookieFormat = new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss");
28  
29      /** Cookie name */
30      protected String name;
31  
32      /** Cookie value */
33      protected String value;
34  
35      /** Date cookie is going expire */
36      protected Date expires;
37  
38      /** Cookie domain */
39      protected String domain;
40  
41      /** Cookie path */
42      protected String path;
43  
44      /** Is cookie secure */
45      protected boolean secure = false;
46  
47      /**
48       * Creates new cookie to be sent to the browser
49       */
50      public Cookie() {
51      }
52  
53      /**
54       * Creates cooke from the request headers
55       * @param header request header
56       * @throws ParseException thrown if "=" is missing
57       */
58      public Cookie(String header) throws ParseException {
59          parseCookie(header);
60      }
61  
62      /**
63       * Returns cookie's domain
64       * @return the domain
65       */
66      public String getDomain() {
67          return domain;
68      }
69  
70      /**
71       * Sets cookie's domain
72       * @param domain the domain to set
73       */
74      public void setDomain(String domain) {
75          this.domain = domain;
76      }
77  
78      /**
79       * Returns expire date of the cookie
80       * @return the expires
81       */
82      public Date getExpires() {
83          return expires;
84      }
85  
86      /**
87       * Sets expire date of the cookie
88       * @param expires the expires to set
89       */
90      public void setExpires(Date expires) {
91          this.expires = expires;
92      }
93  
94      /**
95       * Returns cookie name
96       * @return the name
97       */
98      public String getName() {
99          return name;
100     }
101 
102     /**
103      * Sets cookie name
104      * @param name the name to set
105      */
106     public void setName(String name) {
107         this.name = name;
108     }
109 
110     /**
111      * Return cookie's path
112      * @return the path
113      */
114     public String getPath() {
115         return path;
116     }
117 
118     /**
119      * Sets cookie's path
120      * @param path the path to set
121      */
122     public void setPath(String path) {
123         this.path = path;
124     }
125 
126     /**
127      * Returns is cookie is secured
128      * @return the secure
129      */
130     public boolean isSecure() {
131         return secure;
132     }
133 
134     /**
135      * Sets is cookie secure
136      * @param secure the secure to set
137      */
138     public void setSecure(boolean secure) {
139         this.secure = secure;
140     }
141 
142     /**
143      * Returns cookie value
144      * @return the value
145      */
146     public String getValue() {
147         return value;
148     }
149 
150     /**
151      * Sets cookie value
152      * @param value the value to set
153      */
154     public void setValue(String value) {
155         this.value = value;
156     }
157 
158     /**
159      * Returns cookie as string
160      * @return cookie as string
161      */
162     public String toString() {
163         StringBuffer res = new StringBuffer();
164 
165         res.append(name).append('=').append(value);
166 
167         if (getExpires() != null) {
168 
169             // !!! convert time to GMT first!!!
170 
171             res.append("; expires=" + cookieFormat.format(getExpires()+" GMT"));
172 
173         }
174 
175         if (getPath() != null) {
176 
177             res.append("; path=" + getPath());
178 
179         }
180 
181         if (getDomain() != null) {
182 
183             res.append("; domain=" + getDomain());
184 
185         }
186 
187         if (isSecure()) {
188 
189             res.append("; secure");
190 
191         }
192         return res.toString();
193     }
194 
195     /**
196      * Parses cookie definition (in format <name>=<value&gt) and stores
197      * name and value to this cookie.
198      * @param header cookie as given in a header
199      * @throws ParseException thrown if "=" is missing
200      */
201     protected void parseCookie(String header) throws ParseException {
202         int i = header.indexOf('=');
203         if (i < 0) {
204             throw new ParseException("Missing '='", -1);
205         }
206         setName(header.substring(0, i));
207         setValue(header.substring(i+1).trim());
208     }
209 
210 }