1 package org
.argeo
.cms
.ui
.script
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Enumeration
;
8 import java
.util
.HashMap
;
9 import java
.util
.Hashtable
;
10 import java
.util
.List
;
13 import javax
.jcr
.Node
;
14 import javax
.jcr
.Property
;
15 import javax
.jcr
.PropertyIterator
;
16 import javax
.jcr
.PropertyType
;
17 import javax
.jcr
.Repository
;
18 import javax
.jcr
.RepositoryException
;
19 import javax
.script
.ScriptEngine
;
20 import javax
.servlet
.http
.HttpServletRequest
;
21 import javax
.servlet
.http
.HttpServletResponse
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.api
.cms
.CmsTheme
;
26 import org
.argeo
.cms
.CmsException
;
27 import org
.argeo
.cms
.ui
.CmsConstants
;
28 import org
.argeo
.cms
.ui
.CmsUiProvider
;
29 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
30 import org
.argeo
.cms
.web
.BundleResourceLoader
;
31 import org
.argeo
.cms
.web
.SimpleErgonomics
;
32 import org
.argeo
.cms
.web
.WebThemeUtils
;
33 import org
.eclipse
.rap
.rwt
.application
.Application
;
34 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
35 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
36 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
37 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
38 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
39 import org
.osgi
.framework
.Bundle
;
40 import org
.osgi
.framework
.BundleContext
;
41 import org
.osgi
.framework
.ServiceRegistration
;
42 import org
.osgi
.service
.http
.HttpContext
;
43 import org
.osgi
.service
.http
.HttpService
;
44 import org
.osgi
.service
.http
.NamespaceException
;
46 public class CmsScriptApp
implements Branding
{
47 public final static String CONTEXT_NAME
= "contextName";
49 ServiceRegistration
<ApplicationConfiguration
> appConfigReg
;
51 private ScriptEngine scriptEngine
;
53 private final static Log log
= LogFactory
.getLog(CmsScriptApp
.class);
55 private String webPath
;
56 private String repo
= "(cn=node)";
58 // private Branding branding = new Branding();
59 private CmsTheme theme
;
61 private List
<String
> resources
= new ArrayList
<>();
63 private Map
<String
, AppUi
> ui
= new HashMap
<>();
65 private CmsUiProvider header
;
66 private Integer headerHeight
= null;
67 private CmsUiProvider lead
;
68 private CmsUiProvider end
;
69 private CmsUiProvider footer
;
72 private String themeId
;
73 private String additionalHeaders
;
74 private String bodyHtml
;
75 private String pageTitle
;
76 private String pageOverflow
;
77 private String favicon
;
79 public CmsScriptApp(ScriptEngine scriptEngine
) {
81 this.scriptEngine
= scriptEngine
;
84 public void apply(BundleContext bundleContext
, Repository repository
, Application application
) {
85 BundleResourceLoader bundleRL
= new BundleResourceLoader(bundleContext
.getBundle());
87 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
88 // application.setOperationMode(OperationMode.JEE_COMPATIBILITY);
90 application
.setExceptionHandler(new CmsExceptionHandler());
92 // loading animated gif
93 application
.addResource(CmsConstants
.LOADING_IMAGE
, createResourceLoader(CmsConstants
.LOADING_IMAGE
));
95 application
.addResource(CmsConstants
.NO_IMAGE
, createResourceLoader(CmsConstants
.NO_IMAGE
));
97 for (String resource
: resources
) {
98 application
.addResource(resource
, bundleRL
);
99 if (log
.isTraceEnabled())
100 log
.trace("Resource " + resource
);
104 WebThemeUtils
.apply(application
, theme
);
105 String themeHeaders
= theme
.getHtmlHeaders();
106 if (themeHeaders
!= null) {
107 if (additionalHeaders
== null)
108 additionalHeaders
= themeHeaders
;
110 additionalHeaders
= themeHeaders
+ "\n" + additionalHeaders
;
112 themeId
= theme
.getThemeId();
116 Bundle appBundle
= bundleRL
.getBundle();
117 BundleContext bc
= appBundle
.getBundleContext();
118 HttpService httpService
= bc
.getService(bc
.getServiceReference(HttpService
.class));
119 HttpContext httpContext
= new BundleHttpContext(bc
);
120 Enumeration
<URL
> themeResources
= appBundle
.findEntries("/js/", "*", true);
121 if (themeResources
!= null)
122 bundleResources
: while (themeResources
.hasMoreElements()) {
124 String name
= themeResources
.nextElement().getPath();
125 if (name
.endsWith("/"))
126 continue bundleResources
;
127 String alias
= "/" + getWebPath() + name
;
129 httpService
.registerResources(alias
, name
, httpContext
);
130 if (log
.isDebugEnabled())
131 log
.debug("Mapped " + name
+ " to alias " + alias
);
133 } catch (NamespaceException e
) {
134 // TODO Auto-generated catch block
140 for (String appUiName
: ui
.keySet()) {
141 AppUi appUi
= ui
.get(appUiName
);
142 appUi
.apply(repository
, application
, this, appUiName
);
148 public void applySides(SimpleErgonomics simpleErgonomics
) {
149 simpleErgonomics
.setHeader(header
);
150 simpleErgonomics
.setLead(lead
);
151 simpleErgonomics
.setEnd(end
);
152 simpleErgonomics
.setFooter(footer
);
155 public void register(BundleContext bundleContext
, ApplicationConfiguration appConfig
) {
156 Hashtable
<String
, String
> props
= new Hashtable
<>();
157 props
.put(CONTEXT_NAME
, webPath
);
158 appConfigReg
= bundleContext
.registerService(ApplicationConfiguration
.class, appConfig
, props
);
161 public void reload() {
162 BundleContext bundleContext
= appConfigReg
.getReference().getBundle().getBundleContext();
163 ApplicationConfiguration appConfig
= bundleContext
.getService(appConfigReg
.getReference());
164 appConfigReg
.unregister();
165 register(bundleContext
, appConfig
);
167 // BundleContext bundleContext = (BundleContext)
168 // getScriptEngine().get("bundleContext");
170 // Bundle bundle = bundleContext.getBundle();
173 // } catch (BundleException e) {
174 // // TODO Auto-generated catch block
175 // e.printStackTrace();
179 private static ResourceLoader
createResourceLoader(final String resourceName
) {
180 return new ResourceLoader() {
181 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
182 return getClass().getClassLoader().getResourceAsStream(resourceName
);
187 public List
<String
> getResources() {
191 public AppUi
newUi(String name
) {
192 if (ui
.containsKey(name
))
193 throw new IllegalArgumentException("There is already an UI named " + name
);
194 AppUi appUi
= new AppUi(this);
195 // appUi.setApp(this);
200 public void addUi(String name
, AppUi appUi
) {
201 if (ui
.containsKey(name
))
202 throw new IllegalArgumentException("There is already an UI named " + name
);
203 // appUi.setApp(this);
207 public void applyBranding(Map
<String
, String
> properties
) {
209 properties
.put(WebClient
.THEME_ID
, themeId
);
210 if (additionalHeaders
!= null)
211 properties
.put(WebClient
.HEAD_HTML
, additionalHeaders
);
212 if (bodyHtml
!= null)
213 properties
.put(WebClient
.BODY_HTML
, bodyHtml
);
214 if (pageTitle
!= null)
215 properties
.put(WebClient
.PAGE_TITLE
, pageTitle
);
216 if (pageOverflow
!= null)
217 properties
.put(WebClient
.PAGE_OVERFLOW
, pageOverflow
);
219 properties
.put(WebClient
.FAVICON
, favicon
);
222 class CmsExceptionHandler
implements ExceptionHandler
{
225 public void handleException(Throwable throwable
) {
227 CmsUiUtils
.getCmsView().exception(throwable
);
232 // public Branding getBranding() {
236 ScriptEngine
getScriptEngine() {
240 public static String
toJson(Node node
) {
242 StringBuilder sb
= new StringBuilder();
244 PropertyIterator pit
= node
.getProperties();
246 while (pit
.hasNext()) {
247 Property p
= pit
.nextProperty();
248 int type
= p
.getType();
249 if (type
== PropertyType
.REFERENCE
|| type
== PropertyType
.WEAKREFERENCE
|| type
== PropertyType
.PATH
) {
250 Node ref
= p
.getNode();
254 sb
.append(toJson(ref
));
256 } else if (!p
.isMultiple()) {
259 sb
.append('\"').append(p
.getName()).append("\":\"").append(p
.getString()).append('\"');
264 return sb
.toString();
265 } catch (RepositoryException e
) {
266 throw new CmsException("Cannot convert " + node
+ " to JSON", e
);
270 public void fromJson(Node node
, String json
) {
274 public CmsTheme
getTheme() {
278 public void setTheme(CmsTheme theme
) {
282 public String
getWebPath() {
286 public void setWebPath(String context
) {
287 this.webPath
= context
;
290 public String
getRepo() {
294 public void setRepo(String repo
) {
298 public Map
<String
, AppUi
> getUi() {
302 public void setUi(Map
<String
, AppUi
> ui
) {
307 public String
getThemeId() {
311 public void setThemeId(String themeId
) {
312 this.themeId
= themeId
;
315 public String
getAdditionalHeaders() {
316 return additionalHeaders
;
319 public void setAdditionalHeaders(String additionalHeaders
) {
320 this.additionalHeaders
= additionalHeaders
;
323 public String
getBodyHtml() {
327 public void setBodyHtml(String bodyHtml
) {
328 this.bodyHtml
= bodyHtml
;
331 public String
getPageTitle() {
335 public void setPageTitle(String pageTitle
) {
336 this.pageTitle
= pageTitle
;
339 public String
getPageOverflow() {
343 public void setPageOverflow(String pageOverflow
) {
344 this.pageOverflow
= pageOverflow
;
347 public String
getFavicon() {
351 public void setFavicon(String favicon
) {
352 this.favicon
= favicon
;
355 public CmsUiProvider
getHeader() {
359 public void setHeader(CmsUiProvider header
) {
360 this.header
= header
;
363 public Integer
getHeaderHeight() {
367 public void setHeaderHeight(Integer headerHeight
) {
368 this.headerHeight
= headerHeight
;
371 public CmsUiProvider
getLead() {
375 public void setLead(CmsUiProvider lead
) {
379 public CmsUiProvider
getEnd() {
383 public void setEnd(CmsUiProvider end
) {
387 public CmsUiProvider
getFooter() {
391 public void setFooter(CmsUiProvider footer
) {
392 this.footer
= footer
;
395 static class BundleHttpContext
implements HttpContext
{
396 private BundleContext bundleContext
;
398 public BundleHttpContext(BundleContext bundleContext
) {
400 this.bundleContext
= bundleContext
;
404 public boolean handleSecurity(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
405 // TODO Auto-generated method stub
410 public URL
getResource(String name
) {
412 return bundleContext
.getBundle().getEntry(name
);
416 public String
getMimeType(String name
) {