root/head/ambra/webapp/src/main/java/org/topazproject/ambra/struts2/AmbraFreemarkerResult.java @ 7771

Revision 7771, 3.9 KB (checked in by dragisak, 14 months ago)

Remove the need to build journal template jar files. Instead, templates are placed under directory specified in ambra.virtualJournals.templateDir configuration paramater.

  • Each journal is placed under journals/journal-name
  • Journal configuration file is placed in journals/journal-name/configuration/journal.xml
  • Freemarker templates and static resources (images, css, javascript, etc) are placed under journals/journal-name/webapp

Deployment can be done directly from version control system if ambra.virtualJournals.templateDir is set to point to source head directory.

Addresses #919

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id HeadURL Revision
Line 
1/* $HeadURL::                                                                            $
2 * $Id$
3 *
4 * Copyright (c) 2006-2009 by Topaz, Inc.
5 * http://topazproject.org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.topazproject.ambra.struts2;
21
22import java.io.IOException;
23
24import javax.servlet.http.HttpServletResponse;
25
26import org.apache.struts2.ServletActionContext;
27import org.apache.struts2.views.freemarker.FreemarkerResult;
28import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30import org.topazproject.ambra.web.VirtualJournalContext;
31
32import freemarker.template.SimpleHash;
33import freemarker.template.TemplateModelException;
34import freemarker.ext.servlet.HttpRequestHashModel;
35
36/**
37 * Custom Freemarker Result class so that we can pass the templateFile name into the template
38 * in order to have a limited number of templates for the system.
39 *
40 * @author Stephen Cheng
41 *
42 */
43public class AmbraFreemarkerResult extends FreemarkerResult {
44  private static final Log log = LogFactory.getLog(AmbraFreemarkerResult.class);
45  private String templateFile;
46  private boolean noCache = false;
47
48  /**
49   * @return Returns the templateFile.
50   */
51  public String getTemplateFile() {
52    return templateFile;
53  }
54
55  /**
56   * @param templateFile The templateFile to set.
57   */
58  public void setTemplateFile(String templateFile) {
59    this.templateFile = templateFile;
60  }
61
62  /**
63   * Add journal context path at the beginning of each Freemarker template
64   * @param template template
65   * @param model model
66   * @return super.preTemplateProcess(template, model)
67   * @throws IOException
68   */
69  protected boolean preTemplateProcess(freemarker.template.Template template,
70                                       freemarker.template.TemplateModel model) throws IOException {
71    SimpleHash modelHash = (SimpleHash) model;
72    String templateFileName = templateFile;
73
74    if (templateFile != null && !templateFile.startsWith("/journals/")) {
75      try {
76        HttpRequestHashModel requestModel = (HttpRequestHashModel)modelHash.get("Request");
77        final VirtualJournalContext virtualJournalContext = (VirtualJournalContext) requestModel
78          .getRequest()
79          .getAttribute(VirtualJournalContext.PUB_VIRTUALJOURNAL_CONTEXT);
80        if (virtualJournalContext != null) {
81          templateFileName = "/journals/" + virtualJournalContext.getJournal() + templateFile;
82          if (log.isDebugEnabled()) {
83            log.debug("Changed "+templateFile+" to "+templateFileName);
84          }
85        }
86
87      } catch (TemplateModelException e) {
88        throw new RuntimeException("Error in preTemplateProcess for "+templateFile, e);
89      }
90    }
91
92    modelHash.put("templateFile", templateFileName);
93    if (noCache) {
94      HttpServletResponse response = ServletActionContext.getResponse();
95      // HTTP 1.1 browsers should defeat caching on this header
96      response.setHeader("Cache-Control", "no-cache");
97      // HTTP 1.0 browsers should defeat caching on this header
98      response.setHeader("Pragma", "no-cache");
99      // Last resort for those that ignore all of the above
100      response.setHeader("Expires", "-1");
101    }
102    return super.preTemplateProcess(template, model);
103  }
104
105  /**
106   * @return Returns the noCache.
107   */
108  public boolean getNoCache() {
109    return noCache;
110  }
111
112  /**
113   * @param noCache The noCache to set.
114   */
115  public void setNoCache(boolean noCache) {
116    this.noCache = noCache;
117  }
118}
Note: See TracBrowser for help on using the browser.