001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.util.ContentTypes;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.util.PortalUtil;
020    import com.liferay.portlet.journal.model.JournalTemplateConstants;
021    import com.liferay.portlet.journal.util.JournalUtil;
022    import com.liferay.util.JS;
023    import com.liferay.util.servlet.ServletResponseUtil;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.struts.action.Action;
029    import org.apache.struts.action.ActionForm;
030    import org.apache.struts.action.ActionForward;
031    import org.apache.struts.action.ActionMapping;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class GetTemplateContentAction extends Action {
037    
038            public ActionForward execute(
039                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
040                            HttpServletResponse response)
041                    throws Exception {
042    
043                    try {
044                            String xslContent = JS.decodeURIComponent(
045                                    ParamUtil.getString(request, "xslContent"));
046                            boolean formatXsl = ParamUtil.getBoolean(request, "formatXsl");
047                            String langType = ParamUtil.getString(
048                                    request, "langType", JournalTemplateConstants.LANG_TYPE_XSL);
049    
050                            if (formatXsl) {
051                                    if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
052                                            xslContent = JournalUtil.formatVM(xslContent);
053                                    }
054                                    else {
055                                            xslContent = JournalUtil.formatXML(xslContent);
056                                    }
057                            }
058    
059                            String fileName = "template." + langType;
060                            byte[] bytes = xslContent.getBytes();
061    
062                            ServletResponseUtil.sendFile(
063                                    request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
064    
065                            return null;
066                    }
067                    catch (Exception e) {
068                            PortalUtil.sendError(e, request, response);
069    
070                            return null;
071                    }
072            }
073    
074    }