⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpublishwrapper.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: JPublishWrapper.java,v 1.1 2003/08/17 08:40:12 ajzeneski Exp $
 *
 * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.ofbiz.content.webapp.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jpublish.JPublishContext;
import org.jpublish.Page;
import org.jpublish.Repository;
import org.jpublish.RepositoryWrapper;
import org.jpublish.SiteContext;
import org.jpublish.StaticResourceManager;
import org.jpublish.Template;
import org.jpublish.action.ActionManager;
import org.jpublish.component.ComponentMap;
import org.jpublish.page.PageInstance;
import org.jpublish.util.CharacterEncodingMap;
import org.jpublish.util.DateUtilities;
import org.jpublish.util.NumberUtilities;
import org.jpublish.util.URLUtilities;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;

/**
 * JPublishWrapper - Used for calling pages through JPublish
 *
 * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
 * @version    $Revision: 1.1 $
 * @since      2.2
 */
public class JPublishWrapper {
    
    public static final String module = JPublishWrapper.class.getName();
    
    protected ServletContext servletContext = null;
    protected SiteContext siteContext = null;
    
    public JPublishWrapper(ServletContext context) {
        this.servletContext = context;
        // find the WEB-INF root
        String rootDir = servletContext.getRealPath("/");
        File contextRoot = new File(rootDir);
        File webInfPath = new File(contextRoot, "WEB-INF");

        // configure the classpath for scripting support
        configureClasspath(webInfPath);

        // create the site context
        try {
            //siteContext = new SiteContext(contextRoot, servletConfig.getInitParameter("config"));
            siteContext = new SiteContext(contextRoot, "WEB-INF/jpublish.xml");
            siteContext.setWebInfPath(webInfPath);            
        } catch (Exception e) {
            Debug.logError(e, "Cannot load SiteContext", module);            
        }

        // execute startup actions
        try {
            ActionManager actionManager = siteContext.getActionManager();
            actionManager.executeStartupActions();
        } catch (Exception e) {
            Debug.logError(e, "Problems executing JPublish startup actions", module);            
        }
        
        // set this wrapper in the ServletContext for use by the ViewHandler
        servletContext.setAttribute("jpublishWrapper", this);
    }
    
    protected void configureClasspath(File webInfPath) {
        File webLibPath = new File(webInfPath, "lib");
        File webClassPath = new File(webInfPath, "classes");

        // add WEB-INF/classes to the classpath
        StringBuffer classPath = new StringBuffer();
        classPath.append(System.getProperty("java.class.path"));

        if (webClassPath.exists()) {
            classPath.append(System.getProperty("path.separator"));
            classPath.append(webClassPath);
        }

        // add WEB-INF/lib files to the classpath
        if (webLibPath.exists()) {
            File[] files = webLibPath.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].getName().toLowerCase().endsWith(".jar")
                    || files[i].getName().toLowerCase().endsWith(".zip")) {
                    classPath.append(System.getProperty("path.separator"));
                    classPath.append(files[i]);
                }
            }
        }

        AccessController.doPrivileged(new SetClassPathAction(classPath.toString()));
    }

    protected boolean executeGlobalActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception {
        ActionManager actionManager = siteContext.getActionManager();
        return optionalRedirect(actionManager.executeGlobalActions(context), path, response, allowRedirect);
    }

    protected boolean executePathActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception {
        ActionManager actionManager = siteContext.getActionManager();
        return optionalRedirect(actionManager.executePathActions(path, context), path, response, allowRedirect);
    }

    protected boolean executeParameterActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path, boolean allowRedirect) throws Exception {
        if (!siteContext.isParameterActionsEnabled()) {
            return false;
        }

        ActionManager actionManager = siteContext.getActionManager();
        String[] actionNames = request.getParameterValues(siteContext.getActionIdentifier());
        if (actionNames != null) {
            for (int i = 0; i < actionNames.length; i++) {
                return optionalRedirect(actionManager.execute(actionNames[i], context), path, response, allowRedirect);
            }
        }
        return false;
    }

    protected boolean executePreEvaluationActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path) throws Exception {
        ActionManager actionManager = siteContext.getActionManager();
        return actionManager.executePreEvaluationActions(path, context);
    }

    protected boolean executePostEvaluationActions(HttpServletRequest request, HttpServletResponse response, JPublishContext context, String path) throws Exception {
        ActionManager actionManager = siteContext.getActionManager();
        actionManager.executePostEvaluationActions(path, context);
        return false;
    }

    private boolean optionalRedirect(String redirect, String path, HttpServletResponse response, boolean allowRedirect) throws IOException {
        if (redirect != null && allowRedirect) {
            response.sendRedirect(redirect);
            return true;
        }
        return false;
    }
    
    /**
     * Renders a page and returns the string containing the content of the rendered page
     * @param path Path to the page
     * @param request HttpServletRequest object for page prep
     * @param response HttpServletResponse object (not used for writing).
     * @return a String containing the rendered page
     * @throws GeneralException
     */
    public String render(String path, HttpServletRequest request, HttpServletResponse response) throws GeneralException {
        Writer writer = new StringWriter();   
        String content = null;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -