ResourceParsysConfigProvider.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.wcm.parsys.componentinfo.impl;

  21. import static io.wcm.wcm.parsys.ParsysNameConstants.NN_PARSYS_CONFIG;

  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Set;
  26. import java.util.regex.Pattern;

  27. import org.apache.commons.lang3.ArrayUtils;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.apache.jackrabbit.util.Text;
  30. import org.apache.sling.api.resource.Resource;
  31. import org.apache.sling.api.resource.ResourceResolver;
  32. import org.apache.sling.api.resource.ValueMap;
  33. import org.jetbrains.annotations.NotNull;

  34. import com.day.cq.commons.jcr.JcrConstants;

  35. import io.wcm.wcm.parsys.componentinfo.ParsysConfig;

  36. /**
  37.  * Reads paragraph system configuration from page component resource type definition in repository.
  38.  */
  39. final class ResourceParsysConfigProvider {

  40.   private static final String NN_PATHS = "paths";
  41.   private static final String PN_PATH = "path";
  42.   private static final String PN_PATTERN = "pattern";
  43.   private static final String PN_ALLOWEDCHILDREN = "allowedChildren";
  44.   private static final String PN_DENIEDDCHILDREN = "deniedChildren";
  45.   private static final String PN_ALLOWEDPARENTS = "allowedParents";
  46.   private static final String PN_PARENTANCESTORLEVEL = "parentAncestorLevel";
  47.   private static final String PN_INHERIT = "inherit";

  48.   private final List<ParsysConfig> pathDefs;

  49.   /**
  50.    * @param pageComponentResource Page component resource
  51.    */
  52.   ResourceParsysConfigProvider(Resource pageComponentResource) {
  53.     this.pathDefs = getPathDefs(pageComponentResource);
  54.   }

  55.   private static List<ParsysConfig> getPathDefs(Resource pageComponentResource) {
  56.     List<ParsysConfig> pathDefs = new ArrayList<>();

  57.     ResourceResolver resourceResolver = pageComponentResource.getResourceResolver();
  58.     Resource pathsResource = resourceResolver.getResource(pageComponentResource, "./" + NN_PARSYS_CONFIG + "/" + NN_PATHS);
  59.     if (pathsResource != null) {
  60.       Iterator<Resource> pathDefResources = resourceResolver.listChildren(pathsResource);
  61.       while (pathDefResources.hasNext()) {
  62.         Resource pathDefResource = pathDefResources.next();
  63.         pathDefs.add(new PathDef(pathDefResource, pageComponentResource.getResourceType()));
  64.       }
  65.     }

  66.     return pathDefs;
  67.   }

  68.   /**
  69.    * @return All path definitions
  70.    */
  71.   public List<ParsysConfig> getPathDefs() {
  72.     return this.pathDefs;
  73.   }

  74.   /**
  75.    * Paragraph System configuration path definition.
  76.    */
  77.   private static class PathDef implements ParsysConfig {

  78.     private final String pageComponentPath;
  79.     private final Pattern pathPattern;
  80.     private final Set<String> allowedChildren;
  81.     private final Set<String> deniedChildren;
  82.     private final Set<String> allowedParents;
  83.     private final int parentAncestorLevel;
  84.     private final boolean inheritFromSuperType;

  85.     /**
  86.      * @param pathDefResource Path definition resource
  87.      * @param pageComponentPath resource type of page component
  88.      */
  89.     PathDef(Resource pathDefResource, String pageComponentPath) {
  90.       this.pageComponentPath = pageComponentPath;
  91.       ValueMap pathDefProps = pathDefResource.getValueMap();

  92.       // resolve path/path pattern
  93.       String path = pathDefProps.get(PN_PATH, String.class);
  94.       String patternString = pathDefProps.get(PN_PATTERN, String.class);
  95.       if (StringUtils.isNotEmpty(patternString)) {
  96.         this.pathPattern = Pattern.compile(patternString);
  97.       }
  98.       else if (StringUtils.isNotBlank(path)) {
  99.         this.pathPattern = Pattern.compile("^" + Pattern.quote(path) + "$");
  100.       }
  101.       else {
  102.         String name = Text.getName(pathDefResource.getPath());
  103.         this.pathPattern = Pattern.compile("^" + Pattern.quote(JcrConstants.JCR_CONTENT + "/" + name) + "$");
  104.       }

  105.       // get allowed children/denied children/parents
  106.       this.allowedChildren = Set.of(pathDefProps.get(PN_ALLOWEDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
  107.       this.deniedChildren = Set.of(pathDefProps.get(PN_DENIEDDCHILDREN, ArrayUtils.EMPTY_STRING_ARRAY));
  108.       this.allowedParents = Set.of(pathDefProps.get(PN_ALLOWEDPARENTS, ArrayUtils.EMPTY_STRING_ARRAY));

  109.       // ancestor level
  110.       this.parentAncestorLevel = pathDefProps.get(PN_PARENTANCESTORLEVEL, 1);

  111.       // inherit from supertype
  112.       this.inheritFromSuperType = pathDefProps.get(PN_INHERIT, true);

  113.     }

  114.     @Override
  115.     public @NotNull String getPageComponentPath() {
  116.       return this.pageComponentPath;
  117.     }

  118.     @Override
  119.     public Pattern getPathPattern() {
  120.       return this.pathPattern;
  121.     }

  122.     @Override
  123.     public @NotNull Set<String> getAllowedChildren() {
  124.       return this.allowedChildren;
  125.     }

  126.     @Override
  127.     public @NotNull Set<String> getDeniedChildren() {
  128.       return this.deniedChildren;
  129.     }

  130.     @Override
  131.     public @NotNull Set<String> getAllowedParents() {
  132.       return this.allowedParents;
  133.     }

  134.     @Override
  135.     public int getParentAncestorLevel() {
  136.       return this.parentAncestorLevel;
  137.     }

  138.     @Override
  139.     public boolean isInherit() {
  140.       return this.inheritFromSuperType;
  141.     }

  142.     @Override
  143.     public String toString() {
  144.       return this.pathPattern.toString() + ", "
  145.           + "allowedChildren=[" + StringUtils.join(this.allowedChildren, ",") + "], "
  146.           + "deniedChildren=[" + StringUtils.join(this.deniedChildren, ",") + "], "
  147.           + "allowedParents=[" + StringUtils.join(this.allowedParents, ",") + "], "
  148.           + "parentAncestorLevel=" + this.parentAncestorLevel + ","
  149.           + "inheritFromSuperType=" + this.inheritFromSuperType;
  150.     }

  151.   }

  152. }