1 16 package org.apache.cocoon.webapps.session.context; 17 18 import java.io.IOException ; 19 import java.io.StringReader ; 20 import java.util.Enumeration ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.logger.Logger; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.environment.Cookie; 28 import org.apache.cocoon.environment.ObjectModelHelper; 29 import org.apache.cocoon.environment.Request; 30 import org.apache.cocoon.transformation.CIncludeTransformer; 31 import org.apache.cocoon.xml.IncludeXMLConsumer; 32 import org.apache.cocoon.xml.dom.DOMUtil; 33 import org.apache.commons.lang.BooleanUtils; 34 import org.apache.excalibur.source.SourceParameters; 35 import org.apache.excalibur.xml.sax.SAXParser; 36 import org.apache.excalibur.xml.xpath.XPathProcessor; 37 import org.w3c.dom.DOMException ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.DocumentFragment ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.xml.sax.ContentHandler ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.ext.LexicalHandler ; 46 47 119 public final class RequestSessionContext 120 implements SessionContext { 121 122 private static final String PARAMETERS_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_PARAMETERS_ELEMENT; 123 private static final String PARAMETER_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_PARAMETER_ELEMENT; 124 private static final String NAME_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_NAME_ELEMENT; 125 private static final String VALUE_ELEMENT = "cinclude:" + CIncludeTransformer.CINCLUDE_VALUE_ELEMENT; 126 127 128 protected Logger logger; 129 130 131 private String name; 132 133 134 transient private Request request; 135 136 137 private Document contextData; 138 139 140 private XPathProcessor xpathProcessor; 141 142 public RequestSessionContext(Logger logger) { 143 this.logger = logger; 144 } 145 146 149 public void setup(String value, String loadResource, String saveResource) { 150 this.name = value; 151 } 152 153 156 public void setup(Map objectModel, ServiceManager manager, XPathProcessor processor) 157 throws ProcessingException { 158 this.xpathProcessor = processor; 159 this.request = ObjectModelHelper.getRequest(objectModel); 160 161 contextData = DOMUtil.createDocument(); 162 contextData.appendChild(contextData.createElementNS(null, "context")); 163 164 Element root = contextData.getDocumentElement(); 165 166 SAXParser parser = null; 167 try { 168 parser = (SAXParser) manager.lookup( SAXParser.ROLE ); 169 this.buildParameterXML(root, parser); 170 } catch (ServiceException ce) { 171 throw new ProcessingException("Unable to lookup parser.", ce); 172 } finally { 173 manager.release(parser ); 174 } 175 this.buildAttributesXML(root); 176 this.buildMiscXML(root); 177 this.buildCookiesXML(root); 178 this.buildHeadersXML(root); 179 } 180 181 184 public String getName() { 185 return this.name; 186 } 187 188 191 public Request getRequest() { 192 return this.request; 193 } 194 195 198 private String createPath(String path) { 199 if (path == null) path = "/"; 200 if (path.startsWith("/") == false) path = "/" + path; 201 path = "/context" + path; 202 if (path.endsWith("/") == true) path = path.substring(0, path.length() - 1); 203 return path; 204 } 205 206 private Node createTextNode(Document doc, String value) { 207 return doc.createTextNode(value != null ? value : ""); 208 } 209 210 213 private void buildMiscXML(Element root) { 214 Document doc = root.getOwnerDocument(); 215 216 Element node; 217 218 node = doc.createElementNS(null, "characterEncoding"); 219 node.appendChild(this.createTextNode(doc, this.request.getCharacterEncoding())); 220 root.appendChild(node); 221 node = doc.createElementNS(null, "contentLength"); 222 node.appendChild(this.createTextNode(doc, "" + this.request.getContentLength())); 223 root.appendChild(node); 224 node = doc.createElementNS(null, "contentType"); 225 node.appendChild(this.createTextNode(doc, this.request.getContentType())); 226 root.appendChild(node); 227 node = doc.createElementNS(null, "protocol"); 228 node.appendChild(this.createTextNode(doc, this.request.getProtocol())); 229 root.appendChild(node); 230 node = doc.createElementNS(null, "remoteAddress"); 231 node.appendChild(this.createTextNode(doc, this.request.getRemoteAddr())); 232 root.appendChild(node); 233 node = doc.createElementNS(null, "remoteHost"); 234 node.appendChild(this.createTextNode(doc, this.request.getRemoteHost())); 235 root.appendChild(node); 236 node = doc.createElementNS(null, "scheme"); 237 node.appendChild(this.createTextNode(doc, this.request.getScheme())); 238 root.appendChild(node); 239 node = doc.createElementNS(null, "serverName"); 240 node.appendChild(this.createTextNode(doc, this.request.getServerName())); 241 root.appendChild(node); 242 node = doc.createElementNS(null, "serverPort"); 243 node.appendChild(this.createTextNode(doc, ""+this.request.getServerPort())); 244 root.appendChild(node); 245 node = doc.createElementNS(null, "method"); 246 node.appendChild(this.createTextNode(doc, this.request.getMethod())); 247 root.appendChild(node); 248 node = doc.createElementNS(null, "contextPath"); 249 node.appendChild(this.createTextNode(doc, this.request.getContextPath())); 250 root.appendChild(node); 251 node = doc.createElementNS(null, "pathInfo"); 252 node.appendChild(this.createTextNode(doc, this.request.getPathInfo())); 253 root.appendChild(node); 254 node = doc.createElementNS(null, "pathTranslated"); 255 node.appendChild(this.createTextNode(doc, this.request.getPathTranslated())); 256 root.appendChild(node); 257 node = doc.createElementNS(null, "remoteUser"); 258 node.appendChild(this.createTextNode(doc, this.request.getRemoteUser())); 259 root.appendChild(node); 260 node = doc.createElementNS(null, "requestedSessionId"); 261 node.appendChild(this.createTextNode(doc, this.request.getRequestedSessionId())); 262 root.appendChild(node); 263 node = doc.createElementNS(null, "requestURI"); 264 node.appendChild(this.createTextNode(doc, this.request.getRequestURI())); 265 root.appendChild(node); 266 node = doc.createElementNS(null, "servletPath"); 267 node.appendChild(this.createTextNode(doc, this.request.getServletPath())); 268 root.appendChild(node); 269 node = doc.createElementNS(null, "isRequestedSessionIdFromCookie"); 270 node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromCookie()))); 271 root.appendChild(node); 272 node = doc.createElementNS(null, "isRequestedSessionIdFromURL"); 273 node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdFromURL()))); 274 root.appendChild(node); 275 node = doc.createElementNS(null, "isRequestedSessionIdValid"); 276 node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(this.request.isRequestedSessionIdValid()))); 277 root.appendChild(node); 278 } 279 280 283 private void buildAttributesXML(Element root) 284 throws ProcessingException { 285 Document doc = root.getOwnerDocument(); 286 Element attrElement = doc.createElementNS(null, "attributes"); 287 String attrName; 288 Element attr; 289 290 root.appendChild(attrElement); 291 Enumeration all = this.request.getAttributeNames(); 292 while (all.hasMoreElements() == true) { 293 attrName = (String ) all.nextElement(); 294 try { 295 attr = doc.createElementNS(null, attrName); 296 attrElement.appendChild(attr); 297 DOMUtil.valueOf(attr, this.request.getAttribute(attrName)); 298 } catch (DOMException de) { 299 this.logger.info("RequestSessionContext: Cannot create XML element from request attribute '" + attrName + "' : " + de.getMessage()); 302 } 303 } 304 } 305 306 309 private void buildCookiesXML(Element root) { 310 Document doc = root.getOwnerDocument(); 311 312 Element cookiesElement = doc.createElementNS(null, "cookies"); 313 root.appendChild(cookiesElement); 314 315 Cookie[] cookies = this.request.getCookies(); 316 if (cookies != null) { 317 Cookie current; 318 Element node; 319 Element parent; 320 for(int i = 0; i < cookies.length; i++) { 321 current = cookies[i]; 322 parent = doc.createElementNS(null, "cookie"); 323 parent.setAttributeNS(null, "name", current.getName()); 324 cookiesElement.appendChild(parent); 325 node = doc.createElementNS(null, "comment"); 326 node.appendChild(this.createTextNode(doc, current.getComment())); 327 parent.appendChild(node); 328 node = doc.createElementNS(null, "domain"); 329 node.appendChild(this.createTextNode(doc, current.getDomain())); 330 parent.appendChild(node); 331 node = doc.createElementNS(null, "maxAge"); 332 node.appendChild(this.createTextNode(doc, "" + current.getMaxAge())); 333 parent.appendChild(node); 334 node = doc.createElementNS(null, "name"); 335 node.appendChild(this.createTextNode(doc, current.getName())); 336 parent.appendChild(node); 337 node = doc.createElementNS(null, "path"); 338 node.appendChild(this.createTextNode(doc, current.getPath())); 339 parent.appendChild(node); 340 node = doc.createElementNS(null, "secure"); 341 node.appendChild(doc.createTextNode(BooleanUtils.toStringTrueFalse(current.getSecure()))); 342 parent.appendChild(node); 343 node = doc.createElementNS(null, "value"); 344 node.appendChild(this.createTextNode(doc, current.getValue())); 345 parent.appendChild(node); 346 node = doc.createElementNS(null, "version"); 347 node.appendChild(this.createTextNode(doc, "" + current.getVersion())); 348 parent.appendChild(node); 349 } 350 } 351 } 352 353 356 private void buildHeadersXML(Element root) { 357 Document doc = root.getOwnerDocument(); 358 Element headersElement = doc.createElementNS(null, "headers"); 359 String headerName; 360 Element header; 361 362 root.appendChild(headersElement); 363 Enumeration all = this.request.getHeaderNames(); 364 while (all.hasMoreElements() == true) { 365 headerName = (String ) all.nextElement(); 366 try { 367 header = doc.createElementNS(null, headerName); 368 headersElement.appendChild(header); 369 header.appendChild(this.createTextNode(doc, this.request.getHeader(headerName))); 370 } catch (Exception ignore) { 371 } 373 } 374 } 375 376 379 private void buildParameterXML(Element root, SAXParser parser) { 380 Document doc = root.getOwnerDocument(); 381 Element parameterElement = doc.createElementNS(null, "parameter"); 384 Element parameterValuesElement = doc.createElementNS(null, "parametervalues"); 385 root.appendChild(parameterElement); 386 root.appendChild(parameterValuesElement); 387 String parameterName = null; 388 Enumeration pars = this.request.getParameterNames(); 389 Element parameter; 390 Element element; 391 Node valueNode; 392 String [] values; 393 String parValue; 394 395 element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETERS_ELEMENT); 396 element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:cinclude", CIncludeTransformer.CINCLUDE_NAMESPACE_URI); 397 parameterValuesElement.appendChild(element); 398 parameterValuesElement = element; 399 400 while (pars.hasMoreElements() == true) { 401 parameterName = (String )pars.nextElement(); 402 values = this.request.getParameterValues(parameterName); 403 404 for(int i = 0; i < values.length; i++) { 405 406 parValue = values[i].trim(); 408 if (parValue.length() > 0 && parValue.charAt(0) == '<') { 409 try { 410 valueNode = DOMUtil.getDocumentFragment(parser, new StringReader (parValue)); 411 valueNode = doc.importNode(valueNode, true); 412 } catch (Exception noXMLException) { 413 valueNode = doc.createTextNode(parValue); 414 } 415 } else { 416 valueNode = doc.createTextNode(parValue); 417 } 418 if (i == 0) { 420 try { 421 parameter = doc.createElementNS(null, parameterName); 422 parameter.appendChild(valueNode); 423 parameterElement.appendChild(parameter); 424 } catch (Exception local) { 425 } 427 } 428 429 try { 430 element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, PARAMETER_ELEMENT); 432 parameterValuesElement.appendChild(element); 433 parameter = element; 434 element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, NAME_ELEMENT); 435 parameter.appendChild(element); 436 element.appendChild(doc.createTextNode(parameterName)); 437 element = doc.createElementNS(CIncludeTransformer.CINCLUDE_NAMESPACE_URI, VALUE_ELEMENT); 438 parameter.appendChild(element); 439 element.appendChild(valueNode.cloneNode(true)); 440 } catch (Exception local) { 441 } 443 } 444 } 445 element = doc.createElementNS(null, "querystring"); 447 root.appendChild(element); 448 String value = request.getQueryString(); 449 if (value != null) { 450 element.appendChild(doc.createTextNode('?' + value)); 451 } 452 } 453 454 457 public DocumentFragment getXML(String path) 458 throws ProcessingException { 459 if (path == null || path.charAt(0) != '/') { 460 throw new ProcessingException("Not a valid XPath: " + path); 461 } 462 path = this.createPath(path); 463 DocumentFragment result = null; 464 NodeList list; 465 466 try { 467 list = DOMUtil.selectNodeList(this.contextData, path, this.xpathProcessor); 468 } catch (javax.xml.transform.TransformerException localException) { 469 throw new ProcessingException("Exception: " + localException, localException); 470 } 471 if (list != null && list.getLength() > 0) { 472 result = DOMUtil.getOwnerDocument(contextData).createDocumentFragment(); 473 for(int i = 0; i < list.getLength(); i++) { 474 475 if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { 477 result.appendChild(DOMUtil.getOwnerDocument(contextData).createTextNode(list.item(i).getNodeValue())); 479 } else { 480 NodeList childs = list.item(i).getChildNodes(); 483 if (childs != null) { 484 for(int m = 0; m < childs.getLength(); m++) { 485 result.appendChild(DOMUtil.getOwnerDocument(contextData).importNode(childs.item(m), true)); 486 } 487 } 488 } 489 } 490 } 491 492 return result; 493 } 494 495 498 public void setXML(String path, DocumentFragment fragment) 499 throws ProcessingException { 500 throw new ProcessingException("RequestSessionContext: Setting of xml not allowed"); 501 } 502 503 506 public void setValueOfNode(String path, String value) 507 throws ProcessingException { 508 throw new ProcessingException("RequestSessionContext: Setting of xml not allowed"); 509 } 510 511 514 public void appendXML(String path, DocumentFragment fragment) 515 throws ProcessingException { 516 throw new ProcessingException("RequestSessionContext: Appending of xml not allowed"); 517 } 518 519 522 public void removeXML(String path) 523 throws ProcessingException { 524 throw new ProcessingException("RequestSessionContext: Removing of xml not allowed"); 525 } 526 527 530 public void setAttribute(String key, Object value) 531 throws ProcessingException { 532 if (value == null) { 533 this.request.removeAttribute(key); 534 } else { 535 this.request.setAttribute(key, value); 536 } 537 } 538 539 542 public Object getAttribute(String key) 543 throws ProcessingException { 544 return this.request.getAttribute(key); 545 } 546 547 550 public Object getAttribute(String key, Object defaultObject) 551 throws ProcessingException { 552 Object obj = this.getAttribute(key); 553 return (obj != null ? obj : defaultObject); 554 } 555 556 559 public Node getSingleNode(String path) 560 throws ProcessingException { 561 path = this.createPath(path); 562 Node node = null; 563 564 try { 565 node = DOMUtil.getSingleNode(this.contextData, path, this.xpathProcessor); 566 } catch (javax.xml.transform.TransformerException localException) { 567 throw new ProcessingException("Exception: " + localException, localException); 568 } 569 return node; 570 } 571 572 575 public NodeList getNodeList(String path) 576 throws ProcessingException { 577 path = this.createPath(path); 578 NodeList list = null; 579 580 try { 581 list = DOMUtil.selectNodeList(this.contextData, path, this.xpathProcessor); 582 } catch (javax.xml.transform.TransformerException localException) { 583 throw new ProcessingException("Exception: " + localException, localException); 584 } 585 return list; 586 } 587 588 591 public void setNode(String path, Node node) 592 throws ProcessingException { 593 throw new ProcessingException("RequestSessionContext: Setting of XML not allowed"); 594 } 595 596 600 public String getValueOfNode(String path) 601 throws ProcessingException { 602 String value = null; 603 Node node = this.getSingleNode(path); 604 if (node != null) { 605 value = DOMUtil.getValueOfNode(node); 606 } 607 608 return value; 609 } 610 611 616 public boolean streamXML(String path, 617 ContentHandler contentHandler, 618 LexicalHandler lexicalHandler) 619 throws SAXException , ProcessingException { 620 boolean result = false; 621 NodeList list; 622 623 try { 624 list = DOMUtil.selectNodeList(this.contextData, this.createPath(path), this.xpathProcessor); 625 } catch (javax.xml.transform.TransformerException local) { 626 throw new ProcessingException("TransformerException: " + local, local); 627 } 628 if (list != null && list.getLength() > 0) { 629 result = true; 630 for(int i = 0; i < list.getLength(); i++) { 631 632 if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { 634 String value = list.item(i).getNodeValue(); 636 contentHandler.characters(value.toCharArray(), 0, value.length()); 637 } else { 638 NodeList childs = list.item(i).getChildNodes(); 641 if (childs != null) { 642 for(int m = 0; m < childs.getLength(); m++) { 643 IncludeXMLConsumer.includeNode(childs.item(m), contentHandler, lexicalHandler); 644 } 645 } 646 } 647 } 648 } 649 650 return result; 651 } 652 653 656 public DocumentFragment getParameterAsXML(final String parameterName) 657 throws ProcessingException { 658 return this.getXML("/parameter/"+parameterName); 659 } 660 661 664 public String getParameter(final String parameterName) { 665 return this.request.getParameter(parameterName); 666 } 667 668 673 public void loadXML(String path, 674 SourceParameters parameters) 675 throws SAXException , ProcessingException, IOException { 676 throw new ProcessingException("The context " + this.name + " does not support loading."); 677 } 678 679 684 public void saveXML(String path, 685 SourceParameters parameters) 686 throws SAXException , ProcessingException, IOException { 687 throw new ProcessingException("The context " + this.name + " does not support saving."); 688 } 689 690 } 691 | Popular Tags |