001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.staticexport; 029 030import org.opencms.cache.CmsVfsMemoryObjectCache; 031import org.opencms.file.CmsFile; 032import org.opencms.file.CmsObject; 033import org.opencms.file.CmsResource; 034import org.opencms.main.CmsException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.xml.content.CmsXmlContent; 038import org.opencms.xml.content.CmsXmlContentFactory; 039 040import java.net.URI; 041import java.util.ArrayList; 042import java.util.List; 043import java.util.Locale; 044 045import org.apache.commons.logging.Log; 046 047/** 048 * Advanced link substitution behavior.<p> 049 * You can define additional paths that are always used as external links, even if 050 * they point to the same configured site than the OpenCms itself. 051 * 052 * @since 7.5.0 053 * 054 * @see CmsLinkManager#substituteLink(org.opencms.file.CmsObject, String, String, boolean) 055 * for the method where this handler is used. 056 */ 057public class CmsAdvancedLinkSubstitutionHandler extends CmsDefaultLinkSubstitutionHandler { 058 059 /** Filename of the link exclude definition file. */ 060 private static final String LINK_EXCLUDE_DEFINIFITON_FILE = "linkexcludes"; 061 062 /** The log object for this class. */ 063 private static final Log LOG = CmsLog.getLog(CmsAdvancedLinkSubstitutionHandler.class); 064 065 /** XPath for link exclude in definition file. */ 066 private static final String XPATH_LINK = "link"; 067 068 /** 069 * @see org.opencms.staticexport.CmsDefaultLinkSubstitutionHandler#getLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String, boolean) 070 */ 071 @Override 072 public String getLink(CmsObject cms, String link, String siteRoot, boolean forceSecure) { 073 074 if (isExcluded(cms, link)) { 075 return link; 076 } 077 return super.getLink(cms, link, siteRoot, forceSecure); 078 } 079 080 /** 081 * @see org.opencms.staticexport.I_CmsLinkSubstitutionHandler#getRootPath(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 082 */ 083 @Override 084 public String getRootPath(CmsObject cms, String targetUri, String basePath) { 085 086 if (cms == null) { 087 // required by unit test cases 088 return targetUri; 089 } 090 091 URI uri; 092 String path; 093 094 // check for malformed URI 095 try { 096 uri = new URI(targetUri); 097 path = uri.getPath(); 098 } catch (Exception e) { 099 if (LOG.isWarnEnabled()) { 100 LOG.warn(Messages.get().getBundle().key(Messages.LOG_MALFORMED_URI_1, targetUri), e); 101 } 102 return null; 103 } 104 // opaque URI 105 if (uri.isOpaque()) { 106 return null; 107 } 108 109 // in case the target is the workplace UI 110 if (CmsLinkManager.isWorkplaceUri(uri)) { 111 return null; 112 } 113 if (isExcluded(cms, path)) { 114 return null; 115 } 116 117 return super.getRootPath(cms, targetUri, basePath); 118 } 119 120 /** 121 * Returns if the given path starts with an exclude prefix.<p> 122 * 123 * @param cms the cms context 124 * @param path the path to check 125 * 126 * @return <code>true</code> if the given path starts with an exclude prefix 127 */ 128 protected boolean isExcluded(CmsObject cms, String path) { 129 130 List<String> excludes = getExcludes(cms); 131 // now check if the current link start with one of the exclude links 132 for (int i = 0; i < excludes.size(); i++) { 133 if (path.startsWith(excludes.get(i))) { 134 return true; 135 } 136 } 137 return false; 138 } 139 140 /** 141 * Returns the exclude prefix list.<p> 142 * 143 * @param cms the cms context 144 * 145 * @return the exclude prefix list 146 */ 147 private List<String> getExcludes(CmsObject cms) { 148 149 // get the list of link excludes form the cache if possible 150 CmsVfsMemoryObjectCache cache = CmsVfsMemoryObjectCache.getVfsMemoryObjectCache(); 151 String filePath = OpenCms.getSystemInfo().getConfigFilePath(cms, LINK_EXCLUDE_DEFINIFITON_FILE); 152 @SuppressWarnings("unchecked") 153 List<String> excludes = (List<String>)cache.getCachedObject(cms, filePath); 154 if (excludes == null) { 155 // nothing found in cache, so read definition file and store the result in cache 156 excludes = readLinkExcludes(cms); 157 cache.putCachedObject(cms, filePath, excludes); 158 } 159 return excludes; 160 } 161 162 /** 163 * Reads the link exclude definition file and extracts all excluded links stored in it.<p> 164 * 165 * @param cms the current CmsObject 166 * @return list of Strings, containing link exclude paths 167 */ 168 private List<String> readLinkExcludes(CmsObject cms) { 169 170 List<String> linkExcludes = new ArrayList<String>(); 171 172 try { 173 // get the link exclude file 174 String filePath = OpenCms.getSystemInfo().getConfigFilePath(cms, LINK_EXCLUDE_DEFINIFITON_FILE); 175 CmsResource res = cms.readResource(filePath); 176 CmsFile file = cms.readFile(res); 177 CmsXmlContent linkExcludeDefinitions = CmsXmlContentFactory.unmarshal(cms, file); 178 179 // get number of excludes 180 int count = linkExcludeDefinitions.getIndexCount(XPATH_LINK, Locale.ENGLISH); 181 182 for (int i = 1; i <= count; i++) { 183 184 String exclude = linkExcludeDefinitions.getStringValue(cms, XPATH_LINK + "[" + i + "]", Locale.ENGLISH); 185 linkExcludes.add(exclude); 186 187 } 188 189 } catch (CmsException e) { 190 LOG.error(e.getLocalizedMessage(), e); 191 } 192 193 return linkExcludes; 194 } 195}