KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > EngineRenderer


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 // EV 04.12.2000
14

15 package org.jahia.engines;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23
24 import org.jahia.bin.JahiaErrorDisplay;
25 import org.jahia.data.JahiaData;
26 import org.jahia.data.beans.JahiaBean;
27 import org.jahia.data.beans.PageBean;
28 import org.jahia.data.beans.RequestBean;
29 import org.jahia.data.beans.SiteBean;
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.gui.GuiBean;
32 import org.jahia.params.ParamBean;
33 import org.jahia.utils.FileUtils;
34
35 public class EngineRenderer {
36
37     private static org.apache.log4j.Logger logger =
38             org.apache.log4j.Logger.getLogger (EngineRenderer.class);
39
40     public static final String JavaDoc FORM_TOKEN = "<!--ENGINE_CONTENT-->";
41     private static EngineRenderer instance = null;
42     private static final String JavaDoc ENGINE_JSP = "/jsp/jahia/engines/engine.jsp";
43
44     /**
45      * constructor EV 04.12.2000
46      */

47     private EngineRenderer () {
48         logger.info ("***** Starting EngineRenderer *****");
49     }
50
51     /**
52      * getInstance EV 04.12.2000
53      */

54     public static synchronized EngineRenderer getInstance () {
55         if (instance == null) {
56             instance = new EngineRenderer ();
57         }
58         return instance;
59     }
60
61     /**
62      * render ParamBean render EV 23.12.2000 AK 04.01.2001 remove the fileName from
63      * method arguments
64      */

65     public void render (ParamBean jParams, HashMap JavaDoc engineHashMap)
66             throws JahiaException {
67         renderCore (jParams, engineHashMap);
68     }
69
70     /**
71      * render JahiaData render EV 23.12.2000 AK 04.01.2001 remove the fileName from
72      * method arguments
73      */

74     public void render (JahiaData jData, HashMap JavaDoc engineHashMap)
75             throws JahiaException {
76         jData.params ().getRequest ().setAttribute ("org.jahia.data.JahiaData",
77                 jData);
78         renderCore (jData.params (), engineHashMap);
79     }
80
81     /**
82      * renderCore called by ParamBean render, JahiaData render EV 23.12.2000 AK 04.01.2001
83      * get the fileName from the engineHashMap and not from method arguments AK 04.01.2001
84      * get the render type from the engineHashMap (include or forward) AK 04.01.2001 correct
85      * the jahiaChrono call consoleMsg
86      */

87     private void renderCore (ParamBean jParams, HashMap JavaDoc engineHashMap)
88             throws JahiaException {
89
90         String JavaDoc fileName = JahiaEngine.EMPTY_STRING;
91         try {
92             // add default & common properties to the hashmap...
93
engineHashMap.put ("jahiaBuild",
94                     new Integer JavaDoc (jParams.settings ().getBuildNumber ()));
95             engineHashMap.put ("javaScriptPath",
96                     jParams.settings ().getJsHttpPath ());
97             engineHashMap.put ("imagesPath",
98                     jParams.settings ().getEnginesContext () +
99                     "engines/images/");
100             if (engineHashMap.get (JahiaEngine.ENGINE_OUTPUT_FILE_PARAM) == null)
101                 engineHashMap.put (JahiaEngine.ENGINE_OUTPUT_FILE_PARAM, ENGINE_JSP);
102
103             if (engineHashMap.get (JahiaEngine.ENGINE_URL_PARAM) == null)
104                 engineHashMap.put (JahiaEngine.ENGINE_URL_PARAM, JahiaEngine.EMPTY_STRING);
105
106             String JavaDoc mimeType = "text/html";
107             // did we override the mime type ?
108
if (jParams.getResponseMimeType() != null) {
109                 mimeType = jParams.getResponseMimeType();
110             }
111             // set response attributes...
112
if (jParams.settings ().isUtf8Encoding ()) {
113                 jParams.getResponse ().setContentType (mimeType + ";charset=UTF-8");
114             } else {
115                 jParams.getResponse ().setContentType (mimeType);
116             }
117
118             // add "no cache" to request header only if methode is not a get
119
if (!jParams.getRequest ().getMethod ().toLowerCase ().equals ("get")) {
120                 jParams.getResponse ().setHeader ("Cache-Control", "No-Cache");
121                 jParams.getResponse ().setHeader ("Pragma", "No-Cache");
122                 jParams.getResponse ().setDateHeader ("Expires", 0);
123             }
124
125             // @todo FIXME is this attribute name still used ? If not remove it
126
jParams.getRequest ().setAttribute ("org.jahia.data.JahiaParams", jParams);
127
128             jParams.getRequest ().setAttribute ("org.jahia.params.ParamBean", jParams);
129
130             // now let's set the content access beans.
131
jParams.getRequest ().setAttribute ("currentPage",
132                     new PageBean (jParams.getPage (), jParams));
133             jParams.getRequest ().setAttribute ("currentSite",
134                     new SiteBean (jParams.getSite (), jParams));
135             jParams.getRequest ().setAttribute ("currentJahia", new JahiaBean (jParams));
136             jParams.getRequest ().setAttribute ("currentUser", jParams.getUser ());
137             jParams.getRequest ().setAttribute ("currentRequest",
138                     new RequestBean (new GuiBean (jParams), jParams));
139
140             boolean isIE = false;
141             String JavaDoc userAgent = jParams.getRequest ().getHeader ("user-agent");
142             if (userAgent != null) {
143                 isIE = (userAgent.indexOf ("IE") != -1);
144             }
145
146             jParams.getRequest ().setAttribute ("isIE", new Boolean JavaDoc (isIE));
147             jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap",
148                     engineHashMap);
149             jParams.getRequest ().setAttribute ("javaScriptPath",
150                     jParams.settings ().getJsHttpPath ());
151             jParams.getRequest ().setAttribute ("URL",
152                     getJahiaCoreHttpPath (jParams) +
153                     jParams.settings ().
154                     getEnginesContext ());
155             jParams.getRequest ().setAttribute (JahiaEngine.ENGINE_URL_PARAM,
156                     getJahiaCoreHttpPath (jParams) +
157                     jParams.settings ().
158                     getEnginesContext ());
159             jParams.getRequest ().setAttribute ("serverURL",
160                     getJahiaCoreHttpPath (jParams));
161             jParams.getRequest ().setAttribute ("httpJsContextPath",
162                     getJahiaCoreHttpPath (jParams) +
163                     jParams.settings ().
164                     getJavascriptContext ());
165
166             String JavaDoc jspSource = (String JavaDoc) engineHashMap.get ("jspSource");
167             if (jspSource == null)
168                 jspSource = JahiaEngine.EMPTY_STRING;
169             jParams.getRequest ().setAttribute ("jspSource", jspSource);
170
171             if (jParams.getRequest ().getAttribute ("engineTitle") == null)
172                 jParams.getRequest ().setAttribute ("engineTitle", "No title");
173
174             // get the fileName from the engineHashMap...
175
fileName = (String JavaDoc) engineHashMap.get (JahiaEngine.ENGINE_OUTPUT_FILE_PARAM);
176
177             // get the render type from the engineHashMap... (include or forward)
178
Integer JavaDoc renderType = (Integer JavaDoc) engineHashMap.get (JahiaEngine.RENDER_TYPE_PARAM);
179
180             logger.debug ("Dispatching request to " + fileName +
181                     " using render type " + renderType + "...");
182
183             if (renderType.intValue () == JahiaEngine.RENDERTYPE_INCLUDE) {
184                 jParams.getContext ().getRequestDispatcher (fileName).include (
185                         jParams.getRequest (), jParams.getResponse ());
186             } else if (renderType.intValue () == JahiaEngine.RENDERTYPE_FORWARD) {
187                 jParams.getContext ().getRequestDispatcher (fileName).forward (
188                         jParams.getRequest (), jParams.getResponse ());
189             } else if (renderType.intValue () == JahiaEngine.RENDERTYPE_REDIRECT) {
190                 jParams.getResponse ().sendRedirect (fileName);
191             } else if (renderType.intValue () == JahiaEngine.RENDERTYPE_NAMED_DISPATCHER) {
192                 jParams.getContext ().getNamedDispatcher (fileName).forward (
193                         jParams.getRequest (), jParams.getResponse ());
194             }
195         } catch (IOException JavaDoc ie) {
196             if (fileName == null)
197                 fileName = "undefined file";
198             String JavaDoc errorMsg = "Error while drawing the Engine " + fileName +
199                     " : " + ie.getMessage () + " -> BAILING OUT";
200             logger.error (errorMsg, ie);
201             throw new JahiaException (
202                     "Error while drawing a Jahia engine's content",
203                     errorMsg, JahiaException.WINDOW_ERROR,
204                     JahiaException.CRITICAL_SEVERITY, ie);
205         } catch (ServletException JavaDoc se) {
206             if (fileName == null)
207                 fileName = "undefined file";
208             if (se.getRootCause () != null) {
209                 String JavaDoc errorMsg = "Root cause : Error while forwarding the Engine " +
210                         fileName + " : " +
211                         se.getRootCause ().getMessage () +
212                         " -> BAILING OUT";
213                 logger.error (errorMsg, JahiaErrorDisplay.getNestedException (se));
214                 throw new JahiaException (
215                         "Error while forwarding a Jahia engine's content",
216                         errorMsg,
217                         JahiaException.WINDOW_ERROR,
218                         JahiaException.CRITICAL_SEVERITY,
219                         se);
220             } else {
221                 String JavaDoc errorMsg = "Error while forwarding the Engine " +
222                         fileName +
223                         " : " +
224                         se.getMessage () +
225                         " -> BAILING OUT";
226                 logger.error (errorMsg, se);
227                 throw new JahiaException (
228                         "Error while forwarding a Jahia engine's content",
229                         errorMsg,
230                         JahiaException.WINDOW_ERROR,
231                         JahiaException.CRITICAL_SEVERITY,
232                         se);
233             }
234         }
235     }
236
237     /**
238      * Build an http path containing the server name for the current site, instead of the path
239      * from JahiaPrivateSettings.
240      *
241      * @return An http path leading to Jahia, built with the server name, and the server port if
242      * nonstandard.
243      */

