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

📄 cmsflexrequestdispatcher.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/flex/CmsFlexRequestDispatcher.java,v $
 * Date   : $Date: 2006/03/27 14:52:35 $
 * Version: $Revision: 1.44 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.flex;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsVfsResourceNotFoundException;
import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;

/** 
 * Implementation of the <code>{@link javax.servlet.RequestDispatcher}</code> interface to allow JSPs to be loaded
 * from the OpenCms VFS.<p>
 * 
 * This dispatcher will load data from 3 different data sources:
 * <ol>
 * <li>Form the "real" system Filesystem (e.g. for JSP pages)
 * <li>From the OpenCms VFS
 * <li>From the Flex cache
 * </ol>
 * <p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.44 $ 
 * 
 * @since 6.0.0 
 */
public class CmsFlexRequestDispatcher implements RequestDispatcher {

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsFlexRequestDispatcher.class);

    /** The external target that will be included by the RequestDispatcher, needed if this is not a dispatcher to a cms resource. */
    private String m_extTarget;

    /** The "real" RequestDispatcher, used when a true include (to the file system) is needed. */
    private RequestDispatcher m_rd;

    /** The OpenCms VFS target that will be included by the RequestDispatcher. */
    private String m_vfsTarget;

    /** 
     * Creates a new instance of CmsFlexRequestDispatcher.<p>
     *
     * @param rd the "real" dispatcher, used for include call to file system
     * @param vfs_target the cms resource that represents the external target
     * @param ext_target the external target that the request will be dispatched to
     */
    public CmsFlexRequestDispatcher(RequestDispatcher rd, String vfs_target, String ext_target) {

        m_rd = rd;
        m_vfsTarget = vfs_target;
        m_extTarget = ext_target;
    }

    /** 
     * Wrapper for the standard servlet API call.<p>
     * 
     * Forward calls are actually NOT wrapped by OpenCms as of now.
     * So they should not be used in JSP pages or servlets.<p>
     *
     * @param req the servlet request
     * @param res the servlet response
     * @throws ServletException in case something goes wrong
     * @throws IOException in case something goes wrong
     *
     * @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public void forward(ServletRequest req, ServletResponse res) throws ServletException, IOException {

        CmsFlexController controller = CmsFlexController.getController(req);
        controller.setForwardMode(true);
        m_rd.forward(req, res);
    }

    /** 
     * Wrapper for dispatching to a file from the OpenCms VFS.<p>
     *
     * This method will dispatch to cache, to real file system or
     * to the OpenCms VFS, whatever is needed.<p>
     *
     * This method is much more complex than it should be because of the internal standard 
     * buffering of JSP pages.
     * Because of that I can not just intercept and buffer the stream, since I don't have 
     * access to it (it is wrapped internally in the JSP pages, which have their own buffer).
     * That leads to a solution where the data is first written to the bufferd stream, 
     * but without includes. Then it is parsed again later 
     * in the <code>{@link CmsFlexResponse}</code>, enriched with the 
     * included elements that have been ommitted in the first case.
     * I would love to see a simpler solution, but this works for now.<p>
     *
     * @param req the servlet request
     * @param res the servlet response
     * 
     * @throws ServletException in case something goes wrong
     * @throws IOException in case something goes wrong
     */
    public void include(ServletRequest req, ServletResponse res) throws ServletException, IOException {

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(
                Messages.LOG_FLEXREQUESTDISPATCHER_INCLUDING_TARGET_2,
                m_vfsTarget,
                m_extTarget));
        }

        CmsFlexController controller = CmsFlexController.getController(req);
        CmsObject cms = controller.getCmsObject();
        CmsResource resource = null;

        if ((m_extTarget == null) && (controller != null)) {
            // check if the file exists in the VFS, if not set external target
            try {
                resource = cms.readResource(m_vfsTarget);
            } catch (CmsVfsResourceNotFoundException e) {
                // file not found in VFS, treat it as external file
                m_extTarget = m_vfsTarget;
            } catch (CmsException e) {
                // if other OpenCms exception occured we are in trouble              
                throw new ServletException(Messages.get().getBundle().key(
                    Messages.ERR_FLEXREQUESTDISPATCHER_VFS_ACCESS_EXCEPTION_0), e);
            }
        }

        if ((m_extTarget != null) || (controller == null)) {
            includeExternal(req, res);
        } else if (controller.isForwardMode()) {
            includeInternalNoCache(req, res, controller, cms, resource);
        } else {
            includeInternalWithCache(req, res, controller, cms, resource);
        }
    }

    /**
     * Include an external (non-OpenCms) file using the standard dispatcher.<p>
     * 
     * @param req the servlet request
     * @param res the servlet response
     * @throws ServletException in case something goes wrong
     * @throws IOException in case something goes wrong
     */
    private void includeExternal(ServletRequest req, ServletResponse res) throws ServletException, IOException {

        // This is an external include, probably to a JSP page, dispatch with system dispatcher
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(
                Messages.LOG_FLEXREQUESTDISPATCHER_INCLUDING_EXTERNAL_TARGET_1,
                m_extTarget));
        }
        m_rd.include(req, res);
    }

    /**
     * Includes the requested resouce, ignoring the Flex cache.<p>
     * 
     * @param req the servlet request
     * @param res the servlet response
     * @param controller the flex controller
     * @param cms the cms context
     * @param resource the requested resource (may be <code>null</code>)
     * 
     * @throws ServletException in case something goes wrong
     * @throws IOException in case something goes wrong
     */
    private void includeInternalNoCache(
        ServletRequest req,
        ServletResponse res,
        CmsFlexController controller,
        CmsObject cms,
        CmsResource resource) throws ServletException, IOException {

        // load target with the internal resource loader
        I_CmsResourceLoader loader;

        try {
            if (resource == null) {
                resource = cms.readResource(m_vfsTarget);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_FLEXREQUESTDISPATCHER_LOADING_RESOURCE_TYPE_1,
                    new Integer(resource.getTypeId())));
            }
            loader = OpenCms.getResourceManager().getLoader(resource);
        } catch (CmsException e) {
            // file might not exist or no read permissions
            controller.setThrowable(e, m_vfsTarget);
            throw new ServletException(Messages.get().getBundle().key(
                Messages.ERR_FLEXREQUESTDISPATCHER_ERROR_READING_RESOURCE_1,
                m_vfsTarget), e);
        }

⌨️ 快捷键说明

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