View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 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.ui.granite.pathfield.impl.predicate;
21  
22  import java.util.Set;
23  import java.util.regex.Pattern;
24  
25  import org.apache.commons.collections4.Predicate;
26  import org.apache.commons.lang3.Strings;
27  import org.apache.sling.api.resource.Resource;
28  
29  /**
30   * Hide certain "AEM-internal" content paths when listing resource children.
31   */
32  public class HideInternalContentPathsPredicate implements Predicate<Resource> {
33  
34    /**
35     * List of paths that are hidden by default.
36     * Please note: it's allowed to see them if they are selected as root path, but not visible as children.
37     */
38    private static final Set<String> HIDE_CONTENT_PATHS = Set.of(
39        "/content/catalogs",
40        "/content/cq:tags",
41        "/content/campaigns",
42        "/content/communities",
43        "/content/community",
44        "/content/community-components",
45        "/content/community-templates",
46        "/content/dam",
47        "/content/dam/catalogs",
48        "/content/dam/collections",
49        "/content/dam/formsanddocuments",
50        "/content/dam/formsanddocuments-themes",
51        "/content/dam/templates",
52        "/content/entities",
53        "/content/experience-fragments",
54        "/content/forms",
55        "/content/launches",
56        "/content/mac",
57        "/content/mobileapps",
58        "/content/publications",
59        "/content/usergenerated",
60        "/content/phonegap",
61        "/content/projects",
62        "/content/screens",
63        "/content/sites",
64        "/content/versionhistory");
65  
66    private static final Pattern FIRST_LEVEL_PATH = Pattern.compile("^/[^/]+$");
67    private static final String CONTENT_ROOT_PATH = "/content";
68  
69    @Override
70    public boolean evaluate(Resource resource) {
71      // if resource is a resource on the first level (below root node), allow only /content
72      String path = resource.getPath();
73      if (FIRST_LEVEL_PATH.matcher(path).matches()) {
74        return Strings.CS.equals(path, CONTENT_ROOT_PATH);
75      }
76      // allow all other paths except those from the list above
77      return !HIDE_CONTENT_PATHS.contains(path);
78    }
79  
80  }