1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.beans.config.ContentRepository; 16 import info.magnolia.cms.core.Content; 17 import info.magnolia.cms.core.HierarchyManager; 18 import info.magnolia.cms.core.search.Query; 19 import info.magnolia.cms.core.search.QueryManager; 20 import info.magnolia.cms.core.search.QueryResult; 21 22 import java.util.Iterator ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import javax.jcr.RepositoryException; 27 28 import org.apache.commons.lang.StringUtils; 29 import org.slf4j.Logger; 30 import org.slf4j.LoggerFactory; 31 32 33 41 public final class LinkUtil { 42 43 46 private static HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.WEBSITE); 47 48 51 private static final Pattern linkPattern = Pattern.compile("(<a[^>]+href[ ]*=[ ]*\")(/[^\"]*).html((#[^\"]*)?\"[^>]*>)"); 53 56 private static final Pattern externalLinkPattern = Pattern.compile("^(\\w*://|mailto:|javascript:).*"); 57 58 61 private static Pattern uuidPattern = Pattern.compile("\\$\\{link:\\{uuid:\\{([^\\}]*)\\}," + "repository:\\{[^\\}]*\\}," + "workspace:\\{[^\\}]*\\}," + "path:\\{([^\\}]*)\\}\\}\\}"); 66 69 private static Logger log = LoggerFactory.getLogger(LinkUtil.class); 70 71 74 public static boolean isInternalRelativeLink(String href) { 75 return !externalLinkPattern.matcher(href).matches() && !href.startsWith("/") && !href.startsWith("#"); 77 } 78 79 84 public static String convertAbsoluteLinksToUUIDs(String str) { 85 Matcher matcher = linkPattern.matcher(str); 87 StringBuffer res = new StringBuffer (); 88 while (matcher.find()) { 89 String path = matcher.group(2); 90 String uuid = makeUUIDFromAbsolutePath(path); 91 matcher.appendReplacement(res, "$1\\${link:{" + "uuid:{" + uuid 94 + "}," + "repository:{website}," + "workspace:{default}," + "path:{" + path 99 + "}}}$3"); } 101 matcher.appendTail(res); 102 return res.toString(); 103 } 104 105 110 public static String convertUUIDsToAbsoluteLinks(String str) { 111 Matcher matcher = uuidPattern.matcher(str); 112 StringBuffer res = new StringBuffer (); 113 while (matcher.find()) { 114 String absolutePath = null; 115 String uuid = matcher.group(1); 116 117 if (StringUtils.isNotEmpty(uuid)) { 118 absolutePath = LinkUtil.makeAbsolutePathFromUUID(uuid); 119 } 120 121 if (StringUtils.isEmpty(absolutePath)) { 123 absolutePath = matcher.group(2); 124 log.error( 125 "Was not able to get the page by jcr:uuid nor by mgnl:uuid. Will use the saved path {}", 126 absolutePath); 127 } 128 matcher.appendReplacement(res, absolutePath + ".html"); } 130 matcher.appendTail(res); 131 return res.toString(); 132 } 133 134 140 public static String convertUUIDsToRelativeLinks(String str, Content page) { 141 Matcher matcher = uuidPattern.matcher(str); 142 StringBuffer res = new StringBuffer (); 143 while (matcher.find()) { 144 String absolutePath = null; 145 String uuid = matcher.group(1); 146 147 if (StringUtils.isNotEmpty(uuid)) { 148 absolutePath = LinkUtil.makeAbsolutePathFromUUID(uuid); 149 } 150 151 if (StringUtils.isEmpty(absolutePath)) { 153 absolutePath = matcher.group(2); 154 log.warn( 155 "Was not able to get the page by jcr:uuid nor by mgnl:uuid. Will use the saved path {}", 156 absolutePath); 157 } 158 159 String relativePath = makeRelativePath(absolutePath, page); 161 matcher.appendReplacement(res, relativePath); 162 } 163 matcher.appendTail(res); 164 return res.toString(); 165 } 166 167 173 public static String makeAbsolutePathFromUUID(String uuid) { 174 Content content = null; 175 176 try { 178 content = hm.getContentByUUID(uuid); 179 } 180 181 catch (Exception e) { 184 log.info("Was not able to get the page by the jcr:uuid. will try the old mgnl:uuid"); 185 186 QueryManager qmanager = hm.getQueryManager(); 187 188 if (qmanager != null) { 189 content = getContentByMgnlUUID(qmanager, uuid); 191 } 192 else { 193 log.info( 194 "SearchManager not configured for website repositoy, unable to generate absolute path for UUID {}", 195 uuid); 196 } 197 } 198 199 if (content != null) { 200 return content.getHandle(); 201 } 202 return null; 203 } 204 205 211 public static String makeRelativePath(String absolutePath, Content page) { 212 StringBuffer relativePath = new StringBuffer (); 213 int level; 214 try { 215 level = page.getLevel(); 216 } 217 catch (RepositoryException e) { 218 level = 0; 219 } 220 221 for (int i = 1; i < level; i++) { 222 relativePath.append("../"); } 224 225 if (absolutePath.startsWith("/")) { 226 relativePath.append(StringUtils.substringAfter(absolutePath, "/")); 227 } 228 else { 229 relativePath.append(absolutePath); 230 } 231 232 relativePath.append(".html"); 234 return relativePath.toString(); 235 } 236 237 242 public static String makeUUIDFromAbsolutePath(String path) { 243 try { 244 return hm.getContent(path).getUUID(); 245 } 246 catch (RepositoryException e) { 247 return path; 248 } 249 } 250 251 254 private LinkUtil() { 255 } 256 257 264 private static Content getContentByMgnlUUID(QueryManager queryManager, String uuid) { 265 try { 266 String statement = "SELECT * FROM nt:base where mgnl:uuid like '" + uuid + "'"; Query q = queryManager.createQuery(statement, Query.SQL); 268 QueryResult result = q.execute(); 269 Iterator it = result.getContent().iterator(); 270 while (it.hasNext()) { 271 Content foundObject = (Content) it.next(); 272 return foundObject; 273 } 274 } 275 catch (RepositoryException e) { 276 log.error("Exception caught", e); 277 } 278 return null; 279 } 280 281 } | Popular Tags |