001    /**
002     * Copyright (c) 2000-2012 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.JournalTemplateLocalServiceUtil;
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 mapping, ActionForm form, HttpServletRequest request,
049                            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 =
070                                    JournalTemplateLocalServiceUtil.getTemplate(
071                                            groupId, templateId);
072    
073                            String script = JournalUtil.getTemplateScript(
074                                    template, tokens, languageId, transform);
075    
076                            String extension = JournalTemplateConstants.LANG_TYPE_VM;
077    
078                            if (template.getLangType() != null) {
079                                    extension = template.getLangType();
080                            }
081    
082                            String fileName = null;
083                            byte[] bytes = script.getBytes();
084    
085                            String contentType = ContentTypes.TEXT_PLAIN_UTF8;
086    
087                            if (Validator.equals(
088                                            extension, JournalTemplateConstants.LANG_TYPE_CSS)) {
089    
090                                    contentType = ContentTypes.TEXT_CSS_UTF8;
091                            }
092                            else if (Validator.equals(
093                                            extension, JournalTemplateConstants.LANG_TYPE_XSL)) {
094    
095                                    contentType = ContentTypes.TEXT_XML_UTF8;
096                            }
097    
098                            ServletResponseUtil.sendFile(
099                                    request, response, fileName, bytes, contentType);
100    
101                            return null;
102                    }
103                    catch (Exception e) {
104                            PortalUtil.sendError(e, request, response);
105    
106                            return null;
107                    }
108            }
109    
110            protected String getTemplateId(HttpServletRequest request) {
111                    String templateId = ParamUtil.getString(request, "templateId");
112    
113                    // Backwards compatibility
114    
115                    if (Validator.isNull(templateId)) {
116                            templateId = ParamUtil.getString(request, "template_id");
117                    }
118    
119                    return templateId;
120            }
121    
122    }