protected static String getFinalTarget(ResourceHandle resource, List<String> trace) throws RedirectLoopException { String finalTarget = null; if (resource.isValid()) { String path = resource.getPath(); if (trace.contains(path)) { throw new RedirectLoopException(trace, path); } String redirect = resource.getProperty(PROP_TARGET); if (StringUtils.isBlank(redirect)) { redirect = resource.getProperty(PROP_REDIRECT); } if (StringUtils.isNotBlank(redirect)) { trace.add(path); finalTarget = redirect; if (!URL_PATTERN.matcher(finalTarget).matches()) { ResourceResolver resolver = resource.getResourceResolver(); Resource targetResource = resolver.getResource(finalTarget); if (targetResource != null) { String target = getFinalTarget(ResourceHandle.use(targetResource), trace); if (StringUtils.isNotBlank(target)) { finalTarget = target; } } } } } return finalTarget; }
/** * Returns the extension for a URL to a resource based on a predefined value (can be null or ''). * The result is always not 'null' and can be added without check; it starts with a '.' if not * blank. * * @param resource the referenced resource * @param extension the predefined extension (can be 'null' or blank for determination) * @param detectMimeTypeExtension if 'true' an extension according to the mime type will be * detected * @return the string which has to add to the resources path; '' if nothing should add */ public static String getExtension( ResourceHandle resource, String extension, boolean detectMimeTypeExtension) { if (StringUtils.isBlank(extension) && detectMimeTypeExtension) { if (resource.isFile()) { MimeType mimeType = MimeTypeUtil.getMimeType(resource); if (mimeType != null) { extension = mimeType.getExtension(); } else { String name = resource.getName(); int lastDot = name.lastIndexOf('.'); if (lastDot > 0) { extension = name.substring(lastDot + 1); } } } } if (StringUtils.isNotBlank(extension)) { String name = resource.getName(); if (name.toLowerCase().endsWith(extension.toLowerCase())) { extension = ""; // no extension necessary to add } } if (extension == null) { String resourceType = resource.getResourceType(); if (resourceType != null && !resource.getPrimaryType().equals(resourceType)) { extension = EXT_HTML; // use '.html' by default if a real resource type is present } else { ResourceHandle content = resource.getContentResource(); if (content.isValid() && !ResourceUtil.isNonExistingResource(content)) { resourceType = content.getResourceType(); if (resourceType != null && !content.getPrimaryType().equals(resourceType)) { extension = EXT_HTML; // use '.html' by default if a content resource exists with a real // resource type } } } } if (StringUtils.isNotBlank(extension)) { if (!extension.startsWith(".")) { extension = "." + extension; } } return extension != null ? extension : ""; }
/** * Builds a mapped link to the path (resource path) with optional selectors and extension. * * @param request the request context for path mapping (the result is always mapped) * @param url the URL to use (complete) or the path to an addressed resource (without any * extension) * @param selectors an optional selector string with all necessary selectors (can be 'null') * @param extension an optional extension (can be 'null' for extension determination) * @param mapper the mapping strategy for the final link mapping * @return the mapped url for the referenced resource */ public static String getUrl( SlingHttpServletRequest request, String url, String selectors, String extension, LinkMapper mapper) { // skip blank urls if (StringUtils.isBlank(url)) { return url; } // rebuild URL if not always external only if (!isExternalUrl(url)) { ResourceResolver resolver = request.getResourceResolver(); ResourceHandle resource = ResourceHandle.use(resolver.getResource(url)); // it's possible that the resource can not be resolved / is virtual but is valid... if (resource.isValid()) { // forwards and extensions are resolvable for real resources only... // check for a target and 'forward' to this target if found try { String redirect = getFinalTarget(resource); if (StringUtils.isNotBlank(redirect)) { return getUrl(request, redirect, selectors, extension, mapper); } } catch (RedirectLoopException rlex) { LOG.error(rlex.toString()); } // check for a necessary extension and determine it if not specified extension = getExtension(resource, extension); } // map the path (the url) with the resource resolver (encodes the url) if (mapper != null) { url = mapper.mapUri(request, url); url = adjustMappedUrl(request, url); } if (StringUtils.isNotBlank(extension)) { url += extension; // extension starts with a '.' } // inject selectors into the complete URL because // it's possible, that the name always contains the extension... if (StringUtils.isNotBlank(selectors)) { if (!selectors.startsWith(".")) { selectors = "." + selectors; } Matcher matcher = SELECTOR_PATTERN.matcher(url); if (matcher.matches()) { url = matcher.group(1) + selectors + matcher.group(2); } } } return url; }