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              String contentType = ContentTypes.TEXT_HTML_UTF8;
92  
93              Document doc = null;
94  
95              Element root = null;
96  
97              if (article.isTemplateDriven()) {
98                  SAXReader reader = new SAXReader();
99  
100                 doc = reader.read(new StringReader(xml));
101 
102                 root = doc.getRootElement();
103 
104                 addProcessingInstructions(doc, themeDisplay, article);
105 
106                 JournalUtil.addAllReservedEls(root, tokens, article);
107 
108                 xml = JournalUtil.formatXML(doc);
109 
110                 contentType = ContentTypes.TEXT_XML_UTF8;
111             }
112 
113             String fileName = null;
114             byte[] bytes = xml.getBytes();
115 
116             ServletResponseUtil.sendFile(
117                 response, fileName, bytes, contentType);
118 
119             return null;
120         }
121         catch (Exception e) {
122             PortalUtil.sendError(e, request, response);
123 
124             return null;
125         }
126     }
127 
128     protected void addProcessingInstructions(
129         Document doc, ThemeDisplay themeDisplay, JournalArticle article) {
130 
131         // Add style sheets in the reverse order that they appear in the
132         // document
133 
134         // Theme CSS
135 
136         String url =
137             themeDisplay.getPathThemeCss() + "/main.css?companyId=" +
138                 themeDisplay.getCompanyId() + "&themeId=" +
139                     themeDisplay.getThemeId() + "&colorSchemeId=" +
140                         themeDisplay.getColorSchemeId();
141 
142         Map<String, String> arguments = new LinkedHashMap<String, String>();
143 
144         arguments.put("type", "text/css");
145         arguments.put("href", url);
146         arguments.put("title", "theme css");
147 
148         addStyleSheet(doc, url, arguments);
149 
150         // CSS cached
151 
152         url =
153             themeDisplay.getPathMain() + "/portal/css_cached?themeId=" +
154                 themeDisplay.getThemeId() + "&colorSchemeId=" +
155                     themeDisplay.getColorSchemeId();
156 
157         arguments.clear();
158 
159         arguments.put("type", "text/css");
160         arguments.put("href", url);
161         arguments.put("title", "cached css");
162         arguments.put("alternate", "yes");
163 
164         addStyleSheet(doc, url, arguments);
165 
166         // XSL template
167 
168         String templateId = article.getTemplateId();
169 
170         if (Validator.isNotNull(templateId)) {
171             JournalTemplate template = null;
172 
173             try {
174                 template = JournalTemplateLocalServiceUtil.getTemplate(
175                     article.getGroupId(), templateId);
176 
177                 if (Validator.equals(
178                         template.getLangType(),
179                         JournalTemplateImpl.LANG_TYPE_XSL)) {
180 
181                     url =
182                         themeDisplay.getPathMain() +
183                             "/journal/get_template?groupId=" +
184                                 article.getGroupId() + "&templateId=" +
185                                     templateId;
186 
187                     arguments.clear();
188 
189                     arguments.put("type", "text/xsl");
190                     arguments.put("href", url);
191                     arguments.put("title", "xsl");
192 
193                     addStyleSheet(doc, url, arguments);
194                 }
195             }
196             catch (Exception e) {
197             }
198         }
199     }
200 
201     protected void addStyleSheet(
202         Document doc, String url, Map<String, String> arguments) {
203 
204         List<Object> content = doc.content();
205 
206         ProcessingInstruction pi =
207             DocumentFactory.getInstance().createProcessingInstruction(
208                 "xml-stylesheet", arguments);
209 
210         content.add(0, pi);
211     }
212 
213 }