1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.xml.Document;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.Node;
24  import com.liferay.portal.kernel.xml.ProcessingInstruction;
25  import com.liferay.portal.kernel.xml.SAXReaderUtil;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.journal.model.JournalArticle;
30  import com.liferay.portlet.journal.model.JournalTemplate;
31  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
32  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
33  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
34  import com.liferay.portlet.journal.util.JournalUtil;
35  import com.liferay.util.servlet.ServletResponseUtil;
36  
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Raymond Augé
53   */
54  public class GetArticleAction extends Action {
55  
56      public ActionForward execute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              long groupId = ParamUtil.getLong(request, "groupId");
63              String articleId =  ParamUtil.getString(request, "articleId");
64  
65              String languageId = LanguageUtil.getLanguageId(request);
66  
67              JournalArticle article =
68                  JournalArticleLocalServiceUtil.getLatestArticle(
69                      groupId, articleId, Boolean.TRUE);
70  
71              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
72                  WebKeys.THEME_DISPLAY);
73  
74              Map<String, String> tokens = JournalUtil.getTokens(
75                  groupId, themeDisplay);
76  
77              String xml = article.getContentByLocale(languageId);
78  
79              Document doc = SAXReaderUtil.read(xml);
80  
81              Element root = doc.getRootElement();
82  
83              addProcessingInstructions(doc, request, themeDisplay, article);
84  
85              JournalUtil.addAllReservedEls(root, tokens, article);
86  
87              xml = JournalUtil.formatXML(doc);
88  
89              String contentType = ContentTypes.TEXT_XML_UTF8;
90  
91              String fileName = null;
92              byte[] bytes = xml.getBytes();
93  
94              ServletResponseUtil.sendFile(
95                  request, response, fileName, bytes, contentType);
96  
97              return null;
98          }
99          catch (Exception e) {
100             PortalUtil.sendError(e, request, response);
101 
102             return null;
103         }
104     }
105 
106     protected void addProcessingInstructions(
107         Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
108         JournalArticle article) {
109 
110         // Add style sheets in the reverse order that they appear in the
111         // document
112 
113         // Portal CSS
114 
115         String url =
116             PortalUtil.getStaticResourceURL(
117                 request,
118                 themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
119                     "/html/portal/css.jsp");
120 
121         Map<String, String> arguments = new LinkedHashMap<String, String>();
122 
123         arguments.put("type", "text/css");
124         arguments.put("href", url);
125         arguments.put("title", "portal css");
126         arguments.put("alternate", "yes");
127 
128         addStyleSheet(doc, url, arguments);
129 
130         // Theme CSS
131 
132         url = PortalUtil.getStaticResourceURL(
133             request, themeDisplay.getPathThemeCss() + "/main.css");
134 
135         arguments.clear();
136 
137         arguments.put("type", "text/css");
138         arguments.put("href", url);
139         arguments.put("title", "theme css");
140 
141         addStyleSheet(doc, url, arguments);
142 
143         // XSL template
144 
145         String templateId = article.getTemplateId();
146 
147         if (Validator.isNotNull(templateId)) {
148             JournalTemplate template = null;
149 
150             try {
151                 template = JournalTemplateLocalServiceUtil.getTemplate(
152                     article.getGroupId(), templateId);
153 
154                 if (Validator.equals(
155                         template.getLangType(),
156                         JournalTemplateImpl.LANG_TYPE_XSL)) {
157 
158                     url =
159                         themeDisplay.getPathMain() +
160                             "/journal/get_template?groupId=" +
161                                 article.getGroupId() + "&templateId=" +
162                                     templateId;
163 
164                     arguments.clear();
165 
166                     arguments.put("type", "text/xsl");
167                     arguments.put("href", url);
168                     arguments.put("title", "xsl");
169 
170                     addStyleSheet(doc, url, arguments);
171                 }
172             }
173             catch (Exception e) {
174             }
175         }
176     }
177 
178     protected void addStyleSheet(
179         Document doc, String url, Map<String, String> arguments) {
180 
181         List<Node> content = doc.content();
182 
183         ProcessingInstruction processingInstruction =
184             SAXReaderUtil.createProcessingInstruction(
185                 "xml-stylesheet", arguments);
186 
187         content.add(0, processingInstruction);
188     }
189 
190 }