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