KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > XSPCookieHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 package org.apache.cocoon.components.language.markup.xsp;
17
18 import org.apache.cocoon.Constants;
19 import org.apache.cocoon.environment.Cookie;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.Response;
23 import org.apache.commons.lang.BooleanUtils;
24 import org.xml.sax.ContentHandler JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * This class is a helper class used by Cookie logicsheet
33  *
34  * This class contains following methods:
35  * <pre>
36  * public static void addCookie(Map , String , String , String , String ,
37  * int , String , String , int);
38  * public static Cookie[] getCookies(Map);
39  * public static void getCookies(Map , ContentHandler)
40  * throws SAXException;
41  * public static Cookie getCookie(Map , String ,int )
42  * throws SAXException;
43  * public static void getCookie(Map ,String ,int , ContentHandler)
44  * throws SAXException;
45  * public static String getComment(Map ,String , int);
46  * public static String getDomain(Map , String , int);
47  * public static String getMaxAge(Map ,String , int);
48  * public static String getName(Map ,String , int);
49  * public static String getPath(Map , String , int);
50  * public static String getSecure(Map , String , int);
51  * public static String getValue(Map , String , int);
52  * public static String getVersion(Map , String , int);
53  * private static String returnCookieProperty(Map ,String ,int ,String );
54  * </pre>
55  *
56  * @version CVS $Id: XSPCookieHelper.java 124718 2005-01-09 07:42:10Z antonio $
57  */