244     private final String JavaDoc getJahiaCoreHttpPath (ParamBean jParams) {
245         return jParams.getRequest ().getContextPath ();
246     }
247
248     /*******************************************************************************
249      **** ****
250      **** A PARTIR DE LA CES METHODES SONT DESTINEES A MOURIR.... ****
251      **** ****
252      *******************************************************************************/

253
254     /**
255      * render EV 04.12.2000
256      */

257     public void render (JahiaData jData, String JavaDoc sourceFileName,
258                         String JavaDoc formString)
259             throws JahiaException {
260         String JavaDoc fileName = jData.params ().settings ().getJahiaEnginesDiskPath () +
261                 File.separator + sourceFileName + ".html";
262         String JavaDoc finalSource = decomposeSource (fileName, formString);
263         displaySource (finalSource, jData.params ());
264     } // end render
265

266     /**
267      * render EV 04.12.2000
268      */

269     public void render (ParamBean jParams, String JavaDoc sourceFileName,
270                         String JavaDoc formString)
271             throws JahiaException {
272         String JavaDoc fileName = jParams.settings ().getJahiaEnginesDiskPath () +
273                 File.separator + sourceFileName + ".html";
274         String JavaDoc finalSource = decomposeSource (fileName, formString);
275         displaySource (finalSource, jParams);
276     } // end render
277

