Support media in DocBook servlet.
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 17 Jun 2021 07:15:32 +0000 (09:15 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 17 Jun 2021 07:15:32 +0000 (09:15 +0200)
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java
publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java

index 6bcecb7b3e9fec2d3efa61161f8d94acffe57210..44346b25dbb669c5bfb1c88442509c2c25983d11 100644 (file)
@@ -53,6 +53,16 @@ public class DbkUtils {
                return Jcr.getName(node).equals(type.get());
        }
 
+       /** Whether this node is a DocBook type. */
+       public static boolean isDbk(Node node) {
+               String name = Jcr.getName(node);
+               for (DbkType type : DbkType.values()) {
+                       if (name.equals(type.get()))
+                               return true;
+               }
+               return false;
+       }
+
        public static String getTitle(Node node) {
                return JcrxApi.getXmlValue(node, DbkType.title.get());
        }
index 4e86f21a0067a1315fafc41d86e024ce46d3c5a3..d977377b3a6d4078455faec18fd77579744cf962 100644 (file)
@@ -9,9 +9,11 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 
+import javax.jcr.Node;
 import javax.jcr.Repository;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -28,10 +30,13 @@ import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.xalan.processor.TransformerFactoryImpl;
 import org.argeo.cms.servlet.ServletAuthUtils;
+import org.argeo.docbook.DbkUtils;
 import org.argeo.jcr.Jcr;
 import org.argeo.jcr.JcrException;
+import org.argeo.jcr.JcrUtils;
 import org.w3c.dom.Document;
 
 /**
@@ -48,9 +53,6 @@ public class DbkServlet extends HttpServlet {
 
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-               // TODO customise DocBook so that it outputs UTF-8
-               // see http://www.sagehill.net/docbookxsl/OutputEncoding.html
-               resp.setContentType("text/html; charset=ISO-8859-1");
 
                Session session = null;
                try {
@@ -61,35 +63,60 @@ public class DbkServlet extends HttpServlet {
                                pathInfo = pathInfo.substring(1);
                        String path = URLDecoder.decode(pathInfo, StandardCharsets.UTF_8);
 
-                       // TODO optimise with pipes, SAX, etc. ?
-
-                       byte[] arr;
-                       try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
-                               session.exportDocumentView(path, out, true, false);
-                               arr = out.toByteArray();
+                       Node node = session.getNode(path);
+                       if (DbkUtils.isDbk(node)) {
+                               // TODO customise DocBook so that it outputs UTF-8
+                               // see http://www.sagehill.net/docbookxsl/OutputEncoding.html
+                               resp.setContentType("text/html; charset=ISO-8859-1");
+
+                               // TODO optimise with pipes, SAX, etc. ?
+                               byte[] arr;
+                               try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+                                       session.exportDocumentView(path, out, true, false);
+                                       arr = out.toByteArray();
 //                             System.out.println(new String(arr, StandardCharsets.UTF_8));
-                       } catch (RepositoryException e) {
-                               throw new JcrException(e);
-                       }
+                               } catch (RepositoryException e) {
+                                       throw new JcrException(e);
+                               }
 
-                       try (InputStream in = new ByteArrayInputStream(arr);
+                               try (InputStream in = new ByteArrayInputStream(arr);
 //                                     ByteArrayOutputStream out = new ByteArrayOutputStream();
-                       ) {
+                               ) {
 
-                               Result xmlOutput = new StreamResult(resp.getOutputStream());
+                                       Result xmlOutput = new StreamResult(resp.getOutputStream());
 
-                               DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
+                                       DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
 //                             Document doc = docBuilder.parse(new File(
 //                                             System.getProperty("user.home") + "/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
-                               Document doc = docBuilder.parse(in);
-                               Source xmlInput = new DOMSource(doc);
+                                       Document doc = docBuilder.parse(in);
+                                       Source xmlInput = new DOMSource(doc);
 
-                               Transformer transformer = docBoookTemplates.newTransformer();
-                               transformer.transform(xmlInput, xmlOutput);
+                                       Transformer transformer = docBoookTemplates.newTransformer();
+                                       transformer.transform(xmlInput, xmlOutput);
 //                             resp.getOutputStream().write(out.toByteArray());
-                       } catch (Exception e) {
-                               throw new ServletException("Cannot transform " + path, e);
+                               } catch (Exception e) {
+                                       throw new ServletException("Cannot transform " + path, e);
+                               }
+                       } else if (node.isNodeType(NodeType.NT_FILE)) {// media download etc.
+                               String fileNameLowerCase = node.getName().toLowerCase();
+                               if (fileNameLowerCase.endsWith(".jpg") || fileNameLowerCase.endsWith(".jpeg")) {
+                                       resp.setContentType("image/jpeg");
+                               } else if (fileNameLowerCase.endsWith(".png")) {
+                                       resp.setContentType("image/png");
+                               } else if (fileNameLowerCase.endsWith(".gif")) {
+                                       resp.setContentType("image/gif");
+                               } else if (fileNameLowerCase.endsWith(".svg")) {
+                                       resp.setContentType("image/svg+xml");
+                               } else {
+                                       // TODO know more content types...
+                                       resp.setHeader("Content-Disposition", "attachment; filename=\"" + node.getName() + "\"");
+                               }
+                               IOUtils.copy(JcrUtils.getFileAsStream(node), resp.getOutputStream());
+                       } else {
+                               throw new IllegalArgumentException("Unsupported node " + node);
                        }
+               } catch (RepositoryException e1) {
+                       throw new JcrException(e1);
                } finally {
                        Jcr.logout(session);
                }