1 23 24 package org.infoglue.deliver.util; 25 26 import java.security.Principal ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.servlet.http.HttpServletRequest ; 35 36 import org.apache.log4j.Logger; 37 import org.infoglue.cms.applications.common.Session; 38 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController; 39 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController; 40 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController; 41 import org.infoglue.cms.entities.content.ContentVO; 42 import org.infoglue.cms.entities.management.LanguageVO; 43 import org.infoglue.cms.entities.management.Repository; 44 import org.infoglue.cms.entities.management.RepositoryLanguage; 45 import org.infoglue.cms.entities.management.RepositoryVO; 46 import org.infoglue.cms.entities.structure.SiteNode; 47 import org.infoglue.cms.entities.structure.SiteNodeVO; 48 import org.infoglue.cms.exception.SystemException; 49 import org.infoglue.cms.security.InfoGluePrincipal; 50 import org.infoglue.cms.util.CmsPropertyHandler; 51 import org.infoglue.cms.util.FakeHttpServletRequest; 52 import org.infoglue.cms.util.FakeHttpServletResponse; 53 import org.infoglue.cms.util.FakeHttpSession; 54 import org.infoglue.deliver.applications.databeans.DatabaseWrapper; 55 import org.infoglue.deliver.applications.databeans.DeliveryContext; 56 import org.infoglue.deliver.controllers.kernel.impl.simple.BasicTemplateController; 57 import org.infoglue.deliver.controllers.kernel.impl.simple.ExtranetController; 58 import org.infoglue.deliver.controllers.kernel.impl.simple.IntegrationDeliveryController; 59 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController; 60 import org.infoglue.deliver.controllers.kernel.impl.simple.NodeDeliveryController; 61 import org.infoglue.deliver.controllers.kernel.impl.simple.RepositoryDeliveryController; 62 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController; 63 64 65 public class RequestAndMetaInfoCentricCachePopulator 66 { 67 public final static Logger logger = Logger.getLogger(RequestAndMetaInfoCentricCachePopulator.class.getName()); 68 69 76 77 public void recache(DatabaseWrapper dbWrapper, Integer siteNodeId) throws SystemException, Exception 78 { 79 logger.info("recache starting.."); 80 81 HttpHelper helper = new HttpHelper(); 82 String recacheUrl = CmsPropertyHandler.getRecacheUrl() + "?siteNodeId=" + siteNodeId + "&refresh=true&isRecacheCall=true"; 83 String response = helper.getUrlContent(recacheUrl); 84 85 String recacheBaseUrl = CmsPropertyHandler.getRecacheUrl().replaceAll("/ViewPage.action", ""); 86 String pathsToRecacheOnPublishing = CmsPropertyHandler.getPathsToRecacheOnPublishing(); 87 if(pathsToRecacheOnPublishing.indexOf("pathsToRecacheOnPublishing") == -1) 88 { 89 String [] pathsToRecacheOnPublishingArray = pathsToRecacheOnPublishing.split(","); 90 for(int i = 0; i < pathsToRecacheOnPublishingArray.length; i++) 91 { 92 recacheUrl = recacheBaseUrl + pathsToRecacheOnPublishingArray[i] + "?refresh=true&isRecacheCall=true"; 93 response = helper.getUrlContent(recacheUrl); 95 } 96 } 97 98 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(dbWrapper.getDatabase(), siteNodeId); 99 if(masterLanguageVO == null) 100 throw new SystemException("There was no master language for the siteNode " + siteNodeId); 101 102 Integer languageId = masterLanguageVO.getLanguageId(); 103 if(languageId == null) 104 languageId = masterLanguageVO.getLanguageId(); 105 106 Integer contentId = new Integer (-1); 107 108 Principal principal = (Principal )CacheController.getCachedObject("userCache", "anonymous"); 109 if(principal == null) 110 { 111 Map arguments = new HashMap (); 112 arguments.put("j_username", CmsPropertyHandler.getAnonymousUser()); 113 arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword()); 114 115 principal = ExtranetController.getController().getAuthenticatedPrincipal(dbWrapper.getDatabase(), arguments); 116 117 if(principal != null) 118 CacheController.cacheObject("userCache", "anonymous", principal); 119 } 120 121 FakeHttpSession fakeHttpServletSession = new FakeHttpSession(); 122 FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse(); 123 FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest(); 124 fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId); 125 fakeHttpServletRequest.setParameter("languageId", "" + languageId); 126 fakeHttpServletRequest.setParameter("contentId", "" + contentId); 127 fakeHttpServletRequest.setRequestURI("ViewPage.action"); 128 129 fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId); 130 fakeHttpServletRequest.setAttribute("languageId", "" + languageId); 131 fakeHttpServletRequest.setAttribute("contentId", "" + contentId); 132 133 fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext()); 134 135 BrowserBean browserBean = new BrowserBean(); 136 138 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId); 139 IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId, languageId, contentId); 140 TemplateController templateController = getTemplateController(dbWrapper, siteNodeId, languageId, contentId, new FakeHttpServletRequest(), (InfoGluePrincipal)principal, false, browserBean, nodeDeliveryController, integrationDeliveryController); 141 142 DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(); 143 deliveryContext.setSiteNodeId(siteNodeId); 145 deliveryContext.setContentId(contentId); 146 deliveryContext.setLanguageId(languageId); 147 deliveryContext.setPageKey("" + System.currentTimeMillis()); 148 deliveryContext.setSession(new Session(fakeHttpServletSession)); 149 deliveryContext.setInfoGlueAbstractAction(null); 150 deliveryContext.setHttpServletRequest(fakeHttpServletRequest); 151 deliveryContext.setHttpServletResponse(fakeHttpServletResponse); 152 153 templateController.setDeliveryContext(deliveryContext); 154 155 deliveryContext.setDisablePageCache(true); 157 158 SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId); 159 SiteNodeVO rootSiteNodeVO = templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId()); 160 161 recurseSiteNodeTree(rootSiteNodeVO.getId(), languageId, templateController, principal, 1, 0); 162 163 List templates = ContentController.getContentController().getContentVOWithContentTypeDefinition("HTMLTemplate", dbWrapper.getDatabase()); 164 Iterator templatesIterator = templates.iterator(); 165 { 166 ContentVO template = (ContentVO)templatesIterator.next(); 167 168 String templateString = templateController.getContentAttribute(template.getId(), languageId, "Template", true); 169 } 170 171 RepositoryVO repository = RepositoryDeliveryController.getRepositoryDeliveryController().getMasterRepository(dbWrapper.getDatabase()); 172 173 RepositoryDeliveryController.getRepositoryDeliveryController().getRepositoryVOListFromServerName(dbWrapper.getDatabase(), "localhost", "8080", repository.getName()); 174 175 logger.info("recache stopped.."); 176 } 177 178 179 private void recurseSiteNodeTree(Integer siteNodeId, Integer languageId, TemplateController templateController, Principal principal, int maxLevel, int currentLevel) throws Exception 180 { 181 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, templateController.getDatabase(), true); 182 SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId); 183 184 templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "Title", true); 185 templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "NavigationTitle", true); 186 templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "NiceURIName", true); 187 templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "Description", true); 188 templateController.getContentAttribute(siteNodeVO.getMetaInfoContentId(), languageId, "ComponentStructure", true); 189 190 List childPages = templateController.getChildPages(siteNodeId); 191 192 templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId()); 193 templateController.getParentSiteNode(siteNodeVO.getId()); 194 195 Collection childSiteNodes = siteNode.getChildSiteNodes(); 196 197 216 217 Iterator childSiteNodesIterator = childSiteNodes.iterator(); 218 while(childSiteNodesIterator.hasNext()) 219 { 220 SiteNode childSiteNode = (SiteNode)childSiteNodesIterator.next(); 221 if(maxLevel > currentLevel) 222 recurseSiteNodeTree(childSiteNode.getId(), languageId, templateController, principal, maxLevel, currentLevel + 1); 223 } 224 225 226 Repository repository = RepositoryController.getController().getRepositoryWithId(siteNodeVO.getRepositoryId(), templateController.getDatabase()); 227 Collection languages = repository.getRepositoryLanguages(); 228 Iterator languagesIterator = languages.iterator(); 229 while(languagesIterator.hasNext()) 230 { 231 RepositoryLanguage repositoryLanguage = (RepositoryLanguage)languagesIterator.next(); 232 LanguageDeliveryController.getLanguageDeliveryController().getLanguageIfSiteNodeSupportsIt(templateController.getDatabase(), repositoryLanguage.getLanguage().getId(), siteNodeId); 233 } 234 235 236 Integer contentId = new Integer (-1); 237 238 FakeHttpSession fakeHttpServletSession = new FakeHttpSession(); 239 FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse(); 240 FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest(); 241 fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId); 242 fakeHttpServletRequest.setParameter("languageId", "" + languageId); 243 fakeHttpServletRequest.setParameter("contentId", "" + contentId); 244 fakeHttpServletRequest.setRequestURI("ViewPage.action"); 245 246 fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId); 247 fakeHttpServletRequest.setAttribute("languageId", "" + languageId); 248 fakeHttpServletRequest.setAttribute("contentId", "" + contentId); 249 250 fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext()); 251 252 BrowserBean browserBean = new BrowserBean(); 253 255 NodeDeliveryController nodeDeliveryController = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId); 256 IntegrationDeliveryController integrationDeliveryController = IntegrationDeliveryController.getIntegrationDeliveryController(siteNodeId, languageId, contentId); 257 TemplateController subTemplateController = getTemplateController(templateController.getDatabaseWrapper(), siteNodeId, languageId, contentId, new FakeHttpServletRequest(), (InfoGluePrincipal)principal, false, browserBean, nodeDeliveryController, integrationDeliveryController); 258 259 DeliveryContext deliveryContext = DeliveryContext.getDeliveryContext(); 260 deliveryContext.setSiteNodeId(siteNodeId); 262 deliveryContext.setContentId(contentId); 263 deliveryContext.setLanguageId(languageId); 264 deliveryContext.setPageKey("" + System.currentTimeMillis()); 265 deliveryContext.setSession(new Session(fakeHttpServletSession)); 266 deliveryContext.setInfoGlueAbstractAction(null); 267 deliveryContext.setHttpServletRequest(fakeHttpServletRequest); 268 deliveryContext.setHttpServletResponse(fakeHttpServletResponse); 269 270 subTemplateController.setDeliveryContext(deliveryContext); 271 272 deliveryContext.setDisablePageCache(true); 274 275 SiteNodeVO rootSiteNodeVO = templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId()); 276 277 String pagePath = subTemplateController.getCurrentPagePath(); 278 279 CacheController.cacheObject("newPagePathCache", deliveryContext.getPageKey(), pagePath); 280 } 281 282 288 289 public TemplateController getTemplateController(DatabaseWrapper dbWrapper, Integer siteNodeId, Integer languageId, Integer contentId, HttpServletRequest request, InfoGluePrincipal infoGluePrincipal, boolean allowEditOnSightAtAll, BrowserBean browserBean, NodeDeliveryController nodeDeliveryController, IntegrationDeliveryController integrationDeliveryController) throws SystemException, Exception 290 { 291 TemplateController templateController = new BasicTemplateController(dbWrapper, infoGluePrincipal); 292 templateController.setStandardRequestParameters(siteNodeId, languageId, contentId); 293 templateController.setHttpRequest(request); 294 templateController.setBrowserBean(browserBean); 295 templateController.setDeliveryControllers(nodeDeliveryController, null, integrationDeliveryController); 296 297 return templateController; 298 } 299 300 301 } | Popular Tags |