1 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 ; 25 import org.xml.sax.SAXException ; 26 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Map ; 30 31 58 public class XSPCookieHelper extends XSPObjectHelper { 59 62 private static final String URI = Constants.XSP_COOKIE_URI; 63 private static final String PREFIX = Constants.XSP_COOKIE_PREFIX; 64 65 78 public static void addCookie(Map objectModel, String name, String value, 79 String comment, String domain, int maxage, String path, 80 String 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 109 public static Cookie[] getCookies(Map objectModel) 110 { 111 Request request = ObjectModelHelper.getRequest(objectModel); 112 return request.getCookies(); 113 } 114 115 141 public static void getCookies(Map objectModel, ContentHandler contentHandler) 142 throws SAXException 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 tempStr = null; 152 153 Hashtable nodeTable = new Hashtable (); 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 keys = nodeTable.keys(); 185 while (keys.hasMoreElements()) 186 { 187 String nodeName = (String )keys.nextElement(); 188 String nodeValue = (String )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 211 public static Cookie getCookie(Map objectModel, 212 String 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 275 public static void getCookie(Map objectModel, 276 String cookieName, 277 int cookieIndex, 278 ContentHandler contentHandler) 279 throws SAXException 280 { 281 boolean retrieveByName = false; 282 boolean retrieveByIndex = false; 283 284 String tempStr = null; 285 Hashtable nodeTable = new Hashtable (); 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 nodeTable.put("name", tempStr); 301 302 if ((tempStr = getValue(objectModel , cookieName , cookieIndex)) != null) 304 nodeTable.put("value", tempStr); 305 306 if ((tempStr = getComment(objectModel , cookieName , cookieIndex)) != null) 308 nodeTable.put("comment", tempStr); 309 310 if ((tempStr = getDomain(objectModel , cookieName , cookieIndex)) != null) 312 nodeTable.put("domain", tempStr); 313 314 if((tempStr = getMaxAge(objectModel , cookieName , cookieIndex)) != null) 316 nodeTable.put("maxage", tempStr); 317 318 if((tempStr = getPath(objectModel , cookieName , cookieIndex)) != null) 320 nodeTable.put("path", tempStr); 321 322 if((tempStr = getSecure(objectModel , cookieName , cookieIndex)) != null) 324 nodeTable.put("secure", tempStr); 325 326 if((tempStr = getVersion(objectModel , cookieName , cookieIndex)) != null) 328 nodeTable.put("version", tempStr); 329 330 Enumeration keys = nodeTable.keys(); 331 while (keys.hasMoreElements()) 332 { 333 String nodeName = (String )keys.nextElement(); 334 String nodeValue = (String )nodeTable.get(nodeName); 335 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, nodeName, nodeValue); 336 } 337 338 XSPObjectHelper.end(URI, PREFIX, contentHandler, "cookie"); 339 } 340 } 341 342 355 public static String getComment(Map objectModel, String cookieName, int cookieIndex) 356 { 357 return returnCookieProperty(objectModel , cookieName , cookieIndex , "C"); 358 } 359 360 373 public static String getDomain(Map objectModel, String cookieName , int cookieIndex) 374 { 375 return returnCookieProperty(objectModel , cookieName , cookieIndex , "D"); 376 } 377 378 391 public static String getMaxAge(Map objectModel, String cookieName , int cookieIndex) 392 { 393 return returnCookieProperty(objectModel , cookieName , cookieIndex , "M"); 394 } 395 396 409 public static String getName(Map objectModel, String cookieName , int cookieIndex) 410 { 411 return returnCookieProperty(objectModel , cookieName , cookieIndex , "N"); 412 } 413 414 427 public static String getPath(Map objectModel, String cookieName , int cookieIndex) 428 { 429 return returnCookieProperty(objectModel , cookieName , cookieIndex , "P"); 430 } 431 432 445 public static String getSecure(Map objectModel, String cookieName , int cookieIndex) 446 { 447 return returnCookieProperty(objectModel , cookieName , cookieIndex , "S"); 448 } 449 450 462 public static String getValue(Map objectModel, String cookieName , int cookieIndex) 463 { 464 return returnCookieProperty(objectModel , cookieName , cookieIndex , "V"); 465 } 466 467 480 public static String getVersion(Map objectModel, String cookieName , int cookieIndex) 481 { 482 return returnCookieProperty(objectModel , cookieName , cookieIndex , "Ve"); 483 } 484 485 513 private static String returnCookieProperty(Map objectModel, 514 String cookieName, 515 int cookieIndex, 516 String propertyPrefix) 517 { 518 Cookie currentCookie = getCookie(objectModel, cookieName, cookieIndex); 519 520 String 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 |