1   /**
2    * Copyright (c) 2000-2009 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, request, 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, HttpServletRequest request, ThemeDisplay themeDisplay,
117         JournalArticle article) {
118 
119         // Add style sheets in the reverse order that they appear in the
120         // document
121 
122         // Portal CSS
123 
124         StringBuilder sb = new StringBuilder();
125 
126         sb.append(
127             PortalUtil.getStaticResourceURL(
128                 request,
129                 themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
130                     "/html/portal/css.jsp"));
131 
132         String url = sb.toString();
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", "portal css");
139         arguments.put("alternate", "yes");
140 
141         addStyleSheet(doc, url, arguments);
142 
143         // Theme CSS
144 
145         sb = new StringBuilder();
146 
147         sb.append(
148             PortalUtil.getStaticResourceURL(
149                 request, themeDisplay.getPathThemeCss() + "/main.css"));
150 
151         url = sb.toString();
152 
153         arguments.clear();
154 
155         arguments.put("type", "text/css");
156         arguments.put("href", url);
157         arguments.put("title", "theme css");
158 
159         addStyleSheet(doc, url, arguments);
160 
161         // XSL template
162 
163         String templateId = article.getTemplateId();
164 
165         if (Validator.isNotNull(templateId)) {
166             JournalTemplate template = null;
167 
168             try {
169                 template = JournalTemplateLocalServiceUtil.getTemplate(
170                     article.getGroupId(), templateId);
171 
172                 if (Validator.equals(
173                         template.getLangType(),
174                         JournalTemplateImpl.LANG_TYPE_XSL)) {
175 
176                     url =
177                         themeDisplay.getPathMain() +
178                             "/journal/get_template?groupId=" +
179                                 article.getGroupId() + "&templateId=" +
180                                     templateId;
181 
182                     arguments.clear();
183 
184                     arguments.put("type", "text/xsl");
185                     arguments.put("href", url);
186                     arguments.put("title", "xsl");
187 
188                     addStyleSheet(doc, url, arguments);
189                 }
190             }
191             catch (Exception e) {
192             }
193         }
194     }
195 
196     protected void addStyleSheet(
197         Document doc, String url, Map<String, String> arguments) {
198 
199         List<Node> content = doc.content();
200 
201         ProcessingInstruction processingInstruction =
202             SAXReaderUtil.createProcessingInstruction(
203                 "xml-stylesheet", arguments);
204 
205         content.add(0, processingInstruction);
206     }
207 
208 }