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.kernel.xml.Document;
30 import com.liferay.portal.kernel.xml.Element;
31 import com.liferay.portal.kernel.xml.Node;
32 import com.liferay.portal.kernel.xml.ProcessingInstruction;
33 import com.liferay.portal.kernel.xml.SAXReaderUtil;
34 import com.liferay.portal.theme.ThemeDisplay;
35 import com.liferay.portal.util.PortalUtil;
36 import com.liferay.portal.util.WebKeys;
37 import com.liferay.portlet.journal.model.JournalArticle;
38 import com.liferay.portlet.journal.model.JournalTemplate;
39 import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
40 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
41 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
42 import com.liferay.portlet.journal.util.JournalUtil;
43 import com.liferay.util.servlet.ServletResponseUtil;
44
45 import java.util.LinkedHashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51
52 import org.apache.struts.action.Action;
53 import org.apache.struts.action.ActionForm;
54 import org.apache.struts.action.ActionForward;
55 import org.apache.struts.action.ActionMapping;
56
57
63 public class GetArticleAction extends Action {
64
65 public ActionForward execute(
66 ActionMapping mapping, ActionForm form, HttpServletRequest request,
67 HttpServletResponse response)
68 throws Exception {
69
70 try {
71 long groupId = ParamUtil.getLong(request, "groupId");
72 String articleId = ParamUtil.getString(request, "articleId");
73
74 String languageId = LanguageUtil.getLanguageId(request);
75
76 JournalArticle article =
77 JournalArticleLocalServiceUtil.getLatestArticle(
78 groupId, articleId, Boolean.TRUE);
79
80 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
81 WebKeys.THEME_DISPLAY);
82
83 Map<String, String> tokens = JournalUtil.getTokens(
84 groupId, themeDisplay);
85
86 String xml = article.getContentByLocale(languageId);
87
88 Document doc = SAXReaderUtil.read(xml);
89
90 Element root = doc.getRootElement();
91
92 addProcessingInstructions(doc, themeDisplay, article);
93
94 JournalUtil.addAllReservedEls(root, tokens, article);
95
96 xml = JournalUtil.formatXML(doc);
97
98 String contentType = ContentTypes.TEXT_XML_UTF8;
99
100 String fileName = null;
101 byte[] bytes = xml.getBytes();
102
103 ServletResponseUtil.sendFile(
104 response, fileName, bytes, contentType);
105
106 return null;
107 }
108 catch (Exception e) {
109 PortalUtil.sendError(e, request, response);
110
111 return null;
112 }
113 }
114
115 protected void addProcessingInstructions(
116 Document doc, ThemeDisplay themeDisplay, JournalArticle article) {
117
118
121
123 String url =
124 themeDisplay.getPathThemeCss() + "/main.css?companyId=" +
125 themeDisplay.getCompanyId() + "&themeId=" +
126 themeDisplay.getThemeId() + "&colorSchemeId=" +
127 themeDisplay.getColorSchemeId();
128
129 Map<String, String> arguments = new LinkedHashMap<String, String>();
130
131 arguments.put("type", "text/css");
132 arguments.put("href", url);
133 arguments.put("title", "theme css");
134
135 addStyleSheet(doc, url, arguments);
136
137
139 url =
140 themeDisplay.getPathMain() + "/portal/css_cached?themeId=" +
141 themeDisplay.getThemeId() + "&colorSchemeId=" +
142 themeDisplay.getColorSchemeId();
143
144 arguments.clear();
145
146 arguments.put("type", "text/css");
147 arguments.put("href", url);
148 arguments.put("title", "cached css");
149 arguments.put("alternate", "yes");
150
151 addStyleSheet(doc, url, arguments);
152
153
155 String templateId = article.getTemplateId();
156
157 if (Validator.isNotNull(templateId)) {
158 JournalTemplate template = null;
159
160 try {
161 template = JournalTemplateLocalServiceUtil.getTemplate(
162 article.getGroupId(), templateId);
163
164 if (Validator.equals(
165 template.getLangType(),
166 JournalTemplateImpl.LANG_TYPE_XSL)) {
167
168 url =
169 themeDisplay.getPathMain() +
170 "/journal/get_template?groupId=" +
171 article.getGroupId() + "&templateId=" +
172 templateId;
173
174 arguments.clear();
175
176 arguments.put("type", "text/xsl");
177 arguments.put("href", url);
178 arguments.put("title", "xsl");
179
180 addStyleSheet(doc, url, arguments);
181 }
182 }
183 catch (Exception e) {
184 }
185 }
186 }
187
188 protected void addStyleSheet(
189 Document doc, String url, Map<String, String> arguments) {
190
191 List<Node> content = doc.content();
192
193 ProcessingInstruction processingInstruction =
194 SAXReaderUtil.createProcessingInstruction(
195 "xml-stylesheet", arguments);
196
197 content.add(0, processingInstruction);
198 }
199
200 }