X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FCmsUtils.java;h=3a9337a114929d2b26f6eecc791d494fdbfd08bd;hb=77319330a8dec1f521e78a2df381f66c1b43100d;hp=c1b319fbe99f348e68e3813a102053917ebc3720;hpb=d546fc1b9e19a86eb0e33ac4be824b52239316fc;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java b/org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java index c1b319fbe..3a9337a11 100644 --- a/org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java @@ -1,15 +1,27 @@ 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; import javax.jcr.Property; 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.CurrentUser; +import org.argeo.eclipse.ui.specific.UiContext; import org.argeo.jcr.JcrUtils; import org.eclipse.rap.rwt.RWT; import org.eclipse.rap.rwt.service.ResourceManager; @@ -23,10 +35,73 @@ import org.eclipse.swt.layout.RowData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Table; 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. + */ + public static CmsView getCmsView() { + return UiContext.getData(CmsView.KEY); + } + + 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 { + if (log.isTraceEnabled()) + log.trace(CurrentUser.getUsername() + " : " + node.getPath()); + StringBuilder buf = new StringBuilder(); + if (CurrentUser.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 { + 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. */ @Deprecated public static RowData ROW_DATA_16px = new RowData(16, 16); @@ -64,14 +139,20 @@ public class CmsUtils implements CmsConstants { return new RowData(16, 16); } + /** Style widget */ public static void style(Widget widget, String style) { widget.setData(CmsConstants.STYLE, style); } + /** Enable markups on widget */ public static void markup(Widget widget) { widget.setData(CmsConstants.MARKUP, true); } + public static void setItemHeight(Table table, int height) { + table.setData(CmsConstants.ITEM_HEIGHT, height); + } + /** @return the path or null if not instrumented */ public static String getDataPath(Widget widget) { // JCR item