]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java
Merge security.ui bundle in the cms.ui.workbench bundle
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / CmsUtils.java
index 6011345d1dc6ca716b2512b93c5d62fc7d626e5f..e5d30262cbd102153c676236ebe4ebc953d22d1f 100644 (file)
@@ -1,9 +1,12 @@
 package org.argeo.cms.util;
 
+import static org.argeo.cms.internal.kernel.KernelConstants.WEBDAV_PRIVATE;
 import static org.argeo.cms.internal.kernel.KernelConstants.WEBDAV_PUBLIC;
 import static org.argeo.jcr.ArgeoJcrConstants.ALIAS_NODE;
 
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 
 import javax.jcr.Item;
 import javax.jcr.Node;
@@ -12,9 +15,12 @@ import javax.jcr.RepositoryException;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsConstants;
 import org.argeo.cms.CmsException;
 import org.argeo.cms.CmsView;
+import org.argeo.cms.auth.AuthConstants;
 import org.argeo.eclipse.ui.specific.UiContext;
 import org.argeo.jcr.JcrUtils;
 import org.eclipse.rap.rwt.RWT;
@@ -34,6 +40,8 @@ import org.eclipse.swt.widgets.Widget;
 
 /** Static utilities for the CMS framework. */
 public class CmsUtils implements CmsConstants {
+       private final static Log log = LogFactory.getLog(CmsUtils.class);
+
        /**
         * The CMS view related to this display, or null if none is available from
         * this call.
@@ -42,23 +50,55 @@ public class CmsUtils implements CmsConstants {
                return UiContext.getData(CmsView.KEY);
        }
 
-       public static String getDataUrl(Node node, HttpServletRequest request)
-                       throws RepositoryException {
-               return request.getRequestURL().append(getDataPath(node).substring(1))
-                               .toString();
+       public static StringBuilder getServerBaseUrl(HttpServletRequest request) {
+               try {
+                       URL url = new URL(request.getRequestURL().toString());
+                       StringBuilder buf = new StringBuilder();
+                       buf.append(url.getProtocol()).append("://").append(url.getHost());
+                       if (url.getPort() != -1)
+                               buf.append(':').append(url.getPort());
+                       return buf;
+               } catch (MalformedURLException e) {
+                       throw new CmsException("Cannot extract server base URL from " + request.getRequestURL(), e);
+               }
+       }
+
+       public static String getDataUrl(Node node, HttpServletRequest request) throws RepositoryException {
+               try {
+                       StringBuilder buf = getServerBaseUrl(request);
+                       buf.append(getDataPath(node));
+                       return new URL(buf.toString()).toString();
+               } catch (MalformedURLException e) {
+                       throw new CmsException("Cannot build data URL for " + node, e);
+               }
        }
 
        public static String getDataPath(Node node) throws RepositoryException {
-               return new StringBuilder().append(WEBDAV_PUBLIC).append('/')
-                               .append(ALIAS_NODE + "/")
-                               .append(node.getSession().getWorkspace().getName())
+               assert node != null;
+               String userId = node.getSession().getUserID();
+               if (log.isTraceEnabled())
+                       log.trace(userId + " : " + node.getPath());
+               StringBuilder buf = new StringBuilder();
+               boolean isAnonymous = userId.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS);
+               if (isAnonymous)
+                       buf.append(WEBDAV_PUBLIC);
+               else
+                       buf.append(WEBDAV_PRIVATE);
+               // TODO convey repo alias vie repository properties
+               return buf.append('/').append(ALIAS_NODE).append('/').append(node.getSession().getWorkspace().getName())
                                .append(node.getPath()).toString();
        }
 
-       public static String getCanonicalUrl(Node node, HttpServletRequest request)
-                       throws RepositoryException {
-               return request.getRequestURL().append('!').append(node.getPath())
-                               .toString();
+       public static String getCanonicalUrl(Node node, HttpServletRequest request) throws RepositoryException {
+               try {
+                       StringBuilder buf = getServerBaseUrl(request);
+                       buf.append('/').append('!').append(node.getPath());
+                       return new URL(buf.toString()).toString();
+               } catch (MalformedURLException e) {
+                       throw new CmsException("Cannot build data URL for " + node, e);
+               }
+               // return request.getRequestURL().append('!').append(node.getPath())
+               // .toString();
        }
 
        /** @deprecated Use rowData16px() instead. GridData should not be reused. */
@@ -88,10 +128,8 @@ public class CmsUtils implements CmsConstants {
                return new GridData(SWT.FILL, SWT.FILL, true, true);
        }
 
-       public static GridData grabWidth(int horizontalAlignment,
-                       int verticalAlignment) {
-               return new GridData(horizontalAlignment, horizontalAlignment, true,
-                               false);
+       public static GridData grabWidth(int horizontalAlignment, int verticalAlignment) {
+               return new GridData(horizontalAlignment, horizontalAlignment, true, false);
        }
 
        public static RowData rowData16px() {
@@ -120,8 +158,7 @@ public class CmsUtils implements CmsConstants {
                        try {
                                return ((Item) data).getPath();
                        } catch (RepositoryException e) {
-                               throw new CmsException("Cannot find data path of " + data
-                                               + " for " + widget);
+                               throw new CmsException("Cannot find data path of " + data + " for " + widget);
                        }
                }
 
@@ -142,30 +179,25 @@ public class CmsUtils implements CmsConstants {
        //
        // JCR
        //
-       public static Node getOrAddEmptyFile(Node parent, Enum<?> child)
-                       throws RepositoryException {
+       public static Node getOrAddEmptyFile(Node parent, Enum<?> child) throws RepositoryException {
                if (has(parent, child))
                        return child(parent, child);
                return JcrUtils.copyBytesAsFile(parent, child.name(), new byte[0]);
        }
 
-       public static Node child(Node parent, Enum<?> en)
-                       throws RepositoryException {
+       public static Node child(Node parent, Enum<?> en) throws RepositoryException {
                return parent.getNode(en.name());
        }
 
-       public static Boolean has(Node parent, Enum<?> en)
-                       throws RepositoryException {
+       public static Boolean has(Node parent, Enum<?> en) throws RepositoryException {
                return parent.hasNode(en.name());
        }
 
-       public static Node getOrAdd(Node parent, Enum<?> en)
-                       throws RepositoryException {
+       public static Node getOrAdd(Node parent, Enum<?> en) throws RepositoryException {
                return getOrAdd(parent, en, null);
        }
 
-       public static Node getOrAdd(Node parent, Enum<?> en, String primaryType)
-                       throws RepositoryException {
+       public static Node getOrAdd(Node parent, Enum<?> en, String primaryType) throws RepositoryException {
                if (has(parent, en))
                        return child(parent, en);
                else if (primaryType == null)
@@ -183,11 +215,9 @@ public class CmsUtils implements CmsConstants {
                return img(src, Integer.toString(size.x), Integer.toString(size.y));
        }
 
-       public static StringBuilder imgBuilder(String src, String width,
-                       String height) {
-               return new StringBuilder(64).append("<img width='").append(width)
-                               .append("' height='").append(height).append("' src='")
-                               .append(src).append("'");
+       public static StringBuilder imgBuilder(String src, String width, String height) {
+               return new StringBuilder(64).append("<img width='").append(width).append("' height='").append(height)
+                               .append("' src='").append(src).append("'");
        }
 
        public static String noImg(Point size) {