Fix URLs
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 11 Nov 2015 12:44:35 +0000 (12:44 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 11 Nov 2015 12:44:35 +0000 (12:44 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8566 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeHttp.java
org.argeo.cms/src/org/argeo/cms/util/CmsUtils.java

index 6e3ef32462b234c3abaedde5ae6ebfc0a9414311..320f4e102ab8df05c76f57bf703e35d0a24c9371 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.internal.kernel;
 
 import static javax.jcr.Property.JCR_DESCRIPTION;
+import static javax.jcr.Property.JCR_LAST_MODIFIED;
 import static javax.jcr.Property.JCR_TITLE;
 import static org.argeo.cms.CmsTypes.CMS_IMAGE;
 
@@ -8,6 +9,7 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.security.PrivilegedExceptionAction;
 import java.security.cert.X509Certificate;
+import java.util.Calendar;
 import java.util.Enumeration;
 
 import javax.jcr.Node;
@@ -134,6 +136,8 @@ class NodeHttp implements KernelConstants, ArgeoJcrConstants {
                                                JCR_TITLE).getString() : node.getName();
                                String desc = node.hasProperty(JCR_DESCRIPTION) ? node
                                                .getProperty(JCR_DESCRIPTION).getString() : null;
+                               Calendar lastUpdate = node.hasProperty(JCR_LAST_MODIFIED) ? node
+                                               .getProperty(JCR_LAST_MODIFIED).getDate() : null;
                                String url = CmsUtils.getCanonicalUrl(node, request);
                                String imgUrl = null;
                                for (NodeIterator it = node.getNodes(); it.hasNext();) {
@@ -151,6 +155,9 @@ class NodeHttp implements KernelConstants, ArgeoJcrConstants {
                                        writeMeta(buf, "og:description", desc);
                                if (imgUrl != null)
                                        writeMeta(buf, "og:image", imgUrl);
+                               if (lastUpdate != null)
+                                       writeMeta(buf, "og:updated_time",
+                                                       Long.toString(lastUpdate.getTime().getTime()));
                                buf.append("</head>");
                                buf.append("<body>");
                                buf.append(
@@ -162,6 +169,8 @@ class NodeHttp implements KernelConstants, ArgeoJcrConstants {
                                buf.append("</body>");
                                buf.append("</html>");
                                writer.print(buf.toString());
+
+                               response.setHeader("Content-Type", "text/html");
                                writer.flush();
                        } catch (Exception e) {
                                throw new CmsException("Cannot write canonical answer", e);
index 2788ed9d34205c3336811471a4262cfe4e28c1d7..3a9337a114929d2b26f6eecc791d494fdbfd08bd 100644 (file)
@@ -5,6 +5,8 @@ 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;
@@ -48,10 +50,29 @@ public class CmsUtils implements CmsConstants {
                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 {
-               return request.getRequestURL().append(getDataPath(node).substring(1))
-                               .toString();
+               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 {
@@ -70,8 +91,15 @@ public class CmsUtils implements CmsConstants {
 
        public static String getCanonicalUrl(Node node, HttpServletRequest request)
                        throws RepositoryException {
-               return request.getRequestURL().append('!').append(node.getPath())
-                               .toString();
+               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. */