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.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 actionMapping, ActionForm actionForm,
059                            HttpServletRequest request, 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(
086                                    root, tokens, article, languageId, themeDisplay);
087    
088                            xml = DDMXMLUtil.formatXML(doc);
089    
090                            String contentType = ContentTypes.TEXT_XML_UTF8;
091    
092                            String fileName = null;
093                            byte[] bytes = xml.getBytes();
094    
095                            ServletResponseUtil.sendFile(
096                                    request, response, fileName, bytes, contentType);
097    
098                            return null;
099                    }
100                    catch (Exception e) {
101                            PortalUtil.sendError(e, request, response);
102    
103                            return null;
104                    }
105            }
106    
107            protected void addProcessingInstructions(
108                    Document doc, HttpServletRequest request, ThemeDisplay themeDisplay,
109                    JournalArticle article) {
110    
111                    // Add style sheets in the reverse order that they appear in the
112                    // document
113    
114                    // Portal CSS
115    
116                    String url = PortalUtil.getStaticResourceURL(
117                            request,
118                            themeDisplay.getCDNDynamicResourcesHost() +
119                                    themeDisplay.getPathContext() + "/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 = JournalTemplateServiceUtil.getTemplate(
152                                            article.getGroupId(), templateId);
153    
154                                    if (Validator.equals(
155                                                    template.getLangType(),
156                                                    JournalTemplateConstants.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    }