001: /*
002: * $Id: JPublishWrapper.java,v 1.1 2003/08/17 08:40:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.view;
026:
027: import java.io.File;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.io.StringWriter;
032: import java.io.Writer;
033: import java.security.AccessController;
034: import java.security.PrivilegedAction;
035: import java.util.Iterator;
036:
037: import javax.servlet.ServletContext;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import javax.servlet.http.HttpSession;
041:
042: import org.jpublish.JPublishContext;
043: import org.jpublish.Page;
044: import org.jpublish.Repository;
045: import org.jpublish.RepositoryWrapper;
046: import org.jpublish.SiteContext;
047: import org.jpublish.StaticResourceManager;
048: import org.jpublish.Template;
049: import org.jpublish.action.ActionManager;
050: import org.jpublish.component.ComponentMap;
051: import org.jpublish.page.PageInstance;
052: import org.jpublish.util.CharacterEncodingMap;
053: import org.jpublish.util.DateUtilities;
054: import org.jpublish.util.NumberUtilities;
055: import org.jpublish.util.URLUtilities;
056: import org.ofbiz.base.util.Debug;
057: import org.ofbiz.base.util.GeneralException;
058:
059: /**
060: * JPublishWrapper - Used for calling pages through JPublish
061: *
062: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
063: * @version $Revision: 1.1 $
064: * @since 2.2
065: */
066: public class JPublishWrapper {
067:
068: public static final String module = JPublishWrapper.class.getName();
069:
070: protected ServletContext servletContext = null;
071: protected SiteContext siteContext = null;
072:
073: public JPublishWrapper(ServletContext context) {
074: this .servletContext = context;
075: // find the WEB-INF root
076: String rootDir = servletContext.getRealPath("/");
077: File contextRoot = new File(rootDir);
078: File webInfPath = new File(contextRoot, "WEB-INF");
079:
080: // configure the classpath for scripting support
081: configureClasspath(webInfPath);
082:
083: // create the site context
084: try {
085: //siteContext = new SiteContext(contextRoot, servletConfig.getInitParameter("config"));
086: siteContext = new SiteContext(contextRoot,
087: "WEB-INF/jpublish.xml");
088: siteContext.setWebInfPath(webInfPath);
089: } catch (Exception e) {
090: Debug.logError(e, "Cannot load SiteContext", module);
091: }
092:
093: // execute startup actions
094: try {
095: ActionManager actionManager = siteContext
096: .getActionManager();
097: actionManager.executeStartupActions();
098: } catch (Exception e) {
099: Debug.logError(e,
100: "Problems executing JPublish startup actions",
101: module);
102: }
103:
104: // set this wrapper in the ServletContext for use by the ViewHandler
105: servletContext.setAttribute("jpublishWrapper", this );
106: }
107:
108: protected void configureClasspath(File webInfPath) {
109: File webLibPath = new File(webInfPath, "lib");
110: File webClassPath = new File(webInfPath, "classes");
111:
112: // add WEB-INF/classes to the classpath
113: StringBuffer classPath = new StringBuffer();
114: classPath.append(System.getProperty("java.class.path"));
115:
116: if (webClassPath.exists()) {
117: classPath.append(System.getProperty("path.separator"));
118: classPath.append(webClassPath);
119: }
120:
121: // add WEB-INF/lib files to the classpath
122: if (webLibPath.exists()) {
123: File[] files = webLibPath.listFiles();
124: for (int i = 0; i < files.length; i++) {
125: if (files[i].getName().toLowerCase().endsWith(".jar")
126: || files[i].getName().toLowerCase().endsWith(
127: ".zip")) {
128: classPath.append(System
129: .getProperty("path.separator"));
130: classPath.append(files[i]);
131: }
132: }
133: }
134:
135: AccessController.doPrivileged(new SetClassPathAction(classPath
136: .toString()));
137: }
138:
139: protected boolean executeGlobalActions(HttpServletRequest request,
140: HttpServletResponse response, JPublishContext context,
141: String path, boolean allowRedirect) throws Exception {
142: ActionManager actionManager = siteContext.getActionManager();
143: return optionalRedirect(actionManager
144: .executeGlobalActions(context), path, response,
145: allowRedirect);
146: }
147:
148: protected boolean executePathActions(HttpServletRequest request,
149: HttpServletResponse response, JPublishContext context,
150: String path, boolean allowRedirect) throws Exception {
151: ActionManager actionManager = siteContext.getActionManager();
152: return optionalRedirect(actionManager.executePathActions(path,
153: context), path, response, allowRedirect);
154: }
155:
156: protected boolean executeParameterActions(
157: HttpServletRequest request, HttpServletResponse response,
158: JPublishContext context, String path, boolean allowRedirect)
159: throws Exception {
160: if (!siteContext.isParameterActionsEnabled()) {
161: return false;
162: }
163:
164: ActionManager actionManager = siteContext.getActionManager();
165: String[] actionNames = request.getParameterValues(siteContext
166: .getActionIdentifier());
167: if (actionNames != null) {
168: for (int i = 0; i < actionNames.length; i++) {
169: return optionalRedirect(actionManager.execute(
170: actionNames[i], context), path, response,
171: allowRedirect);
172: }
173: }
174: return false;
175: }
176:
177: protected boolean executePreEvaluationActions(
178: HttpServletRequest request, HttpServletResponse response,
179: JPublishContext context, String path) throws Exception {
180: ActionManager actionManager = siteContext.getActionManager();
181: return actionManager.executePreEvaluationActions(path, context);
182: }
183:
184: protected boolean executePostEvaluationActions(
185: HttpServletRequest request, HttpServletResponse response,
186: JPublishContext context, String path) throws Exception {
187: ActionManager actionManager = siteContext.getActionManager();
188: actionManager.executePostEvaluationActions(path, context);
189: return false;
190: }
191:
192: private boolean optionalRedirect(String redirect, String path,
193: HttpServletResponse response, boolean allowRedirect)
194: throws IOException {
195: if (redirect != null && allowRedirect) {
196: response.sendRedirect(redirect);
197: return true;
198: }
199: return false;
200: }
201:
202: /**
203: * Renders a page and returns the string containing the content of the rendered page
204: * @param path Path to the page
205: * @param request HttpServletRequest object for page prep
206: * @param response HttpServletResponse object (not used for writing).
207: * @return a String containing the rendered page
208: * @throws GeneralException
209: */
210: public String render(String path, HttpServletRequest request,
211: HttpServletResponse response) throws GeneralException {
212: Writer writer = new StringWriter();
213: String content = null;
214: render(path, request, response, writer);
215: try {
216: content = writer.toString();
217: writer.close();
218: } catch (IOException e) {
219: throw new GeneralException("Problems closing the Writer", e);
220: }
221: return content;
222: }
223:
224: public void render(String path, HttpServletRequest request,
225: HttpServletResponse response, Writer writer)
226: throws GeneralException {
227: render(path, request, response, writer, null, false);
228: }
229:
230: public void render(String path, HttpServletRequest request,
231: HttpServletResponse response, Writer writer,
232: OutputStream outputStream) throws GeneralException {
233: render(path, request, response, writer, outputStream, false);
234: }
235:
236: public void render(String path, HttpServletRequest request,
237: HttpServletResponse response, Writer writer,
238: OutputStream outputStream, boolean allowRedirect)
239: throws GeneralException {
240: HttpSession session = request.getSession();
241: ActionManager actionManager = siteContext.getActionManager();
242: //String path = servletContext.getRealPath(pagePath);
243: //Debug.logError("Path:" + path, module);
244:
245: // get the character encoding map
246: CharacterEncodingMap characterEncodingMap = siteContext
247: .getCharacterEncodingManager().getMap(path);
248:
249: // put standard servlet stuff into the context
250: JPublishContext context = new JPublishContext(this );
251: context.put("request", request);
252: context.put("response", response);
253: context.put("session", session);
254: context.put("application", servletContext);
255:
256: // add the character encoding map to the context
257: context.put("characterEncodingMap", characterEncodingMap);
258:
259: // add the URLUtilities to the context
260: URLUtilities urlUtilities = new URLUtilities(request, response);
261: context.put("urlUtilities", urlUtilities);
262:
263: // add the DateUtilities to the context
264: context.put("dateUtilities", DateUtilities.getInstance());
265:
266: // add the NumberUtilities to the context
267: context.put("numberUtilities", NumberUtilities.getInstance());
268:
269: // add the messages log to the context
270: context.put("syslog", SiteContext.syslog);
271:
272: // expose the SiteContext
273: context.put("site", siteContext);
274:
275: if (siteContext.isProtectReservedNames()) {
276: context.enableCheckReservedNames(this );
277: }
278:
279: // add the repositories to the context
280: Iterator repositories = siteContext.getRepositories()
281: .iterator();
282: while (repositories.hasNext()) {
283: Repository repository = (Repository) repositories.next();
284: context.put(repository.getName(), new RepositoryWrapper(
285: repository, context));
286: // add the fs_repository also as the name 'pages' so we can use existing logic in pages
287: // note this is a hack and we should look at doing this a different way; but first need
288: // to investigate how to get content from different repositories
289: if (repository.getName().equals("fs_repository")) {
290: context.put("pages", new RepositoryWrapper(repository,
291: context));
292: }
293: }
294:
295: try {
296: if (executePreEvaluationActions(request, response, context,
297: path))
298: return;
299:
300: // if the page is static
301: StaticResourceManager staticResourceManager = siteContext
302: .getStaticResourceManager();
303: if (staticResourceManager.resourceExists(path)) {
304: if (outputStream != null) {
305: // execute the global actions
306: if (executeGlobalActions(request, response,
307: context, path, allowRedirect))
308: return;
309:
310: // execute path actions
311: if (executePathActions(request, response, context,
312: path, allowRedirect))
313: return;
314:
315: // execute parameter actions
316: if (executeParameterActions(request, response,
317: context, path, allowRedirect))
318: return;
319:
320: // load and return the static resource
321: staticResourceManager.load(path, outputStream);
322: outputStream.flush();
323: return;
324: } else {
325: throw new GeneralException(
326: "Cannot load static resource with a null OutputStream");
327: }
328: }
329:
330: // check and make sure we have a writer
331: if (writer == null)
332: throw new GeneralException(
333: "Cannot load dynamic content with a null Writer");
334:
335: // load the page
336: PageInstance pageInstance = siteContext.getPageManager()
337: .getPage(path);
338: Page page = new Page(pageInstance);
339:
340: context.disableCheckReservedNames(this );
341:
342: // expose the page in the context
343: context.put("page", page);
344:
345: // expose components in the context
346: context.put("components", new ComponentMap(context));
347:
348: if (siteContext.isProtectReservedNames()) {
349: context.enableCheckReservedNames(this );
350: }
351:
352: // execute the global actions
353: if (executeGlobalActions(request, response, context, path,
354: allowRedirect))
355: return;
356:
357: // execute path actions
358: if (executePathActions(request, response, context, path,
359: allowRedirect))
360: return;
361:
362: // execute parameter actions
363: if (executeParameterActions(request, response, context,
364: path, allowRedirect))
365: return;
366:
367: // execute the page actions
368: if (optionalRedirect(page.executeActions(context), path,
369: response, allowRedirect))
370: return;
371:
372: // get the template
373: Template template = siteContext.getTemplateManager()
374: .getTemplate(page.getFullTemplateName());
375:
376: // merge the template
377: template.merge(context, page, writer);
378: writer.flush();
379: } catch (FileNotFoundException e) {
380: throw new GeneralException("File not found", e);
381: } catch (Exception e) {
382: throw new GeneralException("JPublish execution error", e);
383: } finally {
384: try {
385: executePostEvaluationActions(request, response,
386: context, path);
387: } catch (Exception e) {
388: throw new GeneralException(
389: "Error executing JPublish post evaluation actions",
390: e);
391: }
392: }
393: }
394:
395: /**
396: * Privleged action for setting the class path. This is used to get around
397: * the Java security system to set the class path so scripts have full
398: * access to all loaded Java classes.
399: *
400: * <p>Note: This functionality is untested.</p>
401: *
402: * @author Anthony Eden
403: */
404: class SetClassPathAction implements PrivilegedAction {
405: private String classPath;
406:
407: /**
408: * Construct the action to set the class path.
409: * @param classPath The new class path
410: */
411: public SetClassPathAction(String classPath) {
412: this .classPath = classPath;
413: }
414:
415: /**
416: * Set the "java.class.path" property.
417: * @return Returns null
418: */
419: public Object run() {
420: System.setProperty("java.class.path", classPath);
421: return null; // nothing to return
422: }
423: }
424: }
|