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

📄 cmsjsptaginclude.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagInclude.java,v $
 * Date   : $Date: 2006/03/27 14:52:19 $
 * Version: $Revision: 1.36 $
 *
 * 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.jsp;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.flex.CmsFlexController;
import org.opencms.flex.CmsFlexResponse;
import org.opencms.loader.CmsLoaderException;
import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.loader.I_CmsResourceStringDumpLoader;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.staticexport.CmsLinkManager;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.editors.I_CmsEditorActionHandler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * Used to include another OpenCms managed resource in a JSP.<p>
 *
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.36 $ 
 * 
 * @since 6.0.0 
 */
public class CmsJspTagInclude extends BodyTagSupport implements I_CmsJspTagParamParent {

    /** Serial version UID required for safe serialization. */
    private static final long serialVersionUID = 705978510743164951L;

    /** The value of the "attribute" attribute. */
    private String m_attribute;

    /** The value of the "cacheable" attribute. */
    private boolean m_cacheable;

    /** The value of the "editable" attribute. */
    private boolean m_editable;

    /** The value of the "element" attribute. */
    private String m_element;

    /** Hashmap to save parameters to the include in. */
    private Map m_parameterMap;

    /** The value of the "property" attribute. */
    private String m_property;

    /** The value of the "suffix" attribute. */
    private String m_suffix;

    /** The value of the "page" attribute. */
    private String m_target;

    /**
     * Empty constructor, required for attribute value initialization.<p>
     */
    public CmsJspTagInclude() {

        super();
        m_cacheable = true;
    }

    /**
     * Adds parameters to a parameter Map that can be used for a http request.<p>
     * 
     * @param parameters the Map to add the parameters to
     * @param name the name to add
     * @param value the value to add
     * @param overwrite if <code>true</code>, a parameter in the map will be overwritten by
     *      a parameter with the same name, otherwise the request will have multiple parameters 
     *      with the same name (which is possible in http requests)
     */
    public static void addParameter(Map parameters, String name, String value, boolean overwrite) {

        // No null values allowed in parameters
        if ((parameters == null) || (name == null) || (value == null)) {
            return;
        }

        // Check if the parameter name (key) exists
        if (parameters.containsKey(name) && (!overwrite)) {
            // Yes: Check name values if value exists, if so do nothing, else add new value
            String[] values = (String[])parameters.get(name);
            String[] newValues = new String[values.length + 1];
            System.arraycopy(values, 0, newValues, 0, values.length);
            newValues[values.length] = value;
            parameters.put(name, newValues);
        } else {
            // No: Add new parameter name / value pair
            String[] values = new String[] {value};
            parameters.put(name, values);
        }
    }

    /**
     * Includes the selected target.<p>
     * 
     * @param context the current JSP page context
     * @param target the target for the include, might be <code>null</code>
     * @param element the element to select form the target might be <code>null</code>
     * @param editable flag to indicate if the target is editable
     * @param paramMap a map of parameters for the include, will be merged with the request 
     *      parameters, might be <code>null</code>
     * @param req the current request
     * @param res the current response
     *
     * @throws JspException in case something goes wrong
     */
    public static void includeTagAction(
        PageContext context,
        String target,
        String element,
        boolean editable,
        Map paramMap,
        ServletRequest req,
        ServletResponse res) throws JspException {

        // no locale and no cachable parameter are used by default
        includeTagAction(context, target, element, null, editable, true, paramMap, req, res);
    }

