/** * Gets site root level path of a site. * @param path Path of page within the site * @param rootLevel Level of root page * @return Site root path for the site. The path is not checked for validness. */ private String getRootPath(String path, int rootLevel) { String rootPath = Text.getAbsoluteParent(path, rootLevel); // strip off everything after first "." - root path may be passed with selectors/extension which is not relevant if (StringUtils.contains(rootPath, ".")) { rootPath = StringUtils.substringBefore(rootPath, "."); } return rootPath; }
private void addAbsoluteParent(List<String> configurationIds, Resource resource, int absoluteParent) { String configurationId = Text.getAbsoluteParent(resource.getPath(), absoluteParent); if (StringUtils.isNotEmpty(configurationId)) { configurationIds.add(configurationId); } }
/** * Get absolute parent of given path. * If the path is below <code>/content/versionhistory</code> or <code>/content/launches</code> the path level is * adjusted accordingly. * This is a replacement for {@link com.day.text.Text#getAbsoluteParent(String, int)}. * @param path Path * @param parentLevel Parent level * @param resourceResolver Resource resolver * @return Absolute parent path or empty string if path is invalid */ public static String getAbsoluteParent(String path, int parentLevel, ResourceResolver resourceResolver) { if (parentLevel < 0) { return ""; } int level = parentLevel + getParentLevelOffset(path, resourceResolver); return Text.getAbsoluteParent(path, level); }
public ScriptParentResource(ScriptResource scriptResource) { super(scriptResource.getResourceResolver(), Text.getAbsoluteParent(scriptResource.getPath(), 1), null); this.scriptResource = scriptResource; }
@Override public Page getAbsoluteParent(int level) { String parentPath = Text.getAbsoluteParent(resource.getPath(), level - 1); Resource parentResource = resource.getResourceResolver().getResource(parentPath); if (parentResource != null) { return parentResource.adaptTo(Page.class); } return null; }
private SiteConfig getSiteConfigForSiteRoot(Resource contextResource) { if (contextResource == null) { return null; } int siteRootLevel = siteRootDetector.getSiteRootLevel(contextResource); final String siteRootPath; if (siteRootLevel >= 0) { siteRootPath = Text.getAbsoluteParent(contextResource.getPath(), siteRootLevel); } else { siteRootPath = null; } // site root cannot be detected? then get SiteConfig directly from resource without any caching if (StringUtils.isBlank(siteRootPath)) { return getSiteConfigForResource(contextResource); } // get site config for site root resource and cache the result (for a short time) try { return siteConfigCache.get(siteRootPath, () -> { Resource siteRootResource = contextResource.getResourceResolver().getResource(siteRootPath); return getSiteConfigForResourceCacheable(siteRootResource); }); } catch (ExecutionException ex) { log.warn("Unexpected exception.", ex); return null; } }
@Override public void after() throws Exception { try { // revert transient pending changes (that might be invalid) root.refresh(); // remove the test group and second test user Authorizable testGroup = getUserManager(root).getAuthorizable(TEST_GROUP_ID); if (testGroup != null) { testGroup.remove(); } Authorizable testUser2 = getUserManager(root).getAuthorizable(TEST_USER2_ID); if (testUser2 != null) { testUser2.remove(); } for (String p : new String[] {SUPPORTED_PATH, SUPPORTED_PATH2, Text.getAbsoluteParent(SUPPORTED_PATH3, 0), UNSUPPORTED_PATH}) { Tree t = root.getTree(p); if (t.exists()) { t.remove(); } } root.commit(); } finally { super.after(); } }
tp = getTreePermission(readOnlyRoot, Text.getAbsoluteParent(SUPPORTED_PATH3, 0), cugPermProvider); assertSame(TreePermission.NO_RECOURSE, tp);
@Test public void testMayContainWithCug() throws Exception { String cugPath = TreeUtil .addChild(root.getTree(SUPPORTED_PATH3), "child", NodeTypeConstants.NT_OAK_UNSTRUCTURED).getPath(); createCug(cugPath, EveryonePrincipal.getInstance()); root.commit(); Root readOnlyRoot = getRootProvider().createReadOnlyRoot(root); TopLevelPaths tlp = new TopLevelPaths(readOnlyRoot); for (String p : PATHS) { assertEquals(p, Text.isDescendantOrEqual(p, cugPath), tlp.contains(p)); } assertTrue(tlp.contains(Text.getAbsoluteParent(SUPPORTED_PATH3, 0))); assertTrue(tlp.contains(cugPath)); CugPermissionProvider cugPermProvider = createCugPermissionProvider( ImmutableSet.of(SUPPORTED_PATH, SUPPORTED_PATH2, SUPPORTED_PATH3), getTestUser().getPrincipal(), EveryonePrincipal.getInstance()); Tree t = readOnlyRoot.getTree(ROOT_PATH); TreePermission rootTp = cugPermProvider.getTreePermission(t, TreePermission.EMPTY); assertTrue(rootTp instanceof EmptyCugTreePermission); TreePermission tp = rootTp; for (String segm : PathUtils.elements(cugPath)) { t = t.getChild(segm); tp = cugPermProvider.getTreePermission(t, tp); assertCugPermission(tp, Text.isDescendantOrEqual(SUPPORTED_PATH3, t.getPath())); } for (String p : new String[] {SUPPORTED_PATH, SUPPORTED_PATH2, UNSUPPORTED_PATH }) { tp = getTreePermission(readOnlyRoot, p, cugPermProvider); assertSame(p, TreePermission.NO_RECOURSE, tp); } }