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