1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.cocoon.Constants; 20 import org.apache.cocoon.environment.ObjectModelHelper; 21 import org.apache.cocoon.environment.Request; 22 import org.apache.cocoon.environment.Session; 23 24 import org.xml.sax.ContentHandler ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import java.util.Locale ; 32 import java.util.Map ; 33 34 40 public class XSPRequestHelper { 41 44 private static final String URI = Constants.XSP_REQUEST_URI; 45 private static final String PREFIX = Constants.XSP_REQUEST_PREFIX; 46 47 private static void getLocale(Locale locale, ContentHandler handler) 48 throws SAXException 49 { 50 XSPObjectHelper.start(URI, PREFIX, handler, "locale"); 51 52 XSPObjectHelper.elementData(URI, PREFIX, handler, "language", 53 locale.getLanguage()); 54 XSPObjectHelper.elementData(URI, PREFIX, handler, "country", 55 locale.getCountry()); 56 XSPObjectHelper.elementData(URI, PREFIX, handler, "variant", 57 locale.getVariant()); 58 59 XSPObjectHelper.end(URI, PREFIX, handler, "locale"); 60 } 61 62 public static void getLocale(Map objectModel, ContentHandler handler) 63 throws SAXException { 64 Request request = ObjectModelHelper.getRequest(objectModel); 65 getLocale(request.getLocale(), handler); 66 } 67 68 72 public static Locale [] getLocales(Map objectModel) { 73 Request request = ObjectModelHelper.getRequest(objectModel); 74 75 ArrayList a = new ArrayList (10); 76 Enumeration e = request.getLocales(); 77 while (e.hasMoreElements()) { 78 a.add(e.nextElement()); 79 } 80 81 return (Locale [])a.toArray(new Locale [a.size()]); 82 } 83 84 90 public static void getLocales(Map objectModel, ContentHandler contentHandler) 91 throws SAXException { 92 Request request = ObjectModelHelper.getRequest(objectModel); 93 XSPObjectHelper.start(URI, PREFIX, contentHandler, "locales"); 94 Enumeration e = request.getLocales(); 95 while (e.hasMoreElements()) { 96 getLocale((Locale )e.nextElement(), contentHandler); 97 } 98 XSPObjectHelper.end(URI, PREFIX, contentHandler, "locales"); 99 } 100 101 109 public static String getParameter(Map objectModel, String name, 110 String defaultValue) { 111 return getParameter(objectModel, name, defaultValue, null, null); 112 } 113 114 124 public static String getParameter(Map objectModel, String name, 125 String defaultValue, String form_encoding, 126 String container_encoding) { 127 if(container_encoding == null) 128 container_encoding = "ISO-8859-1"; 130 Request request = ObjectModelHelper.getRequest(objectModel); 131 String value = request.getParameter(name); 132 if(form_encoding != null && value != null && value.length() > 0) { 133 try { 134 value = new String (value.getBytes(container_encoding), form_encoding); 135 } catch(java.io.UnsupportedEncodingException uee) { 136 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 137 } 138 } 139 140 if (value == null) { 141 value = defaultValue; 142 } 143 144 return value; 145 } 146 147 157 public static void getParameter(Map objectModel, ContentHandler contentHandler, 158 String name, String defaultValue) 159 throws SAXException { 160 AttributesImpl attr = new AttributesImpl (); 161 XSPObjectHelper.addAttribute(attr, "name", name); 162 163 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter", 164 getParameter(objectModel, name, defaultValue, null, null), attr); 165 } 166 167 179 public static void getParameter(Map objectModel, ContentHandler contentHandler, 180 String name, String defaultValue, 181 String form_encoding, 182 String container_encoding) 183 throws SAXException { 184 AttributesImpl attr = new AttributesImpl (); 185 XSPObjectHelper.addAttribute(attr, "name", name); 186 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter", 187 String.valueOf(getParameter(objectModel, name, defaultValue, 188 form_encoding, container_encoding)), attr); 189 } 190 191 198 public static void getParameterValues(Map objectModel, 199 ContentHandler contentHandler, 200 String name) 201 throws SAXException { 202 Request request = ObjectModelHelper.getRequest(objectModel); 203 AttributesImpl attr = new AttributesImpl (); 204 XSPObjectHelper.addAttribute(attr, "name", name); 205 206 XSPObjectHelper.start(URI, PREFIX, contentHandler, 207 "parameter-values", attr); 208 209 String [] values = request.getParameterValues(name); 210 211 if (values != null) { 212 for (int i = 0; i < values.length; i++) { 213 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, 214 "value", values[i]); 215 } 216 } 217 218 XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-values"); 219 } 220 221 230 public static void getParameterValues(Map objectModel, 231 ContentHandler contentHandler, 232 String name, String form_encoding, 233 String container_encoding) 234 throws SAXException { 235 Request request = ObjectModelHelper.getRequest(objectModel); 236 237 AttributesImpl attr = new AttributesImpl (); 238 XSPObjectHelper.addAttribute(attr, "name", name); 239 XSPObjectHelper.start(URI, PREFIX, contentHandler, 240 "parameter-values", attr); 241 242 String [] values = request.getParameterValues(name); 243 if (values != null) { 244 for (int i = 0; i < values.length; i++) { 245 if(form_encoding != null && values[i] != null && 246 values[i].length() > 0) { 247 try { 248 values[i] = new String (values[i].getBytes(container_encoding), 249 form_encoding); 250 } catch(java.io.UnsupportedEncodingException uee) { 251 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 252 } 253 } 254 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, 255 "value", values[i]); 256 } 257 } 258 259 XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-values"); 260 } 261 262 270 public static String [] getParameterValues(Map objectModel, String name, 271 String form_encoding, 272 String container_encoding) { 273 Request request = ObjectModelHelper.getRequest(objectModel); 274 275 String [] values = request.getParameterValues(name); 276 277 if (values != null) { 278 for (int i = 0; i < values.length; i++) { 279 if(form_encoding != null && values[i] != null && 280 values[i].length() > 0) { 281 try { 282 values[i] = new String (values[i].getBytes(container_encoding), 283 form_encoding); 284 } catch(java.io.UnsupportedEncodingException uee) { 285 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 286 } 287 } 288 } 289 } 290 return values; 291 } 292 293 298 public static String [] getParameterNames(Map objectModel) { 299 Request request = ObjectModelHelper.getRequest(objectModel); 300 301 ArrayList a = new ArrayList (10); 302 Enumeration e = request.getParameterNames(); 303 while (e.hasMoreElements()) { 304 a.add(e.nextElement()); 305 } 306 307 return (String [])a.toArray(new String [a.size()]); 308 } 309 310 317 public static void getParameterNames(Map objectModel, 318 ContentHandler contentHandler) 319 throws SAXException { 320 Request request = ObjectModelHelper.getRequest(objectModel); 321 XSPObjectHelper.start(URI, PREFIX, contentHandler, "parameter-names"); 322 323 Enumeration e = request.getParameterNames(); 324 while (e.hasMoreElements()) { 325 String name = (String ) e.nextElement(); 326 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name); 327 } 328 329 XSPObjectHelper.end(URI, PREFIX, contentHandler, "parameter-names"); 330 } 331 332 339 public static void getHeaderNames(Map objectModel, 340 ContentHandler contentHandler) 341 throws SAXException { 342 Request request = ObjectModelHelper.getRequest(objectModel); 343 XSPObjectHelper.start(URI, PREFIX, contentHandler, "header-names"); 344 345 Enumeration e = request.getHeaderNames(); 346 while (e.hasMoreElements()) { 347 String name = (String ) e.nextElement(); 348 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name); 349 } 350 351 XSPObjectHelper.end(URI, PREFIX, contentHandler, "header-names"); 352 } 353 354 359 public static String [] getHeaderNames(Map objectModel) { 360 Request request = ObjectModelHelper.getRequest(objectModel); 361 362 ArrayList a = new ArrayList (10); 363 Enumeration e = request.getHeaderNames(); 364 while (e.hasMoreElements()) { 365 a.add(e.nextElement()); 366 } 367 368 return (String [])a.toArray(new String [a.size()]); 369 } 370 371 public static String [] getHeaders(Map objectModel, String name) { 372 Request request = ObjectModelHelper.getRequest(objectModel); 373 374 ArrayList a = new ArrayList (10); 375 Enumeration e = request.getHeaders(name); 376 while (e.hasMoreElements()) { 377 a.add(e.nextElement()); 378 } 379 380 return (String [])a.toArray(new String [a.size()]); 381 } 382 383 public static void getHeaders(Map objectModel, String name, 384 ContentHandler contentHandler) 385 throws SAXException { 386 Request request = ObjectModelHelper.getRequest(objectModel); 387 388 AttributesImpl attr = new AttributesImpl (); 389 XSPObjectHelper.addAttribute(attr, "name", name); 390 XSPObjectHelper.start(URI, PREFIX, contentHandler, "header-values"); 391 392 Enumeration e = request.getHeaders(name); 393 while (e.hasMoreElements()) { 394 String value = (String ) e.nextElement(); 395 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "value", value); 396 } 397 398 XSPObjectHelper.end(URI, PREFIX, contentHandler, "header-values"); 399 } 400 401 public static Date getDateHeader(Map objectModel, String name) { 402 Request request = ObjectModelHelper.getRequest(objectModel); 403 404 if (name == null || name.length() == 0) { 405 return null; 406 } 407 long dateHeader = request.getDateHeader(name); 408 if (dateHeader == -1) { 409 return null; 410 } 411 return new Date (dateHeader); 412 } 413 414 public static String getDateHeader(Map objectModel, String name, String format) { 415 Request request = ObjectModelHelper.getRequest(objectModel); 416 417 if (name == null || name.length() == 0) { 418 return null; 419 } 420 long dateHeader = request.getDateHeader(name); 421 if (dateHeader == -1) { 422 return null; 423 } 424 if (format != null) format = format.trim(); 425 return XSPUtil.formatDate(new Date (dateHeader), format); 426 } 427 428 435 public static void getAttributeNames(Map objectModel, 436 ContentHandler contentHandler) 437 throws SAXException { 438 Request request = ObjectModelHelper.getRequest(objectModel); 439 XSPObjectHelper.start(URI, PREFIX, contentHandler, "attribute-names"); 440 441 Enumeration e = request.getAttributeNames(); 442 while (e.hasMoreElements()) { 443 String name = (String ) e.nextElement(); 444 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "name", name); 445 } 446 447 XSPObjectHelper.end(URI, PREFIX, contentHandler, "attribute-names"); 448 } 449 450 455 public static String [] getAttributeNames(Map objectModel) 456 { 457 Request request = ObjectModelHelper.getRequest(objectModel); 458 459 ArrayList a = new ArrayList (10); 460 Enumeration e = request.getAttributeNames(); 461 while (e.hasMoreElements()) { 462 a.add(e.nextElement()); 463 } 464 465 return (String [])a.toArray(new String [a.size()]); 466 } 467 468 public static String getRequestedURL(Map objectModel) { 469 Request request = ObjectModelHelper.getRequest(objectModel); 470 StringBuffer uribuf = null; 471 boolean isSecure = request.isSecure(); 472 int port = request.getServerPort(); 473 474 if (isSecure) { 475 uribuf = new StringBuffer ("https://"); 476 } else { 477 uribuf = new StringBuffer ("http://"); 478 } 479 480 uribuf.append(request.getServerName()); 481 if (isSecure) { 482 if (port != 443) { 483 uribuf.append(":").append(port); 484 } 485 } else { 486 if (port != 80) { 487 uribuf.append(":").append(port); 488 } 489 } 490 491 uribuf.append(request.getRequestURI()); 492 return uribuf.toString(); 493 } 494 495 503 public static Object getSessionAttribute(Map objectModel, String name, 504 Object defaultValue ) { 505 506 Session session = ObjectModelHelper.getRequest(objectModel).getSession(); 507 Object obj = session.getAttribute(name); 508 return (obj != null ? obj : defaultValue ); 509 } 510 511 521 public static void getSessionAttribute(Map objectModel, ContentHandler contentHandler, 522 String name, Object defaultValue) 523 throws SAXException { 524 AttributesImpl attr = new AttributesImpl (); 525 XSPObjectHelper.addAttribute(attr, "name", name); 526 527 XSPObjectHelper.elementData(URI, PREFIX, contentHandler, "parameter", 528 getSessionAttribute(objectModel, name, defaultValue).toString(), attr); 529 } 530 } 531 | Popular Tags |