Ejemplo n.º 1
0
 @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;
 }
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 3
0
 @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));
   }
 }
Ejemplo n.º 4
0
 @Override
 protected ResourceFilter createInstance(
     ResourceHandle resource, Class<? extends ResourceFilter> type) throws Exception {
   ResourceFilter.FilterSet.Rule rule =
       ResourceFilter.FilterSet.Rule.valueOf(resource.getProperty(PROPERTY_RULE, (String) null));
   List<ResourceHandle> filterResources =
       resource.getChildrenByResourceType(RESOURCE_FILTER_TYPE);
   List<ResourceFilter> filterList = new ArrayList<>();
   for (ResourceHandle filterRes : filterResources) {
     ResourceFilter filter = ResourceFilterMapping.fromResource(filterRes);
     filterList.add(filter);
   }
   ResourceFilter filter =
       type.getConstructor(ResourceFilter.FilterSet.Rule.class, List.class)
           .newInstance(rule, filterList);
   return filter;
 }
Ejemplo n.º 5
0
 @Override
 protected ResourceFilter createInstance(
     ResourceHandle resource, Class<? extends ResourceFilter> type) throws Exception {
   Resource stringFilterRes = resource.getChild(NODE_NAME_FILTER);
   StringFilter stringFilter = StringFilterMapping.fromResource(stringFilterRes);
   ResourceFilter filter = type.getConstructor(StringFilter.class).newInstance(stringFilter);
   return filter;
 }
Ejemplo n.º 6
0
 /**
  * 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 : "";
 }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
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;
  }
Ejemplo n.º 10
0
 @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));
 }