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
.argeo
.api
.cms
.CmsTheme
;
24 import org
.argeo
.api
.cms
.CmsLog
;
25 import org
.argeo
.cms
.CmsException
;
26 import org
.argeo
.cms
.ui
.CmsUiConstants
;
27 import org
.argeo
.cms
.ui
.CmsUiProvider
;
28 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
29 import org
.argeo
.cms
.web
.BundleResourceLoader
;
30 import org
.argeo
.cms
.web
.SimpleErgonomics
;
31 import org
.argeo
.cms
.web
.WebThemeUtils
;
32 import org
.eclipse
.rap
.rwt
.application
.Application
;
33 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
34 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
35 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
36 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
37 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
38 import org
.osgi
.framework
.Bundle
;
39 import org
.osgi
.framework
.BundleContext
;
40 import org
.osgi
.framework
.ServiceRegistration
;
41 import org
.osgi
.service
.http
.HttpContext
;
42 import org
.osgi
.service
.http
.HttpService
;
43 import org
.osgi
.service
.http
.NamespaceException
;
45 public class CmsScriptApp
implements Branding
{
46 public final static String CONTEXT_NAME
= "contextName";
48 ServiceRegistration
<ApplicationConfiguration
> appConfigReg
;
50 private ScriptEngine scriptEngine
;
52 private final static CmsLog log
= CmsLog
.getLog(CmsScriptApp
.class);
54 private String webPath
;
55 private String repo
= "(cn=node)";
57 // private Branding branding = new Branding();
58 private CmsTheme theme
;
60 private List
<String
> resources
= new ArrayList
<>();
62 private Map
<String
, AppUi
> ui
= new HashMap
<>();
64 private CmsUiProvider header
;
65 private Integer headerHeight
= null;
66 private CmsUiProvider lead
;
67 private CmsUiProvider end
;
68 private CmsUiProvider footer
;
71 private String themeId
;
72 private String additionalHeaders
;
73 private String bodyHtml
;
74 private String pageTitle
;
75 private String pageOverflow
;
76 private String favicon
;
78 public CmsScriptApp(ScriptEngine scriptEngine
) {
80 this.scriptEngine
= scriptEngine
;
83 public void apply(BundleContext bundleContext
, Repository repository
, Application application
) {
84 BundleResourceLoader bundleRL
= new BundleResourceLoader(bundleContext
.getBundle());
86 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
87 // application.setOperationMode(OperationMode.JEE_COMPATIBILITY);
89 application
.setExceptionHandler(new CmsExceptionHandler());
91 // loading animated gif
92 application
.addResource(CmsUiConstants
.LOADING_IMAGE
, createResourceLoader(CmsUiConstants
.LOADING_IMAGE
));
94 application
.addResource(CmsUiConstants
.NO_IMAGE
, createResourceLoader(CmsUiConstants
.NO_IMAGE
));
96 for (String resource
: resources
) {
97 application
.addResource(resource
, bundleRL
);
98 if (log
.isTraceEnabled())
99 log
.trace("Resource " + resource
);
103 WebThemeUtils
.apply(application
, theme
);
104 String themeHeaders
= theme
.getHtmlHeaders();
105 if (themeHeaders
!= null) {
106 if (additionalHeaders
== null)
107 additionalHeaders
= themeHeaders
;
109 additionalHeaders
= themeHeaders
+ "\n" + additionalHeaders
;
111 themeId
= theme
.getThemeId();
115 Bundle appBundle
= bundleRL
.getBundle();
116 BundleContext bc
= appBundle
.getBundleContext();
117 HttpService httpService
= bc
.getService(bc
.getServiceReference(HttpService
.class));
118 HttpContext httpContext
= new BundleHttpContext(bc
);
119 Enumeration
<URL
> themeResources
= appBundle
.findEntries("/js/", "*", true);
120 if (themeResources
!= null)
121 bundleResources
: while (themeResources
.hasMoreElements()) {
123 String name
= themeResources
.nextElement().getPath();
124 if (name
.endsWith("/"))
125 continue bundleResources
;
126 String alias
= "/" + getWebPath() + name
;
128 httpService
.registerResources(alias
, name
, httpContext
);
129 if (log
.isDebugEnabled())
130 log
.debug("Mapped " + name
+ " to alias " + alias
);
132 } catch (NamespaceException e
) {
133 // TODO Auto-generated catch block
139 for (String appUiName
: ui
.keySet()) {
140 AppUi appUi
= ui
.get(appUiName
);
141 appUi
.apply(repository
, application
, this, appUiName
);
147 public void applySides(SimpleErgonomics simpleErgonomics
) {
148 simpleErgonomics
.setHeader(header
);
149 simpleErgonomics
.setLead(lead
);
150 simpleErgonomics
.setEnd(end
);
151 simpleErgonomics
.setFooter(footer
);
154 public void register(BundleContext bundleContext
, ApplicationConfiguration appConfig
) {
155 Hashtable
<String
, String
> props
= new Hashtable
<>();
156 props
.put(CONTEXT_NAME
, webPath
);
157 appConfigReg
= bundleContext
.registerService(ApplicationConfiguration
.class, appConfig
, props
);
160 public void reload() {
161 BundleContext bundleContext
= appConfigReg
.getReference().getBundle().getBundleContext();
162 ApplicationConfiguration appConfig
= bundleContext
.getService(appConfigReg
.getReference());
163 appConfigReg
.unregister();
164 register(bundleContext
, appConfig
);
166 // BundleContext bundleContext = (BundleContext)
167 // getScriptEngine().get("bundleContext");
169 // Bundle bundle = bundleContext.getBundle();
172 // } catch (BundleException e) {
173 // // TODO Auto-generated catch block
174 // e.printStackTrace();
178 private static ResourceLoader
createResourceLoader(final String resourceName
) {
179 return new ResourceLoader() {
180 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
181 return getClass().getClassLoader().getResourceAsStream(resourceName
);
186 public List
<String
> getResources() {
190 public AppUi
newUi(String name
) {
191 if (ui
.containsKey(name
))
192 throw new IllegalArgumentException("There is already an UI named " + name
);
193 AppUi appUi
= new AppUi(this);
194 // appUi.setApp(this);
199 public void addUi(String name
, AppUi appUi
) {
200 if (ui
.containsKey(name
))
201 throw new IllegalArgumentException("There is already an UI named " + name
);
202 // appUi.setApp(this);
206 public void applyBranding(Map
<String
, String
> properties
) {
208 properties
.put(WebClient
.THEME_ID
, themeId
);
209 if (additionalHeaders
!= null)
210 properties
.put(WebClient
.HEAD_HTML
, additionalHeaders
);
211 if (bodyHtml
!= null)
212 properties
.put(WebClient
.BODY_HTML
, bodyHtml
);
213 if (pageTitle
!= null)
214 properties
.put(WebClient
.PAGE_TITLE
, pageTitle
);
215 if (pageOverflow
!= null)
216 properties
.put(WebClient
.PAGE_OVERFLOW
, pageOverflow
);
218 properties
.put(WebClient
.FAVICON
, favicon
);
221 class CmsExceptionHandler
implements ExceptionHandler
{
224 public void handleException(Throwable throwable
) {
226 CmsUiUtils
.getCmsView().exception(throwable
);
231 // public Branding getBranding() {
235 ScriptEngine
getScriptEngine() {
239 public static String
toJson(Node node
) {
241 StringBuilder sb
= new StringBuilder();
243 PropertyIterator pit
= node
.getProperties();
245 while (pit
.hasNext()) {
246 Property p
= pit
.nextProperty();
247 int type
= p
.getType();
248 if (type
== PropertyType
.REFERENCE
|| type
== PropertyType
.WEAKREFERENCE
|| type
== PropertyType
.PATH
) {
249 Node ref
= p
.getNode();
253 sb
.append(toJson(ref
));
255 } else if (!p
.isMultiple()) {
258 sb
.append('\"').append(p
.getName()).append("\":\"").append(p
.getString()).append('\"');
263 return sb
.toString();
264 } catch (RepositoryException e
) {
265 throw new CmsException("Cannot convert " + node
+ " to JSON", e
);
269 public void fromJson(Node node
, String json
) {
273 public CmsTheme
getTheme() {
277 public void setTheme(CmsTheme theme
) {
281 public String
getWebPath() {
285 public void setWebPath(String context
) {
286 this.webPath
= context
;
289 public String
getRepo() {
293 public void setRepo(String repo
) {
297 public Map
<String
, AppUi
> getUi() {
301 public void setUi(Map
<String
, AppUi
> ui
) {
306 public String
getThemeId() {
310 public void setThemeId(String themeId
) {
311 this.themeId
= themeId
;
314 public String
getAdditionalHeaders() {
315 return additionalHeaders
;
318 public void setAdditionalHeaders(String additionalHeaders
) {
319 this.additionalHeaders
= additionalHeaders
;
322 public String
getBodyHtml() {
326 public void setBodyHtml(String bodyHtml
) {
327 this.bodyHtml
= bodyHtml
;
330 public String
getPageTitle() {
334 public void setPageTitle(String pageTitle
) {
335 this.pageTitle
= pageTitle
;
338 public String
getPageOverflow() {
342 public void setPageOverflow(String pageOverflow
) {
343 this.pageOverflow
= pageOverflow
;
346 public String
getFavicon() {
350 public void setFavicon(String favicon
) {
351 this.favicon
= favicon
;
354 public CmsUiProvider
getHeader() {
358 public void setHeader(CmsUiProvider header
) {
359 this.header
= header
;
362 public Integer
getHeaderHeight() {
366 public void setHeaderHeight(Integer headerHeight
) {
367 this.headerHeight
= headerHeight
;
370 public CmsUiProvider
getLead() {
374 public void setLead(CmsUiProvider lead
) {
378 public CmsUiProvider
getEnd() {
382 public void setEnd(CmsUiProvider end
) {
386 public CmsUiProvider
getFooter() {
390 public void setFooter(CmsUiProvider footer
) {
391 this.footer
= footer
;
394 static class BundleHttpContext
implements HttpContext
{
395 private BundleContext bundleContext
;
397 public BundleHttpContext(BundleContext bundleContext
) {
399 this.bundleContext
= bundleContext
;
403 public boolean handleSecurity(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
404 // TODO Auto-generated method stub
409 public URL
getResource(String name
) {
411 return bundleContext
.getBundle().getEntry(name
);
415 public String
getMimeType(String name
) {