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