58 public class XSPCookieHelper extends XSPObjectHelper {
59     /**
60      * Assign values to the object's namespace uri and prefix
61      */

62     private static final String JavaDoc URI = Constants.XSP_COOKIE_URI;
63     private static final String JavaDoc PREFIX = Constants.XSP_COOKIE_PREFIX;
64
65     /**
66      * This method will set a new cookie with values that are passed through parameters
67      *
68      * @param objectModel
69      * @param name name to be set for the cookie
70      * @param value value to be set for the cookie
71      * @param comment comment to be set for the cookie
72      * @param domain domain to be set for the cookie
73      * @param maxage maxage to be set for the cookie
74      * @param path path to be set for the cookie
75      * @param secure secure property to be set for the cookie
76      * @param version version to be set for the cookie
77      */

78     public static void addCookie(Map JavaDoc objectModel, String JavaDoc name, String JavaDoc value,
79                                     String JavaDoc comment, String JavaDoc domain, int maxage, String JavaDoc path,
80                                     String JavaDoc secure, int version)
81     {
82         Response response = ObjectModelHelper.getResponse(objectModel);
83         Cookie cookieToSet = response.createCookie(name,value);
84
85         if ((comment.trim()).length() > 0)
86             cookieToSet.setComment(comment);
87
88         if ((domain.trim()).length() > 0)
89             cookieToSet.setDomain(domain);
90
91         if (maxage > 0)
92             cookieToSet.setMaxAge(maxage);
93
94         if ((path.trim()).length() > 0)
95             cookieToSet.setPath("/");
96
97         cookieToSet.setSecure(BooleanUtils.toBoolean(secure));
98         cookieToSet.setVersion(version);
99         response.addCookie(cookieToSet);
100     }
101
102
103     /**
104      * This method is used to return all the cookies that present in the passed request object
105      *
106      * @param objectModel
107      * @return an array of Cookie is returned
108      */

109     public static Cookie[] getCookies(Map JavaDoc objectModel)
110     {
111         Request request = ObjectModelHelper.getRequest(objectModel);
112         return request.getCookies();
113     }
114
115     /**
116      * This method is used to write the values of all the cookies in the resulting XML tree
117      * The structure that will be added to the XML tree will be
118      * <pre>
119      * &lt;cookies&gt;
120      * &lt;cookie&gt;
121      * &lt;name&gt;......&lt;/name&gt;
122      * &lt;value&gt;.....&lt;/value&gt;
123      * &lt;comment&gt;...&lt;/comment&gt;
124      * &lt;domain&gt;....&lt;/domain&gt;
125      * &lt;maxage&gt;....&lt;/maxage&gt;
126      * &lt;path&gt;......&lt;/path&gt;
127      * &lt;secure&gt;....&lt;/secure&gt;
128      * &lt;version&gt;...&lt;/version&gt;
129      * &lt;/cookie&gt;
130      * &lt;cookie&gt;
131      * ...
132      * &lt;/cookie&gt;
133      * ...
134      * &lt;/cookies&gt;
135      * </pre>
136      * If the values of any of these is not present those tags will not be present.
137      * @param objectModel
138      * @param contentHandler
139      * @exception SAXException
140      */

141     public static void getCookies(Map JavaDoc objectModel, ContentHandler JavaDoc contentHandler)
142         throws SAXException JavaDoc
143     {
144         Request request = ObjectModelHelper.getRequest(objectModel);
145         Cookie[] cookies = request.getCookies();
146
147         if(cookies != null && cookies.length > 0)
148         {
149             int count = 0;
150
151             String JavaDoc tempStr = null;
152
153             Hashtable JavaDoc nodeTable = new Hashtable JavaDoc();
154             XSPObjectHelper.start(URI, PREFIX, contentHandler, "cookies");
155
156             for(count=0; count<cookies.length; count++)
157             {
158                 XSPObjectHelper.start(URI, PREFIX, contentHandler, "cookie");
159
160                 if ((tempStr = getName(objectModel , null , count)) != null)
161                     nodeTable.put("name", tempStr);
162
163                 if((tempStr = getValue(objectModel , null , count))!=null)
164                     nodeTable.put("value", tempStr);
165
166                 if((tempStr = getComment(objectModel , null , count))!=null)
167                     nodeTable.put("comment", tempStr);
168
169                 if((tempStr = getDomain(objectModel , null , count))!= null)
170                     nodeTable.put("domain", tempStr);
171
172                 if((tempStr = getMaxAge(objectModel , null , count))!=null)
173                     nodeTable.put("maxage", tempStr);
174
175                 if((tempStr = getPath(objectModel , null , count)) != null)
176                     nodeTable.put("path", tempStr);
177
178                 if((tempStr = getSecure(objectModel , null , count)) !=null)
179                     nodeTable.put("secure", tempStr);
180
181                 if((tempStr = getVersion(objectModel , null , count)) != null)
182                     nodeTable.put("version", tempStr);
183
184                 Enumeration JavaDoc keys = nodeTable.keys();
185                 while (keys.hasMoreElements())
186                 {
187                     String JavaDoc nodeName = (String JavaDoc)keys.nextElement();
188                     String JavaDoc nodeValue = (String JavaDoc)nodeTable.get(nodeName);
189                     XSPObjectHelper.elementData(URI, PREFIX, contentHandler, nodeName, nodeValue);
190                 }
191
192                 XSPObjectHelper.end(URI, PREFIX, contentHandler, "cookie");
193             }
194
195             XSPObjectHelper.end(URI, PREFIX, contentHandler, "cookies");
196         }
197     }
198
199     /**
200      * Method used to return a cookie object based on the name or the index that was passed
201      *
202      * If both name and index of cookie to be extracted is passed in, name will take
203      * precedence. Basic thing followed is that, when name is passed, index should be -1 and
204      * when index is passed name should null
205      *
206      * @param objectModel
207      * @param cookieName Name of the cookie which is to be found and returned back
208      * @param cookieIndex Index of the cookie which is to be found and returned
209      * @return cookie object is returned
210      */

211     public static Cookie getCookie(Map JavaDoc objectModel,
212                                    String JavaDoc cookieName,
213                                    int cookieIndex)
214     {
215         boolean retrieveByName = false;
216         boolean retrieveByIndex = false;
217         boolean matchFound = false;
218
219         int count = 0;
220
221         Request request = ObjectModelHelper.getRequest(objectModel);
222         Cookie currentCookie = null;
223
224         if (cookieName != null) {
225             retrieveByName = true;
226         } else if (cookieIndex >=0) {
227             retrieveByIndex = true;
228         }
229
230         Cookie[] cookies = request.getCookies();
231         if (cookies != null && retrieveByName) {
232             for(count = 0; count < cookies.length; count++) {
233                 currentCookie = cookies[count];
234                 if (currentCookie.getName().equals(cookieName)) {
235                     matchFound = true;
236                     break;
237                 }
238             }
239         } else if(cookies != null && retrieveByIndex) {
240             if(cookies.length > cookieIndex) {
241                 currentCookie = cookies[cookieIndex];
242                 matchFound = true;
243             }
244         }
245
246         if (matchFound)
247             return currentCookie;
248         else
249             return null;
250     }
251
252     /**
253      * This method is used to find a cookie by it's name or index and place it in
254      * the XML resulting tree
255      *
256      * The xml structure that will be inserted will be,
257      * <pre>
258      * &lt;cookie&gt;
259      * &lt;name&gt;......&lt;/name&gt;
260      * &lt;value&gt;.....&lt;/value&gt;
261      * &lt;comment&gt;...&lt;/comment&gt;
262      * &lt;domain&gt;....&lt;/domain&gt;
263      * &lt;maxage&gt;....&lt;/maxage&gt;
264      * &lt;path&gt;......&lt;/path&gt;
265      * &lt;secure&gt;....&lt;/secure&gt;
266      * &lt;version&gt;...&lt;/version&gt;
267      * &lt;/cookie&gt;
268      * </pre>
269      * @param objectModel
270      * @param cookieName name of the cookie which is to be found
271      * @param cookieIndex index of the cookie which is to be found
272      * @param contentHandler
273      * @exception SAXException
274      */

275     public static void getCookie(Map JavaDoc objectModel,
276                                  String JavaDoc cookieName,
277                                  int cookieIndex,
278                                  ContentHandler JavaDoc contentHandler)
279         throws SAXException JavaDoc
280     {
281         boolean retrieveByName = false;
282         boolean retrieveByIndex = false;
283
284         String JavaDoc tempStr = null;
285         Hashtable JavaDoc nodeTable = new Hashtable JavaDoc();
286
287         if (cookieName != null) {
288             retrieveByName = true;
289         } else if (cookieIndex >=0) {
290             retrieveByIndex = true;
291         }
292
293         if (retrieveByName || retrieveByIndex)
294             tempStr = getName(objectModel , cookieName , cookieIndex);
295
296         if (tempStr !=null) {
297             XSPObjectHelper.start(URI, PREFIX, contentHandler, "cookie");
298
299             // name
300
nodeTable.put("name", tempStr);
301
302             // value
303
if ((tempStr = getValue(objectModel , cookieName , cookieIndex)) != null)
304                     nodeTable.put("value", tempStr);
305
306             //comment
307
if ((tempStr = getComment(objectModel , cookieName , cookieIndex)) != null)
308                 nodeTable.put("comment", tempStr);
309
310             //value
311
if ((tempStr = getDomain(objectModel , cookieName , cookieIndex)) != null)
312                 nodeTable.put("domain", tempStr);
313
314             // maxage
315
if((tempStr = getMaxAge(objectModel , cookieName , cookieIndex)) != null)
316                     nodeTable.put("maxage", tempStr);
317
318             // path
319
if((tempStr = getPath(objectModel , cookieName , cookieIndex)) != null)
320                 nodeTable.put("path", tempStr);
321
322             // secure
323
if((tempStr = getSecure(objectModel , cookieName , cookieIndex)) != null)
324                 nodeTable.put("secure", tempStr);
325
326             // version
327
if((tempStr = getVersion(objectModel , cookieName , cookieIndex)) != null)
328                 nodeTable.put("version", tempStr);
329
330             Enumeration JavaDoc keys = nodeTable.keys();
331             while (keys.hasMoreElements())
332             {
333                 String JavaDoc nodeName = (String JavaDoc)keys.nextElement();
334                 String JavaDoc nodeValue = (String JavaDoc)nodeTable.get(nodeName);
335                 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, nodeName, nodeValue);
336             }
337
338             XSPObjectHelper.end(URI, PREFIX, contentHandler, "cookie");
339         }
340     }
341
342     /**
343      * Method to return the value of comment for a particular cookie based
344      * on it's name or index
345      *
346      * Rule for passing name and index of the cookie remains same as specified in
347      * previous method(s)
348      *
349      * @param objectModel
350      * @param cookieName name of the cookie whose comment is to be passed
351      * @param cookieIndex index of the cookie whose comment is to be passed
352      * @return a string is returned containing the comment of the cookie, if not found
353      * then null is returned
354      */

