cmslinkmanager.java

来自「找了很久才找到到源代码」· Java 代码 · 共 668 行 · 第 1/2 页

JAVA
668
字号
     */
    public String getRootPath(CmsObject cms, String targetUri) {

        return getRootPath(cms, targetUri, null);
    }

    /**
     * Returns the resource root path in the OpenCms VFS for the given target URI link, or <code>null</code> in
     * case the link points to an external site.<p>
     * 
     * The default implementation applies the following transformations to the link:<ul>
     * <li>In case the link starts with a VFS prefix (for example <code>/opencms/opencms</code>, 
     *      this prefix is removed from the result
     * <li>In case the link is not a root path, the current site root is appended to the result.<p>
     * <li>In case the link is relative, it will be made absolute using the given absolute <code>basePath</code>
     *      as starting point.<p>
     * <li>In case the link contains a server schema (for example <code>http://www.mysite.de/</code>),
     *      which points to a configured site in OpenCms, the server schema is replaced with 
     *      the root path of the site.<p>
     * <li>In case the link points to an external site, or in case it is not a valid URI, 
     *      then <code>null</code> is returned.<p> 
     * </ul>
     * 
     * Please note the above text describes the default behavior as implemented by 
     * {@link CmsDefaultLinkSubstitutionHandler}, which can be fully customized using 
     * the {@link I_CmsLinkSubstitutionHandler} interface.<p> 
     * 
     * @param cms the current users OpenCms context
     * @param targetUri the target URI link
     * @param basePath path to use as base in case the target URI is relative (can be <code>null</code>)
     * 
     * @return the resource root path in the OpenCms VFS for the given target URI link, or <code>null</code> in
     *      case the link points to an external site
     *      
     * @see I_CmsLinkSubstitutionHandler for the interface that can be used to fully customize the link substitution
     * @see CmsDefaultLinkSubstitutionHandler for the default link substitution handler
     *      
     * @since 7.0.2
     */
    public String getRootPath(CmsObject cms, String targetUri, String basePath) {

        return m_linkSubstitutionHandler.getRootPath(cms, targetUri, basePath);
    }

    /**
     * Returns the link for the given resource in the current project, with full server prefix.<p>
     * 
     * Like <code>http://site.enterprise.com:8080/index.html</code>.<p>
     * 
     * In case the resource name is a full root path, the site from the root path will be used. 
     * Otherwise the resource is assumed to be in the current site set be the OpenCms user context.<p>
     * 
     * @param cms the current OpenCms user context
     * @param resourceName the resource to generate the online link for
     * 
     * @return the link for the given resource in the current project, with full server prefix
     * 
     * @see #getOnlineLink(CmsObject, String)
     */
    public String getServerLink(CmsObject cms, String resourceName) {

        String result = substituteLinkForUnknownTarget(cms, resourceName);
        return appendServerPrefix(cms, result);
    }

    /**
     * Sets the internal link substitution handler.<p>
     * 
     * @param cms an OpenCms user context that must have the permissions for role {@link CmsRole#ROOT_ADMIN}.<p>
     * @param linkSubstitutionHandler the handler to set
     * 
     * @throws CmsRoleViolationException in case the provided OpenCms user context does not have the required permissions
     */
    public void setLinkSubstitutionHandler(CmsObject cms, I_CmsLinkSubstitutionHandler linkSubstitutionHandler)
    throws CmsRoleViolationException {

        OpenCms.getRoleManager().checkRole(cms, CmsRole.ROOT_ADMIN);
        m_linkSubstitutionHandler = linkSubstitutionHandler;
    }

    /**
     * Sets the result of an external link validation.<p>
     * 
     * @param externLinkValidationResult the result an external link validation
     */
    public void setPointerLinkValidationResult(CmsExternalLinksValidationResult externLinkValidationResult) {

        m_pointerLinkValidationResult = externLinkValidationResult;
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the given VFS resource, for use on web pages.<p>
     * 
     * The result will contain the configured context path and 
     * servlet name, and in the case of the "online" project it will also be rewritten according to
     * to the configured static export settings.<p>
     * 
     * Should the current site of the given OpenCms user context <code>cms</code> be different from the 
     * site root of the given resource, the result will contain the full server URL to the target resource.<p>
     * 
     * Please note the above text describes the default behavior as implemented by 
     * {@link CmsDefaultLinkSubstitutionHandler}, which can be fully customized using the 
     * {@link I_CmsLinkSubstitutionHandler} interface.<p> 
     * 
     * @param cms the current OpenCms user context
     * @param resource the VFS resource the link should point to
     * 
     * @return a link <i>from</i> the URI stored in the provided OpenCms user context
     *      <i>to</i> the given VFS resource, for use on web pages
     */
    public String substituteLink(CmsObject cms, CmsResource resource) {

        return substituteLinkForRootPath(cms, resource.getRootPath());
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the VFS resource indicated by the given <code>link</code> in the current site, 
     * for use on web pages.<p>
     * 
     * The provided <code>link</code> is assumed to be the contained in the site currently 
     * set in the provided OpenCms user context <code>cms</code>.<p> 
     * 
     * The result will be an absolute link that contains the configured context path and 
     * servlet name, and in the case of the "online" project it will also be rewritten according to
     * to the configured static export settings.<p>
     * 
     * In case <code>link</code> is a relative URI, the current URI contained in the provided 
     * OpenCms user context <code>cms</code> is used to make the relative <code>link</code> absolute.<p>
     * 
     * Please note the above text describes the default behavior as implemented by 
     * {@link CmsDefaultLinkSubstitutionHandler}, which can be fully customized using the 
     * {@link I_CmsLinkSubstitutionHandler} interface.<p> 
     * 
     * @param cms the current OpenCms user context
     * @param link the link to process which is assumed to point to a VFS resource, with optional parameters

     * @return a link <i>from</i> the URI stored in the provided OpenCms user context
     *      <i>to</i> the VFS resource indicated by the given <code>link</code> in the current site
     */
    public String substituteLink(CmsObject cms, String link) {

        return substituteLink(cms, link, null, false);
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the VFS resource indicated by the given <code>link</code> and <code>siteRoot</code>, 
     * for use on web pages.<p>
     * 
     * The result will be an absolute link that contains the configured context path and 
     * servlet name, and in the case of the "online" project it will also be rewritten according to
     * to the configured static export settings.<p>
     * 
     * In case <code>link</code> is a relative URI, the current URI contained in the provided 
     * OpenCms user context <code>cms</code> is used to make the relative <code>link</code> absolute.<p>
     * 
     * The provided <code>siteRoot</code> is assumed to be the "home" of the link.
     * In case the current site of the given OpenCms user context <code>cms</code> is different from the 
     * provided <code>siteRoot</code>, the full server prefix is appended to the result link.<p>
     * 
     * Please note the above text describes the default behavior as implemented by 
     * {@link CmsDefaultLinkSubstitutionHandler}, which can be fully customized using the 
     * {@link I_CmsLinkSubstitutionHandler} interface.<p> 
     * 
     * @param cms the current OpenCms user context
     * @param link the link to process which is assumed to point to a VFS resource, with optional parameters
     * @param siteRoot the site root of the <code>link</code>
     * 
     * @return the substituted link
     */
    public String substituteLink(CmsObject cms, String link, String siteRoot) {

        return substituteLink(cms, link, siteRoot, false);
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the VFS resource indicated by the given <code>link</code> and <code>siteRoot</code>, 
     * for use on web pages, using the configured link substitution handler.<p>
     * 
     * The result will be an absolute link that contains the configured context path and 
     * servlet name, and in the case of the "online" project it will also be rewritten according to
     * to the configured static export settings.<p>
     * 
     * In case <code>link</code> is a relative URI, the current URI contained in the provided 
     * OpenCms user context <code>cms</code> is used to make the relative <code>link</code> absolute.<p>
     * 
     * The provided <code>siteRoot</code> is assumed to be the "home" of the link.
     * In case the current site of the given OpenCms user context <code>cms</code> is different from the 
     * provided <code>siteRoot</code>, the full server prefix is appended to the result link.<p> 
     * 
     * A server prefix is also added if
     * <ul>
     *   <li>the link is contained in a normal document and the link references a secure document</li>
     *   <li>the link is contained in a secure document and the link references a normal document</li>
     * </ul>
     * 
     * Please note the above text describes the default behavior as implemented by 
     * {@link CmsDefaultLinkSubstitutionHandler}, which can be fully customized using the 
     * {@link I_CmsLinkSubstitutionHandler} interface.<p> 
     * 
     * @param cms the current OpenCms user context
     * @param link the link to process which is assumed to point to a VFS resource, with optional parameters
     * @param siteRoot the site root of the <code>link</code>
     * @param forceSecure if <code>true</code> generates always an absolute URL (with protocol and server name) for secure links
     * 
     * @return a link <i>from</i> the URI stored in the provided OpenCms user context
     *      <i>to</i> the VFS resource indicated by the given <code>link</code> and <code>siteRoot</code>
     *      
     * @see I_CmsLinkSubstitutionHandler for the interface that can be used to fully customize the link substitution
     * @see CmsDefaultLinkSubstitutionHandler for the default link substitution handler
     */
    public String substituteLink(CmsObject cms, String link, String siteRoot, boolean forceSecure) {

        return m_linkSubstitutionHandler.getLink(cms, link, siteRoot, forceSecure);
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the VFS resource indicated by the given root path, for use on web pages.<p>
     * 
     * The result will contain the configured context path and 
     * servlet name, and in the case of the "online" project it will also be rewritten according to
     * to the configured static export settings.<p>
     * 
     * Should the current site of the given OpenCms user context <code>cms</code> be different from the 
     * site root of the given resource root path, the result will contain the full server URL to the target resource.<p>
     * 
     * @param cms the current OpenCms user context
     * @param rootPath the VFS resource root path the link should point to
     * 
     * @return a link <i>from</i> the URI stored in the provided OpenCms user context
     *      <i>to</i> the VFS resource indicated by the given root path
     */
    public String substituteLinkForRootPath(CmsObject cms, String rootPath) {

        String siteRoot = OpenCms.getSiteManager().getSiteRoot(rootPath);
        if (siteRoot == null) {
            // use current site root in case no valid site root is available
            // this will also be the case if a "/system" link is used
            siteRoot = cms.getRequestContext().getSiteRoot();
        }
        String sitePath;
        if (rootPath.startsWith(siteRoot)) {
            // only cut the site root if the root part really has this prefix
            sitePath = rootPath.substring(siteRoot.length());
        } else {
            sitePath = rootPath;
        }
        return substituteLink(cms, sitePath, siteRoot, false);
    }

    /**
     * Returns a link <i>from</i> the URI stored in the provided OpenCms user context
     * <i>to</i> the given <code>link</code>, for use on web pages.<p>
     * 
     * A number of tests are performed with the <code>link</code> in order to find out how to create the link:<ul>
     * <li>If <code>link</code> is empty, an empty String is returned.
     * <li>If <code>link</code> starts with an URI scheme component, for example <code>http://</code>,
     * and does not point to an internal OpenCms site, it is returned unchanged.
     * <li>If <code>link</code> is an absolute URI that starts with a configured site root, 
     * the site root is cut from the link and 
     * the same result as {@link #substituteLink(CmsObject, String, String)} is returned.
     * <li>Otherwise the same result as {@link #substituteLink(CmsObject, String)} is returned.
     * </ul>
     * 
     * @param cms the current OpenCms user context
     * @param link the link to process
     * 
     * @return a link <i>from</i> the URI stored in the provided OpenCms user context
     *      <i>to</i> the given <code>link</code>
     */
    public String substituteLinkForUnknownTarget(CmsObject cms, String link) {

        if (CmsStringUtil.isEmpty(link)) {
            return "";
        }
        String sitePath = link;
        String siteRoot = null;
        if (hasScheme(link)) {
            // the link has a scheme, that is starts with something like "http://"
            // usually this should be a link to an external resource, but check anyway
            sitePath = getRootPath(cms, link);
            if (sitePath == null) {
                // probably an external link, don't touch this
                return link;
            }
        }
        // check if we can find a site from the link
        siteRoot = OpenCms.getSiteManager().getSiteRoot(sitePath);
        if (siteRoot == null) {
            // use current site root in case no valid site root is available
            // this will also be the case if a "/system" link is used
            siteRoot = cms.getRequestContext().getSiteRoot();
        } else {
            // we found a site root, cut this from the resource path
            sitePath = link.substring(siteRoot.length());
        }
        return substituteLink(cms, sitePath, siteRoot, false);
    }

    /**
     * Returns the link for the given resource in the current project, with full server prefix.<p>
     * 
     * The input link must already have been processed according to the link substitution rules.
     * This method does just append the server prefix in case this is requires.<p>
     * 
     * @param cms the current OpenCms user context
     * @param link the resource to generate the online link for
     * 
     * @return the link for the given resource in the current project, with full server prefix
     */
    private String appendServerPrefix(CmsObject cms, String link) {

        if (isAbsoluteUri(link) && !hasScheme(link)) {
            // URI is absolute and contains no schema
            // this indicates source and target link are in the same site
            String serverPrefix;
            if (cms.getRequestContext().currentProject().isOnlineProject()) {
                // on online project, get the real site name from the site manager
                CmsSite currentSite = OpenCms.getSiteManager().getSite(link, cms.getRequestContext().getSiteRoot());
                serverPrefix = currentSite.getServerPrefix(cms, link);
            } else {
                // in offline mode, source must be the workplace 
                // so append the workplace server so links can still be clicked
                serverPrefix = OpenCms.getSiteManager().getWorkplaceServer();
            }
            link = serverPrefix + link;
        }
        return link;
    }
}

⌨️ 快捷键说明

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