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.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.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.journal.model.JournalArticle;
33  import com.liferay.portlet.journal.model.JournalTemplate;
34  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
35  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
36  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
37  import com.liferay.portlet.journal.util.JournalUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.io.StringReader;
41  
42  import java.util.LinkedHashMap;
43  import java.util.List;
44  import java.util.Map;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
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 request,
70              HttpServletResponse response)
71          throws Exception {
72  
73          try {
74              long groupId = ParamUtil.getLong(request, "groupId");
75              String articleId =  ParamUtil.getString(request, "articleId");
76  
77              String languageId = LanguageUtil.getLanguageId(request);
78  
79              JournalArticle article =
80                  JournalArticleLocalServiceUtil.getLatestArticle(
81                      groupId, articleId, Boolean.TRUE);
82  
83              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84                  WebKeys.THEME_DISPLAY);
85  
86              Map<String, String> tokens = JournalUtil.getTokens(
87                  groupId, themeDisplay);
88  
89              String xml = article.getContentByLocale(languageId);
90  
91              SAXReader reader = new SAXReader();
92  
93              Document doc = reader.read(new StringReader(xml));
94  
95              Element root = doc.getRootElement();
96  
97              addProcessingInstructions(doc, themeDisplay, article);
98  
99              JournalUtil.addAllReservedEls(root, tokens, article);
100 
101             xml = JournalUtil.formatXML(doc);
102 
103             String contentType = ContentTypes.TEXT_XML_UTF8;
104 
105             String fileName = null;
106             byte[] bytes = xml.getBytes();
107 
108             ServletResponseUtil.sendFile(
109                 response, fileName, bytes, contentType);
110 
111             return null;
112         }
113         catch (Exception e) {
114             PortalUtil.sendError(e, request, response);
115 
116             return null;
117         }
118     }
119 
120     protected void addProcessingInstructions(
121         Document doc, ThemeDisplay themeDisplay, JournalArticle article) {
122 
123         // Add style sheets in the reverse order that they appear in the
124         // document
125 
126         // Theme CSS
127 
128         String url =
129             themeDisplay.getPathThemeCss() + "/main.css?companyId=" +
130                 themeDisplay.getCompanyId() + "&themeId=" +
131                     themeDisplay.getThemeId() + "&colorSchemeId=" +
132                         themeDisplay.getColorSchemeId();
133 
134         Map<String, String> arguments = new LinkedHashMap<String, String>();
135 
136         arguments.put("type", "text/css");
137         arguments.put("href", url);
138         arguments.put("title", "theme css");
139 
140         addStyleSheet(doc, url, arguments);
141 
142         // CSS cached
143 
144         url =
145             themeDisplay.getPathMain() + "/portal/css_cached?themeId=" +
146                 themeDisplay.getThemeId() + "&colorSchemeId=" +
147                     themeDisplay.getColorSchemeId();
148 
149         arguments.clear();
150 
151         arguments.put("type", "text/css");
152         arguments.put("href", url);
153         arguments.put("title", "cached css");
154         arguments.put("alternate", "yes");
155 
156         addStyleSheet(doc, url, arguments);
157 
158         // XSL template
159 
160         String templateId = article.getTemplateId();
161 
162         if (Validator.isNotNull(templateId)) {
163             JournalTemplate template = null;
164 
165             try {
166                 template = JournalTemplateLocalServiceUtil.getTemplate(
167                     article.getGroupId(), templateId);
168 
169                 if (Validator.equals(
170                         template.getLangType(),
171                         JournalTemplateImpl.LANG_TYPE_XSL)) {
172 
173                     url =
174                         themeDisplay.getPathMain() +
175                             "/journal/get_template?groupId=" +
176                                 article.getGroupId() + "&templateId=" +
177                                     templateId;
178 
179                     arguments.clear();
180 
181                     arguments.put("type", "text/xsl");
182                     arguments.put("href", url);
183                     arguments.put("title", "xsl");
184 
185                     addStyleSheet(doc, url, arguments);
186                 }
187             }
188             catch (Exception e) {
189             }
190         }
191     }
192 
193     protected void addStyleSheet(
194         Document doc, String url, Map<String, String> arguments) {
195 
196         List<Object> content = doc.content();
197 
198         ProcessingInstruction pi =
199             DocumentFactory.getInstance().createProcessingInstruction(
200                 "xml-stylesheet", arguments);
201 
202         content.add(0, pi);
203     }
204 
205 }