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.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    }