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.struts.ActionConstants;
30  import com.liferay.portal.theme.ThemeDisplay;
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  import javax.servlet.jsp.PageContext;
49  
50  import org.apache.struts.action.Action;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  import org.dom4j.Document;
56  import org.dom4j.DocumentFactory;
57  import org.dom4j.Element;
58  import org.dom4j.ProcessingInstruction;
59  import org.dom4j.io.SAXReader;
60  
61  /**
62   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Raymond Augé
65   *
66   */
67  public class GetArticleAction extends Action {
68  
69      public ActionForward execute(
70              ActionMapping mapping, ActionForm form, HttpServletRequest req,
71              HttpServletResponse res)
72          throws Exception {
73  
74          try {
75              long groupId = ParamUtil.getLong(req, "groupId");
76              String articleId =  ParamUtil.getString(req, "articleId");
77  
78              String languageId = LanguageUtil.getLanguageId(req);
79  
80              JournalArticle article =
81                  JournalArticleLocalServiceUtil.getLatestArticle(
82                      groupId, articleId, Boolean.TRUE);
83  
84              ThemeDisplay themeDisplay =
85                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
86  
87              Map tokens = JournalUtil.getTokens(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[] byteArray = xml.getBytes();
115 
116             ServletResponseUtil.sendFile(res, fileName, byteArray, contentType);
117 
118             return null;
119         }
120         catch (Exception e) {
121             req.setAttribute(PageContext.EXCEPTION, e);
122 
123             return mapping.findForward(ActionConstants.COMMON_ERROR);
124         }
125     }
126 
127     protected void addProcessingInstructions(
128         Document doc, ThemeDisplay themeDisplay, JournalArticle article) {
129 
130         // Add style sheets in the reverse order that they appear in the
131         // document
132 
133         // Theme CSS
134 
135         String url =
136             themeDisplay.getPathThemeCss() + "/main.css?companyId=" +
137                 themeDisplay.getCompanyId() + "&themeId=" +
138                     themeDisplay.getThemeId() + "&colorSchemeId=" +
139                         themeDisplay.getColorSchemeId();
140 
141         Map arguments = new LinkedHashMap();
142 
143         arguments.put("type", "text/css");
144         arguments.put("href", url);
145         arguments.put("title", "theme css");
146 
147         addStyleSheet(doc, url, arguments);
148 
149         // CSS cached
150 
151         url =
152             themeDisplay.getPathMain() + "/portal/css_cached?themeId=" +
153                 themeDisplay.getThemeId() + "&colorSchemeId=" +
154                     themeDisplay.getColorSchemeId();
155 
156         arguments.clear();
157 
158         arguments.put("type", "text/css");
159         arguments.put("href", url);
160         arguments.put("title", "cached css");
161         arguments.put("alternate", "yes");
162 
163         addStyleSheet(doc, url, arguments);
164 
165         // XSL template
166 
167         String templateId = article.getTemplateId();
168 
169         if (Validator.isNotNull(templateId)) {
170             JournalTemplate template = null;
171 
172             try {
173                 template = JournalTemplateLocalServiceUtil.getTemplate(
174                     article.getGroupId(), templateId);
175 
176                 if (Validator.equals(
177                         template.getLangType(),
178                         JournalTemplateImpl.LANG_TYPE_XSL)) {
179 
180                     url =
181                         themeDisplay.getPathMain() +
182                             "/journal/get_template?groupId=" +
183                                 article.getGroupId() + "&templateId=" +
184                                     templateId;
185 
186                     arguments.clear();
187 
188                     arguments.put("type", "text/xsl");
189                     arguments.put("href", url);
190                     arguments.put("title", "xsl");
191 
192                     addStyleSheet(doc, url, arguments);
193                 }
194             }
195             catch (Exception e) {
196             }
197         }
198     }
199 
200     protected void addStyleSheet(Document doc, String url, Map arguments) {
201         List content = doc.content();
202 
203         ProcessingInstruction pi =
204             DocumentFactory.getInstance().createProcessingInstruction(
205                 "xml-stylesheet", arguments);
206 
207         content.add(0, pi);
208     }
209 
210 }