cmslinkmanager.java
来自「找了很久才找到到源代码」· Java 代码 · 共 668 行 · 第 1/2 页
JAVA
668 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsLinkManager.java,v $
* Date : $Date: 2007-09-11 13:44:23 $
* Version: $Revision: 1.75 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 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.staticexport;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.CmsPermalinkResourceHandler;
import org.opencms.main.OpenCms;
import org.opencms.relations.CmsExternalLinksValidationResult;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.site.CmsSite;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsStringUtil;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.logging.Log;
/**
* Does the link replacement for the ≶link> tags.<p>
*
* Since this functionality is closely related to the static export,
* this class resides in the static export package.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.75 $
*
* @since 6.0.0
*/
public class CmsLinkManager {
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsLinkManager.class);
/** Base URL to calculate absolute links. */
private static URL m_baseUrl;
/** The configured link substitution handler. */
private I_CmsLinkSubstitutionHandler m_linkSubstitutionHandler;
/** Stores the results of a external link validation. */
private CmsExternalLinksValidationResult m_pointerLinkValidationResult;
/**
* Public constructor.<p>
*
* @param linkSubstitutionHandler the link substitution handler to use
*/
public CmsLinkManager(I_CmsLinkSubstitutionHandler linkSubstitutionHandler) {
m_linkSubstitutionHandler = linkSubstitutionHandler;
if (m_linkSubstitutionHandler == null) {
// just make very sure that this is not null
m_linkSubstitutionHandler = new CmsDefaultLinkSubstitutionHandler();
}
}
/**
* Static initializer for the base URL.<p>
*/
static {
m_baseUrl = null;
try {
m_baseUrl = new URL("http://127.0.0.1");
} catch (MalformedURLException e) {
// this won't happen
}
}
/**
* Calculates the absolute URI for the "relativeUri" with the given absolute "baseUri" as start. <p>
*
* If "relativeUri" is already absolute, it is returned unchanged.
* This method also returns "relativeUri" unchanged if it is not well-formed.<p>
*
* @param relativeUri the relative URI to calculate an absolute URI for
* @param baseUri the base URI, this must be an absolute URI
*
* @return an absolute URI calculated from "relativeUri" and "baseUri"
*/
public static String getAbsoluteUri(String relativeUri, String baseUri) {
if (isAbsoluteUri(relativeUri)) {
// URI is null or already absolute
return relativeUri;
}
try {
URL url = new URL(new URL(m_baseUrl, baseUri), relativeUri);
if (url.getQuery() == null) {
return url.getPath();
} else {
StringBuffer result = new StringBuffer(url.getPath().length() + url.getQuery().length() + 2);
result.append(url.getPath());
result.append('?');
result.append(url.getQuery());
return result.toString();
}
} catch (MalformedURLException e) {
return relativeUri;
}
}
/**
* Calculates a relative URI from "fromUri" to "toUri",
* both URI must be absolute.<p>
*
* @param fromUri the URI to start
* @param toUri the URI to calculate a relative path to
* @return a relative URI from "fromUri" to "toUri"
*/
public static String getRelativeUri(String fromUri, String toUri) {
StringBuffer result = new StringBuffer();
int pos = 0;
while (true) {
int i = fromUri.indexOf('/', pos);
int j = toUri.indexOf('/', pos);
if ((i == -1) || (i != j) || !fromUri.regionMatches(pos, toUri, pos, i - pos)) {
break;
}
pos = i + 1;
}
// count hops up from here to the common ancestor
for (int i = fromUri.indexOf('/', pos); i > 0; i = fromUri.indexOf('/', i + 1)) {
result.append("../");
}
// append path down from common ancestor to there
result.append(toUri.substring(pos));
if (result.length() == 0) {
// special case: relative link to the parent folder from a file in that folder
result.append("./");
}
return result.toString();
}
/**
* Returns the resource root path for the given target URI in the OpenCms VFS, or <code>null</code> in
* case the target URI points to an external site.<p>
*
* @param cms the current users OpenCms context
* @param basePath path to use as base site for the target URI (can be <code>null</code>)
* @param targetUri the target URI
*
* @return the resource root path for the given target URI in the OpenCms VFS, or <code>null</code> in
* case the target URI points to an external site
*
* @deprecated use {@link #getRootPath(CmsObject, String, String)} instead, obtain the link manager
* with {@link OpenCms#getLinkManager()}
*/
public static String getSitePath(CmsObject cms, String basePath, String targetUri) {
return OpenCms.getLinkManager().getRootPath(cms, targetUri, basePath);
}
/**
* Tests if the given URI starts with a scheme component.<p>
*
* The scheme component is something like <code>http:</code> or <code>ftp:</code>.<p>
*
* @param uri the URI to test
*
* @return <code>true</code> if the given URI starts with a scheme component
*/
public static boolean hasScheme(String uri) {
int pos = uri.indexOf(':');
// don't want to be misguided by a potential ':' in the query section of the URI (is this possible / allowed?)
// so consider only a ':' in the first 10 chars as a scheme
return (pos > -1) && (pos < 10);
}
/**
* Returns <code>true</code> in case the given URI is absolute.<p>
*
* An URI is considered absolute if one of the following is true:<ul>
* <li>The URI starts with a <code>'/'</code> char.
* <li>The URI contains a <code>':'</code> in the first 10 chars.
* <li>The URI is <code>null</code>
* </ul>
*
* @param uri the URI to test
*
* @return <code>true</code> in case the given URI is absolute
*/
public static boolean isAbsoluteUri(String uri) {
return (uri == null) || ((uri.length() >= 1) && ((uri.charAt(0) == '/') || hasScheme(uri)));
}
/**
* Returns the online link for the given resource, 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>
*
* Please note that this method will always return the link as it will appear in the "Online"
* project, that is after the resource has been published. In case you need a method that
* just returns the link with the full server prefix, use {@link #getServerLink(CmsObject, String)}.<p>
*
* @param cms the current OpenCms user context
* @param resourceName the resource to generate the online link for
*
* @return the online link for the given resource, with full server prefix
*
* @see #getServerLink(CmsObject, String)
*/
public String getOnlineLink(CmsObject cms, String resourceName) {
String result = "";
try {
CmsProject currentProject = cms.getRequestContext().currentProject();
try {
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
result = substituteLinkForUnknownTarget(cms, resourceName);
} finally {
cms.getRequestContext().setCurrentProject(currentProject);
}
result = appendServerPrefix(cms, result);
} catch (CmsException e) {
// should never happen
result = e.getLocalizedMessage();
if (LOG.isErrorEnabled()) {
LOG.error(e.getLocalizedMessage(), e);
}
}
return result;
}
/**
* Returns the perma link for the given resource.<p>
*
* Like
* <code>http://site.enterprise.com:8080/permalink/4b65369f-1266-11db-8360-bf0f6fbae1f8.html</code>.<p>
*
* @param cms the cms context
* @param resourceName the resource to generate the perma link for
*
* @return the perma link
*/
public String getPermalink(CmsObject cms, String resourceName) {
String permalink = "";
try {
permalink = substituteLink(cms, CmsPermalinkResourceHandler.PERMALINK_HANDLER);
String id = cms.readResource(resourceName, CmsResourceFilter.ALL).getStructureId().toString();
permalink += id;
String ext = CmsFileUtil.getExtension(resourceName);
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(ext)) {
permalink += ext;
}
String serverPrefix = OpenCms.getSiteManager().getCurrentSite(cms).getServerPrefix(cms, resourceName);
if (!permalink.startsWith(serverPrefix)) {
permalink = serverPrefix + permalink;
}
} catch (CmsException e) {
// if something wrong
permalink = e.getLocalizedMessage();
if (LOG.isErrorEnabled()) {
LOG.error(e.getLocalizedMessage(), e);
}
}
return permalink;
}
/**
* Returns the result of the last extern link validation.<p>
*
* @return the result of the last extern link validation
*/
public CmsExternalLinksValidationResult getPointerLinkValidationResult() {
return m_pointerLinkValidationResult;
}
/**
* 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>
*
* This methods does not support relative target URI links, so the given URI must be an absolute link.<p>
*
* See {@link #getRootPath(CmsObject, String)} for a full explanation of this method.<p>
*
* @param cms the current users OpenCms context
* @param targetUri the target URI link
*
* @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 #getRootPath(CmsObject, String, String)
*
* @since 7.0.2
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?