KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > httpd > CookieWrapper


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * CookieWrapper.java
20  *
21  * This class is responsible for wrapping the access to the cookie information
22  * containied within, and for parsing cookie headers.
23  */

24
25 // package path
26
package com.rift.coad.lib.httpd;
27
28 // logging import
29
import org.apache.log4j.Logger;
30
31 // java imports
32
import java.net.InetAddress JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.TimeZone JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39
40 // coadunation imports
41
import com.rift.coad.lib.configuration.Configuration;
42 import com.rift.coad.lib.configuration.ConfigurationFactory;
43
44 /**
45  * This class is responsible for wrapping the access to the cookie information
46  * containied within, and for parsing cookie headers.
47  *
48  * @author Brett Chaldecott
49  */

50 public class CookieWrapper {
51     
52     // class constants
53
private final static String JavaDoc PATH = "Path";
54     private final static String JavaDoc DOMAIN = "Domain";
55     public final static String JavaDoc VERSION = "Version";
56     public final static String JavaDoc DOLLAR = "$";
57     private final static String JavaDoc VERSION_NUMBER = "1";
58     private final static String JavaDoc COOKIE_HOST = "cookie_host";
59     
60     // private member variables
61
private Logger log =
62             Logger.getLogger(CookieWrapper.class.getName());
63     private String JavaDoc name = null;
64     private String JavaDoc value = null;
65     private String JavaDoc domain = null;
66     private String JavaDoc path = null;
67     
68     
69     /**
70      * The constructor of the class responsible for processing the raw source.
71      *
72      * @param rawSrouce The raw source to process.
73      * @exception HttpdException
74      */

75     public CookieWrapper(String JavaDoc rawSource)
76             throws HttpdException {
77         // check for valid formatting of cookie
78
if (rawSource.contains("=") == false) {
79             throw new HttpdException(
80                     "Incorrectly formated cookie [" + rawSource + "]");
81         }
82         name = rawSource.substring(0,rawSource.indexOf('='))
83                 .trim().toLowerCase();
84         value = stripInvertedCommas(
85                 rawSource.substring(rawSource.indexOf('=') + 1).trim())
86                 .trim();
87     }
88     
89     
90     /**
91      *
92      * Creates a new instance of CookieWrapper
93      *
94      *
95      * @param name The name associated with this cookie.
96      * @param value The value wrapped within.
97      * @exception HttpdException
98      */

99     public CookieWrapper(String JavaDoc name, String JavaDoc value) throws HttpdException {
100         try {
101             Configuration config = ConfigurationFactory.getInstance().
102                     getConfig(CookieWrapper.class);
103             this.name = name.toLowerCase().trim();
104             this.value = value;
105         } catch (Exception JavaDoc ex) {
106             throw new HttpdException("Failed to init the cookie wrapper : " +
107                     ex.getMessage(),ex);
108         }
109     }
110     
111     
112     /**
113      * This method returns the name of this cookie.
114      *
115      * @return The string containing the name of this cookie.
116      */

117     public String JavaDoc getName() {
118         return name;
119     }
120     
121     
122     /**
123      * This method returns the value contained within.
124      *
125      * @return The string containing the value of the cookie.
126      */

127     public String JavaDoc getValue() {
128         return value;
129     }
130     
131     
132     /**
133      * This method sets the value of the cookie.
134      *
135      * @param value The value to store in the cookie.
136      */

137     public void setValue(String JavaDoc value) {
138         this.value = value;
139     }
140     
141     
142     /**
143      * This method returns the domain name.
144      *
145      * @return The string containing the domain name.
146      */

147     public String JavaDoc getDomain() {
148         return domain;
149     }
150     
151     
152     /**
153      * This method sets the domain key value.
154      *
155      * @param domain The new domain to set.
156      */

157     public void setDomain(String JavaDoc domain) {
158         this.domain = domain;
159     }
160     
161     
162     /**
163      * This method returns the path under which the cookie will be available.
164      *
165      * @return The path the cookie can be used on.
166      */

167     public String JavaDoc getPath() {
168         return path;
169     }
170     
171     
172     /**
173      * This method sets the path.
174      *
175      * @param path The path to the object.
176      */

177     public void setPath(String JavaDoc path) {
178         this.path = path;
179     }
180     
181     
182     /**
183      * This method generates a cookie string that can be used by Set-Cookie.
184      *
185      * @return A string that can be used as a Set cookie header.
186      */

187     public String JavaDoc getSetCookieString() {
188         StringBuffer JavaDoc cookieBuffer = new StringBuffer JavaDoc();
189         cookieBuffer.append(name).append("=").append(value).append(";").
190                 append(VERSION).append("=").append(VERSION_NUMBER);
191         if (domain != null) {
192             cookieBuffer.append(";").append(DOMAIN).append("=").append(domain);
193         }
194         if (path != null) {
195             cookieBuffer.append(";").append(PATH).append("=").append(path);
196         }
197         return cookieBuffer.toString();
198     }
199     
200     
201     /**
202      * This method strips the inverted commas off a string.
203      *
204      * @return The stripped string.
205      * @param value The string to strip the values off of.
206      */

207     private String JavaDoc stripInvertedCommas(String JavaDoc value) {
208         int beginPos = value.indexOf('"');
209         if (beginPos == -1) {
210             return value;
211         }
212         int endPos = value.indexOf('"',beginPos);
213         return value.substring(beginPos,endPos);
214     }
215 }
216
Popular Tags