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