001    /**
002     * Copyright (c) 2000-2013 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.template.TemplateConstants;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.Node;
027    import com.liferay.portal.kernel.xml.ProcessingInstruction;
028    import com.liferay.portal.kernel.xml.SAXReaderUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
035    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
036    import com.liferay.portlet.journal.model.JournalArticle;
037    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
038    import com.liferay.portlet.journal.util.JournalUtil;
039    
040    import java.util.LinkedHashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    import org.apache.struts.action.Action;
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Raymond Aug??
054     */
055    public class GetArticleAction extends Action {
056    
057            @Override
058            public ActionForward execute(
059                            ActionMapping actionMapping, ActionForm actionForm,
060                            HttpServletRequest request, HttpServletResponse response)
061                    throws Exception {
062    
063                    try {
064                            long groupId = ParamUtil.getLong(request, "groupId");
065                            String articleId = ParamUtil.getString(request, "articleId");
066    
067                            String languageId = LanguageUtil.getLanguageId(request);
068    
069                            JournalArticle article = JournalArticleServiceUtil.getLatestArticle(
070                                    groupId, articleId, WorkflowConstants.STATUS_APPROVED);
071    
072                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
073                                    WebKeys.THEME_DISPLAY);
074    
075                            Map<String, String> tokens = JournalUtil.getTokens(
076                                    groupId, themeDisplay);
077    
078                            String xml = article.getContentByLocale(languageId);
079    
080                            Document doc = SAXReaderUtil.read(xml);
081    
082                            Element root = doc.getRootElement();
083    
084                            addProcessingInstructions(doc, request, themeDisplay, article);
085    
086                            JournalUtil.addAllReservedEls(
087                                    root, tokens, article, languageId, themeDisplay);
088    
089                            xml = DDMXMLUtil.formatXML(doc);
090    
091                            String contentType = ContentTypes.TEXT_XML_UTF8;
092    
093                            String fileName = null;
094                            byte[] bytes = xml.getBytes();
095    
096                            ServletResponseUtil.sendFile(
097                                    request, response, fileName, bytes, contentType);
098    
099                            return null;
100                    }
101                    catch (Exception e) {
102                            PortalUtil.sendError(e, request, response);
103    
104                            return null;
105                    }
106            }
107    
108            protected void addProcessingInstructions(
109                    Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
110                    JournalArticle article) {
111    
112                    // Add style sheets in the reverse order that they appear in the
113                    // document
114    
115                    // Portal CSS
116    
117                    String url = PortalUtil.getStaticResourceURL(
118                            request,
119                            themeDisplay.getCDNDynamicResourcesHost() +
120                                    themeDisplay.getPathContext() + "/html/portal/css.jsp");
121    
122                    Map<String, String> arguments = new LinkedHashMap<String, String>();
123    
124                    arguments.put("type", "text/css");
125                    arguments.put("href", url);
126                    arguments.put("title", "portal css");
127                    arguments.put("alternate", "yes");
128    
129                    addStyleSheet(doc, url, arguments);
130    
131                    // Theme CSS
132    
133                    url = PortalUtil.getStaticResourceURL(
134                            request, themeDisplay.getPathThemeCss() + "/main.css");
135    
136                    arguments.clear();
137    
138                    arguments.put("type", "text/css");
139                    arguments.put("href", url);
140                    arguments.put("title", "theme css");
141    
142                    addStyleSheet(doc, url, arguments);
143    
144                    // XSL template
145    
146                    String templateId = article.getTemplateId();
147    
148                    if (Validator.isNull(templateId)) {
149                            return;
150                    }
151    
152                    try {
153                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
154                                    article.getGroupId(),
155                                    PortalUtil.getClassNameId(DDMStructure.class), templateId,
156                                    true);
157    
158                            if (Validator.equals(
159                                            ddmTemplate.getLanguage(),
160                                            TemplateConstants.LANG_TYPE_XSL)) {
161    
162                                    url =
163                                            themeDisplay.getPathMain() +
164                                                    "/journal/get_template?groupId=" +
165                                                            article.getGroupId() + "&templateId=" + templateId;
166    
167                                    arguments.clear();
168    
169                                    arguments.put("type", "text/xsl");
170                                    arguments.put("href", url);
171                                    arguments.put("title", "xsl");
172    
173                                    addStyleSheet(doc, url, arguments);
174                            }
175                    }
176                    catch (Exception e) {
177                    }
178            }
179    
180            protected void addStyleSheet(
181                    Document doc, String url, Map<String, String> arguments) {
182    
183                    List<Node> content = doc.content();
184    
185                    ProcessingInstruction processingInstruction =
186                            SAXReaderUtil.createProcessingInstruction(
187                                    "xml-stylesheet", arguments);
188    
189                    content.add(0, processingInstruction);
190            }
191    
192    }