001    /**
002     * Copyright (c) 2000-2010 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.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.journal.model.JournalTemplate;
025    import com.liferay.portlet.journal.model.JournalTemplateConstants;
026    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
027    import com.liferay.portlet.journal.util.JournalUtil;
028    import com.liferay.util.servlet.ServletResponseUtil;
029    
030    import java.util.Map;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    import org.apache.struts.action.Action;
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Raymond Augé
043     */
044    public class GetTemplateAction extends Action {
045    
046            public ActionForward execute(
047                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
048                            HttpServletResponse response)
049                    throws Exception {
050    
051                    try {
052                            long groupId = ParamUtil.getLong(request, "groupId");
053                            String templateId = getTemplateId(request);
054    
055                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
056                                    WebKeys.THEME_DISPLAY);
057    
058                            Map<String, String> tokens = JournalUtil.getTokens(
059                                    groupId, themeDisplay);
060    
061                            tokens.put("template_id", templateId);
062    
063                            String languageId = LanguageUtil.getLanguageId(request);
064    
065                            boolean transform = ParamUtil.get(request, "transform", true);
066    
067                            JournalTemplate template =
068                                    JournalTemplateLocalServiceUtil.getTemplate(
069                                            groupId, templateId);
070    
071                            String script = JournalUtil.getTemplateScript(
072                                    template, tokens, languageId, transform);
073    
074                            String extension = JournalTemplateConstants.LANG_TYPE_VM;
075    
076                            if (template.getLangType() != null) {
077                                    extension = template.getLangType();
078                            }
079    
080                            String fileName = null;
081                            byte[] bytes = script.getBytes();
082    
083                            String contentType = ContentTypes.TEXT_PLAIN_UTF8;
084    
085                            if (Validator.equals(
086                                            extension, JournalTemplateConstants.LANG_TYPE_CSS)) {
087    
088                                    contentType = ContentTypes.TEXT_CSS_UTF8;
089                            }
090                            else if (Validator.equals(
091                                            extension, JournalTemplateConstants.LANG_TYPE_XSL)) {
092    
093                                    contentType = ContentTypes.TEXT_XML_UTF8;
094                            }
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 String getTemplateId(HttpServletRequest request) {
109                    String templateId = ParamUtil.getString(request, "templateId");
110    
111                    // Backwards compatibility
112    
113                    if (Validator.isNull(templateId)) {
114                            templateId = ParamUtil.getString(request, "template_id");
115                    }
116    
117                    return templateId;
118            }
119    
120    }