1 package org
.argeo
.publishing
.servlet
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.net
.URLDecoder
;
8 import java
.nio
.charset
.StandardCharsets
;
9 import java
.nio
.file
.Files
;
10 import java
.nio
.file
.Paths
;
11 import java
.util
.Collections
;
12 import java
.util
.HashMap
;
15 import javax
.jcr
.Node
;
16 import javax
.jcr
.Repository
;
17 import javax
.jcr
.RepositoryException
;
18 import javax
.jcr
.Session
;
19 import javax
.jcr
.nodetype
.NodeType
;
20 import javax
.servlet
.ServletException
;
21 import javax
.servlet
.http
.HttpServlet
;
22 import javax
.servlet
.http
.HttpServletRequest
;
23 import javax
.servlet
.http
.HttpServletResponse
;
24 import javax
.xml
.parsers
.DocumentBuilder
;
25 import javax
.xml
.parsers
.DocumentBuilderFactory
;
26 import javax
.xml
.transform
.Result
;
27 import javax
.xml
.transform
.Source
;
28 import javax
.xml
.transform
.Templates
;
29 import javax
.xml
.transform
.Transformer
;
30 import javax
.xml
.transform
.TransformerConfigurationException
;
31 import javax
.xml
.transform
.TransformerFactory
;
32 import javax
.xml
.transform
.dom
.DOMSource
;
33 import javax
.xml
.transform
.stream
.StreamResult
;
34 import javax
.xml
.transform
.stream
.StreamSource
;
36 import org
.apache
.commons
.io
.IOUtils
;
37 import org
.apache
.xalan
.processor
.TransformerFactoryImpl
;
38 import org
.argeo
.cms
.servlet
.ServletAuthUtils
;
39 import org
.argeo
.cms
.ui
.CmsTheme
;
40 import org
.argeo
.docbook
.DbkUtils
;
41 import org
.argeo
.jcr
.Jcr
;
42 import org
.argeo
.jcr
.JcrException
;
43 import org
.argeo
.jcr
.JcrUtils
;
44 import org
.w3c
.dom
.Document
;
47 * A servlet transforming a dbk:* JCR node into HTML, using the DocBook XSL.
49 public class DbkServlet
extends HttpServlet
{
50 private static final long serialVersionUID
= 6906020513498289335L;
52 private Repository repository
;
54 private DocumentBuilderFactory documentBuilderFactory
;
55 private TransformerFactory transformerFactory
;
56 private Templates docBoookTemplates
;
58 private Map
<String
, CmsTheme
> themes
= Collections
.synchronizedMap(new HashMap
<>());
61 protected void doGet(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
63 String pathInfo
= req
.getPathInfo();
64 if (pathInfo
.startsWith("//"))
65 pathInfo
= pathInfo
.substring(1);
66 String path
= URLDecoder
.decode(pathInfo
, StandardCharsets
.UTF_8
);
68 if (path
.toLowerCase().endsWith(".css")) {
69 path
= path
.substring(1);
70 int firstSlash
= path
.indexOf('/');
71 String themeId
= path
.substring(0, firstSlash
);
72 String cssPath
= path
.substring(firstSlash
);
73 CmsTheme cmsTheme
= themes
.get(themeId
);
75 throw new IllegalArgumentException("Theme " + themeId
+ " not found.");
76 resp
.setContentType("text/css");
77 IOUtils
.copy(cmsTheme
.getResourceAsStream(cssPath
), resp
.getOutputStream());
81 Session session
= null;
83 session
= ServletAuthUtils
.doAs(() -> Jcr
.login(repository
, null), req
);
84 Node node
= session
.getNode(path
);
85 if (DbkUtils
.isDbk(node
)) {
86 CmsTheme cmsTheme
= null;
87 String themeId
= req
.getParameter("themeId");
88 if (themeId
!= null) {
89 cmsTheme
= themes
.get(themeId
);
91 throw new IllegalArgumentException("Theme " + themeId
+ " not found.");
94 // TODO customise DocBook so that it outputs UTF-8
95 // see http://www.sagehill.net/docbookxsl/OutputEncoding.html
96 resp
.setContentType("text/html; charset=ISO-8859-1");
98 // TODO optimise with pipes, SAX, etc. ?
100 try (ByteArrayOutputStream out
= new ByteArrayOutputStream()) {
101 session
.exportDocumentView(path
, out
, true, false);
102 arr
= out
.toByteArray();
103 // System.out.println(new String(arr, StandardCharsets.UTF_8));
104 } catch (RepositoryException e
) {
105 throw new JcrException(e
);
108 try (InputStream in
= new ByteArrayInputStream(arr
);
109 // ByteArrayOutputStream out = new ByteArrayOutputStream();
112 Result xmlOutput
= new StreamResult(resp
.getOutputStream());
114 DocumentBuilder docBuilder
= documentBuilderFactory
.newDocumentBuilder();
115 // Document doc = docBuilder.parse(new File(
116 // System.getProperty("user.home") + "/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
117 Document doc
= docBuilder
.parse(in
);
118 Source xmlInput
= new DOMSource(doc
);
120 Transformer transformer
= docBoookTemplates
.newTransformer();
123 if (cmsTheme
!= null) {
124 StringBuilder sb
= new StringBuilder();
125 for (String cssPath
: cmsTheme
.getWebCssPaths()) {
126 sb
.append(req
.getContextPath()).append(req
.getServletPath()).append('/');
127 sb
.append(themeId
).append('/').append(cssPath
).append(' ');
129 sb
.append("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap").append(' ');
130 sb
.append("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,600;1,400&display=swap").append(' ');
132 transformer
.setParameter("html.stylesheet", sb
.toString());
134 transformer
.transform(xmlInput
, xmlOutput
);
135 // resp.getOutputStream().write(out.toByteArray());
136 } catch (Exception e
) {
137 throw new ServletException("Cannot transform " + path
, e
);
139 } else if (node
.isNodeType(NodeType
.NT_FILE
)) {// media download etc.
140 String fileNameLowerCase
= node
.getName().toLowerCase();
141 if (fileNameLowerCase
.endsWith(".jpg") || fileNameLowerCase
.endsWith(".jpeg")) {
142 resp
.setContentType("image/jpeg");
143 } else if (fileNameLowerCase
.endsWith(".png")) {
144 resp
.setContentType("image/png");
145 } else if (fileNameLowerCase
.endsWith(".gif")) {
146 resp
.setContentType("image/gif");
147 } else if (fileNameLowerCase
.endsWith(".svg")) {
148 resp
.setContentType("image/svg+xml");
150 // TODO know more content types...
151 resp
.setHeader("Content-Disposition", "attachment; filename=\"" + node
.getName() + "\"");
153 IOUtils
.copy(JcrUtils
.getFileAsStream(node
), resp
.getOutputStream());
155 throw new IllegalArgumentException("Unsupported node " + node
);
157 } catch (RepositoryException e1
) {
158 throw new JcrException(e1
);
165 public void init() throws ServletException
{
167 // TODO improve configuration and provisioning of DocBook XSL
168 String xslBase
= System
.getProperty("argeo.docbook.xsl");
169 if (xslBase
== null) {
170 String defaultXslBase
= "/opt/docbook-xsl";
171 if (!Files
.exists(Paths
.get(defaultXslBase
))) {
172 throw new ServletException("System property argeo.docbook.xsl is not set and default location "
173 + defaultXslBase
+ " does not exist.");
175 xslBase
= defaultXslBase
;
178 String xsl
= xslBase
+ "/html/docbook.xsl";
180 documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
181 documentBuilderFactory
.setXIncludeAware(true);
182 documentBuilderFactory
.setNamespaceAware(true);
184 // We must explicitly use the non-XSLTC transformer, as XSLTC is not working
185 // with DocBook stylesheets
186 transformerFactory
= new TransformerFactoryImpl();
188 Source xslSource
= new StreamSource(xsl
);
190 docBoookTemplates
= transformerFactory
.newTemplates(xslSource
);
191 if (docBoookTemplates
== null)
192 throw new ServletException("Could not instantiate XSL " + xsl
);
193 } catch (TransformerConfigurationException e
) {
194 throw new ServletException("Cannot instantiate XSL " + xsl
, e
);
198 public void setRepository(Repository repository
) {
199 this.repository
= repository
;
202 public void addTheme(CmsTheme theme
, Map
<String
, String
> properties
) {
203 themes
.put(theme
.getThemeId(), theme
);
206 public void removeTheme(CmsTheme theme
, Map
<String
, String
> properties
) {
207 themes
.remove(theme
.getThemeId());