1   /**
2    * Copyright (c) 2000-2008 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.PortalException;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.journal.NoSuchArticleException;
36  import com.liferay.portlet.journal.model.JournalArticle;
37  import com.liferay.portlet.journal.model.JournalTemplate;
38  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
39  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
40  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
41  import com.liferay.portlet.journal.util.JournalUtil;
42  import com.liferay.util.servlet.ServletResponseUtil;
43  
44  import java.io.StringReader;
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  import javax.servlet.jsp.PageContext;
53  
54  import org.apache.struts.action.Action;
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  import org.dom4j.Document;
60  import org.dom4j.DocumentFactory;
61  import org.dom4j.Element;
62  import org.dom4j.ProcessingInstruction;
63  import org.dom4j.io.SAXReader;
64  
65  /**
66   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Raymond Augé
69   *
70   */
71  public class GetArticleAction extends Action {
72  
73      public ActionForward execute(
74              ActionMapping mapping, ActionForm form, HttpServletRequest req,
75              HttpServletResponse res)
76          throws Exception {
77  
78          try {
79              long groupId = ParamUtil.getLong(req, "groupId");
80              String articleId =  ParamUtil.getString(req, "articleId");
81  
82              String languageId = LanguageUtil.getLanguageId(req);
83  
84              JournalArticle article =
85                  JournalArticleLocalServiceUtil.getLatestArticle(
86                      groupId, articleId, Boolean.TRUE);
87  
88              ThemeDisplay themeDisplay =
89                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
90  
91              Map<String, String> tokens = JournalUtil.getTokens(
92                  groupId, themeDisplay);
93  
94              String xml = article.getContentByLocale(languageId);
95  
96              String contentType = ContentTypes.TEXT_HTML_UTF8;
97  
98              Document doc = null;
99  
100             Element root = null;
101 
102             if (article.isTemplateDriven()) {
103                 SAXReader reader = new SAXReader();
104 
105                 doc = reader.read(new StringReader(xml));
106 
107                 root = doc.getRootElement();
108 
109                 addProcessingInstructions(doc, themeDisplay, article);
110 
111                 JournalUtil.addAllReservedEls(root, tokens, article);
112 
113                 xml = JournalUtil.formatXML(doc);
114 
115                 contentType = ContentTypes.TEXT_XML_UTF8;
116             }
117 
118             String fileName = null;
119             byte[] byteArray = xml.getBytes();
120 
121             ServletResponseUtil.sendFile(res, fileName, byteArray, contentType);
122 
123             return null;
124         }
125         catch (PortalException pe) {
126             if (pe instanceof PrincipalException) {
127                 PortalUtil.sendError(
128                     HttpServletResponse.SC_FORBIDDEN, pe, req, res);
129             }
130             else if (pe instanceof NoSuchArticleException) {
131                 PortalUtil.sendError(
132                     HttpServletResponse.SC_NOT_FOUND, pe, req, res);
133             }
134 
135             return null;
136         }
137         catch (Exception e) {
138             req.setAttribute(PageContext.EXCEPTION, e);
139 
140             return mapping.findForward(ActionConstants.COMMON_ERROR);
141         }
142     }
143 
144     protected void addProcessingInstructions(
145         Document doc, ThemeDisplay themeDisplay, JournalArticle article) {
146 
147         // Add style sheets in the reverse order that they appear in the
148         // document
149 
150         // Theme CSS
151 
152         String url =
153             themeDisplay.getPathThemeCss() + "/main.css?companyId=" +
154                 themeDisplay.getCompanyId() + "&themeId=" +
155                     themeDisplay.getThemeId() + "&colorSchemeId=" +
156                         themeDisplay.getColorSchemeId();
157 
158         Map<String, String> arguments = new LinkedHashMap<String, String>();
159 
160         arguments.put("type", "text/css");
161         arguments.put("href", url);
162         arguments.put("title", "theme css");
163 
164         addStyleSheet(doc, url, arguments);
165 
166         // CSS cached
167 
168         url =
169             themeDisplay.getPathMain() + "/portal/css_cached?themeId=" +
170                 themeDisplay.getThemeId() + "&colorSchemeId=" +
171                     themeDisplay.getColorSchemeId();
172 
173         arguments.clear();
174 
175         arguments.put("type", "text/css");
176         arguments.put("href", url);
177         arguments.put("title", "cached css");
178         arguments.put("alternate", "yes");
179 
180         addStyleSheet(doc, url, arguments);
181 
182         // XSL template
183 
184         String templateId = article.getTemplateId();
185 
186         if (Validator.isNotNull(templateId)) {
187             JournalTemplate template = null;
188 
189             try {
190                 template = JournalTemplateLocalServiceUtil.getTemplate(
191                     article.getGroupId(), templateId);
192 
193                 if (Validator.equals(
194                         template.getLangType(),
195                         JournalTemplateImpl.LANG_TYPE_XSL)) {
196 
197                     url =
198                         themeDisplay.getPathMain() +
199                             "/journal/get_template?groupId=" +
200                                 article.getGroupId() + "&templateId=" +
201                                     templateId;
202 
203                     arguments.clear();
204 
205                     arguments.put("type", "text/xsl");
206                     arguments.put("href", url);
207                     arguments.put("title", "xsl");
208 
209                     addStyleSheet(doc, url, arguments);
210                 }
211             }
212             catch (Exception e) {
213             }
214         }
215     }
216 
217     protected void addStyleSheet(
218         Document doc, String url, Map<String, String> arguments) {
219 
220         List<Object> content = doc.content();
221 
222         ProcessingInstruction pi =
223             DocumentFactory.getInstance().createProcessingInstruction(
224                 "xml-stylesheet", arguments);
225 
226         content.add(0, pi);
227     }
228 
229 }