001    /**
002     * Copyright (c) 2000-2012 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.servlet.ServletResponseUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.Node;
026    import com.liferay.portal.kernel.xml.ProcessingInstruction;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.journal.model.JournalTemplate;
034    import com.liferay.portlet.journal.model.JournalTemplateConstants;
035    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
036    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
037    import com.liferay.portlet.journal.util.JournalUtil;
038    
039    import java.util.LinkedHashMap;
040    import java.util.List;
041    import java.util.Map;
042    
043    import javax.servlet.http.HttpServletRequest;
044    import javax.servlet.http.HttpServletResponse;
045    
046    import org.apache.struts.action.Action;
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Raymond Augé
053     */
054    public class GetArticleAction extends Action {
055    
056            @Override
057            public ActionForward execute(
058                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
059                            HttpServletResponse response)
060                    throws Exception {
061    
062                    try {
063                            long groupId = ParamUtil.getLong(request, "groupId");
064                            String articleId = ParamUtil.getString(request, "articleId");
065    
066                            String languageId = LanguageUtil.getLanguageId(request);
067    
068                            JournalArticle article = JournalArticleServiceUtil.getLatestArticle(
069                                    groupId, articleId, WorkflowConstants.STATUS_APPROVED);
070    
071                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
072                                    WebKeys.THEME_DISPLAY);
073    
074                            Map<String, String> tokens = JournalUtil.getTokens(
075                                    groupId, themeDisplay);
076    
077                            String xml = article.getContentByLocale(languageId);
078    
079                            Document doc = SAXReaderUtil.read(xml);
080    
081                            Element root = doc.getRootElement();
082    
083                            addProcessingInstructions(doc, request, themeDisplay, article);
084    
085                            JournalUtil.addAllReservedEls(root, tokens, article, languageId);
086    
087                            xml = DDMXMLUtil.formatXML(doc);
088    
089                            String contentType = ContentTypes.TEXT_XML_UTF8;
090    
091                            String fileName = null;
092                            byte[] bytes = xml.getBytes();
093    
094                            ServletResponseUtil.sendFile(
095                                    request, response, fileName, bytes, contentType);
096    
097                            return null;
098                    }
099                    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 = PortalUtil.getStaticResourceURL(
116                            request,
117                            themeDisplay.getCDNDynamicResourcesHost() +
118                                    themeDisplay.getPathContext() + "/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 = JournalTemplateServiceUtil.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    }