From dd5d3b4edb76ba0b6497f6cf8af7838abef16e96 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 17 Jun 2021 09:15:32 +0200 Subject: [PATCH] Support media in DocBook servlet. --- .../src/org/argeo/docbook/DbkUtils.java | 10 +++ .../argeo/publishing/servlet/DbkServlet.java | 71 +++++++++++++------ 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java index 6bcecb7..44346b2 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java @@ -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()); } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java index 4e86f21..d977377 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java @@ -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); } -- 2.30.2