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.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  /**
58   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Raymond Augé
61   */
62  public class GetArticleAction extends Action {
63  
64      public ActionForward execute(
65              ActionMapping mapping, ActionForm form, HttpServletRequest request,
66              HttpServletResponse response)
67          throws Exception {
68  
69          try {
70              long groupId = ParamUtil.getLong(request, "groupId");
71              String articleId =  ParamUtil.getString(request, "articleId");
72  
73              String languageId = LanguageUtil.getLanguageId(request);
74  
75              JournalArticle article =
76                  JournalArticleLocalServiceUtil.getLatestArticle(
77                      groupId, articleId, Boolean.TRUE);
78  
79              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
80                  WebKeys.THEME_DISPLAY);
81  
82              Map<String, String> tokens = JournalUtil.getTokens(
83                  groupId, themeDisplay);
84  
85              String xml = article.getContentByLocale(languageId);
86  
87              Document doc = SAXReaderUtil.read(xml);
88  
89              Element root = doc.getRootElement();
90  
91              addProcessingInstructions(doc, request, themeDisplay, article);
92  
93              JournalUtil.addAllReservedEls(root, tokens, article);
94  
95              xml = JournalUtil.formatXML(doc);
96  
97              String contentType = ContentTypes.TEXT_XML_UTF8;
98  
99              String fileName = null;
100             byte[] bytes = xml.getBytes();
101 
102             ServletResponseUtil.sendFile(
103                 response, fileName, bytes, contentType);
104 
105             return null;
106         }
107         catch (Exception e) {
108             PortalUtil.sendError(e, request, response);
109 
110             return null;
111         }
112     }
113 
114     protected void addProcessingInstructions(
115         Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
116         JournalArticle article) {
117 
118         // Add style sheets in the reverse order that they appear in the
119         // document
120 
121         // Portal CSS
122 
123         StringBuilder sb = new StringBuilder();
124 
125         sb.append(
126             PortalUtil.getStaticResourceURL(
127                 request,
128                 themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
129                     "/html/portal/css.jsp"));
130 
131         String url = sb.toString();
132 
133         Map<String, String> arguments = new LinkedHashMap<String, String>();
134 
135         arguments.put("type", "text/css");
136         arguments.put("href", url);
137         arguments.put("title", "portal css");
138         arguments.put("alternate", "yes");
139 
140         addStyleSheet(doc, url, arguments);
141 
142         // Theme CSS
143 
144         sb = new StringBuilder();
145 
146         sb.append(
147             PortalUtil.getStaticResourceURL(
148                 request, themeDisplay.getPathThemeCss() + "/main.css"));
149 
150         url = sb.toString();
151 
152         arguments.clear();
153 
154         arguments.put("type", "text/css");
155         arguments.put("href", url);
156         arguments.put("title", "theme css");
157 
158         addStyleSheet(doc, url, arguments);
159 
160         // XSL template
161 
162         String templateId = article.getTemplateId();
163 
164         if (Validator.isNotNull(templateId)) {
165             JournalTemplate template = null;
166 
167             try {
168                 template = JournalTemplateLocalServiceUtil.getTemplate(
169                     article.getGroupId(), templateId);
170 
171                 if (Validator.equals(
172                         template.getLangType(),
173                         JournalTemplateImpl.LANG_TYPE_XSL)) {
174 
175                     url =
176                         themeDisplay.getPathMain() +
177                             "/journal/get_template?groupId=" +
178                                 article.getGroupId() + "&templateId=" +
179                                     templateId;
180 
181                     arguments.clear();
182 
183                     arguments.put("type", "text/xsl");
184                     arguments.put("href", url);
185                     arguments.put("title", "xsl");
186 
187                     addStyleSheet(doc, url, arguments);
188                 }
189             }
190             catch (Exception e) {
191             }
192         }
193     }
194 
195     protected void addStyleSheet(
196         Document doc, String url, Map<String, String> arguments) {
197 
198         List<Node> content = doc.content();
199 
200         ProcessingInstruction processingInstruction =
201             SAXReaderUtil.createProcessingInstruction(
202                 "xml-stylesheet", arguments);
203 
204         content.add(0, processingInstruction);
205     }
206 
207 }