1 16 package org.apache.cocoon.environment; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.OutputStream ; 21 import java.lang.reflect.Method ; 22 import java.net.MalformedURLException ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.CascadingRuntimeException; 28 import org.apache.avalon.framework.component.ComponentException; 29 import org.apache.avalon.framework.component.ComponentManager; 30 import org.apache.avalon.framework.logger.AbstractLogEnabled; 31 import org.apache.cocoon.Constants; 32 import org.apache.cocoon.ProcessingException; 33 import org.apache.cocoon.components.CocoonComponentManager; 34 import org.apache.cocoon.components.source.SourceUtil; 35 import org.apache.cocoon.util.BufferedOutputStream; 36 import org.apache.cocoon.util.ClassUtils; 37 import org.apache.cocoon.util.Deprecation; 38 import org.apache.commons.collections.iterators.IteratorEnumeration; 39 import org.apache.excalibur.source.SourceException; 40 import org.xml.sax.SAXException ; 41 42 50 public abstract class AbstractEnvironment extends AbstractLogEnabled implements Environment { 51 52 53 protected String uris; 54 55 56 protected StringBuffer prefix = new StringBuffer (); 57 58 59 protected String view; 60 61 62 protected String action; 63 64 65 protected String context; 66 67 68 private String tempInitContext; 69 70 71 protected String rootContext; 72 73 74 protected HashMap objectModel; 75 76 77 protected org.apache.excalibur.source.SourceResolver sourceResolver; 78 79 80 protected ComponentManager manager; 81 82 83 private Map attributes = new HashMap (); 84 85 86 protected BufferedOutputStream secureOutputStream; 87 88 89 protected OutputStream outputStream; 90 91 92 static protected Method avalonToCocoonSourceWrapper; 93 94 95 protected boolean initializedComponents = false; 96 97 100 public AbstractEnvironment(String uri, String view, File file) 101 throws MalformedURLException { 102 this(uri, view, file, null); 103 } 104 105 108 public AbstractEnvironment(String uri, String view, File file, String action) 109 throws MalformedURLException { 110 this(uri, view, file.toURL().toExternalForm(), action); 111 } 112 113 116 public AbstractEnvironment(String uri, String view, String context, String action) 117 throws MalformedURLException { 118 this.uris = uri; 119 this.view = view; 120 this.tempInitContext = context; 121 this.action = action; 122 this.objectModel = new HashMap (); 123 } 124 125 129 protected void setView(String view) { 130 if (this.view != null) { 131 throw new IllegalStateException ("View was already set on this environment"); 132 } 133 this.view = view; 134 } 135 136 140 protected void setAction(String action) { 141 if (this.action != null) { 142 throw new IllegalStateException ("Action was already set on this environment"); 143 } 144 this.action = action; 145 } 146 147 150 protected static String extractView(Request request) { 151 return request.getParameter(Constants.VIEW_PARAM); 152 } 153 154 157 protected static String extractAction(Request req) { 158 String action = req.getParameter(Constants.ACTION_PARAM); 159 if (action != null) { 160 161 return action; 162 } else { 163 for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) { 164 String name = (String )e.nextElement(); 165 if (name.startsWith(Constants.ACTION_PARAM_PREFIX)) { 166 if (name.endsWith(".x") || name.endsWith(".y")) { 167 return name.substring(Constants.ACTION_PARAM_PREFIX.length(),name.length()-2); 168 } else { 169 return name.substring(Constants.ACTION_PARAM_PREFIX.length()); 170 } 171 } 172 } 173 return null; 174 } 175 } 176 177 179 182 public String getURI() { 183 return this.uris; 184 } 185 186 189 public String getRootContext() { 190 if ( !this.initializedComponents) { 191 this.initComponents(); 192 } 193 return this.rootContext; 194 } 195 196 199 public String getContext() { 200 if (!this.initializedComponents) { 201 this.initComponents(); 202 } 203 return this.context; 204 } 205 206 209 public String getURIPrefix() { 210 return this.prefix.toString(); 211 } 212 213 216 protected void setURIPrefix(String prefix) { 217 if (getLogger().isDebugEnabled()) { 218 getLogger().debug("Set the URI Prefix (OLD=" + getURIPrefix() + ", NEW=" + prefix + ")"); 219 } 220 this.prefix = new StringBuffer (prefix); 221 } 222 223 226 protected void setContext(String context) { 227 this.context = context; 228 } 229 230 234 public void setContext(String prefix, String uri, String context) { 235 this.setContext(context); 236 this.setURIPrefix(prefix == null ? "" : prefix); 237 this.uris = uri; 238 if (getLogger().isDebugEnabled()) { 239 getLogger().debug("Reset context to " + this.context); 240 } 241 } 242 243 246 public void changeContext(String newPrefix, String newContext) 247 throws IOException { 248 if (!this.initializedComponents) { 249 this.initComponents(); 250 } 251 252 if (getLogger().isDebugEnabled()) { 253 getLogger().debug("Changing Cocoon context"); 254 getLogger().debug(" from context(" + this.context + ") and prefix(" + this.prefix + ")"); 255 getLogger().debug(" to context(" + newContext + ") and prefix(" + newPrefix + ")"); 256 getLogger().debug(" at URI " + this.uris); 257 } 258 259 int l = newPrefix.length(); 260 if (l >= 1) { 261 if (!this.uris.startsWith(newPrefix)) { 262 String message = "The current URI (" + this.uris + 263 ") doesn't start with given prefix (" + newPrefix + ")"; 264 getLogger().error(message); 265 throw new RuntimeException (message); 266 } 267 this.prefix.append(newPrefix); 268 this.uris = this.uris.substring(l); 269 270 if (this.uris.startsWith("/")) { 272 this.uris = this.uris.substring(1); 273 this.prefix.append('/'); 274 } 275 } 276 277 if (this.context.startsWith("zip:")) { 278 if (getLogger().isDebugEnabled()) { 281 getLogger().debug("Base context is zip: " + this.context); 282 } 283 284 org.apache.excalibur.source.Source source = null; 285 try { 286 source = this.sourceResolver.resolveURI(this.context + newContext); 287 this.context = source.getURI(); 288 } finally { 289 this.sourceResolver.release(source); 290 } 291 } else if (newContext.length() > 0){ 292 String sContext; 293 if (newContext.charAt(0) == '/') { 295 sContext = "file:" + newContext; 297 } else if (newContext.indexOf(':') > 1) { 298 sContext = newContext; 300 } else { 301 sContext = this.context + '/' + newContext; 303 } 304 305 int i = sContext.lastIndexOf('/'); 307 if (i != -1 && i + 1 < sContext.length()) { 308 sContext = sContext.substring(0, i + 1); 309 } 310 311 org.apache.excalibur.source.Source source = null; 312 try { 313 source = this.sourceResolver.resolveURI(sContext); 314 this.context = source.getURI(); 315 } finally { 316 this.sourceResolver.release(source); 317 } 318 } 319 320 if (getLogger().isDebugEnabled()) { 321 getLogger().debug("New context is " + this.context); 322 } 323 } 324 325 328 public abstract void redirect(boolean sessionmode, String newURL) throws IOException ; 329 330 public void globalRedirect(boolean sessionmode, String newURL) throws IOException { 331 redirect(sessionmode, newURL); 332 } 333 334 336 339 public String getView() { 340 return this.view; 341 } 342 343 346 public String getAction() { 347 return this.action; 348 } 349 350 352 355 public void setStatus(int statusCode) { 356 } 357 358 360 363 public Map getObjectModel() { 364 return this.objectModel; 365 } 366 367 371 public Source resolve(String systemId) 372 throws ProcessingException, SAXException , IOException { 373 Deprecation.logger.warn("The method SourceResolver.resolve(String) is " 374 + "deprecated. Use resolveURI(String) instead."); 375 if (!this.initializedComponents) { 376 initComponents(); 377 } 378 379 if (getLogger().isDebugEnabled()) { 380 getLogger().debug("Resolving '" + systemId + "' in context '" + this.context + "'"); 381 } 382 383 if (systemId == null) { 384 throw new SAXException ("Invalid System ID"); 385 } 386 387 Class clazz; 390 try { 391 clazz = ClassUtils.loadClass("org.apache.cocoon.components.source.impl.AvalonToCocoonSourceInvocationHandler"); 392 } catch (Exception e) { 393 throw new ProcessingException("The deprecated resolve() method of the environment was called." 394 +"Please either update your code to use the new resolveURI() method or" 395 +" install the deprecation support.", e); 396 } 397 398 if (null == avalonToCocoonSourceWrapper) { 399 synchronized (getClass()) { 400 try { 401 avalonToCocoonSourceWrapper = clazz.getDeclaredMethod("createProxy", 402 new Class [] {ClassUtils.loadClass("org.apache.excalibur.source.Source"), 403 ClassUtils.loadClass("org.apache.excalibur.source.SourceResolver"), 404 ClassUtils.loadClass(Environment.class.getName()), 405 ClassUtils.loadClass(ComponentManager.class.getName())}); 406 } catch (Exception e) { 407 throw new ProcessingException("The deprecated resolve() method of the environment was called." 408 +"Please either update your code to use the new resolveURI() method or" 409 +" install the deprecation support.", e); 410 } 411 } 412 } 413 414 try { 415 org.apache.excalibur.source.Source source = resolveURI(systemId); 416 Source wrappedSource = (Source)avalonToCocoonSourceWrapper.invoke( 417 clazz, 418 new Object [] {source, this.sourceResolver, this, this.manager}); 419 return wrappedSource; 420 } catch (SourceException se) { 421 throw SourceUtil.handle(se); 422 } catch (Exception e) { 423 throw new ProcessingException("Unable to create source wrapper.", e); 424 } 425 } 426 427 435 public boolean isResponseModified(long lastModified) { 436 return true; } 438 439 442 public void setResponseIsNotModified() { 443 } 445 446 public Object getAttribute(String name) { 447 return this.attributes.get(name); 448 } 449 450 public void setAttribute(String name, Object value) { 451 this.attributes.put(name, value); 452 } 453 454 protected boolean hasAttribute(String name) { 455 return this.attributes.containsKey(name); 456 } 457 458 public void removeAttribute(String name) { 459 this.attributes.remove(name); 460 } 461 462 public Enumeration getAttributeNames() { 463 return new IteratorEnumeration(this.attributes.keySet().iterator()); 464 } 465 466 470 public OutputStream getOutputStream() throws IOException { 471 Deprecation.logger.warn("The method Environment.getOutputStream() " + 472 "is deprecated. Use getOutputStream(-1) instead."); 473 return this.getOutputStream(-1); 475 } 476 477 485 public OutputStream getOutputStream(int bufferSize) 486 throws IOException { 487 488 492 if (bufferSize == -1) { 493 if (this.secureOutputStream == null) { 494 this.secureOutputStream = new BufferedOutputStream(this.outputStream); 495 } 496 return this.secureOutputStream; 497 } else if (bufferSize == 0) { 498 if (this.secureOutputStream != null) { 500 this.secureOutputStream = null; 501 } 502 return this.outputStream; 503 } else { 504 this.outputStream = new java.io.BufferedOutputStream (this.outputStream, bufferSize); 506 return this.outputStream; 507 } 508 } 509 510 517 public boolean tryResetResponse() 518 throws IOException { 519 if (this.secureOutputStream != null) { 520 this.secureOutputStream.clearBuffer(); 521 return true; 522 } 523 return false; 524 } 525 526 529 public void commitResponse() 530 throws IOException { 531 if (this.secureOutputStream != null) { 532 this.setContentLength(this.secureOutputStream.getCount()); 533 this.secureOutputStream.realFlush(); 534 } else if (this.outputStream != null) { 535 this.outputStream.flush(); 536 } 537 } 538 539 542 public org.apache.excalibur.source.Source resolveURI(final String location) 543 throws MalformedURLException , IOException , SourceException { 544 return this.resolveURI(location, null, null); 545 } 546 547 550 public org.apache.excalibur.source.Source resolveURI(final String location, 551 String baseURI, 552 final Map parameters) 553 throws MalformedURLException , IOException , SourceException { 554 if (!this.initializedComponents) { 555 this.initComponents(); 556 } 557 return this.sourceResolver.resolveURI(location, baseURI, parameters); 558 } 559 560 563 public void release(final org.apache.excalibur.source.Source source) { 564 if (null != source) { 565 this.sourceResolver.release(source); 566 } 567 } 568 569 573 protected void initComponents() { 574 this.initializedComponents = true; 575 try { 576 this.manager = CocoonComponentManager.getSitemapComponentManager(); 577 this.sourceResolver = (org.apache.excalibur.source.SourceResolver)this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 578 if (this.tempInitContext != null) { 579 org.apache.excalibur.source.Source source = null; 580 try { 581 source = this.sourceResolver.resolveURI(this.tempInitContext); 582 this.context = source.getURI(); 583 584 if (this.rootContext == null) this.rootContext = this.context; 586 } finally { 587 this.sourceResolver.release(source); 588 } 589 this.tempInitContext = null; 590 } 591 } catch (ComponentException ce) { 592 throw new CascadingRuntimeException("Unable to lookup component.", ce); 594 } catch (IOException ie) { 595 throw new CascadingRuntimeException("Unable to resolve URI: "+this.tempInitContext, ie); 596 } 597 } 598 599 602 public void startingProcessing() { 603 } 605 606 610 public void finishingProcessing() { 611 if (null != this.manager) { 612 this.manager.release(this.sourceResolver); 613 this.manager = null; 614 this.sourceResolver = null; 615 } 616 this.initializedComponents = false; 617 } 618 619 622 public boolean isInternalRedirect() { 623 return false; 624 } 625 } 626 | Popular Tags |