Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/flex/CmsXmlTemplateLoader.java


1   /*
2    * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/Attic/CmsXmlTemplateLoader.java,v $
3    * Date   : $Date: 2003/05/13 13:18:20 $
4    * Version: $Revision: 1.20.2.1 $
5    *
6    * This library is part of OpenCms -
7    * the Open Source Content Mananagement System
8    *
9    * Copyright (C) 2002 - 2003 Alkacon Software (http://www.alkacon.com)
10   *
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   *
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   * Lesser General Public License for more details.
20   *
21   * For further information about Alkacon Software, please see the
22   * company website: http://www.alkacon.com
23   *
24   * For further information about OpenCms, please see the
25   * project website: http://www.opencms.org
26   * 
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30   */
31  
32  package com.opencms.flex;
33  
34  import com.opencms.core.A_OpenCms;
35  import com.opencms.core.I_CmsRequest;
36  import com.opencms.file.CmsFile;
37  import com.opencms.file.CmsObject;
38  import com.opencms.file.CmsResource;
39  import com.opencms.flex.cache.CmsFlexCache;
40  import com.opencms.flex.cache.CmsFlexController;
41  import com.opencms.flex.cache.CmsFlexRequest;
42  import com.opencms.flex.cache.CmsFlexResponse;
43  import com.opencms.launcher.CmsXmlLauncher;
44  import com.opencms.util.Encoder;
45  
46  import java.io.IOException;
47  
48  import javax.servlet.ServletException;
49  import javax.servlet.ServletRequest;
50  import javax.servlet.ServletResponse;
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  /**
55   * Implementation of the {@link I_CmsResourceLoader} and 
56   * the {@link com.opencms.launcher.I_CmsLauncher} interface for 
57   * XMLTemplates.<p>
58   * 
59   * This implementation can deliver XMLTemplates directly since it extends
60   * the {@link com.opencms.launcher.CmsXmlLauncher}. It is also usable to include 
61   * XMLTemplates as sub-elements on a JSP page since it implements the
62   * {@link I_CmsResourceLoader} interface. 
63   *
64   * @author  Alexander Kandzior (a.kandzior@alkacon.com)
65   *
66   * @version $Revision: 1.20.2.1 $
67   * @since FLEX alpha 1
68   */
69  public class CmsXmlTemplateLoader extends CmsXmlLauncher implements I_CmsResourceLoader {
70      
71      /** The CmsFlexCache used to store generated cache entries in */
72      private static CmsFlexCache m_cache;
73  
74      /** Required to access the XMLTemplate load mechanism */    
75      private A_OpenCms m_openCms = null;    
76  
77      /** Flag for debugging output. Set to 9 for maximum verbosity. */ 
78      private static final int DEBUG = 0;
79          
80      /**
81       * The constructor of the class is empty and does nothing.
82       */
83      public CmsXmlTemplateLoader() {
84          // NOOP
85      }
86          
87      /** 
88       * Destroy this ResourceLoder, this is a NOOP so far.  
89       */
90      public void destroy() {
91          // NOOP
92      }
93      
94      /**
95       * Return a String describing the ResourceLoader,
96       * which is <code>"A XMLTemplate loader that extends from com.opencms.launcher.CmsXmlLauncher"</code>.<p>
97       * 
98       * @return a describing String for the ResourceLoader 
99       */
100     public String getResourceLoaderInfo() {
101         return "A XMLTemplate loader that extends from com.opencms.launcher.CmsXmlLauncher";
102     }
103     
104     /** 
105      * Initialize the ResourceLoader, the OpenCms parameter is
106      * saved here for later access to <code>generateOutput()</code>.<p>
107      * 
108      * @param openCms used to access <code>generateOutput()</code> later
109      */
110     public void init(A_OpenCms openCms) {
111         // This must be saved for call to this.generateOutput();
112         m_openCms = openCms;
113         m_cache = (CmsFlexCache)A_OpenCms.getRuntimeProperty(C_LOADER_CACHENAME);        
114         if (C_LOGGING && A_OpenCms.isLogging(C_FLEX_LOADER)) 
115             A_OpenCms.log(C_FLEX_LOADER, this.getClass().getName() + " initialized!");     
116     }
117     
118     /**
119      * Basic top-page processing method for this I_CmsResourceLoader,
120      * this method is called if the page is called as a sub-element 
121      * on a page not already loded with a I_CmsResourceLoader,
122      * which most often would be an I_CmsLauncher then.<p>
123      *
124      * @param cms the initialized CmsObject which provides user permissions
125      * @param file the requested OpenCms VFS resource
126      * @param req the original servlet request
127      * @param res the original servlet response
128      * 
129      * @throws ServletException might be thrown in the process of including the JSP 
130      * @throws IOException might be thrown in the process of including the JSP 
131      * 
132      * @see I_CmsResourceLoader
133      * @see #service(CmsObject, CmsResource, ServletRequest, ServletResponse)
134      */
135     public void load(CmsObject cms, CmsFile file, HttpServletRequest req, HttpServletResponse res) 
136     throws ServletException, IOException {    
137         
138         CmsFlexController controller = (CmsFlexController)req.getAttribute(CmsFlexController.ATTRIBUTE_NAME);
139                     
140         CmsFlexRequest f_req; 
141         CmsFlexResponse f_res;
142         
143         if (controller != null) {
144             // re-use currently wrapped request / response
145             f_req = controller.getCurrentRequest();
146             f_res = controller.getCurrentResponse();
147         } else {
148             // create new request / response wrappers
149             controller = new CmsFlexController(cms, file, m_cache, req, res);
150             req.setAttribute(CmsFlexController.ATTRIBUTE_NAME, controller);
151             f_req = new CmsFlexRequest(req, controller);
152             f_res = new CmsFlexResponse(res, controller, false, false);
153             controller.pushRequest(f_req);
154             controller.pushResponse(f_res);
155         }              
156         service(cms, file, f_req, f_res);
157     }
158     
159     /**
160      * Does the job of including the XMLTemplate, 
161      * this method is called directly if the element is 
162      * called as a sub-element from another I_CmsResourceLoader.<p>
163      * 
164      * @param cms used to access the OpenCms VFS
165      * @param file the reqested JSP file resource in the VFS
166      * @param req the current request
167      * @param res the current response
168      * 
169      * @throws ServletException might be thrown in the process of including the JSP 
170      * @throws IOException might be thrown in the process of including the JSP 
171      * 
172      * @see com.opencms.flex.cache.CmsFlexRequestDispatcher
173      */    
174     public void service(CmsObject cms, CmsResource file, ServletRequest req, ServletResponse res)
175     throws ServletException, IOException {
176         long timer1 = 0;
177         if (DEBUG > 0) {
178             timer1 = System.currentTimeMillis();        
179             System.err.println("============ CmsXmlTemplateLoader loading: " + file.getAbsolutePath());            
180             System.err.println("CmsXmlTemplateLoader.service() cms uri is: " + cms.getRequestContext().getUri());            
181         }
182         // save the original context settings
183         String rnc = cms.getRequestContext().getEncoding().trim();
184         String oldUri = cms.getRequestContext().getUri();
185         I_CmsRequest cms_req = cms.getRequestContext().getRequest();        
186         HttpServletRequest originalreq = (HttpServletRequest)cms_req.getOriginalRequest();
187         try {                        
188             // get the CmsRequest
189             byte[] result = null;
190             com.opencms.file.CmsFile fx = cms.readFile(file.getAbsolutePath());            
191             // care about encoding issues
192             String dnc = A_OpenCms.getDefaultEncoding().trim();
193             String enc = cms.readProperty(fx.getAbsolutePath(), C_PROPERTY_CONTENT_ENCODING, true, dnc).trim();
194             // fake the called URI (otherwise XMLTemplate / ElementCache would not work)
195             cms.getRequestContext().setUri(fx.getAbsolutePath());            
196             cms_req.setOriginalRequest(req);
197             cms.getRequestContext().setEncoding(enc);      
198             if (DEBUG > 1) {
199                 System.err.println("CmsXmlTemplateLoader.service(): Encodig set to " + cms.getRequestContext().getEncoding());
200                 System.err.println("CmsXmlTemplateLoader.service(): Uri set to " + cms.getRequestContext().getUri());
201             }
202             // process the included XMLTemplate
203             result = generateOutput(cms, fx, fx.getLauncherClassname(), cms_req);                                    
204             // append the result to the output stream
205             if(result != null) {
206                 // Encoding project:
207                 // The byte array must internally be encoded in the OpenCms
208                 // default encoding. It will be converted to the requested encoding 
209                 // on the most top-level JSP element
210                 result = Encoder.changeEncoding(result, enc, dnc);
211                 if (DEBUG > 1) System.err.println("CmsXmlTemplateLoader.service(): encoding=" + enc + " requestEncoding=" + rnc + " defaultEncoding=" + dnc);                             
212                 res.getOutputStream().write(result);
213             }        
214         }  catch (Exception e) {
215             if (DEBUG > 0) e.printStackTrace(System.err);
216             throw new ServletException("Error in CmsXmlTemplateLoader processing", e);       
217         } finally {
218             // restore the context settings
219             cms_req.setOriginalRequest(originalreq);
220             cms.getRequestContext().setEncoding(rnc);
221             cms.getRequestContext().setUri(oldUri);
222             if (DEBUG > 1) {
223                 System.err.println("CmsXmlTemplateLoader.service(): Encodig reset to " + cms.getRequestContext().getEncoding());
224                 System.err.println("CmsXmlTemplateLoader.service(): Uri reset to " + cms.getRequestContext().getUri());
225             }
226         }
227         if (DEBUG > 0) {
228             long timer2 = System.currentTimeMillis() - timer1;        
229             System.err.println("============ CmsXmlTemplateLoader time delivering XmlTemplate for " + file.getAbsolutePath() + ": " + timer2 + "ms");            
230         }
231     }   
232 }