1 13 17 package org.jahia.engines.core; 18 19 import java.io.File ; 20 import java.util.Date ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 26 import org.jahia.content.ContentContainerListsXRefManager; 27 import org.jahia.content.ContentFieldXRefManager; 28 import org.jahia.content.ObjectKey; 29 import org.jahia.data.JahiaData; 30 import org.jahia.data.events.JahiaEvent; 31 import org.jahia.engines.EngineRenderer; 32 import org.jahia.engines.JahiaEngine; 33 import org.jahia.exceptions.JahiaException; 34 import org.jahia.exceptions.JahiaForbiddenAccessException; 35 import org.jahia.exceptions.JahiaPageNotFoundException; 36 import org.jahia.params.ParamBean; 37 import org.jahia.registries.ServicesRegistry; 38 import org.jahia.services.pages.JahiaPageDefinition; 39 40 41 public class Core_Engine implements JahiaEngine { 42 43 private static final org.apache.log4j.Logger logger = 44 org.apache.log4j.Logger.getLogger (Core_Engine.class); 45 46 private static Core_Engine instance = null; 47 48 49 public static final String ENGINE_NAME = "core"; 50 51 52 55 private Core_Engine () { 56 logger.debug ("***** Starting " + Core_Engine.class.getName () + " engine *****"); 57 } 58 59 60 63 public static synchronized Core_Engine getInstance () { 64 if (instance == null) { 65 instance = new Core_Engine (); 66 } 67 return instance; 68 } 69 70 71 74 public boolean authoriseRender (ParamBean jParams) { 75 return (jParams.getOperationMode () == ParamBean.EDIT); 76 } 77 78 79 82 public boolean needsJahiaData (ParamBean jParams) { 83 return true; 84 } 85 86 87 90 public String renderLink (ParamBean jParams, Object theObj) 91 throws JahiaException { 92 String params = EMPTY_STRING; 93 params += "/op/" + jParams.getOperationMode (); 96 params += "/pid/" + jParams.getPageID (); 97 return jParams.composeEngineUrl (ENGINE_NAME, params); 98 99 } 100 101 102 105 public void handleActions (ParamBean jParams, JahiaData jData) 106 throws JahiaException { 107 String ipAddr = jParams.getRequest ().getRemoteAddr (); 108 logger.debug ("Generating content for " + ipAddr + "..."); 109 processCore (jData); 110 postProcessCore (jData); 111 } 112 113 118 public final String getName () { 119 return ENGINE_NAME; 120 } 121 122 123 133 private void processCore (JahiaData jData) 134 throws JahiaException { 135 136 if (jData.params ().getPage () == null) { 137 if (!jData.params().getContentPage().checkReadAccess(jData.params(). 138 getUser())) { 139 throw new JahiaForbiddenAccessException(); 140 } 141 } 142 143 if (jData.params ().getPage () == null 144 || jData.params ().getPage ().getPageTemplate () == null) { 145 throw new JahiaPageNotFoundException (jData.params ().getPageID (), 148 jData.params ().getLocale ().toString (), 149 jData.params ().getOperationMode ()); 150 151 } 152 153 if (jData.params().settings().isPageCounterIncrementActivated()) { 154 if (!(jData.params ().getEntryLoadRequest ().isVersioned () || 156 jData.params ().getEntryLoadRequest ().isDeleted ()) 157 && (jData.params ().newPageRequest () 158 || (jData.params ().getLastEngineName () == null) 159 || (jData.params ().getLastEngineName ().equals (ENGINE_NAME)))) { 160 jData.params ().getPage ().incrementCounter (); 161 jData.params ().getPage ().commitChanges (false); 162 } 163 } 164 165 JahiaEvent theEvent = new JahiaEvent(this, jData.params(), jData.params ().getPage ()); 166 ServicesRegistry.getInstance().getJahiaEventService(). 167 fireLoadPage(theEvent); 168 169 String fileName = resolveJSPFullFileName(jData); 171 172 HashMap engineHashMap = new HashMap (); 174 engineHashMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 175 engineHashMap.put (ENGINE_OUTPUT_FILE_PARAM, fileName); 176 engineHashMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 177 178 EngineRenderer.getInstance ().render (jData, engineHashMap); 179 } 180 181 private String resolveJSPFullFileName (JahiaData jData) { 182 String jspFullFileName = null; 183 184 JahiaPageDefinition template = jData.params ().getPage ().getPageTemplate (); 185 if (template != null) { 186 jspFullFileName = template.getSourcePath (); 187 } 188 189 if (jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER) != null) { 191 194 String pathToAlternateTemplate = jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER); 195 if (pathToAlternateTemplate.indexOf("..") == -1) { 196 logger.debug ("template source path :" + jspFullFileName); 197 198 String justFileName = pathToAlternateTemplate; 205 String justPath = ""; 206 int altLastSlashPos = pathToAlternateTemplate.lastIndexOf("/"); 207 if (altLastSlashPos != -1) { 208 justFileName = pathToAlternateTemplate.substring(altLastSlashPos + 1); 209 justPath = pathToAlternateTemplate.substring(0, altLastSlashPos + 1); 210 } 211 String mimeType = jData.params().getContext().getMimeType(justFileName); 212 if (mimeType != null) { 213 logger.debug("Using mime type " + mimeType); 215 jData.params().setResponseMimeType(mimeType); 216 } 217 int extensionPos = justFileName.lastIndexOf("."); 218 if (extensionPos != -1) { 219 String extension = justFileName.substring(extensionPos); 220 if (!extension.equalsIgnoreCase(".jsp")) { 221 justFileName = justFileName.substring(0, extensionPos) + ".jsp"; 222 } 223 } 224 pathToAlternateTemplate = justPath + justFileName; 225 226 int lastSlashPos = jspFullFileName.lastIndexOf ("/"); 227 if (lastSlashPos != -1) { 228 jspFullFileName = jspFullFileName.substring(0, lastSlashPos + 1) + pathToAlternateTemplate; 229 } 230 logger.debug ("resolvedJSPFullFileName :" + jspFullFileName); 231 232 File jspFile = new File (jData.params().getContext ().getRealPath (jspFullFileName)); 233 if (!jspFile.exists ()) { 234 jspFullFileName = "/jsp/test/" + jData.params ().getParameter (ParamBean.TEMPLATE_PARAMETER); 235 } 236 logger.debug ("Overriding page template with URL template : " + jspFullFileName); 237 238 jData.params().setCacheExpirationDate(new Date ()); 242 } 243 } 244 245 return jspFullFileName; 246 } 247 248 249 258 private void postProcessCore (JahiaData jData) 259 throws JahiaException { 260 handleAbsoluteReferencesChanges (jData); 261 } 262 263 276 private void handleAbsoluteReferencesChanges (JahiaData jData) 277 throws JahiaException { 278 Set absoluteFieldAccesses = jData.fields ().getAbsoluteFieldAccesses (); 280 Set absoluteContainerListAccesses = jData.containers () 281 .getAbsoluteContainerListAccesses (); 282 283 286 Set fieldKeys = ContentFieldXRefManager.getInstance ().getAbsoluteFieldsFromPageID ( 287 jData.params ().getPageID ()); 288 Set containerListKeys = ContentContainerListsXRefManager.getInstance () 289 .getAbsoluteContainerListsFromPageID (jData.params ().getPageID ()); 290 291 Set databaseAbsoluteFieldIDs = new HashSet (); 293 Iterator fieldKeyIter = fieldKeys.iterator (); 294 while (fieldKeyIter.hasNext ()) { 295 ObjectKey curKey = (ObjectKey) fieldKeyIter.next (); 296 logger.debug ("Converting databaseAbsoluteFieldKey " + curKey); 297 databaseAbsoluteFieldIDs.add (new Integer (curKey.getIdInType ())); 298 } 299 Set databaseAbsoluteContainerListIDs = new HashSet (); 300 Iterator containerListKeyIter = containerListKeys.iterator (); 301 while (containerListKeyIter.hasNext ()) { 302 ObjectKey curKey = (ObjectKey) containerListKeyIter.next (); 303 logger.debug ("Converting databaseAbsoluteContainerListKey " + curKey); 304 databaseAbsoluteContainerListIDs.add (new Integer (curKey.getIdInType ())); 305 } 306 307 Set addedFieldIDs = new HashSet (absoluteFieldAccesses); 309 addedFieldIDs.removeAll (databaseAbsoluteFieldIDs); 310 Set removedFieldIDs = new HashSet (databaseAbsoluteFieldIDs); 311 removedFieldIDs.removeAll (absoluteFieldAccesses); 312 313 Set addedContainerListIDs = new HashSet (absoluteContainerListAccesses); 314 addedContainerListIDs.removeAll (databaseAbsoluteContainerListIDs); 315 Set removedContainerListIDs = new HashSet (databaseAbsoluteContainerListIDs); 316 removedContainerListIDs.removeAll (absoluteContainerListAccesses); 317 318 Iterator addedFieldIDIter = addedFieldIDs.iterator (); 320 while (addedFieldIDIter.hasNext ()) { 321 Integer curFieldID = (Integer ) addedFieldIDIter.next (); 322 logger.debug ( 323 "Adding reference from page ID " + jData.params ().getPageID () + 324 " to field " + 325 curFieldID); 326 ContentFieldXRefManager.getInstance ().setAbsoluteFieldPageID ( 327 curFieldID.intValue (), 328 jData.params ().getPageID ()); 329 } 330 Iterator removedFieldIDIter = removedFieldIDs.iterator (); 331 while (removedFieldIDIter.hasNext ()) { 332 Integer curFieldID = (Integer ) removedFieldIDIter.next (); 333 logger.debug ( 334 "Removing reference from page ID " + jData.params ().getPageID () + 335 " to field " + 336 curFieldID); 337 ContentFieldXRefManager.getInstance ().removeAbsoluteFieldPageID ( 338 curFieldID.intValue (), jData.params ().getPageID ()); 339 } 340 341 Iterator addedContainerListIDIter = addedContainerListIDs.iterator (); 342 while (addedContainerListIDIter.hasNext ()) { 343 Integer curContainerListID = (Integer ) addedContainerListIDIter.next (); 344 logger.debug ( 345 "Adding reference from page ID " + jData.params ().getPageID () + 346 " to container list " + 347 curContainerListID); 348 ContentContainerListsXRefManager.getInstance ().setAbsoluteContainerListPageID ( 349 curContainerListID.intValue (), jData.params ().getPageID ()); 350 } 351 Iterator removedContainerListIDIter = removedContainerListIDs.iterator (); 352 while (removedContainerListIDIter.hasNext ()) { 353 Integer curContainerListID = (Integer ) removedContainerListIDIter.next (); 354 logger.debug ( 355 "Removing reference from page ID " + jData.params ().getPageID () + 356 " to container list " + 357 curContainerListID); 358 ContentContainerListsXRefManager.getInstance ().removeAbsoluteContainerListPageID ( 359 curContainerListID.intValue (), jData.params ().getPageID ()); 360 } 361 } 362 363 } 364 | Popular Tags |