1 31 32 package org.opencms.staticexport; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.types.CmsResourceTypeImage; 36 import org.opencms.main.CmsException; 37 import org.opencms.main.CmsLog; 38 import org.opencms.main.OpenCms; 39 import org.opencms.site.CmsSite; 40 import org.opencms.site.CmsSiteManager; 41 import org.opencms.site.CmsSiteMatcher; 42 import org.opencms.util.CmsStringUtil; 43 import org.opencms.validation.CmsPointerLinkValidationResult; 44 import org.opencms.workplace.CmsWorkplace; 45 46 import java.net.MalformedURLException ; 47 import java.net.URI ; 48 import java.net.URL ; 49 50 import org.apache.commons.logging.Log; 51 52 64 public class CmsLinkManager { 65 66 67 private static final Log LOG = CmsLog.getLog(CmsLinkManager.class); 68 69 70 private static URL m_baseUrl; 71 72 73 private CmsPointerLinkValidationResult m_pointerLinkValidationResult; 74 75 78 public CmsLinkManager() { 79 80 } 82 83 86 static { 87 m_baseUrl = null; 88 try { 89 m_baseUrl = new URL ("http://127.0.0.1"); 90 } catch (MalformedURLException e) { 91 } 93 } 94 95 105 public static String getAbsoluteUri(String relativeUri, String baseUri) { 106 107 if ((relativeUri == null) || (relativeUri.length() >= 1 && relativeUri.charAt(0) == '/')) { 108 return relativeUri; 110 } 111 try { 112 URL url = new URL (new URL (m_baseUrl, baseUri), relativeUri); 113 if (url.getQuery() == null) { 114 return url.getPath(); 115 } else { 116 StringBuffer result = new StringBuffer (url.getPath().length() + url.getQuery().length() + 2); 117 result.append(url.getPath()); 118 result.append('?'); 119 result.append(url.getQuery()); 120 return result.toString(); 121 } 122 } catch (MalformedURLException e) { 123 return relativeUri; 124 } 125 } 126 127 135 public static String getRelativeUri(String fromUri, String toUri) { 136 137 StringBuffer result = new StringBuffer (); 138 int pos = 0; 139 140 while (true) { 141 int i = fromUri.indexOf('/', pos); 142 int j = toUri.indexOf('/', pos); 143 if ((i == -1) || (i != j) || !fromUri.regionMatches(pos, toUri, pos, i - pos)) { 144 break; 145 } 146 pos = i + 1; 147 } 148 149 for (int i = fromUri.indexOf('/', pos); i > 0; i = fromUri.indexOf('/', i + 1)) { 151 result.append("../"); 152 } 153 154 result.append(toUri.substring(pos)); 156 157 if (result.length() == 0) { 158 result.append("./"); 160 } 161 162 return result.toString(); 163 } 164 165 207 public static String getSitePath(CmsObject cms, String relativePath, String targetUri) { 208 209 if (cms == null) { 210 return targetUri; 212 } 213 214 URI uri; 215 String path; 216 String fragment; 217 String query; 218 String suffix; 219 220 try { 222 223 uri = new URI (targetUri); 224 path = uri.getPath(); 225 226 fragment = uri.getFragment(); 227 if (fragment != null) { 228 fragment = "#" + fragment; 229 } else { 230 fragment = ""; 231 } 232 233 query = uri.getQuery(); 234 if (query != null) { 235 query = "?" + query; 236 } else { 237 query = ""; 238 } 239 240 } catch (Exception e) { 241 if (LOG.isWarnEnabled()) { 242 LOG.warn(Messages.get().getBundle().key(Messages.LOG_MALFORMED_URI_1, targetUri), e); 243 } 244 return null; 245 } 246 247 suffix = fragment.concat(query); 249 250 if (uri.isOpaque()) { 252 return null; 253 } 254 255 if (uri.isAbsolute()) { 257 CmsSiteMatcher matcher = new CmsSiteMatcher(targetUri); 258 if (OpenCms.getSiteManager().isMatching(matcher)) { 259 if (path.startsWith(OpenCms.getSystemInfo().getOpenCmsContext())) { 260 path = path.substring(OpenCms.getSystemInfo().getOpenCmsContext().length()); 261 } 262 263 return cms.getRequestContext().addSiteRoot( 264 OpenCms.getSiteManager().matchSite(matcher).getSiteRoot(), 265 path + suffix); 266 } else { 267 return null; 268 } 269 } 270 271 String context = OpenCms.getSystemInfo().getOpenCmsContext(); 273 if ((context != null) && path.startsWith(context)) { 274 276 String siteRoot = null; 277 if (relativePath != null) { 278 siteRoot = CmsSiteManager.getSiteRoot(relativePath); 279 } 280 281 path = path.substring(context.length()); 283 284 if (siteRoot != null) { 285 if (!path.startsWith(siteRoot)) { 287 return cms.getRequestContext().addSiteRoot(siteRoot, path + suffix); 289 } else { 290 return path + suffix; 292 } 293 } else { 294 return cms.getRequestContext().addSiteRoot(path + suffix); 296 } 297 } 298 299 if (CmsStringUtil.isNotEmpty(path) && (path.charAt(0) != '/')) { 302 if (relativePath != null) { 303 String absolutePath; 304 int pos = path.indexOf("../../galleries/pics/"); 305 if (pos >= 0) { 306 return CmsWorkplace.VFS_PATH_SYSTEM + path.substring(pos + 6) + suffix; 308 } 309 absolutePath = getAbsoluteUri(path, cms.getRequestContext().addSiteRoot(relativePath)); 310 if (CmsSiteManager.getSiteRoot(absolutePath) != null) { 311 return absolutePath + suffix; 312 } 313 absolutePath = getAbsoluteUri(path, cms.getRequestContext().getSiteRoot() 315 + CmsWorkplace.VFS_PATH_EDITORS); 316 if (CmsSiteManager.getSiteRoot(absolutePath) != null) { 317 return absolutePath + suffix; 318 } 319 absolutePath = getAbsoluteUri(path, cms.getRequestContext().getSiteRoot() 321 + CmsWorkplace.VFS_PATH_EDITORS 322 + "xmlcontent/"); 323 if (CmsSiteManager.getSiteRoot(absolutePath) != null) { 324 return absolutePath + suffix; 325 } 326 } 327 328 return null; 329 } 330 331 if (CmsStringUtil.isNotEmpty(path)) { 333 return cms.getRequestContext().addSiteRoot(path) + suffix; 334 } 335 336 return suffix; 338 } 339 340 345 public CmsPointerLinkValidationResult getPointerLinkValidationResult() { 346 347 return m_pointerLinkValidationResult; 348 } 349 350 355 public void setPointerLinkValidationResult(CmsPointerLinkValidationResult externLinkValidationResult) { 356 357 m_pointerLinkValidationResult = externLinkValidationResult; 358 } 359 360 369 public String substituteLink(CmsObject cms, String link) { 370 371 return substituteLink(cms, link, null); 372 } 373 374 392 public String substituteLink(CmsObject cms, String link, String siteRoot) { 393 394 return substituteLink(cms, link, siteRoot, false); 395 } 396 397 416 public String substituteLink(CmsObject cms, String link, String siteRoot, boolean forceSecure) { 417 418 if (CmsStringUtil.isEmpty(link)) { 419 return ""; 421 } 422 String absoluteLink = CmsLinkManager.getAbsoluteUri(link, cms.getRequestContext().getUri()); 424 425 String vfsName; 426 String parameters; 427 int pos = absoluteLink.indexOf('?'); 428 if (pos >= 0) { 430 vfsName = absoluteLink.substring(0, pos); 431 parameters = absoluteLink.substring(pos); 432 } else { 433 vfsName = absoluteLink; 434 parameters = null; 435 } 436 437 String resultLink = null; 438 String uriBaseName = null; 439 boolean useRelativeLinks = false; 440 441 CmsSite targetSite; 443 if (CmsStringUtil.isNotEmpty(siteRoot)) { 444 targetSite = CmsSiteManager.getSite(siteRoot); 445 } else { 446 targetSite = CmsSiteManager.getCurrentSite(cms); 447 } 448 String serverPrefix = ""; 449 if (targetSite != CmsSiteManager.getCurrentSite(cms)) { 451 serverPrefix = targetSite.getUrl(); 452 } 453 454 if (cms.getRequestContext().currentProject().isOnlineProject()) { 455 456 CmsStaticExportManager exportManager = OpenCms.getStaticExportManager(); 457 if (exportManager.relativeLinksInExport(cms.getRequestContext().getSiteRoot() 459 + cms.getRequestContext().getUri())) { 460 uriBaseName = exportManager.getCachedOnlineLink(exportManager.getCacheKey( 462 cms.getRequestContext().getSiteRoot(), 463 cms.getRequestContext().getUri())); 464 if (uriBaseName == null) { 465 if (exportManager.isExportLink(cms, cms.getRequestContext().getUri())) { 467 uriBaseName = exportManager.getRfsName(cms, 469 cms.getRequestContext().getUri()); 471 } else { 472 uriBaseName = exportManager.getVfsPrefix() + cms.getRequestContext().getUri(); 474 } 475 exportManager.cacheOnlineLink(exportManager.getCacheKey( 477 cms.getRequestContext().getSiteRoot(), 478 cms.getRequestContext().getUri()), uriBaseName); 479 } 480 useRelativeLinks = uriBaseName.startsWith(OpenCms.getStaticExportManager().getRfsPrefix( 482 cms.getRequestContext().getSiteRoot() + cms.getRequestContext().getUri())); 483 } 484 485 resultLink = exportManager.getCachedOnlineLink(cms.getRequestContext().getSiteRoot() + ":" + absoluteLink); 487 if (resultLink == null) { 488 cms.getRequestContext().saveSiteRoot(); 489 cms.getRequestContext().setSiteRoot(targetSite.getSiteRoot()); 490 if (exportManager.isExportLink(cms, vfsName)) { 492 resultLink = exportManager.getRfsName(cms, vfsName, parameters); 494 parameters = null; 496 } else { 497 resultLink = exportManager.getVfsPrefix().concat(vfsName); 499 if (parameters != null) { 501 resultLink = resultLink.concat(parameters); 502 } 503 } 504 cms.getRequestContext().restoreSiteRoot(); 505 exportManager.cacheOnlineLink(cms.getRequestContext().getSiteRoot() + ":" + absoluteLink, resultLink); 507 } 508 509 if (targetSite.hasSecureServer() || CmsSiteManager.getCurrentSite(cms).hasSecureServer()) { 511 if (!vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) { 512 513 int linkType = -1; 514 try { 515 linkType = cms.readResource(vfsName).getTypeId(); 517 } catch (CmsException e) { 518 if (LOG.isInfoEnabled()) { 520 String message = Messages.get().getBundle().key( 521 Messages.LOG_RESOURCE_ACESS_ERROR_3, 522 vfsName, 523 cms.getRequestContext().currentUser().getName(), 524 cms.getRequestContext().getSiteRoot()); 525 if (LOG.isDebugEnabled()) { 526 LOG.debug(message, e); 527 } else { 528 LOG.info(message); 529 } 530 } 531 } 532 533 if (linkType != CmsResourceTypeImage.getStaticTypeId()) { 535 boolean secureLink = exportManager.isSecureLink(cms, vfsName, targetSite.getSiteRoot()); 537 boolean secureRequest = exportManager.isSecureLink(cms, cms.getRequestContext().getUri()); 538 if (secureLink && (forceSecure || !secureRequest)) { 541 serverPrefix = targetSite.getSecureUrl(); 542 } else if (!secureLink && secureRequest) { 543 serverPrefix = targetSite.getUrl(); 544 } 545 } 546 } 547 } 548 if (useRelativeLinks && CmsStringUtil.isEmpty(serverPrefix)) { 551 resultLink = getRelativeUri(uriBaseName, resultLink); 552 } 553 554 } else { 555 556 if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 558 resultLink = OpenCms.getStaticExportManager().getVfsPrefix().concat(vfsName); 560 } 561 562 if (parameters != null) { 564 resultLink = resultLink.concat(parameters); 565 } 566 567 } 568 return serverPrefix.concat(resultLink); 569 } 570 571 } | Popular Tags |