001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.action;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.ContentTypes;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.struts.ActionConstants;
028: import com.liferay.portal.theme.ThemeDisplay;
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.impl.JournalTemplateImpl;
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.io.StringReader;
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: import javax.servlet.jsp.PageContext;
047:
048: import org.apache.struts.action.Action;
049: import org.apache.struts.action.ActionForm;
050: import org.apache.struts.action.ActionForward;
051: import org.apache.struts.action.ActionMapping;
052:
053: import org.dom4j.Document;
054: import org.dom4j.DocumentFactory;
055: import org.dom4j.Element;
056: import org.dom4j.ProcessingInstruction;
057: import org.dom4j.io.SAXReader;
058:
059: /**
060: * <a href="GetArticleAction.java.html"><b><i>View Source</i></b></a>
061: *
062: * @author Raymond Augé
063: *
064: */
065: public class GetArticleAction extends Action {
066:
067: public ActionForward execute(ActionMapping mapping,
068: ActionForm form, HttpServletRequest req,
069: HttpServletResponse res) throws Exception {
070:
071: try {
072: long groupId = ParamUtil.getLong(req, "groupId");
073: String articleId = ParamUtil.getString(req, "articleId");
074:
075: String languageId = LanguageUtil.getLanguageId(req);
076:
077: JournalArticle article = JournalArticleLocalServiceUtil
078: .getLatestArticle(groupId, articleId, Boolean.TRUE);
079:
080: ThemeDisplay themeDisplay = (ThemeDisplay) req
081: .getAttribute(WebKeys.THEME_DISPLAY);
082:
083: Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
084:
085: String xml = article.getContentByLocale(languageId);
086:
087: String contentType = ContentTypes.TEXT_HTML_UTF8;
088:
089: Document doc = null;
090:
091: Element root = null;
092:
093: if (article.isTemplateDriven()) {
094: SAXReader reader = new SAXReader();
095:
096: doc = reader.read(new StringReader(xml));
097:
098: root = doc.getRootElement();
099:
100: addProcessingInstructions(doc, themeDisplay, article);
101:
102: JournalUtil.addAllReservedEls(root, tokens, article);
103:
104: xml = JournalUtil.formatXML(doc);
105:
106: contentType = ContentTypes.TEXT_XML_UTF8;
107: }
108:
109: String fileName = null;
110: byte[] byteArray = xml.getBytes();
111:
112: ServletResponseUtil.sendFile(res, fileName, byteArray,
113: contentType);
114:
115: return null;
116: } catch (Exception e) {
117: req.setAttribute(PageContext.EXCEPTION, e);
118:
119: return mapping.findForward(ActionConstants.COMMON_ERROR);
120: }
121: }
122:
123: protected void addProcessingInstructions(Document doc,
124: ThemeDisplay themeDisplay, JournalArticle article) {
125:
126: // Add style sheets in the reverse order that they appear in the
127: // document
128:
129: // Theme CSS
130:
131: String url = themeDisplay.getPathThemeCss()
132: + "/main.css?companyId=" + themeDisplay.getCompanyId()
133: + "&themeId=" + themeDisplay.getThemeId()
134: + "&colorSchemeId=" + themeDisplay.getColorSchemeId();
135:
136: Map arguments = new LinkedHashMap();
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: // CSS cached
145:
146: url = themeDisplay.getPathMain()
147: + "/portal/css_cached?themeId="
148: + themeDisplay.getThemeId() + "&colorSchemeId="
149: + themeDisplay.getColorSchemeId();
150:
151: arguments.clear();
152:
153: arguments.put("type", "text/css");
154: arguments.put("href", url);
155: arguments.put("title", "cached css");
156: arguments.put("alternate", "yes");
157:
158: addStyleSheet(doc, url, arguments);
159:
160: // XSL template
161:
162: String templateId = article.getTemplateId();
163:
164: if (Validator.isNotNull(templateId)) {
165: JournalTemplate template = null;
166:
167: try {
168: template = JournalTemplateLocalServiceUtil.getTemplate(
169: article.getGroupId(), templateId);
170:
171: if (Validator.equals(template.getLangType(),
172: JournalTemplateImpl.LANG_TYPE_XSL)) {
173:
174: url = themeDisplay.getPathMain()
175: + "/journal/get_template?groupId="
176: + article.getGroupId() + "&templateId="
177: + templateId;
178:
179: arguments.clear();
180:
181: arguments.put("type", "text/xsl");
182: arguments.put("href", url);
183: arguments.put("title", "xsl");
184:
185: addStyleSheet(doc, url, arguments);
186: }
187: } catch (Exception e) {
188: }
189: }
190: }
191:
192: protected void addStyleSheet(Document doc, String url, Map arguments) {
193: List content = doc.content();
194:
195: ProcessingInstruction pi = DocumentFactory.getInstance()
196: .createProcessingInstruction("xml-stylesheet",
197: arguments);
198:
199: content.add(0, pi);
200: }
201:
202: }
|