com.liferay.portlet.journal.action.GetTemplateAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.journal.action.GetTemplateAction.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.journal.action;

import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.journal.model.JournalTemplate;
import com.liferay.portlet.journal.model.JournalTemplateConstants;
import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
import com.liferay.portlet.journal.util.JournalUtil;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * CST-SA: LPS-28550 Able to view any journal structure/template's source
 *
 * @author Brian Wing Shun Chan
 * @author Raymond Aug
 */
public class GetTemplateAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            long groupId = ParamUtil.getLong(request, "groupId");
            String templateId = getTemplateId(request);

            ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

            Map<String, String> tokens = JournalUtil.getTokens(groupId, themeDisplay);

            tokens.put("template_id", templateId);

            String languageId = LanguageUtil.getLanguageId(request);

            boolean transform = ParamUtil.getBoolean(request, "transform", true);

            JournalTemplate template = JournalTemplateServiceUtil.getTemplate(groupId, templateId, true);

            String script = JournalUtil.getTemplateScript(template, tokens, languageId, transform);

            String extension = JournalTemplateConstants.LANG_TYPE_VM;

            if (template.getLangType() != null) {
                extension = template.getLangType();
            }

            String fileName = null;
            byte[] bytes = script.getBytes();

            String contentType = ContentTypes.TEXT_PLAIN_UTF8;

            if (Validator.equals(extension, JournalTemplateConstants.LANG_TYPE_CSS)) {

                contentType = ContentTypes.TEXT_CSS_UTF8;
            } else if (Validator.equals(extension, JournalTemplateConstants.LANG_TYPE_XSL)) {

                contentType = ContentTypes.TEXT_XML_UTF8;
            }

            ServletResponseUtil.sendFile(request, response, fileName, bytes, contentType);

            return null;
        } catch (Exception e) {
            PortalUtil.sendError(e, request, response);

            return null;
        }
    }

    protected String getTemplateId(HttpServletRequest request) {
        String templateId = ParamUtil.getString(request, "templateId");

        // Backwards compatibility

        if (Validator.isNull(templateId)) {
            templateId = ParamUtil.getString(request, "template_id");
        }

        return templateId;
    }

}