| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | package org.topazproject.ambra.struts2; |
|---|
| 21 | |
|---|
| 22 | import java.io.IOException; |
|---|
| 23 | |
|---|
| 24 | import javax.servlet.http.HttpServletResponse; |
|---|
| 25 | |
|---|
| 26 | import org.apache.struts2.ServletActionContext; |
|---|
| 27 | import org.apache.struts2.views.freemarker.FreemarkerResult; |
|---|
| 28 | import org.apache.commons.logging.Log; |
|---|
| 29 | import org.apache.commons.logging.LogFactory; |
|---|
| 30 | import org.topazproject.ambra.web.VirtualJournalContext; |
|---|
| 31 | |
|---|
| 32 | import freemarker.template.SimpleHash; |
|---|
| 33 | import freemarker.template.TemplateModelException; |
|---|
| 34 | import freemarker.ext.servlet.HttpRequestHashModel; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | public 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 | |
|---|
| 50 | |
|---|
| 51 | public String getTemplateFile() { |
|---|
| 52 | return templateFile; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | public void setTemplateFile(String templateFile) { |
|---|
| 59 | this.templateFile = templateFile; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 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 | |
|---|
| 96 | response.setHeader("Cache-Control", "no-cache"); |
|---|
| 97 | |
|---|
| 98 | response.setHeader("Pragma", "no-cache"); |
|---|
| 99 | |
|---|
| 100 | response.setHeader("Expires", "-1"); |
|---|
| 101 | } |
|---|
| 102 | return super.preTemplateProcess(template, model); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | public boolean getNoCache() { |
|---|
| 109 | return noCache; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | public void setNoCache(boolean noCache) { |
|---|
| 116 | this.noCache = noCache; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|