KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > PageStreamer


1 /*
2  * Created on Oct 19, 2004
3  */

4 package com.openedit.page;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
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 /**
23  * @author Matthew Avery, mavery@einnovation.com
24  */

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 JavaDoc fieldWebPageRequestedCount;
33     protected List JavaDoc fieldChildContentList;
34
35     public PageStreamer()
36     {
37         log.debug("Created streamer");
38     }
39     
40     /**
41      * This is called only once
42      * We need to exec all the page actions ahead of time
43      * And check the read permissions ahead of time
44      * Then we can do the rendering
45      * @throws OpenEditException
46      */

47
48     public void render() throws OpenEditException
49     {
50         Page page = getWebPageRequest().getPage();
51         if (page.isHtml() )
52         {
53             getChildContentList().add(getWebPageRequest().getContentPage() );
54             //allow someone to programatically override the top level inner layout
55
String JavaDoc override = (String JavaDoc)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             //handle layout
69
String JavaDoc 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 JavaDoc 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     /**
113      * @deprecated use renderContent()
114      * @throws OpenEditException
115      */

116     public void streamContent() throws OpenEditException
117     {
118         renderContent();
119     }
120     public void renderContent() throws OpenEditException
121     {
122         //if I am being called from an inner layout make sure I am at the top first
123
if ( getChildContentList().size() == 0)
124         {
125             //throw new OpenEditException("Ran out of content on " + getWebPageRequest().getPath());
126
//stream(getPage());
127
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     /**
139      * Allows a page to be streamed that uses this request as the parent request
140      */

141     public void include(Page inPage, WebPageRequest inContext) throws OpenEditException
142     {
143         int hitcount = incrementWebPageRequestedCount(inPage);
144         //This is used when viewing a layout page directly (i.e. /layout.html)
145
if (hitcount >= 10)
146         {
147             //ok its an infinite loop
148
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         //This is trickly to understand. The contents actions have already been run. Now we either
155
//run the actions or if its the content page those actions have already been run
156

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 ) //This could be a draft version
164
{
165                 request = inContext.copy(torender);
166                 getEngine().executePageActions(request);
167             }
168         }
169         else
170         {
171             //these are the included pages ie. /sidebar.html that may have their own actions
172
request = inContext.copy(inPage);
173             // Ensure the FILE_PATH variable has the correct value for this streamed page.
174

175             request.putPageValue(PageRequestKeys.FILE_PATH, inPage.getDirectory()); //This is legacy from OE 4.0 can be deleted anytime
176
torender = getPageManager().getPage(inPage, request );
177             if ( torender != inPage) //This could be a draft version
178
{
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 JavaDoc inPath, WebPageRequest inReq) throws OpenEditException
190     {
191         String JavaDoc[] parts = inPath.split("[?]");
192         String JavaDoc 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 JavaDoc[] args = parts[1].split("&");
199             for (int i = 0; i < args.length; i++)
200             {
201                 String JavaDoc[] pairs = args[i].split("=");
202                 getWebPageRequest().setRequestParameter(pairs[0], pairs[1]);
203             }
204         }
205         include(page, inReq);
206     }
207     public void include(String JavaDoc inPath) throws OpenEditException
208     {
209         include( inPath, getWebPageRequest());
210     }
211     /**
212      * Use include
213      * @deprecated
214      */

215     public void stream(Page inPage) throws OpenEditException
216     {
217         include( inPage);
218     }
219     /**
220      * Use include
221      * @deprecated
222      */

223     public void stream(Page inPage, WebPageRequest inContext) throws OpenEditException
224     {
225         include( inPage, inContext);
226     }
227     /**
228      * Use include
229      * @deprecated
230      */

231     public void stream(String JavaDoc inPath, WebPageRequest inReq) throws OpenEditException
232     {
233         include( inPath, inReq);
234     }
235     /**
236      * Use include
237      * @deprecated
238      */

239     public void stream(String JavaDoc inPath) throws OpenEditException
240     {
241         include( inPath );
242     }
243     public Map JavaDoc getWebPageRequestedCount()
244     {
245         if (fieldWebPageRequestedCount == null)
246         {
247             fieldWebPageRequestedCount = new HashMap JavaDoc();
248         }
249         return fieldWebPageRequestedCount;
250     }
251
252     protected int incrementWebPageRequestedCount(Page inPage)
253     {
254         Integer JavaDoc count = (Integer JavaDoc) 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 JavaDoc(hitcount));
263         return hitcount;
264     }
265
266     public boolean doesExist(String JavaDoc 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 JavaDoc inPath) throws OpenEditException
276     {
277         String JavaDoc fullPath = PathUtilities.buildRelative(inPath, getWebPageRequest().getContentPage().getPath());
278         return getPage(fullPath);
279     }
280
281     public Page getPage(String JavaDoc 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 JavaDoc path, WebPageRequest inReq) throws OpenEditException
312     {
313         //if this is the error page then we can get into an infinite loop
314
//WebPageRequest WebPageRequest = getWebPageRequest().copy();
315
//WebPageRequest.setPage( getPage( path ) );
316
//getEngine().render( WebPageRequest );
317
Page original = inReq.getPage();
318         Page page = getPage(path);
319         //remove layout overrides
320

321         WebPageRequest req = getWebPageRequest();
322         req.removePageValue(PageRequestKeys.INNERLAYOUTOVERRIDE);
323         req.removePageValue(PageRequestKeys.LAYOUTOVERRIDE);
324         if( req.getContentPage().getPath().equals(inReq.getPage().getPath())) //are we rendeing the content page?
325
{
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         //Now it continues to render as normal but with alternative content.
334
//Does not support custom layouts
335
}
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 JavaDoc getLayout()
348     {
349         String JavaDoc override = (String JavaDoc) 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 JavaDoc layout = page.getLayout();
363             layout = page.getPageSettings().replaceProperty(layout);
364             return layout;
365         }
366         return null;
367     }
368
369     public List JavaDoc getChildContentList()
370     {
371         if ( fieldChildContentList == null)
372         {
373             fieldChildContentList = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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