PrivilegeXmlExporter { public static void writePrivileges(final WriterOpener writerOpener, final Session session, final List<String> onlyNames, final boolean includeBuiltins) throws RepositoryException, IOException { final Workspace workspace = session.getWorkspace(); if (!(workspace instanceof JackrabbitWorkspace)) { throw new RepositoryException("Workspace must be instance of JackrabbitWorkspace, but isn't. type: " + workspace.getClass().getName()); } final NamePathResolver resolver = new DefaultNamePathResolver(session); final Function<String, Name> mapper = Fun.tryOrDefault1(resolver::getQName, null); final Set<Name> builtinPrivileges = JsonCnd.BUILTIN_PRIVILEGES.stream() .map(mapper).filter(Objects::nonNull).collect(toSet()); final Predicate<Name> builtinFilter = includeBuiltins ? name -> true : ((Predicate<Name>) builtinPrivileges::contains).negate(); final Set<Name> explicitNames = (onlyNames != null) ? onlyNames.stream().map(mapper).filter(Objects::nonNull).filter(builtinFilter).collect(toSet()) : Collections.emptySet(); final Predicate<Name> shouldInclude = !explicitNames.isEmpty() ? explicitNames::contains : builtinFilter; final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); final List<PrivilegeDefinition> privilegeDefinitions = new ArrayList<>(); for (Privilege privilege : privilegeManager.getRegisteredPrivileges()) { if (!shouldInclude.test(mapper.apply(privilege.getName()))) { continue; } final PrivilegeDefinitionImpl privilegeDefinition = new PrivilegeDefinitionImpl( mapper.apply(privilege.getName()), privilege.isAbstract(), Stream.of(privilege.getDeclaredAggregatePrivileges()) .map(Privilege::getName) .map(mapper) .filter(Objects::nonNull) .collect(toSet())); privilegeDefinitions.add(privilegeDefinition); } final Set<String> uris = privilegeDefinitions.stream() .flatMap(def -> Stream.concat(Stream.of(def.getName()), def.getDeclaredAggregateNames().stream())) .map(Name::getNamespaceURI).collect(toSet()); final Map<String, String> namespaces = uris.stream() .collect(toMap(Fun.tryOrDefault1(session::getNamespacePrefix, null), Function.identity())); PrivilegeDefinitionWriter pdWriter = new PrivilegeDefinitionWriter("text/xml"); try (Writer writer = writerOpener.open()) { pdWriter.writeDefinitions(writer, privilegeDefinitions.toArray(new PrivilegeDefinition[0]), namespaces); } } private  PrivilegeXmlExporter(); static void writePrivileges(final WriterOpener writerOpener,
                                       final Session session,
                                       final List<String> onlyNames,
                                       final boolean includeBuiltins);  }
@Test public void testWritePrivileges() throws Exception { final File tempDir = new File(testBaseDir, "testWritePrivileges"); final File fromRepoDir = new File(tempDir, "fromRepo/segmentstore"); final File toRepoDir = new File(tempDir, "toRepo/segmentstore"); final File exportedXml = new File(tempDir, "exported.xml"); TestUtil.prepareRepo(fromRepoDir, session -> { final URL aemPrivileges = getClass().getResource("/aem_privileges.xml"); TestUtil.installPrivilegesFromURL(session, aemPrivileges); }); if (exportedXml.exists()) { exportedXml.delete(); } TestUtil.withReadOnlyFixture(fromRepoDir, session -> { final Workspace workspace = session.getWorkspace(); final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); assertNotNull("crx:replicate should be imported", privilegeManager.getPrivilege("crx:replicate")); assertNotNull("cq:storeUGC should be imported", privilegeManager.getPrivilege("cq:storeUGC")); PrivilegeXmlExporter.writePrivileges(() -> new OutputStreamWriter( new FileOutputStream(exportedXml)), session, Arrays.asList("crx:replicate"), false); }); TestUtil.prepareRepo(toRepoDir, session -> { final URL exportedUrl = exportedXml.toURL(); TestUtil.installPrivilegesFromURL(session, exportedUrl); }); TestUtil.withReadOnlyFixture(toRepoDir, session -> { final Workspace workspace = session.getWorkspace(); final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); assertNotNull("crx:replicate should be imported", privilegeManager.getPrivilege("crx:replicate")); boolean thrown = false; try { privilegeManager.getPrivilege("cq:storeUGC"); } catch (Exception e) { thrown = true; } assertTrue("cq:storeUGC should NOT be imported", thrown); }); }
@Test public void testWritePrivileges_defaults() throws Exception { final File tempDir = new File(testBaseDir, "testWritePrivileges_defaults"); final File fromRepoDir = new File(tempDir, "fromRepo/segmentstore"); final File toRepoDir = new File(tempDir, "toRepo/segmentstore"); final File exportedXml = new File(tempDir, "exported.xml"); TestUtil.prepareRepo(fromRepoDir, session -> { final URL aemPrivileges = getClass().getResource("/aem_privileges.xml"); TestUtil.installPrivilegesFromURL(session, aemPrivileges); }); if (exportedXml.exists()) { exportedXml.delete(); } TestUtil.withReadOnlyFixture(fromRepoDir, session -> { final Workspace workspace = session.getWorkspace(); final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); assertNotNull("crx:replicate should be imported", privilegeManager.getPrivilege("crx:replicate")); assertNotNull("cq:storeUGC should be imported", privilegeManager.getPrivilege("cq:storeUGC")); PrivilegeXmlExporter.writePrivileges(() -> new OutputStreamWriter( new FileOutputStream(exportedXml)), session, null, true); }); TestUtil.prepareRepo(toRepoDir, session -> { final URL exportedUrl = exportedXml.toURL(); resultNothing1((URL url) -> TestUtil.installPrivilegesWithIndivResults(session, url)).apply(exportedUrl); }); TestUtil.withReadOnlyFixture(toRepoDir, session -> { final Workspace workspace = session.getWorkspace(); final PrivilegeManager privilegeManager = ((JackrabbitWorkspace) workspace).getPrivilegeManager(); assertNotNull("crx:replicate should be imported", privilegeManager.getPrivilege("crx:replicate")); }); }
@Test(expected = RepositoryException.class) public void testWritePrivileges_throws() throws Exception { final Session session = mock(Session.class); final Workspace workspace = mock(Workspace.class); when(session.getWorkspace()).thenReturn(workspace); PrivilegeXmlExporter.writePrivileges(StringWriter::new, session, Collections.emptyList(), false); }