1 package org
.argeo
.cms
.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
.cms
.CmsException
;
26 import org
.argeo
.cms
.ui
.CmsConstants
;
27 import org
.argeo
.cms
.ui
.CmsUiProvider
;
28 import org
.argeo
.cms
.util
.BundleResourceLoader
;
29 import org
.argeo
.cms
.util
.CmsUtils
;
30 import org
.argeo
.cms
.util
.SimpleErgonomics
;
31 import org
.eclipse
.rap
.rwt
.application
.Application
;
32 import org
.eclipse
.rap
.rwt
.application
.Application
.OperationMode
;
33 import org
.eclipse
.rap
.rwt
.application
.ApplicationConfiguration
;
34 import org
.eclipse
.rap
.rwt
.application
.ExceptionHandler
;
35 import org
.eclipse
.rap
.rwt
.client
.WebClient
;
36 import org
.eclipse
.rap
.rwt
.service
.ResourceLoader
;
37 import org
.osgi
.framework
.Bundle
;
38 import org
.osgi
.framework
.BundleContext
;
39 import org
.osgi
.framework
.ServiceRegistration
;
40 import org
.osgi
.service
.http
.HttpContext
;
41 import org
.osgi
.service
.http
.HttpService
;
42 import org
.osgi
.service
.http
.NamespaceException
;
44 public class CmsScriptApp
implements Branding
{
45 public final static String CONTEXT_NAME
= "contextName";
47 ServiceRegistration
<ApplicationConfiguration
> appConfigReg
;
49 private ScriptEngine scriptEngine
;
51 private final static Log log
= LogFactory
.getLog(CmsScriptApp
.class);
53 private String webPath
;
54 private String repo
= "(cn=node)";
56 // private Branding branding = new Branding();
59 private List
<String
> resources
= new ArrayList
<>();
61 private Map
<String
, AppUi
> ui
= new HashMap
<>();
63 private CmsUiProvider header
;
64 private Integer headerHeight
= null;
65 private CmsUiProvider lead
;
66 private CmsUiProvider end
;
67 private CmsUiProvider footer
;
70 private String themeId
;
71 private String additionalHeaders
;
72 private String bodyHtml
;
73 private String pageTitle
;
74 private String pageOverflow
;
75 private String favicon
;
77 public CmsScriptApp(ScriptEngine scriptEngine
) {
79 this.scriptEngine
= scriptEngine
;
82 public void apply(BundleContext bundleContext
, Repository repository
, Application application
) {
83 BundleResourceLoader bundleRL
= new BundleResourceLoader(bundleContext
.getBundle());
85 application
.setOperationMode(OperationMode
.SWT_COMPATIBILITY
);
86 // application.setOperationMode(OperationMode.JEE_COMPATIBILITY);
88 application
.setExceptionHandler(new CmsExceptionHandler());
90 // loading animated gif
91 application
.addResource(CmsConstants
.LOADING_IMAGE
, createResourceLoader(CmsConstants
.LOADING_IMAGE
));
93 application
.addResource(CmsConstants
.NO_IMAGE
, createResourceLoader(CmsConstants
.NO_IMAGE
));
95 for (String resource
: resources
) {
96 application
.addResource(resource
, bundleRL
);
97 if (log
.isTraceEnabled())
98 log
.trace("Resource " + resource
);
102 theme
.apply(application
);
103 String themeHeaders
= theme
.getAdditionalHeaders();
104 if (themeHeaders
!= null) {
105 if (additionalHeaders
== null)
106 additionalHeaders
= themeHeaders
;
108 additionalHeaders
= themeHeaders
+ "\n" + additionalHeaders
;
110 themeId
= theme
.getThemeId();
114 Bundle appBundle
= bundleRL
.getBundle();
115 BundleContext bc
= appBundle
.getBundleContext();
116 HttpService httpService
= bc
.getService(bc
.getServiceReference(HttpService
.class));
117 HttpContext httpContext
= new BundleHttpContext(bc
);
118 Enumeration
<URL
> themeResources
= appBundle
.findEntries("/js/", "*", true);
119 if (themeResources
!= null)
120 bundleResources
: while (themeResources
.hasMoreElements()) {
122 String name
= themeResources
.nextElement().getPath();
123 if (name
.endsWith("/"))
124 continue bundleResources
;
125 String alias
= "/" + getWebPath() + name
;
127 httpService
.registerResources(alias
, name
, httpContext
);
128 if (log
.isDebugEnabled())
129 log
.debug("Mapped " + name
+ " to alias " + alias
);
131 } catch (NamespaceException e
) {
132 // TODO Auto-generated catch block
138 for (String appUiName
: ui
.keySet()) {
139 AppUi appUi
= ui
.get(appUiName
);
140 appUi
.apply(repository
, application
, this, appUiName
);
146 public void applySides(SimpleErgonomics simpleErgonomics
) {
147 simpleErgonomics
.setHeader(header
);
148 simpleErgonomics
.setLead(lead
);
149 simpleErgonomics
.setEnd(end
);
150 simpleErgonomics
.setFooter(footer
);
153 public void register(BundleContext bundleContext
, ApplicationConfiguration appConfig
) {
154 Hashtable
<String
, String
> props
= new Hashtable
<>();
155 props
.put(CONTEXT_NAME
, webPath
);
156 appConfigReg
= bundleContext
.registerService(ApplicationConfiguration
.class, appConfig
, props
);
159 public void reload() {
160 BundleContext bundleContext
= appConfigReg
.getReference().getBundle().getBundleContext();
161 ApplicationConfiguration appConfig
= bundleContext
.getService(appConfigReg
.getReference());
162 appConfigReg
.unregister();
163 register(bundleContext
, appConfig
);
165 // BundleContext bundleContext = (BundleContext)
166 // getScriptEngine().get("bundleContext");
168 // Bundle bundle = bundleContext.getBundle();
171 // } catch (BundleException e) {
172 // // TODO Auto-generated catch block
173 // e.printStackTrace();
177 private static ResourceLoader
createResourceLoader(final String resourceName
) {
178 return new ResourceLoader() {
179 public InputStream
getResourceAsStream(String resourceName
) throws IOException
{
180 return getClass().getClassLoader().getResourceAsStream(resourceName
);
185 public List
<String
> getResources() {
189 public AppUi
newUi(String name
) {
190 if (ui
.containsKey(name
))
191 throw new IllegalArgumentException("There is already an UI named " + name
);
192 AppUi appUi
= new AppUi(this);
193 // appUi.setApp(this);
198 public void addUi(String name
, AppUi appUi
) {
199 if (ui
.containsKey(name
))
200 throw new IllegalArgumentException("There is already an UI named " + name
);
201 // appUi.setApp(this);
205 public void applyBranding(Map
<String
, String
> properties
) {
207 properties
.put(WebClient
.THEME_ID
, themeId
);
208 if (additionalHeaders
!= null)
209 properties
.put(WebClient
.HEAD_HTML
, additionalHeaders
);
210 if (bodyHtml
!= null)
211 properties
.put(WebClient
.BODY_HTML
, bodyHtml
);
212 if (pageTitle
!= null)
213 properties
.put(WebClient
.PAGE_TITLE
, pageTitle
);
214 if (pageOverflow
!= null)
215 properties
.put(WebClient
.PAGE_OVERFLOW
, pageOverflow
);
217 properties
.put(WebClient
.FAVICON
, favicon
);
220 class CmsExceptionHandler
implements ExceptionHandler
{
223 public void handleException(Throwable throwable
) {
225 CmsUtils
.getCmsView().exception(throwable
);
230 // public Branding getBranding() {
234 ScriptEngine
getScriptEngine() {
238 public static String
toJson(Node node
) {
240 StringBuilder sb
= new StringBuilder();
242 PropertyIterator pit
= node
.getProperties();
244 while (pit
.hasNext()) {
245 Property p
= pit
.nextProperty();
246 int type
= p
.getType();
247 if (type
== PropertyType
.REFERENCE
|| type
== PropertyType
.WEAKREFERENCE
|| type
== PropertyType
.PATH
) {
248 Node ref
= p
.getNode();
252 sb
.append(toJson(ref
));
254 } else if (!p
.isMultiple()) {
257 sb
.append('\"').append(p
.getName()).append("\":\"").append(p
.getString()).append('\"');
262 return sb
.toString();
263 } catch (RepositoryException e
) {
264 throw new CmsException("Cannot convert " + node
+ " to JSON", e
);
268 public void fromJson(Node node
, String json
) {
272 public Theme
getTheme() {
276 public void setTheme(Theme theme
) {
280 public String
getWebPath() {
284 public void setWebPath(String context
) {
285 this.webPath
= context
;
288 public String
getRepo() {
292 public void setRepo(String repo
) {
296 public Map
<String
, AppUi
> getUi() {
300 public void setUi(Map
<String
, AppUi
> ui
) {
305 public String
getThemeId() {
309 public void setThemeId(String themeId
) {
310 this.themeId
= themeId
;
313 public String
getAdditionalHeaders() {
314 return additionalHeaders
;
317 public void setAdditionalHeaders(String additionalHeaders
) {
318 this.additionalHeaders
= additionalHeaders
;
321 public String
getBodyHtml() {
325 public void setBodyHtml(String bodyHtml
) {
326 this.bodyHtml
= bodyHtml
;
329 public String
getPageTitle() {
333 public void setPageTitle(String pageTitle
) {
334 this.pageTitle
= pageTitle
;
337 public String
getPageOverflow() {
341 public void setPageOverflow(String pageOverflow
) {
342 this.pageOverflow
= pageOverflow
;
345 public String
getFavicon() {
349 public void setFavicon(String favicon
) {
350 this.favicon
= favicon
;
353 public CmsUiProvider
getHeader() {
357 public void setHeader(CmsUiProvider header
) {
358 this.header
= header
;
361 public Integer
getHeaderHeight() {
365 public void setHeaderHeight(Integer headerHeight
) {
366 this.headerHeight
= headerHeight
;
369 public CmsUiProvider
getLead() {
373 public void setLead(CmsUiProvider lead
) {
377 public CmsUiProvider
getEnd() {
381 public void setEnd(CmsUiProvider end
) {
385 public CmsUiProvider
getFooter() {
389 public void setFooter(CmsUiProvider footer
) {
390 this.footer
= footer
;
393 static class BundleHttpContext
implements HttpContext
{
394 private BundleContext bundleContext
;
396 public BundleHttpContext(BundleContext bundleContext
) {
398 this.bundleContext
= bundleContext
;
402 public boolean handleSecurity(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
403 // TODO Auto-generated method stub
408 public URL
getResource(String name
) {
410 return bundleContext
.getBundle().getEntry(name
);
414 public String
getMimeType(String name
) {