1 16 package org.apache.cocoon.environment.wrapper; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.net.MalformedURLException ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.component.ComponentManager; 26 import org.apache.avalon.framework.logger.Logger; 27 import org.apache.cocoon.Constants; 28 import org.apache.cocoon.environment.AbstractEnvironment; 29 import org.apache.cocoon.environment.Environment; 30 import org.apache.cocoon.environment.ObjectModelHelper; 31 import org.apache.cocoon.environment.Request; 32 import org.apache.cocoon.environment.Response; 33 import org.apache.cocoon.util.BufferedOutputStream; 34 import org.apache.cocoon.util.Deprecation; 35 36 37 46 public class EnvironmentWrapper 47 extends AbstractEnvironment 48 implements Environment { 49 50 51 protected Environment environment; 52 53 54 protected Map objectModel; 55 56 57 protected String redirectURL; 58 59 60 protected Request request; 61 62 63 protected OutputStream outputStream; 64 65 protected String contentType; 66 67 protected boolean internalRedirect = false; 68 69 73 public EnvironmentWrapper(Environment env, 74 String requestURI, 75 String queryString, 76 Logger logger) 77 throws MalformedURLException { 78 this(env, requestURI, queryString, logger, false); 79 } 80 81 85 public EnvironmentWrapper(Environment env, 86 String requestURI, 87 String queryString, 88 Logger logger, 89 boolean rawMode) 90 throws MalformedURLException { 91 this(env, requestURI, queryString, logger, null, rawMode); 92 } 93 94 98 public EnvironmentWrapper(Environment env, 99 String requestURI, 100 String queryString, 101 Logger logger, 102 ComponentManager manager, 103 boolean rawMode) 104 throws MalformedURLException { 105 this(env, requestURI, queryString, logger, null, rawMode,env.getView(), true); 106 } 107 108 112 public EnvironmentWrapper(Environment env, 113 String requestURI, 114 String queryString, 115 Logger logger, 116 ComponentManager manager, 117 boolean rawMode, 118 String view) 119 throws MalformedURLException { 120 this(env, requestURI, queryString, logger, manager, rawMode, view, true); 121 } 122 123 127 public EnvironmentWrapper(Environment env, 128 String requestURI, 129 String queryString, 130 Logger logger, 131 ComponentManager manager, 132 boolean rawMode, 133 String view, 134 boolean wrapResponse) 135 throws MalformedURLException { 136 super(env.getURI(), view, env.getContext(), env.getAction()); 137 init(env, requestURI, queryString, logger, manager, rawMode, view, wrapResponse); 138 } 139 140 private void init(Environment env, 141 String requestURI, 142 String queryString, 143 Logger logger, 144 ComponentManager manager, 145 boolean rawMode, 146 String view, 147 boolean wrapResponse) 148 throws MalformedURLException { 149 this.rootContext = env.getRootContext(); 151 152 this.enableLogging(logger); 153 this.environment = env; 154 this.view = view; 155 156 this.prefix = new StringBuffer (env.getURIPrefix()); 157 158 Map oldObjectModel = env.getObjectModel(); 160 if (oldObjectModel instanceof HashMap ) { 161 this.objectModel = (Map )((HashMap )oldObjectModel).clone(); 162 } else { 163 this.objectModel = new HashMap (oldObjectModel.size()*2); 164 Iterator entries = oldObjectModel.entrySet().iterator(); 165 Map.Entry entry; 166 while (entries.hasNext()) { 167 entry = (Map.Entry )entries.next(); 168 this.objectModel.put(entry.getKey(), entry.getValue()); 169 } 170 } 171 this.request = new RequestWrapper(ObjectModelHelper.getRequest(oldObjectModel), 172 requestURI, 173 queryString, 174 this, 175 rawMode); 176 this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request); 177 if (wrapResponse) { 178 Response response = new ResponseWrapper(ObjectModelHelper.getResponse(oldObjectModel)); 179 this.objectModel.put(ObjectModelHelper.RESPONSE_OBJECT, response); 180 } 181 } 182 183 public EnvironmentWrapper(Environment env, ComponentManager manager, String uri, Logger logger, boolean wrapResponse) throws MalformedURLException { 184 super(env.getURI(), env.getView(), env.getContext(), env.getAction()); 185 186 boolean rawMode = false; 188 189 int position = uri.indexOf(':') + 1; 191 if (position != 0) { 192 if (uri.startsWith("raw:", position)) { 195 position += 4; 196 rawMode = true; 197 } 198 } else { 199 throw new MalformedURLException ("No protocol found for sitemap source in " + uri); 200 } 201 202 String prefix; 204 if (uri.startsWith("//", position)) { 205 position += 2; 206 prefix = ""; } else if (uri.startsWith("/", position)) { 213 position ++; 214 prefix = null; 215 } else { 217 throw new MalformedURLException ("Malformed cocoon URI: " + uri); 218 } 219 220 String queryString = null; 222 int queryStringPos = uri.indexOf('?', position); 223 if (queryStringPos != -1) { 224 queryString = uri.substring(queryStringPos + 1); 225 uri = uri.substring(position, queryStringPos); 226 } else if (position > 0) { 227 uri = uri.substring(position); 228 } 229 230 231 String view = null; 233 if (queryString != null) { 234 int index = queryString.indexOf(Constants.VIEW_PARAM); 235 if (index != -1 236 && (index == 0 || queryString.charAt(index-1) == '&') 237 && queryString.length() > index + Constants.VIEW_PARAM.length() 238 && queryString.charAt(index+Constants.VIEW_PARAM.length()) == '=') { 239 240 String tmp = queryString.substring(index+Constants.VIEW_PARAM.length()+1); 241 index = tmp.indexOf('&'); 242 if (index != -1) { 243 view = tmp.substring(0,index); 244 } else { 245 view = tmp; 246 } 247 } else { 248 view = env.getView(); 249 } 250 } else { 251 view = env.getView(); 252 } 253 254 String requestURI = (prefix == null ? env.getURIPrefix() + uri : uri); 256 257 262 this.init(env, requestURI, queryString, logger, manager, rawMode, view, wrapResponse); 263 this.setURI(prefix, uri); 264 265 } 266 267 270 public void redirect(boolean sessionmode, String newURL) 271 throws IOException { 272 this.redirectURL = newURL; 273 274 if (sessionmode) { 276 request.getSession(true); 278 } 279 } 280 281 284 public void globalRedirect(boolean sessionmode, String newURL) 285 throws IOException { 286 if (environment instanceof EnvironmentWrapper) { 287 ((EnvironmentWrapper)environment).globalRedirect(sessionmode, newURL); 288 } else if ( environment instanceof MutableEnvironmentFacade ) { 289 ((MutableEnvironmentFacade)environment).getDelegate().globalRedirect(sessionmode, newURL); 290 } else { 291 environment.redirect(sessionmode,newURL); 292 } 293 } 294 295 299 public OutputStream getOutputStream() 300 throws IOException { 301 Deprecation.logger.warn("The method Environment.getOutputStream() " + 302 "is deprecated. Use getOutputStream(-1) instead."); 303 return this.outputStream == null 304 ? this.environment.getOutputStream() 305 : this.outputStream; 306 } 307 308 311 public OutputStream getOutputStream(int bufferSize) 312 throws IOException { 313 return this.outputStream == null 314 ? this.environment.getOutputStream(bufferSize) 315 : this.outputStream; 316 } 317 318 322 public void setOutputStream(OutputStream stream) { 323 this.outputStream = stream; 324 } 325 326 333 public boolean tryResetResponse() 334 throws IOException { 335 if (getOutputStream() != null 336 && getOutputStream() instanceof BufferedOutputStream) { 337 ((BufferedOutputStream)getOutputStream()).clearBuffer(); 338 return true; 339 } 340 else 341 return super.tryResetResponse(); 342 } 343 344 347 public void commitResponse() 348 throws IOException { 349 if (getOutputStream() != null 350 && getOutputStream() instanceof BufferedOutputStream) { 351 ((BufferedOutputStream)getOutputStream()).realFlush(); 352 } 353 else 354 super.commitResponse(); 355 } 356 357 361 public String getRedirectURL() { 362 return this.redirectURL; 363 } 364 365 public void reset() { 366 this.redirectURL = null; 367 } 368 369 372 public void setStatus(int statusCode) { 373 } 375 376 public void setContentLength(int length) { 377 } 379 380 383 public void setContentType(String contentType) { 384 this.contentType = contentType; 385 } 386 387 390 public String getContentType() { 391 return this.contentType; 392 } 393 394 397 public Map getObjectModel() { 398 return this.objectModel; 399 } 400 401 407 public void setURI(String prefix, String uris) { 408 if(getLogger().isDebugEnabled()) { 409 getLogger().debug("Setting uri (prefix=" + prefix + ", uris=" + uris + ")"); 410 } 411 if ( !this.initializedComponents) { 412 this.initComponents(); 413 } 414 if (prefix != null) { 415 setContext(getRootContext()); 416 setURIPrefix(prefix); 417 } 418 this.uris = uris; 419 } 420 421 430 public Object getAttribute(String name) 431 { 432 Object value = super.getAttribute(name); 433 434 if (value == null && !hasAttribute(name)) { 436 value = this.environment.getAttribute(name); 437 } 438 439 return value; 440 } 441 442 447 public void removeAttribute(String name) { 448 super.removeAttribute(name); 449 } 450 451 454 public boolean isExternal() { 455 return false; 456 } 457 458 public void setInternalRedirect(boolean flag) { 459 this.internalRedirect = flag; 460 if ( flag ) { 461 ((RequestWrapper)this.request).setRequestURI(this.prefix.toString(), this.uris); 462 } 463 } 464 465 468 public boolean isInternalRedirect() { 469 return this.internalRedirect; 470 } 471 } 472 | Popular Tags |