1 4 package com.openedit.page; 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import com.openedit.OpenEditException; 15 import com.openedit.WebPageRequest; 16 import com.openedit.generators.Output; 17 import com.openedit.page.manage.PageManager; 18 import com.openedit.servlet.OpenEditEngine; 19 import com.openedit.util.PathUtilities; 20 import com.openedit.util.strainer.Filter; 21 22 25 public class PageStreamer 26 { 27 private static final Log log = LogFactory.getLog(PageStreamer.class); 28 protected OpenEditEngine fieldEngine; 29 protected Output fieldOutput; 30 31 protected WebPageRequest fieldWebPageRequest; 32 protected Map fieldWebPageRequestedCount; 33 protected List fieldChildContentList; 34 35 public PageStreamer() 36 { 37 log.debug("Created streamer"); 38 } 39 40 47 48 public void render() throws OpenEditException 49 { 50 Page page = getWebPageRequest().getPage(); 51 if (page.isHtml() ) 52 { 53 getChildContentList().add(getWebPageRequest().getContentPage() ); 54 String override = (String )getWebPageRequest().getPageValue(PageRequestKeys.INNERLAYOUTOVERRIDE); 56 if( override != null) 57 { 58 if( !override.equals(Page.BLANK_LAYOUT)) 59 { 60 Page il = getPage(override); 61 addInnerLayout( il); 62 } 63 } 64 else 65 { 66 addInnerLayout( getWebPageRequest().getContentPage()); 67 } 68 String layout = getLayout(); 70 if ( layout != null ) 71 { 72 Page layoutPage = getPage(layout); 73 getChildContentList().add(0,layoutPage); 74 } 75 streamContent(); 76 } 77 else 78 { 79 page.generate(getWebPageRequest(), getOutput() ); 80 } 81 } 82 83 protected void addInnerLayout(Page inContentPage) throws OpenEditException 84 { 85 if ( inContentPage.hasInnerLayout()) 86 { 87 String innerlayout = inContentPage.getInnerLayout(); 88 innerlayout = inContentPage.getPageSettings().replaceProperty(innerlayout); 89 90 if( !inContentPage.getPath().equalsIgnoreCase(innerlayout)) 91 { 92 Page il = getPage(innerlayout); 93 getChildContentList().add(0,il); 94 addInnerLayout(il); 95 } 96 } 97 } 98 protected void renderLayout() throws OpenEditException 99 { 100 Page inLayout = getEngine().getPageManager().getPage(getLayout()); 101 if (!inLayout.exists()) 102 { 103 getWebPageRequest().putPageValue("missingLayoutPage", inLayout.getPath()); 104 inLayout = getPage("/layoutnotfound.html"); 105 if (!inLayout.exists()) 106 { 107 inLayout = getPage("/openedit/layoutnotfound.html"); 108 } 109 } 110 include(inLayout); 111 } 112 116 public void streamContent() throws OpenEditException 117 { 118 renderContent(); 119 } 120 public void renderContent() throws OpenEditException 121 { 122 if ( getChildContentList().size() == 0) 124 { 125 return; 128 } 129 Page topChild = (Page)getChildContentList().get(0); 130 131 getChildContentList().remove(0); 132 include( topChild , getWebPageRequest() ); 133 } 134 public void include(Page inPage) throws OpenEditException 135 { 136 include( inPage, getWebPageRequest() ); 137 } 138 141 public void include(Page inPage, WebPageRequest inContext) throws OpenEditException 142 { 143 int hitcount = incrementWebPageRequestedCount(inPage); 144 if (hitcount >= 10) 146 { 147 throw new OpenEditException( 149 "Page loop detected in " 150 + inPage.getPath() 151 + ". Including a page in itself is not permitted.\n" 152 + "If you are trying to view a layout, make sure the layout is not trying to use itself as a layout."); 153 } 154 157 Page torender = null; 158 WebPageRequest request = inContext; 159 160 if (inPage == inContext.getContentPage()) 161 { 162 torender = getPageManager().getPage(inPage, request ); 163 if ( torender != inPage ) { 165 request = inContext.copy(torender); 166 getEngine().executePageActions(request); 167 } 168 } 169 else 170 { 171 request = inContext.copy(inPage); 173 175 request.putPageValue(PageRequestKeys.FILE_PATH, inPage.getDirectory()); torender = getPageManager().getPage(inPage, request ); 177 if ( torender != inPage) { 179 request = request.copy(torender); 180 } 181 if (!request.hasRedirected() ) 182 { 183 getEngine().executePageActions(request); 184 } 185 } 186 request.putPageValue("originalPage", inPage); 187 torender.generate(request, getOutput() ); 188 } 189 public void include(String inPath, WebPageRequest inReq) throws OpenEditException 190 { 191 String [] parts = inPath.split("[?]"); 192 String fullPath = PathUtilities.resolveRelativePath(parts[0], getWebPageRequest().getContentPage() 193 .getPath()); 194 Page page = getEngine().getPageManager().getPage(fullPath); 195 196 if (parts.length > 1) 197 { 198 String [] args = parts[1].split("&"); 199 for (int i = 0; i < args.length; i++) 200 { 201 String [] pairs = args[i].split("="); 202 getWebPageRequest().setRequestParameter(pairs[0], pairs[1]); 203 } 204 } 205 include(page, inReq); 206 } 207 public void include(String inPath) throws OpenEditException 208 { 209 include( inPath, getWebPageRequest()); 210 } 211 215 public void stream(Page inPage) throws OpenEditException 216 { 217 include( inPage); 218 } 219 223 public void stream(Page inPage, WebPageRequest inContext) throws OpenEditException 224 { 225 include( inPage, inContext); 226 } 227 231 public void stream(String inPath, WebPageRequest inReq) throws OpenEditException 232 { 233 include( inPath, inReq); 234 } 235 239 public void stream(String inPath) throws OpenEditException 240 { 241 include( inPath ); 242 } 243 public Map getWebPageRequestedCount() 244 { 245 if (fieldWebPageRequestedCount == null) 246 { 247 fieldWebPageRequestedCount = new HashMap (); 248 } 249 return fieldWebPageRequestedCount; 250 } 251 252 protected int incrementWebPageRequestedCount(Page inPage) 253 { 254 Integer count = (Integer ) getWebPageRequestedCount().get(inPage); 255 int hitcount = 0; 256 257 if (count != null) 258 { 259 hitcount = count.intValue() + 1; 260 } 261 262 getWebPageRequestedCount().put(inPage, new Integer (hitcount)); 263 return hitcount; 264 } 265 266 public boolean doesExist(String inPath) throws OpenEditException 267 { 268 if (inPath == null) 269 { 270 return false; 271 } 272 return loadRelativePath(inPath).exists(); 273 } 274 275 protected Page loadRelativePath(String inPath) throws OpenEditException 276 { 277 String fullPath = PathUtilities.buildRelative(inPath, getWebPageRequest().getContentPage().getPath()); 278 return getPage(fullPath); 279 } 280 281 public Page getPage(String inPath) throws OpenEditException 282 { 283 return getPageManager().getPage(inPath); 284 } 285 286 public PageManager getPageManager() 287 { 288 return getEngine().getPageManager(); 289 } 290 291 public OpenEditEngine getEngine() 292 { 293 return fieldEngine; 294 } 295 296 public void setEngine(OpenEditEngine engine) 297 { 298 fieldEngine = engine; 299 } 300 301 public WebPageRequest getWebPageRequest() 302 { 303 return fieldWebPageRequest; 304 } 305 306 public void setWebPageRequest(WebPageRequest WebPageRequest) 307 { 308 fieldWebPageRequest = WebPageRequest; 309 } 310 311 public void forward(String path, WebPageRequest inReq) throws OpenEditException 312 { 313 Page original = inReq.getPage(); 318 Page page = getPage(path); 319 321 WebPageRequest req = getWebPageRequest(); 322 req.removePageValue(PageRequestKeys.INNERLAYOUTOVERRIDE); 323 req.removePageValue(PageRequestKeys.LAYOUTOVERRIDE); 324 if( req.getContentPage().getPath().equals(inReq.getPage().getPath())) { 326 req.putProtectedPageValue(PageRequestKeys.CONTENT,page); 327 } 328 req.putProtectedPageValue(PageRequestKeys.PAGE,page); 329 req.putPageValue("forwardpage", original); 330 getEngine().getModuleManager().executePathActions(page, req); 331 getEngine().getModuleManager().executePageActions( page,req ); 332 inReq.setHasForwarded(true); 333 } 336 337 public PageStreamer copy() 338 { 339 PageStreamer streamer = new PageStreamer(); 340 streamer.setEngine(getEngine()); 341 streamer.fieldChildContentList = getChildContentList(); 342 streamer.setWebPageRequest(getWebPageRequest()); 343 streamer.setOutput(getOutput()); 344 return streamer; 345 } 346 347 protected String getLayout() 348 { 349 String override = (String ) getWebPageRequest().getPageValue("layoutoverride"); 350 if (override != null) 351 { 352 if (override.equals(Page.BLANK_LAYOUT)) 353 { 354 return null; 355 } 356 return override; 357 } 358 Page page = getWebPageRequest().getPage(); 359 360 if (page.hasLayout()) 361 { 362 String layout = page.getLayout(); 363 layout = page.getPageSettings().replaceProperty(layout); 364 return layout; 365 } 366 return null; 367 } 368 369 public List getChildContentList() 370 { 371 if ( fieldChildContentList == null) 372 { 373 fieldChildContentList = new ArrayList (); 374 } 375 return fieldChildContentList; 376 } 377 378 public Output getOutput() 379 { 380 return fieldOutput; 381 } 382 383 public void setOutput(Output inOutput) 384 { 385 fieldOutput = inOutput; 386 } 387 388 public boolean canView(String inPath) throws OpenEditException 389 { 390 Page linkpage = getPageManager().getPage(inPath); 391 if (linkpage.exists()) 392 { 393 Filter filter = linkpage.getViewFilter(); 394 WebPageRequest req = getWebPageRequest().copy(linkpage); 395 boolean value= ((filter == null) || filter.passes( req )); 396 return value; 397 } 398 return false; 399 } 400 401 public boolean canEdit(String inPath) throws OpenEditException 402 { 403 Page linkpage = getPageManager().getPage(inPath); 404 Filter filter = linkpage.getEditFilter(); 405 WebPageRequest req = getWebPageRequest().copy(linkpage); 406 boolean value= ((filter == null) || filter.passes( req )); 407 return value; 408 } 409 } 410 | Popular Tags |