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
54 public class GetTemplateAction extends Action {
55
56 public ActionForward execute(
57 ActionMapping mapping, ActionForm form, HttpServletRequest request,
58 HttpServletResponse response)
59 throws Exception {
60
61 try {
62 long groupId = ParamUtil.getLong(request, "groupId");
63 String templateId = getTemplateId(request);
64
65 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66 WebKeys.THEME_DISPLAY);
67
68 Map<String, String> tokens = JournalUtil.getTokens(
69 groupId, themeDisplay);
70
71 tokens.put("template_id", templateId);
72
73 String languageId = LanguageUtil.getLanguageId(request);
74
75 boolean transform = ParamUtil.get(request, "transform", true);
76
77 JournalTemplate template =
78 JournalTemplateLocalServiceUtil.getTemplate(
79 groupId, templateId);
80
81 String script = JournalUtil.getTemplateScript(
82 template, tokens, languageId, transform);
83
84 String extension = JournalTemplateImpl.LANG_TYPE_VM;
85
86 if (template.getLangType() != null) {
87 extension = template.getLangType();
88 }
89
90 String fileName = null;
91 byte[] bytes = script.getBytes();
92
93 String contentType = ContentTypes.TEXT_PLAIN_UTF8;
94
95 if (Validator.equals(
96 extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
97
98 contentType = ContentTypes.TEXT_CSS_UTF8;
99 }
100 else if (Validator.equals(
101 extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
102
103 contentType = ContentTypes.TEXT_XML_UTF8;
104 }
105
106 ServletResponseUtil.sendFile(
107 response, fileName, bytes, contentType);
108
109 return null;
110 }
111 catch (Exception e) {
112 PortalUtil.sendError(e, request, response);
113
114 return null;
115 }
116 }
117
118 protected String getTemplateId(HttpServletRequest request) {
119 String templateId = ParamUtil.getString(request, "templateId");
120
121
123 if (Validator.isNull(templateId)) {
124 templateId = ParamUtil.getString(request, "template_id");
125 }
126
127 return templateId;
128 }
129
130 }