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