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.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.journal.model.JournalTemplate;
026    import com.liferay.portlet.journal.model.JournalTemplateConstants;
027    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
028    import com.liferay.portlet.journal.util.JournalUtil;
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            @Override
047            public ActionForward execute(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            HttpServletRequest request, HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            long groupId = ParamUtil.getLong(request, "groupId");
054                            String templateId = getTemplateId(request);
055    
056                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057                                    WebKeys.THEME_DISPLAY);
058    
059                            Map<String, String> tokens = JournalUtil.getTokens(
060                                    groupId, themeDisplay);
061    
062                            tokens.put("template_id", templateId);
063    
064                            String languageId = LanguageUtil.getLanguageId(request);
065    
066                            boolean transform = ParamUtil.getBoolean(
067                                    request, "transform", true);
068    
069                            JournalTemplate template = JournalTemplateServiceUtil.getTemplate(
070                                    groupId, templateId, true);
071    
072                            String script = JournalUtil.getTemplateScript(
073                                    template, tokens, languageId, transform);
074    
075                            String extension = JournalTemplateConstants.LANG_TYPE_VM;
076    
077                            if (template.getLangType() != null) {
078                                    extension = template.getLangType();
079                            }
080    
081                            String fileName = null;
082                            byte[] bytes = script.getBytes();
083    
084                            String contentType = ContentTypes.TEXT_PLAIN_UTF8;
085    
086                            if (Validator.equals(
087                                            extension, JournalTemplateConstants.LANG_TYPE_CSS)) {
088    
089                                    contentType = ContentTypes.TEXT_CSS_UTF8;
090                            }
091                            else if (Validator.equals(
092                                                    extension, JournalTemplateConstants.LANG_TYPE_XSL)) {
093    
094                                    contentType = ContentTypes.TEXT_XML_UTF8;
095                            }
096    
097                            ServletResponseUtil.sendFile(
098                                    request, response, fileName, bytes, contentType);
099    
100                            return null;
101                    }
102                    catch (Exception e) {
103                            PortalUtil.sendError(e, request, response);
104    
105                            return null;
106                    }
107            }
108    
109            protected String getTemplateId(HttpServletRequest request) {
110                    String templateId = ParamUtil.getString(request, "templateId");
111    
112                    // Backwards compatibility
113    
114                    if (Validator.isNull(templateId)) {
115                            templateId = ParamUtil.getString(request, "template_id");
116                    }
117    
118                    return templateId;
119            }
120    
121    }