355     public static String JavaDoc getComment(Map JavaDoc objectModel, String JavaDoc cookieName, int cookieIndex)
356     {
357         return returnCookieProperty(objectModel , cookieName , cookieIndex , "C");
358     }
359
360     /**
361      * Method to return the value of domain for a particular cookie based
362      * on it's name or index
363      *
364      * Rule for passing name and index of the cookie remains same as specified in
365      * previous method(s)
366      *
367      * @param objectModel
368      * @param cookieName name of the cookie whose domain is to be passed
369      * @param cookieIndex index of the cookie whose domain is to be passed
370      * @return a string is returned containing the domain of the cookie, if not found
371      * then null is returned
372      */

373     public static String JavaDoc getDomain(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
374     {
375         return returnCookieProperty(objectModel , cookieName , cookieIndex , "D");
376     }
377
378     /**
379      * Method to return the value of maxage for a particular cookie based
380      * on it's name or index
381      *
382      * Rule for passing name and index of the cookie remains same as specified in
383      * previous method(s)
384      *
385      * @param objectModel
386      * @param cookieName name of the cookie whose maxage is to be passed
387      * @param cookieIndex index of the cookie whose maxage is to be passed
388      * @return a string is returned containing the maxage of the cookie, if not found
389      * then null is returned
390      */

391     public static String JavaDoc getMaxAge(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
392     {
393         return returnCookieProperty(objectModel , cookieName , cookieIndex , "M");
394     }
395
396     /**
397      * Method to return the value of name for a particular cookie based
398      * on it's name or index
399      *
400      * Rule for passing name and index of the cookie remains same as specified in
401      * previous method(s)
402      *
403      * @param objectModel
404      * @param cookieName name of the cookie whose name is to be passed
405      * @param cookieIndex index of the cookie whose name is to be passed
406      * @return a string is returned containing the name of the cookie, if not found
407      * then null is returned
408      */

409     public static String JavaDoc getName(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
410     {
411         return returnCookieProperty(objectModel , cookieName , cookieIndex , "N");
412     }
413
414     /**
415      * Method to return the value of path for a particular cookie based
416      * on it's name or index
417      *
418      * Rule for passing name and index of the cookie remains same as specified in
419      * previous method(s)
420      *
421      * @param objectModel
422      * @param cookieName name of the cookie whose path is to be passed
423      * @param cookieIndex index of the cookie whose path is to be passed
424      * @return a string is returned containing the path of the cookie, if not found
425      * then null is returned
426      */

427     public static String JavaDoc getPath(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
428     {
429         return returnCookieProperty(objectModel , cookieName , cookieIndex , "P");
430     }
431
432     /**
433      * Method to return the value of secure property for a particular cookie based
434      * on it's name or index
435      *
436      * Rule for passing name and index of the cookie remains same as specified in
437      * previous method(s)
438      *
439      * @param objectModel
440      * @param cookieName name of the cookie whose secure property is to be passed
441      * @param cookieIndex index of the cookie whose secure property is to be passed
442      * @return a string is returned containing the secure property of the cookie, if not found
443      * then null is returned
444      */

445     public static String JavaDoc getSecure(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
446     {
447         return returnCookieProperty(objectModel , cookieName , cookieIndex , "S");
448     }
449
450     /**
451      * Method to return the value for a particular cookie based on it's name or index
452      *
453      * Rule for passing name and index of the cookie remains same as specified in
454      * previous method(s)
455      *
456      * @param objectModel
457      * @param cookieName name of the cookie whose value is to be passed
458      * @param cookieIndex index of the cookie whose value
459      * @return a string is returned containing the value of the cookie, if not found
460      * then null is returned
461      */

462     public static String JavaDoc getValue(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
463     {
464         return returnCookieProperty(objectModel , cookieName , cookieIndex , "V");
465     }
466
467     /**
468      * Method to return the version of comment for a particular cookie based
469      * on it's name or index
470      *
471      * Rule for passing name and index of the cookie remains same as specified in
472      * previous method(s)
473      *
474      * @param objectModel
475      * @param cookieName name of the cookie whose version is to be passed
476      * @param cookieIndex index of the cookie whose version is to be passed
477      * @return a string is returned containing the version of the cookie, if not found
478      * then null is returned
479      */

480     public static String JavaDoc getVersion(Map JavaDoc objectModel, String JavaDoc cookieName , int cookieIndex)
481     {
482         return returnCookieProperty(objectModel , cookieName , cookieIndex , "Ve");
483     }
484
485     /**
486      * Method returnCookieProperty will be used to retrieve the property
487      * value of cookie. This method will return the value based on the name
488      * or the index of the cookie that is passed.
489      *
490      * Cookie properties,
491      * comment = "C"
492      * domain = "D"
493      * maxage = "M"
494      * name = "N"
495      * path = "P"
496      * secure = "S"
497      * value = "V"
498      * version = "Ve"
499      *
500      * @param objectModel
501      * @param cookieName Name of the cookie whose property is to be returned.
502      * This value will be null if cookie index is specified
503      * @param cookieIndex Index of the cookie whose property is to be returned.
504      * This property will be -1 if cookie name is specified.
505      * If both name and index are specified, name will take
506      * preference.
507      * @param propertyPrefix Specifies the property whose value if to be retrieved.
508      * Properties have been specifed above
509      * @return If the name or index that is passed is improper then a null value
510      * will be returned, otherwise whatever will be the extracted value of
511      * the property will be returned.
512      */

513     private static String JavaDoc returnCookieProperty(Map JavaDoc objectModel,
514                                                String JavaDoc cookieName,
515                                                int cookieIndex,
516                                                String JavaDoc propertyPrefix)
517     {
518         Cookie currentCookie = getCookie(objectModel, cookieName, cookieIndex);
519
520         String JavaDoc returnValue = null;
521         if (currentCookie != null) {
522             if(propertyPrefix.equals("C"))
523                 returnValue = currentCookie.getComment();
524             else if(propertyPrefix.equals("D"))
525                 returnValue = currentCookie.getDomain();
526             else if(propertyPrefix.equals("M"))
527                 returnValue = Integer.toString(currentCookie.getMaxAge());
528             else if(propertyPrefix.equals("N"))
529                 returnValue = currentCookie.getName();
530             else if(propertyPrefix.equals("P"))
531                 returnValue = currentCookie.getPath();
532             else if(propertyPrefix.equals("S"))
533                 returnValue = String.valueOf(currentCookie.getSecure());
534             else if(propertyPrefix.equals("V"))
535                 returnValue = currentCookie.getValue();
536             else if(propertyPrefix.equals("Ve"))
537                 returnValue = Integer.toString(currentCookie.getVersion());
538         }
539
540         return returnValue;
541     }
542 }
543
Popular Tags