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.theme.ThemeDisplay;
30 import com.liferay.portal.util.PortalUtil;
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
43 import org.apache.struts.action.Action;
44 import org.apache.struts.action.ActionForm;
45 import org.apache.struts.action.ActionForward;
46 import org.apache.struts.action.ActionMapping;
47
48
55 public class GetTemplateAction extends Action {
56
57 public ActionForward execute(
58 ActionMapping mapping, ActionForm form, HttpServletRequest request,
59 HttpServletResponse response)
60 throws Exception {
61
62 try {
63 long groupId = ParamUtil.getLong(request, "groupId");
64 String templateId = getTemplateId(request);
65
66 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67 WebKeys.THEME_DISPLAY);
68
69 Map<String, String> tokens = JournalUtil.getTokens(
70 groupId, themeDisplay);
71
72 tokens.put("template_id", templateId);
73
74 String languageId = LanguageUtil.getLanguageId(request);
75
76 boolean transform = ParamUtil.get(request, "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[] bytes = 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(
108 response, fileName, bytes, contentType);
109
110 return null;
111 }
112 catch (Exception e) {
113 PortalUtil.sendError(e, request, response);
114
115 return null;
116 }
117 }
118
119 protected String getTemplateId(HttpServletRequest request) {
120 String templateId = ParamUtil.getString(request, "templateId");
121
122
124 if (Validator.isNull(templateId)) {
125 templateId = ParamUtil.getString(request, "template_id");
126 }
127
128 return templateId;
129 }
130
131 }