1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.model.Theme;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.journal.model.JournalArticle;
39  import com.liferay.portlet.journal.model.JournalTemplate;
40  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
43  import com.liferay.portlet.journal.util.JournalUtil;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.util.LinkedHashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.struts.action.Action;
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Raymond Augé
62   *
63   */
64  public class GetArticleAction extends Action {
65  
66      public ActionForward execute(
67              ActionMapping mapping, ActionForm form, HttpServletRequest request,
68              HttpServletResponse response)
69          throws Exception {
70  
71          try {
72              long groupId = ParamUtil.getLong(request, "groupId");
73              String articleId =  ParamUtil.getString(request, "articleId");
74  
75              String languageId = LanguageUtil.getLanguageId(request);
76  
77              JournalArticle article =
78                  JournalArticleLocalServiceUtil.getLatestArticle(
79                      groupId, articleId, Boolean.TRUE);
80  
81              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
82                  WebKeys.THEME_DISPLAY);
83  
84              Map<String, String> tokens = JournalUtil.getTokens(
85                  groupId, themeDisplay);
86  
87              String xml = article.getContentByLocale(languageId);
88  
89              Document doc = SAXReaderUtil.read(xml);
90  
91              Element root = doc.getRootElement();
92  
93              addProcessingInstructions(doc, request, themeDisplay, article);
94  
95              JournalUtil.addAllReservedEls(root, tokens, article);
96  
97              xml = JournalUtil.formatXML(doc);
98  
99              String contentType = ContentTypes.TEXT_XML_UTF8;
100 
101             String fileName = null;
102             byte[] bytes = xml.getBytes();
103 
104             ServletResponseUtil.sendFile(
105                 response, fileName, bytes, contentType);
106 
107             return null;
108         }
109         catch (Exception e) {
110             PortalUtil.sendError(e, request, response);
111 
112             return null;
113         }
114     }
115 
116     protected void addProcessingInstructions(
117         Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
118         JournalArticle article) {
119 
120         // Add style sheets in the reverse order that they appear in the
121         // document
122 
123         // Portal CSS
124 
125         Theme theme = themeDisplay.getTheme();
126 
127         StringBuilder sb = new StringBuilder();
128 
129         sb.append(
130             PortalUtil.getStaticResourceURL(
131                 request,
132                 themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
133                     "/html/portal/css.jsp"));
134 
135         String url = sb.toString();
136 
137         Map<String, String> arguments = new LinkedHashMap<String, String>();
138 
139         arguments.put("type", "text/css");
140         arguments.put("href", url);
141         arguments.put("title", "portal css");
142         arguments.put("alternate", "yes");
143 
144         addStyleSheet(doc, url, arguments);
145 
146         // Theme CSS
147 
148         sb = new StringBuilder();
149 
150         sb.append(
151             PortalUtil.getStaticResourceURL(
152                 request, themeDisplay.getPathThemeCss() + "/main.css"));
153 
154         url = sb.toString();
155 
156         arguments.clear();
157 
158         arguments.put("type", "text/css");
159         arguments.put("href", url);
160         arguments.put("title", "theme css");
161 
162         addStyleSheet(doc, url, arguments);
163 
164         // XSL template
165 
166         String templateId = article.getTemplateId();
167 
168         if (Validator.isNotNull(templateId)) {
169             JournalTemplate template = null;
170 
171             try {
172                 template = JournalTemplateLocalServiceUtil.getTemplate(
173                     article.getGroupId(), templateId);
174 
175                 if (Validator.equals(
176                         template.getLangType(),
177                         JournalTemplateImpl.LANG_TYPE_XSL)) {
178 
179                     url =
180                         themeDisplay.getPathMain() +
181                             "/journal/get_template?groupId=" +
182                                 article.getGroupId() + "&templateId=" +
183                                     templateId;
184 
185                     arguments.clear();
186 
187                     arguments.put("type", "text/xsl");
188                     arguments.put("href", url);
189                     arguments.put("title", "xsl");
190 
191                     addStyleSheet(doc, url, arguments);
192                 }
193             }
194             catch (Exception e) {
195             }
196         }
197     }
198 
199     protected void addStyleSheet(
200         Document doc, String url, Map<String, String> arguments) {
201 
202         List<Node> content = doc.content();
203 
204         ProcessingInstruction processingInstruction =
205             SAXReaderUtil.createProcessingInstruction(
206                 "xml-stylesheet", arguments);
207 
208         content.add(0, processingInstruction);
209     }
210 
211 }