1 23 24 package org.infoglue.deliver.invokers; 25 26 import java.io.ByteArrayOutputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.OutputStream ; 29 import java.io.PrintWriter ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import java.util.zip.GZIPOutputStream ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipOutputStream ; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 import org.apache.log4j.Logger; 40 import org.exolab.castor.jdo.Database; 41 import org.infoglue.cms.entities.management.LanguageVO; 42 import org.infoglue.cms.exception.SystemException; 43 import org.infoglue.cms.util.CmsPropertyHandler; 44 import org.infoglue.deliver.applications.databeans.DatabaseWrapper; 45 import org.infoglue.deliver.applications.databeans.DeliveryContext; 46 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController; 47 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController; 48 import org.infoglue.deliver.portal.PortalController; 49 import org.infoglue.deliver.util.CacheController; 50 import org.infoglue.deliver.util.CompressionHelper; 51 52 59 60 public abstract class PageInvoker 61 { 62 private final static Logger logger = Logger.getLogger(PageInvoker.class.getName()); 63 64 private static CompressionHelper compressionHelper = new CompressionHelper(); 65 66 private DatabaseWrapper dbWrapper = null; 67 private HttpServletRequest request = null; 68 private HttpServletResponse response = null; 69 private TemplateController templateController = null; 70 private DeliveryContext deliveryContext = null; 71 72 private String pageString = null; 73 74 78 79 86 96 97 101 102 public abstract PageInvoker getDecoratedPageInvoker() throws SystemException; 103 104 111 112 public void setParameters(DatabaseWrapper dbWrapper, HttpServletRequest request, HttpServletResponse response, TemplateController templateController, DeliveryContext deliveryContext) 113 { 114 this.dbWrapper = dbWrapper; 115 this.request = request; 116 this.response = response; 117 this.templateController = templateController; 118 this.deliveryContext = deliveryContext; 119 this.templateController.setDeliveryContext(this.deliveryContext); 120 } 121 122 public Database getDatabase() throws SystemException 123 { 124 130 return dbWrapper.getDatabase(); 131 } 132 133 134 140 141 public abstract void invokePage() throws SystemException, Exception ; 142 143 144 148 149 public void deliverPage() throws Exception 150 { 151 logger.info("PageKey:" + this.getDeliveryContext().getPageKey()); 152 logger.info("PageCache:" + this.getDeliveryContext().getDisablePageCache()); 153 154 LanguageVO languageVO = LanguageDeliveryController.getLanguageDeliveryController().getLanguageVO(getDatabase(), this.getTemplateController().getLanguageId()); 155 logger.info("languageVO:" + languageVO); 156 if(languageVO == null) 157 throw new SystemException("There was no such active language for the page with languageId:" + this.getTemplateController().getLanguageId()); 158 159 String isPageCacheOn = CmsPropertyHandler.getIsPageCacheOn(); 160 logger.info("isPageCacheOn:" + isPageCacheOn); 161 String refresh = this.getRequest().getParameter("refresh"); 162 163 if(isPageCacheOn.equalsIgnoreCase("true") && (refresh == null || !refresh.equalsIgnoreCase("true")) && getRequest().getMethod().equals("GET")) 164 { 165 String compressPageCache = CmsPropertyHandler.getCompressPageCache(); 166 if(compressPageCache != null && compressPageCache.equalsIgnoreCase("true")) 167 { 168 byte[] cachedCompressedData = (byte[])CacheController.getCachedObjectFromAdvancedCache("pageCache", this.getDeliveryContext().getPageKey()); 169 if(cachedCompressedData != null) 170 this.pageString = compressionHelper.decompress(cachedCompressedData); 171 } 172 else 173 { 174 this.pageString = (String )CacheController.getCachedObjectFromAdvancedCache("pageCache", this.getDeliveryContext().getPageKey()); 175 } 176 177 if(this.pageString == null) 178 { 179 invokePage(); 180 this.pageString = getPageString(); 181 182 if(!this.getTemplateController().getIsPageCacheDisabled() && !this.getDeliveryContext().getDisablePageCache()) { 184 if(compressPageCache != null && compressPageCache.equalsIgnoreCase("true")) 185 { 186 long startCompression = System.currentTimeMillis(); 187 byte[] compressedData = compressionHelper.compress(this.pageString); 188 logger.info("Compressing page for pageCache took " + (System.currentTimeMillis() - startCompression) + " with a compressionFactor of " + (this.pageString.length() / compressedData.length)); 189 if(this.getTemplateController().getOperatingMode().intValue() == 3) 190 CacheController.cacheObjectInAdvancedCache("pageCache", this.getDeliveryContext().getPageKey(), compressedData, this.getDeliveryContext().getAllUsedEntities(), false); 191 else 192 CacheController.cacheObjectInAdvancedCache("pageCache", this.getDeliveryContext().getPageKey(), compressedData, this.getDeliveryContext().getAllUsedEntities(), true); 193 } 194 else 195 { 196 if(this.getTemplateController().getOperatingMode().intValue() == 3) 197 CacheController.cacheObjectInAdvancedCache("pageCache", this.getDeliveryContext().getPageKey(), pageString, this.getDeliveryContext().getAllUsedEntities(), false); 198 else 199 CacheController.cacheObjectInAdvancedCache("pageCache", this.getDeliveryContext().getPageKey(), pageString, this.getDeliveryContext().getAllUsedEntities(), true); 200 } 201 } 202 else 203 { 204 logger.info("Page caching was disabled for the page " + this.getDeliveryContext().getSiteNodeId() + " with pageKey " + this.getDeliveryContext().getPageKey() + " - modifying the logic to enable page caching would boast performance."); 205 } 206 } 207 else 208 { 209 logger.info("There was a cached copy..."); } 211 212 logger.info("Caching the pagePath..."); 214 this.getDeliveryContext().setPagePath((String )CacheController.getCachedObject("pagePathCache", this.getDeliveryContext().getPageKey())); 215 if(this.getDeliveryContext().getPagePath() == null) 216 { 217 this.getDeliveryContext().setPagePath(this.getTemplateController().getCurrentPagePath()); 218 219 if(!this.getTemplateController().getIsPageCacheDisabled() && !this.getDeliveryContext().getDisablePageCache()) CacheController.cacheObject("pagePathCache", this.getDeliveryContext().getPageKey(), this.getDeliveryContext().getPagePath()); 221 } 222 logger.info("Done caching the pagePath..."); 223 } 224 else 225 { 226 invokePage(); 227 this.pageString = getPageString(); 228 229 this.getDeliveryContext().setPagePath(this.templateController.getCurrentPagePath()); 230 } 231 232 if(this.getRequest().getParameter("includeUsedEntities") != null && this.getRequest().getParameter("includeUsedEntities").equals("true") && !CmsPropertyHandler.getOperatingMode().equals("3")) 233 { 234 StringBuffer sb = new StringBuffer ("<usedEntities>"); 235 String [] usedEntities = this.getDeliveryContext().getAllUsedEntities(); 236 for(int i=0; i<usedEntities.length; i++) 237 sb.append(usedEntities[i]).append(","); 238 sb.append("</usedEntities>"); 239 240 this.pageString = this.pageString + sb.toString(); 241 } 242 243 String contentType = this.getTemplateController().getPageContentType(); 244 if(this.deliveryContext.getContentType() != null && !contentType.equalsIgnoreCase(this.deliveryContext.getContentType())) 246 contentType = this.deliveryContext.getContentType(); 247 248 this.getResponse().setContentType(contentType + "; charset=" + languageVO.getCharset()); 250 logger.info("contentType:" + contentType + "; charset=" + languageVO.getCharset()); 251 252 String compressPageResponse = CmsPropertyHandler.getCompressPageResponse(); 253 logger.info("compressPageResponse:" + compressPageResponse); 254 if(compressPageResponse != null && compressPageResponse.equalsIgnoreCase("true")) 255 { 256 OutputStream out = null; 257 258 String encodings = this.getRequest().getHeader("Accept-Encoding"); 259 if (encodings != null && encodings.indexOf("gzip") != -1) 260 { 261 this.getResponse().setHeader("Content-Encoding", "gzip"); 262 out = new GZIPOutputStream (this.getResponse().getOutputStream()); 263 } 264 else if (encodings != null && encodings.indexOf("compress") != -1) 265 { 266 this.getResponse().setHeader("Content-Encoding", "x-compress"); 267 out = new ZipOutputStream (this.getResponse().getOutputStream()); 268 ((ZipOutputStream )out).putNextEntry(new ZipEntry ("dummy name")); 269 } 270 else 271 { 272 out = this.getResponse().getOutputStream(); 273 } 274 275 out.write(pageString.getBytes(languageVO.getCharset())); 276 out.flush(); 277 out.close(); 278 } 279 else 280 { 281 PrintWriter out = this.getResponse().getWriter(); 282 out.println(pageString); 283 out.flush(); 284 out.close(); 285 } 286 287 logger.info("sent all data to client:" + pageString.length()); 288 } 289 290 291 292 295 296 public void cachePage() 297 { 298 299 } 300 301 public final DeliveryContext getDeliveryContext() 302 { 303 return deliveryContext; 304 } 305 306 public final HttpServletRequest getRequest() 307 { 308 return request; 309 } 310 311 public final HttpServletResponse getResponse() 312 { 313 return response; 314 } 315 316 public final TemplateController getTemplateController() 317 { 318 return templateController; 319 } 320 321 public String getPageString() 322 { 323 return pageString; 324 } 325 326 public void setPageString(String string) 327 { 328 if(string != null && this.deliveryContext.getTrimResponse()) 329 string = string.trim(); 330 331 pageString = string; 332 } 333 334 335 343 344 public Map getDefaultContext() 345 { 346 Map context = new HashMap (); 347 context.put("templateLogic", getTemplateController()); 348 349 String portalEnabled = CmsPropertyHandler.getEnablePortal() ; 351 boolean active = ((portalEnabled != null) && portalEnabled.equals("true")); 352 if (active) 353 { 354 PortalController pController = new PortalController(getRequest(), getResponse()); 355 context.put(PortalController.NAME, pController); 356 logger.info("PortalController.NAME:" + PortalController.NAME); 357 logger.info("pController:" + pController); 358 } 359 360 return context; 361 } 362 363 } 364 | Popular Tags |