X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fjcr%2FJcrUtils.java;h=6871e60c76a57a1f1479aff03003f246596315b9;hb=7fe4a16dad045373bb014724733c1bbb175d44b5;hp=6afd8e13c3467175d2f84716714d1a5ed7fb188f;hpb=d2ed44a6c5968200c6ba18809dc0845749072b7f;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index 6afd8e13c..6871e60c7 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -16,6 +16,8 @@ package org.argeo.jcr; +import java.net.MalformedURLException; +import java.net.URL; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; @@ -100,6 +102,27 @@ public class JcrUtils { return dateAsPath(cal, false); } + /** + * Creates a deep path based on a URL: + * http://subdomain.example.com/to/content?args => + * com/example/subdomain/to/content + */ + public static String urlAsPath(String url) { + try { + URL u = new URL(url); + StringBuffer path = new StringBuffer(url.length()); + // invert host + String[] hostTokens = u.getHost().split("\\."); + for (int i = hostTokens.length - 1; i >= 0; i--) + path.append(hostTokens[i]).append('/'); + // we don't put port since it may not always be there and may change + path.append(u.getPath()); + return path.toString(); + } catch (MalformedURLException e) { + throw new ArgeoException("Cannot generate URL path for " + url, e); + } + } + /** * The provided data as a path ('/' at the end, not the beginning) * @@ -168,12 +191,30 @@ public class JcrUtils { /** Creates the nodes making path, if they don't exist. */ public static Node mkdirs(Session session, String path) { - return mkdirs(session, path, null, false); + return mkdirs(session, path, null, null, false); } - /** Creates the nodes making path, if they don't exist. */ + /** + * @deprecated use {@link #mkdirs(Session, String, String, String, Boolean)} + * instead. + */ + @Deprecated public static Node mkdirs(Session session, String path, String type, Boolean versioning) { + return mkdirs(session, path, type, type, false); + } + + /** + * @param type + * the type of the leaf node + */ + public static Node mkdirs(Session session, String path, String type) { + return mkdirs(session, path, type, null, false); + } + + /** Creates the nodes making path, if they don't exist. */ + public static Node mkdirs(Session session, String path, String type, + String intermediaryNodeType, Boolean versioning) { try { if (path.equals('/')) return session.getRootNode(); @@ -198,8 +239,11 @@ public class JcrUtils { String part = st.nextToken(); current.append(part).append('/'); if (!session.itemExists(current.toString())) { - if (type != null) + if (!st.hasMoreTokens() && type != null) currentNode = currentNode.addNode(part, type); + else if (st.hasMoreTokens() && intermediaryNodeType != null) + currentNode = currentNode.addNode(part, + intermediaryNodeType); else currentNode = currentNode.addNode(part); if (versioning) @@ -495,9 +539,9 @@ public class JcrUtils { public static String normalize(String name) { return name.replace(':', '_'); } - - public static void closeQuietly(Binary binary){ - if(binary==null) + + public static void closeQuietly(Binary binary) { + if (binary == null) return; binary.dispose(); }