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
.DbkType
;
41 import org
.argeo
.docbook
.DbkUtils
;
42 import org
.argeo
.jcr
.Jcr
;
43 import org
.argeo
.jcr
.JcrException
;
44 import org
.argeo
.jcr
.JcrUtils
;
45 import org
.w3c
.dom
.Document
;
48 * A servlet transforming a dbk:* JCR node into HTML, using the DocBook XSL.
50 public class DbkServlet
extends HttpServlet
{
51 private static final long serialVersionUID
= 6906020513498289335L;
53 private Repository repository
;
55 private DocumentBuilderFactory documentBuilderFactory
;
56 private TransformerFactory transformerFactory
;
57 private Templates docBoookTemplates
;
59 private Map
<String
, CmsTheme
> themes
= Collections
.synchronizedMap(new HashMap
<>());
62 protected void doGet(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
64 String pathInfo
= req
.getPathInfo();
65 if (pathInfo
.startsWith("//"))
66 pathInfo
= pathInfo
.substring(1);
67 String path
= URLDecoder
.decode(pathInfo
, StandardCharsets
.UTF_8
);
69 if (path
.toLowerCase().endsWith(".css")) {
70 path
= path
.substring(1);
71 int firstSlash
= path
.indexOf('/');
72 String themeId
= path
.substring(0, firstSlash
);
73 String cssPath
= path
.substring(firstSlash
);
74 CmsTheme cmsTheme
= themes
.get(themeId
);
76 throw new IllegalArgumentException("Theme " + themeId
+ " not found.");
77 resp
.setContentType("text/css");
78 IOUtils
.copy(cmsTheme
.getResourceAsStream(cssPath
), resp
.getOutputStream());
82 Session session
= null;
84 session
= ServletAuthUtils
.doAs(() -> Jcr
.login(repository
, null), req
);
85 Node documentNode
= session
.getNode(path
);
87 if (documentNode
.hasNode(DbkType
.article
.get()))
88 node
= documentNode
.getNode(DbkType
.article
.get());
90 throw new IllegalArgumentException("Unsupported node " + documentNode
);
92 if (DbkUtils
.isDbk(node
)) {
93 CmsTheme cmsTheme
= null;
94 String themeId
= req
.getParameter("themeId");
95 if (themeId
!= null) {
96 cmsTheme
= themes
.get(themeId
);
98 throw new IllegalArgumentException("Theme " + themeId
+ " not found.");
101 // TODO customise DocBook so that it outputs UTF-8
102 // see http://www.sagehill.net/docbookxsl/OutputEncoding.html
103 resp
.setContentType("text/html; charset=ISO-8859-1");
105 // TODO optimise with pipes, SAX, etc. ?
107 try (ByteArrayOutputStream out
= new ByteArrayOutputStream()) {
108 session
.exportDocumentView(node
.getPath(), out
, true, false);
109 arr
= out
.toByteArray();
110 // System.out.println(new String(arr, StandardCharsets.UTF_8));
111 } catch (RepositoryException e
) {
112 throw new JcrException(e
);
115 try (InputStream in
= new ByteArrayInputStream(arr
);
116 // ByteArrayOutputStream out = new ByteArrayOutputStream();
119 Result xmlOutput
= new StreamResult(resp
.getOutputStream());
121 DocumentBuilder docBuilder
= documentBuilderFactory
.newDocumentBuilder();
122 // Document doc = docBuilder.parse(new File(
123 // System.getProperty("user.home") + "/dev/git/gpl/argeo-qa/doc/platform/argeo-platform.dbk.xml"));
124 Document doc
= docBuilder
.parse(in
);
125 Source xmlInput
= new DOMSource(doc
);
127 Transformer transformer
= docBoookTemplates
.newTransformer();
130 if (cmsTheme
!= null) {
131 StringBuilder sb
= new StringBuilder();
132 for (String cssPath
: cmsTheme
.getWebCssPaths()) {
133 sb
.append(req
.getContextPath()).append(req
.getServletPath()).append('/');
134 sb
.append(themeId
).append('/').append(cssPath
).append(' ');
136 // FIXME make it more generic
137 sb
.append("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap")
140 "https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,600;1,400&display=swap")
143 transformer
.setParameter("html.stylesheet", sb
.toString());
145 transformer
.transform(xmlInput
, xmlOutput
);
146 // resp.getOutputStream().write(out.toByteArray());
147 } catch (Exception e
) {
148 throw new ServletException("Cannot transform " + path
, e
);
150 } else if (node
.isNodeType(NodeType
.NT_FILE
)) {// media download etc.
151 String fileNameLowerCase
= node
.getName().toLowerCase();
152 if (fileNameLowerCase
.endsWith(".jpg") || fileNameLowerCase
.endsWith(".jpeg")) {
153 resp
.setContentType("image/jpeg");
154 } else if (fileNameLowerCase
.endsWith(".png")) {
155 resp
.setContentType("image/png");
156 } else if (fileNameLowerCase
.endsWith(".gif")) {
157 resp
.setContentType("image/gif");
158 } else if (fileNameLowerCase
.endsWith(".svg")) {
159 resp
.setContentType("image/svg+xml");
161 // TODO know more content types...
162 resp
.setHeader("Content-Disposition", "attachment; filename=\"" + node
.getName() + "\"");
164 IOUtils
.copy(JcrUtils
.getFileAsStream(node
), resp
.getOutputStream());
166 throw new IllegalArgumentException("Unsupported node " + node
);
168 } catch (RepositoryException e1
) {
169 throw new JcrException(e1
);
176 public void init() throws ServletException
{
178 // TODO improve configuration and provisioning of DocBook XSL
179 String xslBase
= System
.getProperty("argeo.docbook.xsl");
180 if (xslBase
== null) {
181 String defaultXslBase
= "/opt/docbook-xsl";
182 if (!Files
.exists(Paths
.get(defaultXslBase
))) {
183 throw new ServletException("System property argeo.docbook.xsl is not set and default location "
184 + defaultXslBase
+ " does not exist.");
186 xslBase
= defaultXslBase
;
189 String xsl
= xslBase
+ "/html/docbook.xsl";
191 documentBuilderFactory
= DocumentBuilderFactory
.newInstance();
192 documentBuilderFactory
.setXIncludeAware(true);
193 documentBuilderFactory
.setNamespaceAware(true);
195 // We must explicitly use the non-XSLTC transformer, as XSLTC is not working
196 // with DocBook stylesheets
197 transformerFactory
= new TransformerFactoryImpl();
199 Source xslSource
= new StreamSource(xsl
);
201 docBoookTemplates
= transformerFactory
.newTemplates(xslSource
);
202 if (docBoookTemplates
== null)
203 throw new ServletException("Could not instantiate XSL " + xsl
);
204 } catch (TransformerConfigurationException e
) {
205 throw new ServletException("Cannot instantiate XSL " + xsl
, e
);
209 public void setRepository(Repository repository
) {
210 this.repository
= repository
;
213 public void addTheme(CmsTheme theme
, Map
<String
, String
> properties
) {
214 themes
.put(theme
.getThemeId(), theme
);
217 public void removeTheme(CmsTheme theme
, Map
<String
, String
> properties
) {
218 themes
.remove(theme
.getThemeId());