+ public static String toPath(List<String> segments) {
+ // TODO checks
+ StringJoiner sj = new StringJoiner("/");
+ segments.forEach((s) -> sj.add(s));
+ return sj.toString();
+ }
+
+ public static List<String> toPathSegments(String path) {
+ List<String> res = new ArrayList<>();
+ if (EMPTY.equals(path) || ROOT_SLASH.equals(path))
+ return res;
+ collectPathSegments(path, res);
+ return res;
+ }
+
+ private static void collectPathSegments(String path, List<String> segments) {
+ String[] parent = getParentPath(path);
+ if (EMPTY.equals(parent[1])) // root
+ return;
+ segments.add(0, parent[1]);
+ if (EMPTY.equals(parent[0])) // end
+ return;
+ collectPathSegments(parent[0], segments);
+ }
+
+ public static void checkDoubleSlash(String path) {
+ if (path.contains(SLASH + "" + SLASH))
+ throw new IllegalArgumentException("Path " + path + " contains //");
+ }
+
+ public static Content roleToContent(CmsUserManager userManager, ContentSession contentSession, Role role) {
+ UserDirectory userDirectory = userManager.getDirectory(role);
+ String path = CmsContentRepository.DIRECTORY_BASE + SLASH + userDirectory.getName() + SLASH
+ + userDirectory.getRolePath(role);
+ Content content = contentSession.get(path);
+ return content;
+ }
+
+ /*
+ * CONSUMER UTILS
+ */
+
+ public static Content createCollections(ContentSession session, String path) {
+ if (session.exists(path)) {
+ Content content = session.get(path);
+ if (!content.isContentClass(CrName.collection.qName())) {
+ throw new IllegalStateException("Content " + path + " already exists, but is not a collection");
+ } else {
+ return content;
+ }
+ } else {
+ String[] parentPath = getParentPath(path);
+ Content parent = createCollections(session, parentPath[0]);
+ Content content = parent.add(parentPath[1], CrName.collection.qName());
+ return content;
+ }
+ }
+
+ public static ContentSession openDataAdminSession(ContentRepository repository) {
+ LoginContext loginContext;
+ try {
+ loginContext = CmsAuth.DATA_ADMIN.newLoginContext();
+ loginContext.login();
+ } catch (LoginException e1) {
+ throw new RuntimeException("Could not login as data admin", e1);
+ } finally {
+ }
+
+ ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(ContentUtils.class.getClassLoader());
+ return CurrentSubject.callAs(loginContext.getSubject(), () -> repository.get());
+ } finally {
+ Thread.currentThread().setContextClassLoader(currentCl);
+ }
+ }
+