1 16 package org.apache.cocoon.webapps.portal.context; 17 18 import org.apache.cocoon.ProcessingException; 19 import org.apache.cocoon.environment.ObjectModelHelper; 20 import org.apache.cocoon.environment.Request; 21 import org.apache.cocoon.webapps.portal.PortalConstants; 22 import org.apache.cocoon.webapps.portal.components.PortalManager; 23 import org.apache.cocoon.webapps.portal.components.PortalManagerImpl; 24 import org.apache.cocoon.webapps.session.context.SessionContext; 25 import org.apache.cocoon.xml.IncludeXMLConsumer; 26 import org.apache.cocoon.xml.dom.DOMBuilder; 27 import org.apache.cocoon.xml.dom.DOMUtil; 28 import org.apache.excalibur.source.SourceParameters; 29 import org.apache.excalibur.xml.xpath.XPathProcessor; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.ext.LexicalHandler ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.DocumentFragment ; 38 import org.w3c.dom.Attr ; 39 40 import java.io.IOException ; 41 import java.util.HashMap ; 42 import java.util.Map ; 43 44 65 public final class SessionContextImpl 66 implements SessionContext { 67 68 69 public static ThreadLocal copletInfo = new ThreadLocal (); 70 71 72 private String name; 73 74 75 private Map attributes = new HashMap (); 76 77 78 private Document layoutDOM; 79 80 81 private Document configurationDOM; 82 83 84 private Map profile; 85 86 87 88 private SourceParameters copletPars; 89 90 91 private Element statusProfile; 92 93 94 private String copletID; 95 96 97 private String copletNumber; 98 99 100 private String profileID; 101 102 103 private String portalURI; 104 105 106 private String mediaType; 107 108 109 private Request request; 110 111 112 private XPathProcessor xpathProcessor; 113 114 public SessionContextImpl(String name, 115 Map objectModel, 116 PortalManager portal, 117 XPathProcessor xpathProcessor) 118 throws IOException , SAXException , ProcessingException { 119 this.xpathProcessor = xpathProcessor; 120 this.setup(name, null, null); 121 122 Map info = (Map )SessionContextImpl.copletInfo.get(); 124 if (info != null) { 125 SessionContextImpl.copletInfo.set(null); 126 this.copletPars = (SourceParameters)info.get(PortalConstants.COPLETINFO_PARAMETERS); 127 this.portalURI = (String )info.get(PortalConstants.COPLETINFO_PORTALURI); 128 if (this.copletPars != null) { 129 this.copletID = this.copletPars.getParameter(PortalConstants.PARAMETER_ID); 130 this.copletNumber = this.copletPars.getParameter(PortalConstants.PARAMETER_NUMBER); 131 if (this.copletID != null && this.copletNumber != null) { 132 this.portalURI = this.portalURI + (this.portalURI.indexOf('?') == -1 ? '?' : '&') 133 + "portalcmd=update_" + this.copletID + "_" + this.copletNumber; 134 } 135 } 136 this.statusProfile = (Element )info.get(PortalConstants.COPLETINFO_STATUSPROFILE); 137 } 138 this.mediaType = (this.copletPars != null ? (String )copletPars.getParameter(PortalConstants.PARAMETER_MEDIA) 139 : portal.getMediaType()); 140 142 SessionContext context = portal.getContext(false); 143 if (context != null) { 144 if (context.getAttribute(PortalManager.ATTRIBUTE_PORTAL_ROLE) != null) { 145 this.profileID = portal.getProfileID(PortalManager.BUILDTYPE_VALUE_ID, 146 (String )context.getAttribute(PortalManager.ATTRIBUTE_PORTAL_ROLE), 147 (String )context.getAttribute(PortalManager.ATTRIBUTE_PORTAL_ID), false); 148 this.profile = portal.retrieveProfile(this.profileID); 149 } 150 } 151 this.getConfigurationDOM(portal); 152 this.request = ObjectModelHelper.getRequest( objectModel ); 153 } 154 155 158 public String getName() { 159 return this.name; 160 } 161 162 public Request getRequest() { 163 return this.request; 164 } 165 166 169 private void getLayoutDOM() 170 throws ProcessingException { 171 if (this.layoutDOM == null && this.profile != null) { 172 try { 173 Map portalLayouts = (Map )this.profile.get(PortalConstants.PROFILE_PORTAL_LAYOUTS); 174 Map copletLayouts = (Map )this.profile.get(PortalConstants.PROFILE_COPLET_LAYOUTS); 175 DOMBuilder builder = new DOMBuilder(); 176 builder.startDocument(); 177 PortalManagerImpl.streamLayoutProfile(builder, portalLayouts, copletLayouts, this.mediaType); 178 builder.endDocument(); 179 this.layoutDOM = builder.getDocument(); 180 } catch (SAXException local) { 181 throw new ProcessingException("Unable to get portal." + local, local); 182 } 183 } 184 } 185 186 189 private void getConfigurationDOM(PortalManager portal) 190 throws ProcessingException, IOException { 191 if (this.configurationDOM == null && portal != null) { 192 try { 193 String contextID = null; 194 if (this.copletID != null && this.copletNumber != null) { 195 contextID = "coplet_"+copletID+"_"+copletNumber; 196 } 197 DOMBuilder builder = new DOMBuilder(); 198 builder.startDocument(); 199 portal.streamConfiguration(builder, 200 this.portalURI, 201 this.profileID, 202 this.mediaType, 203 contextID); 204 builder.endDocument(); 205 this.configurationDOM = builder.getDocument(); 206 } catch (SAXException local) { 207 throw new ProcessingException("Unable to get portal." + local, local); 208 } 209 } 210 } 211 212 213 public void setup(String value, String load, String save) { 214 name = value; 215 } 216 217 220 public synchronized DocumentFragment getXML(String path) 221 throws ProcessingException { 222 DocumentFragment result = null; 223 224 if (path.startsWith("/")) path = path.substring(1); 225 NodeList list = null; 226 227 if (path == null || path.equals("")) { 228 Document doc = DOMUtil.createDocument(); 229 result = doc.createDocumentFragment(); 230 this.getLayoutDOM(); 231 if (this.layoutDOM != null) { 232 result.appendChild(doc.importNode(this.layoutDOM.getDocumentElement(), true)); 233 } 234 if (this.configurationDOM != null) { 235 result.appendChild(doc.importNode(this.configurationDOM.getDocumentElement(), true)); 236 } 237 238 if (this.statusProfile != null) { 239 if (this.copletID != null && this.copletNumber != null) { 240 String statusPath = "customization/coplet[@id='"+copletID+"' and @number='"+copletNumber+"']"; 241 try { 242 Node node = DOMUtil.getSingleNode(this.statusProfile, statusPath, this.xpathProcessor); 243 if (node != null) { 244 Element copletData = doc.createElementNS(null, "coplet-data"); 245 NodeList childs = node.getChildNodes(); 246 if (childs != null) { 247 for(int l=0; l<childs.getLength(); l++) { 248 copletData.appendChild(doc.importNode(childs.item(l), true)); 249 } 250 } 251 result.appendChild(copletData); 252 } 253 } catch (javax.xml.transform.TransformerException localException) { 254 throw new ProcessingException("TransformerException: " + localException, localException); 255 } 256 } 257 } 258 } 259 260 if (path.equals("layout") || path.startsWith("layout/") ) { 261 try { 262 this.getLayoutDOM(); 263 if (this.layoutDOM != null) list = DOMUtil.selectNodeList(this.layoutDOM, path, this.xpathProcessor); 264 } catch (javax.xml.transform.TransformerException localException) { 265 throw new ProcessingException("TransformerException: " + localException, localException); 266 } 267 } 268 269 if (path.equals("configuration") || path.startsWith("configuration/") ) { 270 try { 271 if (this.configurationDOM != null) list = DOMUtil.selectNodeList(this.configurationDOM, path, this.xpathProcessor); 272 } catch (javax.xml.transform.TransformerException localException) { 273 throw new ProcessingException("TransformerException: " + localException, localException); 274 } 275 } 276 277 if (path.startsWith("coplet-data/") || path.equals("coplet-data") ) { 278 279 if (this.statusProfile != null) { 280 if (this.copletID != null && this.copletNumber != null) { 281 String statusPath = "customization/coplet[@id='"+copletID+"' and @number='"+copletNumber+"']"; 282 if (path.startsWith("coplet-data/")) { 283 statusPath = statusPath + path.substring(11); 284 } 285 try { 286 list = DOMUtil.selectNodeList(this.statusProfile, statusPath, this.xpathProcessor); 287 } catch (javax.xml.transform.TransformerException localException) { 288 throw new ProcessingException("TransformerException: " + localException, localException); 289 } 290 } 291 } 292 } 293 294 if (list != null && list.getLength() > 0) { 295 Document doc = DOMUtil.createDocument(); 296 result = doc.createDocumentFragment(); 297 298 for(int i = 0; i < list.getLength(); i++) { 299 300 if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { 302 result.appendChild(doc.createTextNode(list.item(i).getNodeValue())); 304 } else { 305 NodeList childs = list.item(i).getChildNodes(); 308 if (childs != null) { 309 for(int m = 0; m < childs.getLength(); m++) { 310 result.appendChild(doc.importNode(childs.item(m), true)); 311 } 312 } 313 } 314 } 315 } 316 317 return result; 318 } 319 320 321 324 public synchronized void setXML(String path, DocumentFragment fragment) 325 throws ProcessingException { 326 if (path != null) { 327 if (path.startsWith("/") ) path = path.substring(1); 328 if (path.startsWith("coplet-data/") || path.equals("coplet-data") ) { 329 330 if (this.statusProfile != null) { 331 if (this.copletID != null && this.copletNumber != null) { 332 String statusPath = "customization/coplet[@id='"+copletID+"' and @number='"+copletNumber+"']"; 333 if (path.startsWith("coplet-data/")) { 334 statusPath = statusPath + path.substring(11); 335 } 336 337 Node node = DOMUtil.selectSingleNode(this.statusProfile, statusPath, this.xpathProcessor); 338 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 339 Attr attr = (Attr )node; 341 attr.setNodeValue(DOMUtil.getValueOfNode(fragment)); 342 } else { 343 344 while (node.hasChildNodes()) { 346 node.removeChild(node.getFirstChild()); 347 } 348 349 NodeList childs = fragment.getChildNodes(); 351 if (childs != null && childs.getLength() > 0) { 352 for(int i = 0; i < childs.getLength(); i++) { 353 Node n = this.statusProfile.getOwnerDocument().importNode(childs.item(i), true); 354 node.appendChild(n); 355 } 356 } 357 } 358 359 if (this.copletPars.getParameter(PortalConstants.PARAMETER_PERSISTENT, "false").equals("true") ) { 360 this.profile.put(PortalConstants.PROFILE_SAVE_STATUS_FLAG, "true"); 361 } 362 } 363 } 364 } 365 } 366 367 } 368 369 376 public synchronized void appendXML(String path, DocumentFragment fragment) 377 throws ProcessingException { 378 throw new ProcessingException("appendXML() not implemented."); 379 } 380 381 384 public synchronized void removeXML(String path) 385 throws ProcessingException { 386 throw new ProcessingException("removeXML() not implemented."); 387 } 388 389 392 public synchronized Node getSingleNode(String path) 393 throws ProcessingException { 394 throw new ProcessingException("getSingleNode() not implemented."); 396 } 398 399 402 public synchronized NodeList getNodeList(String path) 403 throws ProcessingException { 404 throw new ProcessingException("getNodeList() not implemented."); 406 } 408 409 412 public synchronized void setNode(String path, Node node) 413 throws ProcessingException { 414 throw new ProcessingException("setNode() not implemented."); 415 } 416 417 418 421 public synchronized void setAttribute(String key, Object value) { 422 if (value == null) { 423 attributes.remove(key); 424 } else { 425 attributes.put(key, value); 426 } 427 } 428 429 432 public synchronized Object getAttribute(String key) { 433 return attributes.get(key); 434 } 435 436 439 public synchronized Object getAttribute(String key, Object defaultObject) { 440 Object value = attributes.get(key); 441 if (value == null) value = defaultObject; 442 return value; 443 } 444 445 449 public synchronized String getValueOfNode(String path) 450 throws ProcessingException { 451 throw new ProcessingException("getValueOfNode() not implemented."); 453 } 455 456 459 public synchronized void setValueOfNode(String path, String value) 460 throws ProcessingException { 461 throw new ProcessingException("setValueOfNode() not implemented."); 462 } 463 464 469 public synchronized boolean streamXML(String path, 470 ContentHandler contentHandler, 471 LexicalHandler lexicalHandler) 472 throws SAXException , ProcessingException { 473 boolean streamed = false; 474 DocumentFragment fragment = this.getXML(path); 475 if (fragment != null) { 476 streamed = true; 477 IncludeXMLConsumer.includeNode(fragment, contentHandler, lexicalHandler); 478 } 479 return streamed; 480 } 481 482 487 public void loadXML(String path, 488 SourceParameters parameters) 489 throws SAXException , ProcessingException, IOException { 490 throw new ProcessingException("The context " + this.name + " does not support loading."); 491 } 492 493 498 public void saveXML(String path, 499 SourceParameters parameters) 500 throws SAXException , ProcessingException, IOException { 501 throw new ProcessingException("The context " + this.name + " does not support saving."); 502 } 503 } 504 505 | Popular Tags |