001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.Node;
025    import com.liferay.portal.kernel.xml.ProcessingInstruction;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.journal.model.JournalArticle;
031    import com.liferay.portlet.journal.model.JournalTemplate;
032    import com.liferay.portlet.journal.model.JournalTemplateConstants;
033    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
035    import com.liferay.portlet.journal.util.JournalUtil;
036    import com.liferay.util.servlet.ServletResponseUtil;
037    
038    import java.util.LinkedHashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    import org.apache.struts.action.Action;
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Raymond Augé
052     */
053    public class GetArticleAction extends Action {
054    
055            public ActionForward execute(
056                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
057                            HttpServletResponse response)
058                    throws Exception {
059    
060                    try {
061                            long groupId = ParamUtil.getLong(request, "groupId");
062                            String articleId =  ParamUtil.getString(request, "articleId");
063    
064                            String languageId = LanguageUtil.getLanguageId(request);
065    
066                            JournalArticle article =
067                                    JournalArticleLocalServiceUtil.getLatestArticle(
068                                            groupId, articleId, WorkflowConstants.STATUS_APPROVED);
069    
070                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
071                                    WebKeys.THEME_DISPLAY);
072    
073                            Map<String, String> tokens = JournalUtil.getTokens(
074                                    groupId, themeDisplay);
075    
076                            String xml = article.getContentByLocale(languageId);
077    
078                            Document doc = SAXReaderUtil.read(xml);
079    
080                            Element root = doc.getRootElement();
081    
082                            addProcessingInstructions(doc, request, themeDisplay, article);
083    
084                            JournalUtil.addAllReservedEls(root, tokens, article);
085    
086                            xml = JournalUtil.formatXML(doc);
087    
088                            String contentType = ContentTypes.TEXT_XML_UTF8;
089    
090                            String fileName = null;
091                            byte[] bytes = xml.getBytes();
092    
093                            ServletResponseUtil.sendFile(
094                                    request, response, fileName, bytes, contentType);
095    
096                            return null;
097                    }
098                    catch (Exception e) {
099                            PortalUtil.sendError(e, request, response);
100    
101                            return null;
102                    }
103            }
104    
105            protected void addProcessingInstructions(
106                    Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
107                    JournalArticle article) {
108    
109                    // Add style sheets in the reverse order that they appear in the
110                    // document
111    
112                    // Portal CSS
113    
114                    String url =
115                            PortalUtil.getStaticResourceURL(
116                                    request,
117                                    themeDisplay.getCDNHost() + themeDisplay.getPathContext() +
118                                            "/html/portal/css.jsp");
119    
120                    Map<String, String> arguments = new LinkedHashMap<String, String>();
121    
122                    arguments.put("type", "text/css");
123                    arguments.put("href", url);
124                    arguments.put("title", "portal css");
125                    arguments.put("alternate", "yes");
126    
127                    addStyleSheet(doc, url, arguments);
128    
129                    // Theme CSS
130    
131                    url = PortalUtil.getStaticResourceURL(
132                            request, themeDisplay.getPathThemeCss() + "/main.css");
133    
134                    arguments.clear();
135    
136                    arguments.put("type", "text/css");
137                    arguments.put("href", url);
138                    arguments.put("title", "theme css");
139    
140                    addStyleSheet(doc, url, arguments);
141    
142                    // XSL template
143    
144                    String templateId = article.getTemplateId();
145    
146                    if (Validator.isNotNull(templateId)) {
147                            JournalTemplate template = null;
148    
149                            try {
150                                    template = JournalTemplateLocalServiceUtil.getTemplate(
151                                            article.getGroupId(), templateId);
152    
153                                    if (Validator.equals(
154                                                    template.getLangType(),
155                                                    JournalTemplateConstants.LANG_TYPE_XSL)) {
156    
157                                            url =
158                                                    themeDisplay.getPathMain() +
159                                                            "/journal/get_template?groupId=" +
160                                                                    article.getGroupId() + "&templateId=" +
161                                                                            templateId;
162    
163                                            arguments.clear();
164    
165                                            arguments.put("type", "text/xsl");
166                                            arguments.put("href", url);
167                                            arguments.put("title", "xsl");
168    
169                                            addStyleSheet(doc, url, arguments);
170                                    }
171                            }
172                            catch (Exception e) {
173                            }
174                    }
175            }
176    
177            protected void addStyleSheet(
178                    Document doc, String url, Map<String, String> arguments) {
179    
180                    List<Node> content = doc.content();
181    
182                    ProcessingInstruction processingInstruction =
183                            SAXReaderUtil.createProcessingInstruction(
184                                    "xml-stylesheet", arguments);
185    
186                    content.add(0, processingInstruction);
187            }
188    
189    }