1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.workflow.StatusConstants;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.Node;
25  import com.liferay.portal.kernel.xml.ProcessingInstruction;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.journal.model.JournalArticle;
31  import com.liferay.portlet.journal.model.JournalTemplate;
32  import com.liferay.portlet.journal.model.JournalTemplateConstants;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
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.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Raymond Augé
54   */
55  public class GetArticleAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              long groupId = ParamUtil.getLong(request, "groupId");
64              String articleId =  ParamUtil.getString(request, "articleId");
65  
66              String languageId = LanguageUtil.getLanguageId(request);
67  
68              JournalArticle article =
69                  JournalArticleLocalServiceUtil.getLatestArticle(
70                      groupId, articleId, StatusConstants.APPROVED);
71  
72              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73                  WebKeys.THEME_DISPLAY);
74  
75              Map<String, String> tokens = JournalUtil.getTokens(
76                  groupId, themeDisplay);
77  
78              String xml = article.getContentByLocale(languageId);
79  
80              Document doc = SAXReaderUtil.read(xml);
81  
82              Element root = doc.getRootElement();
83  
84              addProcessingInstructions(doc, request, themeDisplay, article);
85  
86              JournalUtil.addAllReservedEls(root, tokens, article);
87  
88              xml = JournalUtil.formatXML(doc);
89  
90              String contentType = ContentTypes.TEXT_XML_UTF8;
91  
92              String fileName = null;
93              byte[] bytes = xml.getBytes();
94  
95              ServletResponseUtil.sendFile(
96                  response, fileName, bytes, contentType);
97  
98              return null;
99          }
100         catch (Exception e) {
101             PortalUtil.sendError(e, request, response);
102 
103             return null;
104         }
105     }
106 
107     protected void addProcessingInstructions(
108         Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
109         JournalArticle article) {
110 
111         // Add style sheets in the reverse order that they appear in the
112         // document
113 
114         // Portal CSS
115 
116         String url =
117             PortalUtil.getStaticResourceURL(
118                 request,
119                 themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
120                     "/html/portal/css.jsp");
121 
122         Map<String, String> arguments = new LinkedHashMap<String, String>();
123 
124         arguments.put("type", "text/css");
125         arguments.put("href", url);
126         arguments.put("title", "portal css");
127         arguments.put("alternate", "yes");
128 
129         addStyleSheet(doc, url, arguments);
130 
131         // Theme CSS
132 
133         url = PortalUtil.getStaticResourceURL(
134             request, themeDisplay.getPathThemeCss() + "/main.css");
135 
136         arguments.clear();
137 
138         arguments.put("type", "text/css");
139         arguments.put("href", url);
140         arguments.put("title", "theme css");
141 
142         addStyleSheet(doc, url, arguments);
143 
144         // XSL template
145 
146         String templateId = article.getTemplateId();
147 
148         if (Validator.isNotNull(templateId)) {
149             JournalTemplate template = null;
150 
151             try {
152                 template = JournalTemplateLocalServiceUtil.getTemplate(
153                     article.getGroupId(), templateId);
154 
155                 if (Validator.equals(
156                         template.getLangType(),
157                         JournalTemplateConstants.LANG_TYPE_XSL)) {
158 
159                     url =
160                         themeDisplay.getPathMain() +
161                             "/journal/get_template?groupId=" +
162                                 article.getGroupId() + "&templateId=" +
163                                     templateId;
164 
165                     arguments.clear();
166 
167                     arguments.put("type", "text/xsl");
168                     arguments.put("href", url);
169                     arguments.put("title", "xsl");
170 
171                     addStyleSheet(doc, url, arguments);
172                 }
173             }
174             catch (Exception e) {
175             }
176         }
177     }
178 
179     protected void addStyleSheet(
180         Document doc, String url, Map<String, String> arguments) {
181 
182         List<Node> content = doc.content();
183 
184         ProcessingInstruction processingInstruction =
185             SAXReaderUtil.createProcessingInstruction(
186                 "xml-stylesheet", arguments);
187 
188         content.add(0, processingInstruction);
189     }
190 
191 }