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.JournalTemplate;
031: import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
032: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
033: import com.liferay.portlet.journal.util.JournalUtil;
034: import com.liferay.util.servlet.ServletResponseUtil;
035:
036: import java.util.Map;
037:
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import javax.servlet.jsp.PageContext;
041:
042: import org.apache.struts.action.Action;
043: import org.apache.struts.action.ActionForm;
044: import org.apache.struts.action.ActionForward;
045: import org.apache.struts.action.ActionMapping;
046:
047: /**
048: * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: * @author Raymond Augé
052: *
053: */
054: public class GetTemplateAction extends Action {
055:
056: public ActionForward execute(ActionMapping mapping,
057: ActionForm form, HttpServletRequest req,
058: HttpServletResponse res) throws Exception {
059:
060: try {
061: long groupId = ParamUtil.getLong(req, "groupId");
062: String templateId = getTemplateId(req);
063:
064: ThemeDisplay themeDisplay = (ThemeDisplay) req
065: .getAttribute(WebKeys.THEME_DISPLAY);
066:
067: Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
068:
069: tokens.put("template_id", templateId);
070:
071: String languageId = LanguageUtil.getLanguageId(req);
072:
073: boolean transform = ParamUtil.get(req, "transform", true);
074:
075: JournalTemplate template = JournalTemplateLocalServiceUtil
076: .getTemplate(groupId, templateId);
077:
078: String script = JournalUtil.getTemplateScript(template,
079: tokens, languageId, transform);
080:
081: String extension = JournalTemplateImpl.LANG_TYPE_VM;
082:
083: if (template.getLangType() != null) {
084: extension = template.getLangType();
085: }
086:
087: String fileName = null;
088: byte[] byteArray = script.getBytes();
089:
090: String contentType = ContentTypes.TEXT_PLAIN_UTF8;
091:
092: if (Validator.equals(extension,
093: JournalTemplateImpl.LANG_TYPE_CSS)) {
094:
095: contentType = ContentTypes.TEXT_CSS_UTF8;
096: } else if (Validator.equals(extension,
097: JournalTemplateImpl.LANG_TYPE_XSL)) {
098:
099: contentType = ContentTypes.TEXT_XML_UTF8;
100: }
101:
102: ServletResponseUtil.sendFile(res, fileName, byteArray,
103: contentType);
104:
105: return null;
106: } catch (Exception e) {
107: req.setAttribute(PageContext.EXCEPTION, e);
108:
109: return mapping.findForward(ActionConstants.COMMON_ERROR);
110: }
111: }
112:
113: protected String getTemplateId(HttpServletRequest req) {
114: String templateId = ParamUtil.getString(req, "templateId");
115:
116: // Backwards compatibility
117:
118: if (Validator.isNull(templateId)) {
119: templateId = ParamUtil.getString(req, "template_id");
120: }
121:
122: return templateId;
123: }
124:
125: }
|