1 13 15 package org.jahia.engines; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.PrintWriter ; 20 import java.util.HashMap ; 21 22 import javax.servlet.ServletException ; 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 FORM_TOKEN = "<!--ENGINE_CONTENT-->"; 41 private static EngineRenderer instance = null; 42 private static final String ENGINE_JSP = "/jsp/jahia/engines/engine.jsp"; 43 44 47 private EngineRenderer () { 48 logger.info ("***** Starting EngineRenderer *****"); 49 } 50 51 54 public static synchronized EngineRenderer getInstance () { 55 if (instance == null) { 56 instance = new EngineRenderer (); 57 } 58 return instance; 59 } 60 61 65 public void render (ParamBean jParams, HashMap engineHashMap) 66 throws JahiaException { 67 renderCore (jParams, engineHashMap); 68 } 69 70 74 public void render (JahiaData jData, HashMap engineHashMap) 75 throws JahiaException { 76 jData.params ().getRequest ().setAttribute ("org.jahia.data.JahiaData", 77 jData); 78 renderCore (jData.params (), engineHashMap); 79 } 80 81 87 private void renderCore (ParamBean jParams, HashMap engineHashMap) 88 throws JahiaException { 89 90 String fileName = JahiaEngine.EMPTY_STRING; 91 try { 92 engineHashMap.put ("jahiaBuild", 94 new Integer (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 mimeType = "text/html"; 107 if (jParams.getResponseMimeType() != null) { 109 mimeType = jParams.getResponseMimeType(); 110 } 111 if (jParams.settings ().isUtf8Encoding ()) { 113 jParams.getResponse ().setContentType (mimeType + ";charset=UTF-8"); 114 } else { 115 jParams.getResponse ().setContentType (mimeType); 116 } 117 118 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 jParams.getRequest ().setAttribute ("org.jahia.data.JahiaParams", jParams); 127 128 jParams.getRequest ().setAttribute ("org.jahia.params.ParamBean", jParams); 129 130 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 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 (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 jspSource = (String ) 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 fileName = (String ) engineHashMap.get (JahiaEngine.ENGINE_OUTPUT_FILE_PARAM); 176 177 Integer renderType = (Integer ) 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 ie) { 196 if (fileName == null) 197 fileName = "undefined file"; 198 String 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 se) { 206 if (fileName == null) 207 fileName = "undefined file"; 208 if (se.getRootCause () != null) { 209 String 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 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 244 private final String getJahiaCoreHttpPath (ParamBean jParams) { 245 return jParams.getRequest ().getContextPath (); 246 } 247 248 253 254 257 public void render (JahiaData jData, String sourceFileName, 258 String formString) 259 throws JahiaException { 260 String fileName = jData.params ().settings ().getJahiaEnginesDiskPath () + 261 File.separator + sourceFileName + ".html"; 262 String finalSource = decomposeSource (fileName, formString); 263 displaySource (finalSource, jData.params ()); 264 } 266 269 public void render (ParamBean jParams, String sourceFileName, 270 String formString) 271 throws JahiaException { 272 String fileName = jParams.settings ().getJahiaEnginesDiskPath () + 273 File.separator + sourceFileName + ".html"; 274 String finalSource = decomposeSource (fileName, formString); 275 displaySource (finalSource, jParams); 276 } 278 281 282 private String decomposeSource (String fileName, String form) 283 throws JahiaException { 284 String source = FileUtils.getInstance ().readFile (fileName); 285 String firstPart = source.substring (0, source.indexOf (FORM_TOKEN)); 286 String secondPart = source.substring (source.indexOf (FORM_TOKEN) + 287 FORM_TOKEN.length (), source.length ()); 288 return firstPart + form + secondPart; 289 } 291 294 public void displaySource (String source, ParamBean jParams) 295 throws JahiaException { 296 try { 297 PrintWriter out = jParams.getResponse ().getWriter (); 298 jParams.getResponse ().setContentType ("text/html"); 299 out.println (source); 300 } catch (IOException ie) { 301 String 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 } 311 316 317 } 318 | Popular Tags |