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.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.util.PortalUtil;
021    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
022    import com.liferay.portlet.journal.model.JournalTemplateConstants;
023    import com.liferay.portlet.journal.util.JournalUtil;
024    import com.liferay.util.JS;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.struts.action.Action;
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class GetTemplateContentAction extends Action {
038    
039            @Override
040            public ActionForward execute(
041                            ActionMapping actionMapping, ActionForm actionForm,
042                            HttpServletRequest request, HttpServletResponse response)
043                    throws Exception {
044    
045                    try {
046                            String xslContent = JS.decodeURIComponent(
047                                    ParamUtil.getString(request, "xslContent"));
048                            boolean formatXsl = ParamUtil.getBoolean(request, "formatXsl");
049                            String langType = ParamUtil.getString(
050                                    request, "langType", JournalTemplateConstants.LANG_TYPE_XSL);
051    
052                            if (formatXsl) {
053                                    if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
054                                            xslContent = JournalUtil.formatVM(xslContent);
055                                    }
056                                    else {
057                                            xslContent = DDMXMLUtil.formatXML(xslContent);
058                                    }
059                            }
060    
061                            String fileName = "template." + langType;
062                            byte[] bytes = xslContent.getBytes();
063    
064                            ServletResponseUtil.sendFile(
065                                    request, response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
066    
067                            return null;
068                    }
069                    catch (Exception e) {
070                            PortalUtil.sendError(e, request, response);
071    
072                            return null;
073                    }
074            }
075    
076    }