1
22
23 package com.liferay.portlet.journal.action;
24
25 import com.liferay.portal.kernel.language.LanguageUtil;
26 import com.liferay.portal.kernel.util.ContentTypes;
27 import com.liferay.portal.kernel.util.ParamUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.struts.ActionConstants;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.WebKeys;
32 import com.liferay.portlet.journal.model.JournalTemplate;
33 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
34 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
35 import com.liferay.portlet.journal.util.JournalUtil;
36 import com.liferay.util.servlet.ServletResponseUtil;
37
38 import java.util.Map;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.jsp.PageContext;
43
44 import org.apache.struts.action.Action;
45 import org.apache.struts.action.ActionForm;
46 import org.apache.struts.action.ActionForward;
47 import org.apache.struts.action.ActionMapping;
48
49
56 public class GetTemplateAction extends Action {
57
58 public ActionForward execute(
59 ActionMapping mapping, ActionForm form, HttpServletRequest req,
60 HttpServletResponse res)
61 throws Exception {
62
63 try {
64 long groupId = ParamUtil.getLong(req, "groupId");
65 String templateId = getTemplateId(req);
66
67 ThemeDisplay themeDisplay =
68 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
69
70 Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
71
72 tokens.put("template_id", templateId);
73
74 String languageId = LanguageUtil.getLanguageId(req);
75
76 boolean transform = ParamUtil.get(req, "transform", true);
77
78 JournalTemplate template =
79 JournalTemplateLocalServiceUtil.getTemplate(
80 groupId, templateId);
81
82 String script = JournalUtil.getTemplateScript(
83 template, tokens, languageId, transform);
84
85 String extension = JournalTemplateImpl.LANG_TYPE_VM;
86
87 if (template.getLangType() != null) {
88 extension = template.getLangType();
89 }
90
91 String fileName = null;
92 byte[] byteArray = script.getBytes();
93
94 String contentType = ContentTypes.TEXT_PLAIN_UTF8;
95
96 if (Validator.equals(
97 extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
98
99 contentType = ContentTypes.TEXT_CSS_UTF8;
100 }
101 else if (Validator.equals(
102 extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
103
104 contentType = ContentTypes.TEXT_XML_UTF8;
105 }
106
107 ServletResponseUtil.sendFile(res, fileName, byteArray, contentType);
108
109 return null;
110 }
111 catch (Exception e) {
112 req.setAttribute(PageContext.EXCEPTION, e);
113
114 return mapping.findForward(ActionConstants.COMMON_ERROR);
115 }
116 }
117
118 protected String getTemplateId(HttpServletRequest req) {
119 String templateId = ParamUtil.getString(req, "templateId");
120
121
123 if (Validator.isNull(templateId)) {
124 templateId = ParamUtil.getString(req, "template_id");
125 }
126
127 return templateId;
128 }
129
130 }