001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.markup.xsp;
018:
019: import org.apache.cocoon.Constants;
020: import org.apache.cocoon.environment.Cookie;
021: import org.apache.cocoon.environment.ObjectModelHelper;
022: import org.apache.cocoon.environment.Request;
023: import org.apache.cocoon.environment.Response;
024: import org.apache.commons.lang.BooleanUtils;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.SAXException;
027:
028: import java.util.Enumeration;
029: import java.util.Hashtable;
030: import java.util.Map;
031:
032: /**
033: * This class is a helper class used by Cookie logicsheet
034: *
035: * This class contains following methods:
036: * <pre>
037: * public static void addCookie(Map , String , String , String , String ,
038: * int , String , String , int);
039: * public static Cookie[] getCookies(Map);
040: * public static void getCookies(Map , ContentHandler)
041: * throws SAXException;
042: * public static Cookie getCookie(Map , String ,int )
043: * throws SAXException;
044: * public static void getCookie(Map ,String ,int , ContentHandler)
045: * throws SAXException;
046: * public static String getComment(Map ,String , int);
047: * public static String getDomain(Map , String , int);
048: * public static String getMaxAge(Map ,String , int);
049: * public static String getName(Map ,String , int);
050: * public static String getPath(Map , String , int);
051: * public static String getSecure(Map , String , int);
052: * public static String getValue(Map , String , int);
053: * public static String getVersion(Map , String , int);
054: * private static String returnCookieProperty(Map ,String ,int ,String );
055: * </pre>
056: *
057: * @version CVS $Id: XSPCookieHelper.java 433543 2006-08-22 06:22:54Z crossley $
058: */
059: public class XSPCookieHelper extends XSPObjectHelper {
060: /**
061: * Assign values to the object's namespace uri and prefix
062: */
063: private static final String URI = Constants.XSP_COOKIE_URI;
064: private static final String PREFIX = Constants.XSP_COOKIE_PREFIX;
065:
066: /**
067: * This method will set a new cookie with values that are passed through parameters
068: *
069: * @param objectModel
070: * @param name name to be set for the cookie
071: * @param value value to be set for the cookie
072: * @param comment comment to be set for the cookie
073: * @param domain domain to be set for the cookie
074: * @param maxage maxage to be set for the cookie
075: * @param path path to be set for the cookie
076: * @param secure secure property to be set for the cookie
077: * @param version version to be set for the cookie
078: */
079: public static void addCookie(Map objectModel, String name,
080: String value, String comment, String domain, int maxage,
081: String path, String secure, int version) {
082: Response response = ObjectModelHelper.getResponse(objectModel);
083: Cookie cookieToSet = response.createCookie(name, value);
084:
085: if ((comment.trim()).length() > 0)
086: cookieToSet.setComment(comment);
087:
088: if ((domain.trim()).length() > 0)
089: cookieToSet.setDomain(domain);
090:
091: if (maxage > 0)
092: cookieToSet.setMaxAge(maxage);
093:
094: if ((path.trim()).length() > 0)
095: cookieToSet.setPath("/");
096:
097: cookieToSet.setSecure(BooleanUtils.toBoolean(secure));
098: cookieToSet.setVersion(version);
099: response.addCookie(cookieToSet);
100: }
101:
102: /**
103: * This method is used to return all the cookies that present in the passed request object
104: *
105: * @param objectModel
106: * @return an array of Cookie is returned
107: */
108: public static Cookie[] getCookies(Map objectModel) {
109: Request request = ObjectModelHelper.getRequest(objectModel);
110: return request.getCookies();
111: }
112:
113: /**
114: * This method is used to write the values of all the cookies in the resulting XML tree
115: * The structure that will be added to the XML tree will be
116: * <pre>
117: * <cookies>
118: * <cookie>
119: * <name>......</name>
120: * <value>.....</value>
121: * <comment>...</comment>
122: * <domain>....</domain>
123: * <maxage>....</maxage>
124: * <path>......</path>
125: * <secure>....</secure>
126: * <version>...</version>
127: * </cookie>
128: * <cookie>
129: * ...
130: * </cookie>
131: * ...
132: * </cookies>
133: * </pre>
134: * If the values of any of these is not present those tags will not be present.
135: * @param objectModel
136: * @param contentHandler
137: * @exception SAXException
138: */
139: public static void getCookies(Map objectModel,
140: ContentHandler contentHandler) throws SAXException {
141: Request request = ObjectModelHelper.getRequest(objectModel);
142: Cookie[] cookies = request.getCookies();
143:
144: if (cookies != null && cookies.length > 0) {
145: int count = 0;
146:
147: String tempStr = null;
148:
149: Hashtable nodeTable = new Hashtable();
150: XSPObjectHelper.start(URI, PREFIX, contentHandler,
151: "cookies");
152:
153: for (count = 0; count < cookies.length; count++) {
154: XSPObjectHelper.start(URI, PREFIX, contentHandler,
155: "cookie");
156:
157: if ((tempStr = getName(objectModel, null, count)) != null)
158: nodeTable.put("name", tempStr);
159:
160: if ((tempStr = getValue(objectModel, null, count)) != null)
161: nodeTable.put("value", tempStr);
162:
163: if ((tempStr = getComment(objectModel, null, count)) != null)
164: nodeTable.put("comment", tempStr);
165:
166: if ((tempStr = getDomain(objectModel, null, count)) != null)
167: nodeTable.put("domain", tempStr);
168:
169: if ((tempStr = getMaxAge(objectModel, null, count)) != null)
170: nodeTable.put("maxage", tempStr);
171:
172: if ((tempStr = getPath(objectModel, null, count)) != null)
173: nodeTable.put("path", tempStr);
174:
175: if ((tempStr = getSecure(objectModel, null, count)) != null)
176: nodeTable.put("secure", tempStr);
177:
178: if ((tempStr = getVersion(objectModel, null, count)) != null)
179: nodeTable.put("version", tempStr);
180:
181: Enumeration keys = nodeTable.keys();
182: while (keys.hasMoreElements()) {
183: String nodeName = (String) keys.nextElement();
184: String nodeValue = (String) nodeTable.get(nodeName);
185: XSPObjectHelper.elementData(URI, PREFIX,
186: contentHandler, nodeName, nodeValue);
187: }
188:
189: XSPObjectHelper.end(URI, PREFIX, contentHandler,
190: "cookie");
191: }
192:
193: XSPObjectHelper.end(URI, PREFIX, contentHandler, "cookies");
194: }
195: }
196:
197: /**
198: * Method used to return a cookie object based on the name or the index that was passed
199: *
200: * If both name and index of cookie to be extracted is passed in, name will take
201: * precedence. Basic thing followed is that, when name is passed, index should be -1 and
202: * when index is passed name should null
203: *
204: * @param objectModel
205: * @param cookieName Name of the cookie which is to be found and returned back
206: * @param cookieIndex Index of the cookie which is to be found and returned
207: * @return cookie object is returned
208: */
209: public static Cookie getCookie(Map objectModel, String cookieName,
210: int cookieIndex) {
211: boolean retrieveByName = false;
212: boolean retrieveByIndex = false;
213: boolean matchFound = false;
214:
215: int count = 0;
216:
217: Request request = ObjectModelHelper.getRequest(objectModel);
218: Cookie currentCookie = null;
219:
220: if (cookieName != null) {
221: retrieveByName = true;
222: } else if (cookieIndex >= 0) {
223: retrieveByIndex = true;
224: }
225:
226: Cookie[] cookies = request.getCookies();
227: if (cookies != null && retrieveByName) {
228: for (count = 0; count < cookies.length; count++) {
229: currentCookie = cookies[count];
230: if (currentCookie.getName().equals(cookieName)) {
231: matchFound = true;
232: break;
233: }
234: }
235: } else if (cookies != null && retrieveByIndex) {
236: if (cookies.length > cookieIndex) {
237: currentCookie = cookies[cookieIndex];
238: matchFound = true;
239: }
240: }
241:
242: if (matchFound)
243: return currentCookie;
244: else
245: return null;
246: }
247:
248: /**
249: * This method is used to find a cookie by it's name or index and place it in
250: * the XML resulting tree
251: *
252: * The xml structure that will be inserted will be,
253: * <pre>
254: * <cookie>
255: * <name>......</name>
256: * <value>.....</value>
257: * <comment>...</comment>
258: * <domain>....</domain>
259: * <maxage>....</maxage>
260: * <path>......</path>
261: * <secure>....</secure>
262: * <version>...</version>
263: * </cookie>
264: * </pre>
265: * @param objectModel
266: * @param cookieName name of the cookie which is to be found
267: * @param cookieIndex index of the cookie which is to be found
268: * @param contentHandler
269: * @exception SAXException
270: */
271: public static void getCookie(Map objectModel, String cookieName,
272: int cookieIndex, ContentHandler contentHandler)
273: throws SAXException {
274: boolean retrieveByName = false;
275: boolean retrieveByIndex = false;
276:
277: String tempStr = null;
278: Hashtable nodeTable = new Hashtable();
279:
280: if (cookieName != null) {
281: retrieveByName = true;
282: } else if (cookieIndex >= 0) {
283: retrieveByIndex = true;
284: }
285:
286: if (retrieveByName || retrieveByIndex)
287: tempStr = getName(objectModel, cookieName, cookieIndex);
288:
289: if (tempStr != null) {
290: XSPObjectHelper
291: .start(URI, PREFIX, contentHandler, "cookie");
292:
293: // name
294: nodeTable.put("name", tempStr);
295:
296: // value
297: if ((tempStr = getValue(objectModel, cookieName,
298: cookieIndex)) != null)
299: nodeTable.put("value", tempStr);
300:
301: //comment
302: if ((tempStr = getComment(objectModel, cookieName,
303: cookieIndex)) != null)
304: nodeTable.put("comment", tempStr);
305:
306: //value
307: if ((tempStr = getDomain(objectModel, cookieName,
308: cookieIndex)) != null)
309: nodeTable.put("domain", tempStr);
310:
311: // maxage
312: if ((tempStr = getMaxAge(objectModel, cookieName,
313: cookieIndex)) != null)
314: nodeTable.put("maxage", tempStr);
315:
316: // path
317: if ((tempStr = getPath(objectModel, cookieName, cookieIndex)) != null)
318: nodeTable.put("path", tempStr);
319:
320: // secure
321: if ((tempStr = getSecure(objectModel, cookieName,
322: cookieIndex)) != null)
323: nodeTable.put("secure", tempStr);
324:
325: // version
326: if ((tempStr = getVersion(objectModel, cookieName,
327: cookieIndex)) != null)
328: nodeTable.put("version", tempStr);
329:
330: Enumeration keys = nodeTable.keys();
331: while (keys.hasMoreElements()) {
332: String nodeName = (String) keys.nextElement();
333: String nodeValue = (String) nodeTable.get(nodeName);
334: XSPObjectHelper.elementData(URI, PREFIX,
335: 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 getComment(Map objectModel, String cookieName,
356: int cookieIndex) {
357: return returnCookieProperty(objectModel, cookieName,
358: cookieIndex, "C");
359: }
360:
361: /**
362: * Method to return the value of domain for a particular cookie based
363: * on it's name or index
364: *
365: * Rule for passing name and index of the cookie remains same as specified in
366: * previous method(s)
367: *
368: * @param objectModel
369: * @param cookieName name of the cookie whose domain is to be passed
370: * @param cookieIndex index of the cookie whose domain is to be passed
371: * @return a string is returned containing the domain of the cookie, if not found
372: * then null is returned
373: */
374: public static String getDomain(Map objectModel, String cookieName,
375: int cookieIndex) {
376: return returnCookieProperty(objectModel, cookieName,
377: cookieIndex, "D");
378: }
379:
380: /**
381: * Method to return the value of maxage for a particular cookie based
382: * on it's name or index
383: *
384: * Rule for passing name and index of the cookie remains same as specified in
385: * previous method(s)
386: *
387: * @param objectModel
388: * @param cookieName name of the cookie whose maxage is to be passed
389: * @param cookieIndex index of the cookie whose maxage is to be passed
390: * @return a string is returned containing the maxage of the cookie, if not found
391: * then null is returned
392: */
393: public static String getMaxAge(Map objectModel, String cookieName,
394: int cookieIndex) {
395: return returnCookieProperty(objectModel, cookieName,
396: cookieIndex, "M");
397: }
398:
399: /**
400: * Method to return the value of name for a particular cookie based
401: * on it's name or index
402: *
403: * Rule for passing name and index of the cookie remains same as specified in
404: * previous method(s)
405: *
406: * @param objectModel
407: * @param cookieName name of the cookie whose name is to be passed
408: * @param cookieIndex index of the cookie whose name is to be passed
409: * @return a string is returned containing the name of the cookie, if not found
410: * then null is returned
411: */
412: public static String getName(Map objectModel, String cookieName,
413: int cookieIndex) {
414: return returnCookieProperty(objectModel, cookieName,
415: cookieIndex, "N");
416: }
417:
418: /**
419: * Method to return the value of path for a particular cookie based
420: * on it's name or index
421: *
422: * Rule for passing name and index of the cookie remains same as specified in
423: * previous method(s)
424: *
425: * @param objectModel
426: * @param cookieName name of the cookie whose path is to be passed
427: * @param cookieIndex index of the cookie whose path is to be passed
428: * @return a string is returned containing the path of the cookie, if not found
429: * then null is returned
430: */
431: public static String getPath(Map objectModel, String cookieName,
432: int cookieIndex) {
433: return returnCookieProperty(objectModel, cookieName,
434: cookieIndex, "P");
435: }
436:
437: /**
438: * Method to return the value of secure property for a particular cookie based
439: * on it's name or index
440: *
441: * Rule for passing name and index of the cookie remains same as specified in
442: * previous method(s)
443: *
444: * @param objectModel
445: * @param cookieName name of the cookie whose secure property is to be passed
446: * @param cookieIndex index of the cookie whose secure property is to be passed
447: * @return a string is returned containing the secure property of the cookie, if not found
448: * then null is returned
449: */
450: public static String getSecure(Map objectModel, String cookieName,
451: int cookieIndex) {
452: return returnCookieProperty(objectModel, cookieName,
453: cookieIndex, "S");
454: }
455:
456: /**
457: * Method to return the value for a particular cookie based on it's name or index
458: *
459: * Rule for passing name and index of the cookie remains same as specified in
460: * previous method(s)
461: *
462: * @param objectModel
463: * @param cookieName name of the cookie whose value is to be passed
464: * @param cookieIndex index of the cookie whose value
465: * @return a string is returned containing the value of the cookie, if not found
466: * then null is returned
467: */
468: public static String getValue(Map objectModel, String cookieName,
469: int cookieIndex) {
470: return returnCookieProperty(objectModel, cookieName,
471: cookieIndex, "V");
472: }
473:
474: /**
475: * Method to return the version of comment for a particular cookie based
476: * on it's name or index
477: *
478: * Rule for passing name and index of the cookie remains same as specified in
479: * previous method(s)
480: *
481: * @param objectModel
482: * @param cookieName name of the cookie whose version is to be passed
483: * @param cookieIndex index of the cookie whose version is to be passed
484: * @return a string is returned containing the version of the cookie, if not found
485: * then null is returned
486: */
487: public static String getVersion(Map objectModel, String cookieName,
488: int cookieIndex) {
489: return returnCookieProperty(objectModel, cookieName,
490: cookieIndex, "Ve");
491: }
492:
493: /**
494: * Method returnCookieProperty will be used to retrieve the property
495: * value of cookie. This method will return the value based on the name
496: * or the index of the cookie that is passed.
497: *
498: * Cookie properties,
499: * comment = "C"
500: * domain = "D"
501: * maxage = "M"
502: * name = "N"
503: * path = "P"
504: * secure = "S"
505: * value = "V"
506: * version = "Ve"
507: *
508: * @param objectModel
509: * @param cookieName Name of the cookie whose property is to be returned.
510: * This value will be null if cookie index is specified
511: * @param cookieIndex Index of the cookie whose property is to be returned.
512: * This property will be -1 if cookie name is specified.
513: * If both name and index are specified, name will take
514: * preference.
515: * @param propertyPrefix Specifies the property whose value if to be retrieved.
516: * Properties have been specifed above
517: * @return If the name or index that is passed is improper then a null value
518: * will be returned, otherwise whatever will be the extracted value of
519: * the property will be returned.
520: */
521: private static String returnCookieProperty(Map objectModel,
522: String cookieName, int cookieIndex, String propertyPrefix) {
523: Cookie currentCookie = getCookie(objectModel, cookieName,
524: cookieIndex);
525:
526: String returnValue = null;
527: if (currentCookie != null) {
528: if (propertyPrefix.equals("C"))
529: returnValue = currentCookie.getComment();
530: else if (propertyPrefix.equals("D"))
531: returnValue = currentCookie.getDomain();
532: else if (propertyPrefix.equals("M"))
533: returnValue = Integer.toString(currentCookie
534: .getMaxAge());
535: else if (propertyPrefix.equals("N"))
536: returnValue = currentCookie.getName();
537: else if (propertyPrefix.equals("P"))
538: returnValue = currentCookie.getPath();
539: else if (propertyPrefix.equals("S"))
540: returnValue = String.valueOf(currentCookie.getSecure());
541: else if (propertyPrefix.equals("V"))
542: returnValue = currentCookie.getValue();
543: else if (propertyPrefix.equals("Ve"))
544: returnValue = Integer.toString(currentCookie
545: .getVersion());
546: }
547:
548: return returnValue;
549: }
550: }
|