+ public static ContentSession openSession(ContentRepository contentRepository, CmsSession cmsSession) {
+ return CurrentSubject.callAs(cmsSession.getSubject(), () -> contentRepository.get());
+ }
+
+ /**
+ * Constructs a relative path between a base path and a given path.
+ *
+ * @throws IllegalArgumentException if the base path is not an ancestor of the
+ * path
+ */
+ public static String relativize(String basePath, String path) throws IllegalArgumentException {
+ Objects.requireNonNull(basePath);
+ Objects.requireNonNull(path);
+ if (!path.startsWith(basePath))
+ throw new IllegalArgumentException(basePath + " is not an ancestor of " + path);
+ String relativePath = path.substring(basePath.length());
+ if (relativePath.length() > 0 && relativePath.charAt(0) == '/')
+ relativePath = relativePath.substring(1);
+ return relativePath;
+ }
+
+ /** A path in the node repository */
+ public static String getDataPath(Content node) {
+ // TODO make it more configurable?
+ StringBuilder buf = new StringBuilder(CmsConstants.PATH_API_ACR);
+ buf.append(node.getPath());
+ return buf.toString();
+ }
+
+ /** A path in the node repository */
+ public static String getDataPathForUrl(Content node) {
+ return cleanPathForUrl(getDataPath(node));
+ }
+
+ /** Clean reserved URL characters for use in HTTP links. */
+ public static String cleanPathForUrl(String path) {
+ StringTokenizer st = new StringTokenizer(path, "/");
+ StringBuilder sb = new StringBuilder();
+ while (st.hasMoreElements()) {
+ sb.append('/');
+ String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8);
+ encoded = encoded.replace("+", "%20");
+ sb.append(encoded);
+
+ }
+ return sb.toString();
+ }
+