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;
/** Static utilities for the CMS framework. */
public class CmsUtils implements CmsConstants {
- /** The CMS view related to this display. */
+ 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() {
- Display display = Display.getCurrent();
- if (display == null)
- throw new CmsException("No display available");
- CmsView cmsView = (CmsView) display.getData(CmsView.KEY);
- if (cmsView == null)
- throw new CmsException("No CMS view available");
- return cmsView;
+ 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. */