KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > invokers > PageInvoker


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.invokers;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.zip.GZIPOutputStream JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34 import java.util.zip.ZipOutputStream JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
53  * @author Mattias Bogeblad
54  *
55  * This interface defines what a Invoker of a page have to be able to do.
56  * The invokers are used to deliver a page to the user in a certain fashion.
57  *
58  */

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 JavaDoc request = null;
68     private HttpServletResponse JavaDoc response = null;
69     private TemplateController templateController = null;
70     private DeliveryContext deliveryContext = null;
71     
72     private String JavaDoc pageString = null;
73     
74     /*public PageInvoker()
75     {
76     }
77     */

78     
79     /**
80      * The default constructor for PageInvokers.
81      * @param request
82      * @param response
83      * @param templateController
84      * @param deliveryContext
85      */

86     /*
87     public PageInvoker(HttpServletRequest request, HttpServletResponse response, TemplateController templateController, DeliveryContext deliveryContext)
88     {
89         this.request = request;
90         this.response = response;
91         this.templateController = templateController;
92         this.deliveryContext = deliveryContext;
93         this.templateController.setDeliveryContext(this.deliveryContext);
94     }
95     */

96     
97     /**
98      * This method should return an instance of the class that should be used for page editing inside the tools or in working.
99      * Makes it possible to have an alternative to the ordinary delivery optimized class.
100      */

101     
102     public abstract PageInvoker getDecoratedPageInvoker() throws SystemException;
103     
104     /**
105      * The default initializer for PageInvokers.
106      * @param request
107      * @param response
108      * @param templateController
109      * @param deliveryContext
110      */

111
112     public void setParameters(DatabaseWrapper dbWrapper, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc 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         /*
125         if(this.db == null || this.db.isClosed() || !this.db.isActive())
126         {
127             beginTransaction();
128         }
129         */

130         return dbWrapper.getDatabase();
131     }
132
133     
134     /**
135      * This is the method that will deliver the page to the user. It can have special
136      * handling of all sorts to enable all sorts of handlers. An example of uses might be to
137      * be to implement a WAP-version of page delivery where you have to set certain headers in the response
138      * or a redirect page which just redirects you to another page.
139      */

140     
141     public abstract void invokePage() throws SystemException, Exception JavaDoc;
142     
143
144     /**
145      * This method is used to send the page out to the browser or other device.
146      * Override this if you need to set other headers or do other specialized things.
147      */

148
149     public void deliverPage() throws Exception JavaDoc
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 JavaDoc isPageCacheOn = CmsPropertyHandler.getIsPageCacheOn();
160         logger.info("isPageCacheOn:" + isPageCacheOn);
161         String JavaDoc refresh = this.getRequest().getParameter("refresh");
162         
163         if(isPageCacheOn.equalsIgnoreCase("true") && (refresh == null || !refresh.equalsIgnoreCase("true")) && getRequest().getMethod().equals("GET"))
164         {
165             String JavaDoc 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 JavaDoc)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()) //Caching page if not disabled
183
{
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..."); // + pageString);
210
}
211             
212             //Caching the pagePath
213
logger.info("Caching the pagePath...");
214             this.getDeliveryContext().setPagePath((String JavaDoc)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()) //Caching page path if not disabled
220
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 JavaDoc sb = new StringBuffer JavaDoc("<usedEntities>");
235             String JavaDoc[] 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 JavaDoc contentType = this.getTemplateController().getPageContentType();
244         //logger.info("ContentType in deliveryContext:" + this.deliveryContext.getContentType());
245
if(this.deliveryContext.getContentType() != null && !contentType.equalsIgnoreCase(this.deliveryContext.getContentType()))
246             contentType = this.deliveryContext.getContentType();
247         
248         //logger.info("ContentType:" + contentType);
249
this.getResponse().setContentType(contentType + "; charset=" + languageVO.getCharset());
250         logger.info("contentType:" + contentType + "; charset=" + languageVO.getCharset());
251         
252         String JavaDoc compressPageResponse = CmsPropertyHandler.getCompressPageResponse();
253         logger.info("compressPageResponse:" + compressPageResponse);
254         if(compressPageResponse != null && compressPageResponse.equalsIgnoreCase("true"))
255         {
256             OutputStream JavaDoc out = null;
257             
258             String JavaDoc 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 JavaDoc(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 JavaDoc(this.getResponse().getOutputStream());
268                 ((ZipOutputStream JavaDoc)out).putNextEntry(new ZipEntry JavaDoc("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 JavaDoc 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     /**
293      * This method is used to allow pagecaching on a general level.
294      */

295
296     public void cachePage()
297     {
298         
299     }
300     
301     public final DeliveryContext getDeliveryContext()
302     {
303         return deliveryContext;
304     }
305
306     public final HttpServletRequest JavaDoc getRequest()
307     {
308         return request;
309     }
310
311     public final HttpServletResponse JavaDoc getResponse()
312     {
313         return response;
314     }
315
316     public final TemplateController getTemplateController()
317     {
318         return templateController;
319     }
320
321     public String JavaDoc getPageString()
322     {
323         return pageString;
324     }
325
326     public void setPageString(String JavaDoc string)
327     {
328         if(string != null && this.deliveryContext.getTrimResponse())
329             string = string.trim();
330
331         pageString = string;
332     }
333
334     
335     /**
336      * Creates and returns a defaultContext, currently with the templateLogic
337      * and if the portal support is enabled the portalLogic object.
338      * (Added to avoid duplication of context creation in the concrete
339      * implementations of pageInvokers)
340      * @author robert
341      * @return A default context with the templateLogic and portalLogic object in it.
342      */

343     
344     public Map JavaDoc getDefaultContext()
345     {
346         Map JavaDoc context = new HashMap JavaDoc();
347         context.put("templateLogic", getTemplateController());
348         
349         // -- check if the portal is active
350
String JavaDoc 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