View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Modifies the way that components are rendered when Surf is running in preview mode.
42   * This allows Web Studio to pull "pulled in" to any Surf Preview application.
43   * 
44   * Primarily, this enables the regions to output additional Web Studio
45   * specific JavaScript to bind client-side DOM elements together.
46   * 
47   * @author muzquiano
48   */
49  public class WebStudioTemplateInstanceRenderer extends TemplateInstanceRenderer implements ApplicationContextAware
50  {
51      private ApplicationContext applicationContext = null;
52      private WebStudioService webStudioService = null;
53         
54      /* (non-Javadoc)
55       * @see org.alfresco.web.framework.render.AbstractRenderer#setApplicationContext(org.springframework.context.ApplicationContext)
56       */
57      public void setApplicationContext(ApplicationContext applicationContext)
58      {
59          this.applicationContext = applicationContext;
60      }
61      
62      /**
63       * Sets the web studio service.
64       * 
65       * @param webStudioService the new web studio service
66       */
67      public void setWebStudioService(WebStudioService webStudioService)
68      {
69          this.webStudioService = webStudioService;
70      }
71      
72      /**
73       * Gets the web studio service.
74       * 
75       * @return the web studio service
76       */
77      protected WebStudioService getWebStudioService()
78      {
79          return this.webStudioService;
80      }
81      
82      /**
83       * Gets the web studio configuration.
84       * 
85       * @return the web studio configuration
86       */
87      protected WebStudioConfigElement getWebStudioConfiguration()
88      {
89          return getWebStudioService().getWebStudioConfiguration();
90      }
91          
92      public void postHeaderProcess(RenderContext context)
93              throws RendererExecutionException
94      {
95          // TODO: check whether web studio is enabled
96          boolean webStudioEnabled = true;
97          if (webStudioEnabled)
98          {
99              // get the context path to the webapp (i.e. /alfwf)
100             String contextPath = context.getRequest().getContextPath();
101 
102             // append in the Web Studio signature
103             print(context, RenderService.NEWLINE);
104             print(context, WebFrameworkConstants.WEB_STUDIO_SIGNATURE);
105             
106             // append in common include file (all of the mandatory js and css)
107             print(context, RenderService.NEWLINE);
108             include(context, "/includes/common.html", true);
109             
110             // include development or production mode files
111             // TODO: fix up config options
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                 //print(context, RenderService.NEWLINE);
121                 //include(context, "/includes/production.html", true);
122             }
123             
124             // start of inline javascript
125             print(context, RenderService.NEWLINE);
126             print(context, "<script type=\"text/javascript\">");
127             
128             // include web framework context
129             print(context, RenderService.NEWLINE);
130             include(context, "/includes/context.html", true);
131             
132             // application config
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             // end of inline javascript
144             print(context, RenderService.NEWLINE);
145             print(context, "</script>");
146             
147             // include web studio init script
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                 // variable substitution
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                 // root page
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                 // store binding
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                 // request information
204                 Map queryStringMap = WebUtil.getQueryStringMap(context.getRequest());
205                 // TODO: append any adjustments here
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                 // simple string replace on this token
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         // convert the configuration to JSON
231         // append into json
232         String jsonConfigString = getWebStudioService().getOverlayConfiguration().toString();
233         print(context, RenderService.NEWLINE);
234         print(context, "WebStudio.config = " + jsonConfigString + ";");
235         print(context, RenderService.NEWLINE);
236     }
237 }