001
014
015 package com.liferay.portlet.journal.action;
016
017 import com.liferay.portal.kernel.language.LanguageUtil;
018 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
019 import com.liferay.portal.kernel.util.ContentTypes;
020 import com.liferay.portal.kernel.util.ParamUtil;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.theme.ThemeDisplay;
023 import com.liferay.portal.util.PortalUtil;
024 import com.liferay.portal.util.WebKeys;
025 import com.liferay.portlet.journal.model.JournalTemplate;
026 import com.liferay.portlet.journal.model.JournalTemplateConstants;
027 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
028 import com.liferay.portlet.journal.util.JournalUtil;
029
030 import java.util.Map;
031
032 import javax.servlet.http.HttpServletRequest;
033 import javax.servlet.http.HttpServletResponse;
034
035 import org.apache.struts.action.Action;
036 import org.apache.struts.action.ActionForm;
037 import org.apache.struts.action.ActionForward;
038 import org.apache.struts.action.ActionMapping;
039
040
044 public class GetTemplateAction extends Action {
045
046 @Override
047 public ActionForward execute(
048 ActionMapping mapping, ActionForm form, HttpServletRequest request,
049 HttpServletResponse response)
050 throws Exception {
051
052 try {
053 long groupId = ParamUtil.getLong(request, "groupId");
054 String templateId = getTemplateId(request);
055
056 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057 WebKeys.THEME_DISPLAY);
058
059 Map<String, String> tokens = JournalUtil.getTokens(
060 groupId, themeDisplay);
061
062 tokens.put("template_id", templateId);
063
064 String languageId = LanguageUtil.getLanguageId(request);
065
066 boolean transform = ParamUtil.getBoolean(
067 request, "transform", true);
068
069 JournalTemplate template =
070 JournalTemplateLocalServiceUtil.getTemplate(
071 groupId, templateId);
072
073 String script = JournalUtil.getTemplateScript(
074 template, tokens, languageId, transform);
075
076 String extension = JournalTemplateConstants.LANG_TYPE_VM;
077
078 if (template.getLangType() != null) {
079 extension = template.getLangType();
080 }
081
082 String fileName = null;
083 byte[] bytes = script.getBytes();
084
085 String contentType = ContentTypes.TEXT_PLAIN_UTF8;
086
087 if (Validator.equals(
088 extension, JournalTemplateConstants.LANG_TYPE_CSS)) {
089
090 contentType = ContentTypes.TEXT_CSS_UTF8;
091 }
092 else if (Validator.equals(
093 extension, JournalTemplateConstants.LANG_TYPE_XSL)) {
094
095 contentType = ContentTypes.TEXT_XML_UTF8;
096 }
097
098 ServletResponseUtil.sendFile(
099 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
114
115 if (Validator.isNull(templateId)) {
116 templateId = ParamUtil.getString(request, "template_id");
117 }
118
119 return templateId;
120 }
121
122 }