Example #1
0
 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;
 }
 @Override
 public void toResource(Resource resource, ResourceFilter filter) throws RepositoryException {
   super.toResource(resource, filter);
   ResourceFilter.FilterSet filterSet = (ResourceFilter.FilterSet) filter;
   ResourceHandle handle = ResourceHandle.use(resource);
   ResourceFilter.FilterSet.Rule rule = filterSet.getRule();
   handle.setProperty(PROPERTY_RULE, rule.name());
   List<ResourceFilter> set = filterSet.getSet();
   ResourceHandle entry;
   for (int i = 0; i < set.size(); i++) {
     entry =
         ResourceHandle.use(
             ResourceUtil.getOrCreateChild(
                 resource, NODE_NAME_ENTRY + "-" + i, ResourceUtil.TYPE_UNSTRUCTURED));
     ResourceFilterMapping.toResource(entry, set.get(i));
   }
 }
 @Override
 public ResourceFilter fromResource(Resource resource) throws Exception {
   ResourceHandle handle = ResourceHandle.use(resource);
   String typeName = handle.getProperty(PROPERTY_TYPE, (String) null);
   Class<? extends ResourceFilter> type = getType(typeName);
   ResourceFilter filter = createInstance(handle, type);
   return filter;
 }
 public static ResourceFilter fromResource(Resource resource) throws Exception {
   ResourceFilter filter = null;
   if (resource != null) {
     ResourceHandle handle = ResourceHandle.use(resource);
     String typeName = handle.getProperty(PROPERTY_TYPE);
     Class<? extends ResourceFilter> type = getType(typeName);
     MappingStrategy strategy = getStrategy(type);
     filter = strategy.fromResource(resource);
   }
   return filter;
 }
Example #5
0
 /**
  * Retrieves the target for a resource if there are redirects declared.
  *
  * @return the target path or url (can be external); 'null' if no redirect detected
  * @throws RedirectLoopException if a 'loop' has been detected during redirect resolving
  */
 public static String getFinalTarget(Resource resource) throws RedirectLoopException {
   ResourceHandle handle = ResourceHandle.use(resource);
   String finalTarget = getFinalTarget(handle, new ArrayList<String>());
   return finalTarget;
 }
Example #6
0
  /**
   * 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;
  }
 @Override
 public void toResource(Resource resource, ResourceFilter filter) throws RepositoryException {
   ResourceHandle handle = ResourceHandle.use(resource);
   handle.setProperty(ResourceUtil.PROP_RESOURCE_TYPE, RESOURCE_FILTER_TYPE);
   handle.setProperty(PROPERTY_TYPE, getTypeName(filter));
 }