1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.render.bean;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.Map;
24
25 import org.json.JSONException;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.ApplicationContextAware;
28 import org.springframework.core.io.Resource;
29 import org.springframework.extensions.config.WebStudioConfigElement;
30 import org.springframework.extensions.surf.WebFrameworkConstants;
31 import org.springframework.extensions.surf.exception.RendererExecutionException;
32 import org.springframework.extensions.surf.render.RenderContext;
33 import org.springframework.extensions.surf.render.RenderService;
34 import org.springframework.extensions.surf.render.bean.TemplateInstanceRenderer;
35 import org.springframework.extensions.surf.studio.WebStudioService;
36 import org.springframework.extensions.surf.util.DataUtil;
37 import org.springframework.extensions.surf.util.WebUtil;
38
39
40
41
42
43
44
45
46
47
48
49 public class WebStudioTemplateInstanceRenderer extends TemplateInstanceRenderer implements ApplicationContextAware
50 {
51 private ApplicationContext applicationContext = null;
52 private WebStudioService webStudioService = null;
53
54
55
56
57 public void setApplicationContext(ApplicationContext applicationContext)
58 {
59 this.applicationContext = applicationContext;
60 }
61
62
63
64
65
66
67 public void setWebStudioService(WebStudioService webStudioService)
68 {
69 this.webStudioService = webStudioService;
70 }
71
72
73
74
75
76
77 protected WebStudioService getWebStudioService()
78 {
79 return this.webStudioService;
80 }
81
82
83
84
85
86
87 protected WebStudioConfigElement getWebStudioConfiguration()
88 {
89 return getWebStudioService().getWebStudioConfiguration();
90 }
91
92 public void postHeaderProcess(RenderContext context)
93 throws RendererExecutionException
94 {
95
96 boolean webStudioEnabled = true;
97 if (webStudioEnabled)
98 {
99
100 String contextPath = context.getRequest().getContextPath();
101
102
103 print(context, RenderService.NEWLINE);
104 print(context, WebFrameworkConstants.WEB_STUDIO_SIGNATURE);
105
106
107 print(context, RenderService.NEWLINE);
108 include(context, "/includes/common.html", true);
109
110
111
112 boolean developmentMode = getWebStudioConfiguration().isDeveloperMode();
113 if (developmentMode)
114 {
115 print(context, RenderService.NEWLINE);
116 include(context, "/includes/development.html", true);
117 }
118 else
119 {
120
121
122 }
123
124
125 print(context, RenderService.NEWLINE);
126 print(context, "<script type=\"text/javascript\">");
127
128
129 print(context, RenderService.NEWLINE);
130 include(context, "/includes/context.html", true);
131
132
133 print(context, RenderService.NEWLINE);
134 try
135 {
136 printApplicationConfig(context);
137 }
138 catch (Exception e)
139 {
140 throw new RendererExecutionException(e);
141 }
142
143
144 print(context, RenderService.NEWLINE);
145 print(context, "</script>");
146
147
148 print(context, RenderService.NEWLINE);
149 print(context, "<script type=\"text/javascript\" src=\""
150 + contextPath + "/res/js/web-studio-init.js\"></script>");
151 }
152 }
153
154 protected String nullStringCheck(String str)
155 {
156 String value = "null";
157
158 if (str != null)
159 {
160 value = "\"" + str + "\"";
161 }
162
163 return value;
164 }
165
166 protected void include(RenderContext context, String path, boolean doSubstitutions)
167 throws RendererExecutionException
168 {
169 try
170 {
171 Resource resource = this.applicationContext.getResource("classpath:" + path);
172 InputStream is = resource.getInputStream();
173 String html = DataUtil.copyToString(is, null, true);
174
175 if (doSubstitutions)
176 {
177
178 html = html.replace("%{context.id}", nullStringCheck(context.getId()));
179 html = html.replace("%{context.websiteTitle}", nullStringCheck(context.getWebsiteTitle()));
180 html = html.replace("%{context.pageTitle}", nullStringCheck(context.getPageTitle()));
181 html = html.replace("%{context.uri}", nullStringCheck(context.getPageTitle()));
182 html = html.replace("%{context.pageId}", nullStringCheck(context.getPageId()));
183 html = html.replace("%{context.templateId}", nullStringCheck(context.getTemplateId()));
184 html = html.replace("%{context.currentObjectId}", nullStringCheck(context.getCurrentObjectId()));
185 html = html.replace("%{context.formatId}", nullStringCheck(context.getFormatId()));
186 html = html.replace("%{context.themeId}", nullStringCheck(context.getThemeId()));
187
188
189 String rootPageId = null;
190 String rootPageTitle = null;
191 if (context.getRootPage() != null)
192 {
193 rootPageId = context.getRootPage().getId();
194 rootPageTitle = context.getRootPage().getTitle();
195 }
196 html = html.replace("%{context.rootPageId}", nullStringCheck(rootPageId));
197 html = html.replace("%{context.rootPageTitle}", nullStringCheck(rootPageTitle));
198
199
200 html = html.replace("%{persistence.storeId}", nullStringCheck(context.getModel().getObjectManager().getContext().getStoreId()));
201 html = html.replace("%{persistence.webappId}", nullStringCheck(context.getModel().getObjectManager().getContext().getWebappId()));
202
203
204 Map queryStringMap = WebUtil.getQueryStringMap(context.getRequest());
205
206 String queryString = WebUtil.getQueryStringForMap(queryStringMap);
207 String alfrescoTicket = null;
208 String jSessionID = null;
209 String contextPath = context.getRequest().getContextPath();
210 String servletPath = context.getRequest().getServletPath();
211
212 html = html.replace("%{request.queryString}", nullStringCheck(queryString));
213 html = html.replace("%{request.alfrescoTicket}", nullStringCheck(alfrescoTicket));
214 html = html.replace("%{request.jSessionID}", nullStringCheck(jSessionID));
215 html = html.replace("%{request.contextPath}", nullStringCheck(contextPath));
216 html = html.replace("%{request.servletPath}", nullStringCheck(servletPath));
217
218
219 html = html.replace("${request.contextPath}", (contextPath != null ? contextPath : ""));
220 }
221
222 print(context, html);
223 }
224 catch (IOException ioe) { ioe.printStackTrace(); }
225 }
226
227 protected void printApplicationConfig(RenderContext context)
228 throws JSONException, RendererExecutionException
229 {
230
231
232 String jsonConfigString = getWebStudioService().getOverlayConfiguration().toString();
233 print(context, RenderService.NEWLINE);
234 print(context, "WebStudio.config = " + jsonConfigString + ";");
235 print(context, RenderService.NEWLINE);
236 }
237 }