1 package org
.argeo
.cms
.internal
.http
;
4 import java
.io
.IOException
;
5 import java
.net
.URLDecoder
;
6 import java
.nio
.charset
.StandardCharsets
;
7 import java
.security
.PrivilegedExceptionAction
;
8 import java
.util
.HashMap
;
11 import javax
.jcr
.Node
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.Session
;
14 import javax
.security
.auth
.Subject
;
15 import javax
.security
.auth
.login
.LoginContext
;
16 import javax
.servlet
.ServletException
;
17 import javax
.servlet
.http
.HttpServlet
;
18 import javax
.servlet
.http
.HttpServletRequest
;
19 import javax
.servlet
.http
.HttpServletResponse
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.argeo
.cms
.auth
.HttpRequestCallbackHandler
;
23 import org
.argeo
.fm
.jcr
.JcrModel
;
24 import org
.argeo
.jcr
.JcrUtils
;
25 import org
.argeo
.node
.NodeConstants
;
26 import org
.osgi
.framework
.BundleContext
;
27 import org
.osgi
.framework
.FrameworkUtil
;
28 import org
.osgi
.framework
.ServiceReference
;
29 import org
.osgi
.util
.tracker
.ServiceTracker
;
31 import freemarker
.template
.Configuration
;
32 import freemarker
.template
.SimpleScalar
;
33 import freemarker
.template
.Template
;
34 import freemarker
.template
.TemplateExceptionHandler
;
36 public class HtmlServlet
extends HttpServlet
{
37 private static final long serialVersionUID
= 2083925371199357045L;
38 static String base
= System
.getProperty("user.home") + File
.separator
+ "dev" + File
.separator
+ "work"
39 + File
.separator
+ "ftl";
40 static Configuration cfg
;
43 cfg
= new Configuration(Configuration
.VERSION_2_3_28
);
44 cfg
.setDirectoryForTemplateLoading(new File(base
));
45 cfg
.setDefaultEncoding("UTF-8");
46 cfg
.setTemplateExceptionHandler(TemplateExceptionHandler
.HTML_DEBUG_HANDLER
);
47 cfg
.setLogTemplateExceptions(false);
48 cfg
.setWrapUncheckedExceptions(true);
49 } catch (IOException e
) {
50 // TODO Auto-generated catch block
55 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
56 private ServiceTracker
<Repository
, Repository
> repositorySt
;
58 private Repository repository
;
61 public void init() throws ServletException
{
62 repositorySt
= new ServiceTracker
<Repository
, Repository
>(bc
, Repository
.class, null) {
65 public Repository
addingService(ServiceReference
<Repository
> reference
) {
66 String cn
= reference
.getProperty(NodeConstants
.CN
).toString();
67 Repository repo
= super.addingService(reference
);
68 if (NodeConstants
.NODE
.equals(cn
))
78 public void destroy() {
79 if (repositorySt
!= null)
84 protected void doGet(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
85 // String path = req.getPathInfo();
86 String path
= URLDecoder
.decode(req
.getPathInfo(), StandardCharsets
.UTF_8
.name());
87 String servletPath
= req
.getServletPath();
88 String
[] p
= path
.split("/");
90 String basePath
= servletPath
+ '/' + p
[1];
91 String template
= p
[1] + ".ftl";
92 StringBuilder sb
= new StringBuilder();
93 for (int i
= 2; i
< p
.length
; i
++)
94 sb
.append('/').append(p
[i
]);
95 String nodePath
= sb
.toString();
96 if ("".equals(nodePath
.trim()))
97 nodePath
= "/"; // root node
98 Session session
= null;
100 LoginContext lc
= new LoginContext(NodeConstants
.LOGIN_CONTEXT_USER
,
101 new HttpRequestCallbackHandler(req
, resp
));
103 session
= Subject
.doAs(lc
.getSubject(), new PrivilegedExceptionAction
<Session
>() {
106 public Session
run() throws Exception
{
107 Session session
= repository
.login();
112 Node node
= session
.getNode(nodePath
);
114 Template t
= cfg
.getTemplate(template
);
115 Map
<String
, Object
> root
= new HashMap
<>();
116 root
.put("node", new JcrModel(node
));
117 root
.put("basePath", new SimpleScalar(basePath
));
119 t
.process(root
, resp
.getWriter());
121 resp
.setContentType("text/html");
122 } catch (Exception e
) {
123 throw new CmsException("Cannot process template", e
);
125 JcrUtils
.logoutQuietly(session
);