278     /**
279      * decomposeSource EV 04.12.2000
280      */

281
282     private String JavaDoc decomposeSource (String JavaDoc fileName, String JavaDoc form)
283             throws JahiaException {
284         String JavaDoc source = FileUtils.getInstance ().readFile (fileName);
285         String JavaDoc firstPart = source.substring (0, source.indexOf (FORM_TOKEN));
286         String JavaDoc secondPart = source.substring (source.indexOf (FORM_TOKEN) +
287                 FORM_TOKEN.length (), source.length ());
288         return firstPart + form + secondPart;
289     } // end decomposeSource
290

291     /**
292      * displaySource EV 04.12.2000
293      */

294     public void displaySource (String JavaDoc source, ParamBean jParams)
295             throws JahiaException {
296         try {
297             PrintWriter JavaDoc out = jParams.getResponse ().getWriter ();
298             jParams.getResponse ().setContentType ("text/html");
299             out.println (source);
300         } catch (IOException JavaDoc ie) {
301             String JavaDoc errorMsg = "Error while drawing the Engine Window : " +
302                     ie.getMessage () + " -> BAILING OUT";
303             logger.error (errorMsg, ie);
304             throw new JahiaException (
305                     "Error while drawing a Jahia window's content",
306                     errorMsg, JahiaException.WINDOW_ERROR,
307                     JahiaException.CRITICAL_SEVERITY, ie);
308         }
309     } // end displaySource
310

311     /*******************************************************************************
312      **** ****
313      **** JUSQU'A LA CES METHODES SONT DESTINEES A MOURIR.... ****
314      **** ****
315      *******************************************************************************/

316
317 }
318
Popular Tags