    /**
     * Includes the selected target.<p>
     * 
     * @param context the current JSP page context
     * @param target the target for the include, might be <code>null</code>
     * @param element the element to select form the target, might be <code>null</code>
     * @param locale the locale to use for the selected element, might be <code>null</code> 
     * @param editable flag to indicate if the target is editable
     * @param cacheable flag to indicate if the target should be cacheable in the Flex cache
     * @param paramMap a map of parameters for the include, will be merged with the request 
     *      parameters, might be <code>null</code>
     * @param req the current request
     * @param res the current response
     *
     * @throws JspException in case something goes wrong
     */
    public static void includeTagAction(
        PageContext context,
        String target,
        String element,
        Locale locale,
        boolean editable,
        boolean cacheable,
        Map paramMap,
        ServletRequest req,
        ServletResponse res) throws JspException {

        // the Flex controller provides access to the interal OpenCms structures
        CmsFlexController controller = CmsFlexController.getController(req);

        if (target == null) {
            // set target to default
            target = controller.getCmsObject().getRequestContext().getUri();
        }

        // resolve possible relative URI
        target = CmsLinkManager.getAbsoluteUri(target, controller.getCurrentRequest().getElementUri());

        try {
            // now resolve additional include extensions that might be required for special loader implementations
            target = OpenCms.getResourceManager().resolveIncludeExtensions(
                target,
                element,
                editable,
                paramMap,
                req,
                res);
        } catch (CmsException e) {
            // store exception in controller and discontinue
            controller.setThrowable(e, target);
            throw new JspException(e);
        }

        // include direct edit "start" element (if enabled)
        String directEditPermissions = null;
        if (editable) {
            directEditPermissions = CmsJspTagEditable.includeDirectEditElement(
                context,
                I_CmsEditorActionHandler.DIRECT_EDIT_AREA_START,
                target,
                element,
                null,
                null,
                null);
        }

        // save old parameters from request
        Map oldParameterMap = req.getParameterMap();
        try {
            // each include will have it's unique map of parameters
            Map parameterMap = (paramMap == null) ? new HashMap() : new HashMap(paramMap);
            if (cacheable && (element != null)) {
                // add template element selector for JSP templates (only required if cacheable)
                addParameter(parameterMap, I_CmsResourceLoader.PARAMETER_ELEMENT, element, true);
            }
            // add parameters to set the correct element
            controller.getCurrentRequest().addParameterMap(parameterMap);
            if (cacheable) {
                // use include with cache
                includeActionWithCache(controller, context, target, parameterMap, req, res);
            } else {
                // no cache required
                includeActionNoCache(controller, context, target, element, locale, req, res);
            }
        } finally {
            // restore old parameter map (if required)
            if (oldParameterMap != null) {
                controller.getCurrentRequest().setParameterMap(oldParameterMap);
            }
        }

        // include direct edit "end" element (if required)
        if (directEditPermissions != null) {
            CmsJspTagEditable.includeDirectEditElement(
                context,
                I_CmsEditorActionHandler.DIRECT_EDIT_AREA_END,
                target,
                element,
                null,
                directEditPermissions,
                null);
        }
    }

    /**
     * Includes the selected target without caching.<p>
     * 
     * @param controller the current JSP controller
     * @param context the current JSP page context
     * @param target the target for the include
     * @param element the element to select form the target
     * @param locale the locale to select from the target
     * @param req the current request
     * @param res the current response
     *
     * @throws JspException in case something goes wrong
     */
    private static void includeActionNoCache(
        CmsFlexController controller,
        PageContext context,
        String target,
        String element,
        Locale locale,
        ServletRequest req,
        ServletResponse res) throws JspException {

        try {
            // include is not cachable 
            CmsFile file = controller.getCmsObject().readFile(target);
            CmsObject cms = controller.getCmsObject();
            if (locale == null) {
                locale = cms.getRequestContext().getLocale();
            }
            // get the loader for the requested file 
            I_CmsResourceLoader loader = OpenCms.getResourceManager().getLoader(file);
            String content;
            if (loader instanceof I_CmsResourceStringDumpLoader) {
                // loder can provide content as a String
                I_CmsResourceStringDumpLoader strLoader = (I_CmsResourceStringDumpLoader)loader;
                content = strLoader.dumpAsString(cms, file, element, locale, req, res);
            } else {
                if (!(req instanceof HttpServletRequest) || !(res instanceof HttpServletResponse)) {
                    // http type is required for loader (no refactoring to avoid changes to interface)
                    CmsLoaderException e = new CmsLoaderException(Messages.get().container(
                        Messages.ERR_BAD_REQUEST_RESPONSE_0));
                    throw new JspException(e);
                }
                // get the bytes from the loader and convert them to a String
                byte[] result = loader.dump(
                    cms,
                    file,
                    element,
                    locale,
                    (HttpServletRequest)req,
                    (HttpServletResponse)res);
                // use the encoding from the property or the system default if not available
                String encoding = cms.readPropertyObject(file, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue(
                    OpenCms.getSystemInfo().getDefaultEncoding());
                content = new String(result, encoding);
            }
            // write the content String to the JSP output writer
            context.getOut().print(content);

        } catch (ServletException e) {
            // store original Exception in controller in order to display it later
            Throwable t = (e.getRootCause() != null) ? e.getRootCause() : e;
            t = controller.setThrowable(t, target);
            throw new JspException(t);
        } catch (IOException e) {
            // store original Exception in controller in order to display it later
            Throwable t = controller.setThrowable(e, target);
            throw new JspException(t);
        } catch (CmsException e) {
            // store original Exception in controller in order to display it later
            Throwable t = controller.setThrowable(e, target);
            throw new JspException(t);
        }

⌨️ 快捷键说明

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