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.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
025    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.struts.action.Action;
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Juan Fern??ndez
037     */
038    public class GetTemplateAction extends Action {
039    
040            @Override
041            public ActionForward execute(
042                            ActionMapping actionMapping, ActionForm actionForm,
043                            HttpServletRequest request, HttpServletResponse response)
044                    throws Exception {
045    
046                    try {
047                            long templateId = ParamUtil.getLong(request, "templateId");
048    
049                            DDMTemplate template = DDMTemplateServiceUtil.getTemplate(
050                                    templateId);
051    
052                            String script = template.getScript();
053    
054                            String contentType = null;
055    
056                            String type = template.getType();
057                            String language = GetterUtil.getString(
058                                    template.getLanguage(), TemplateConstants.LANG_TYPE_VM);
059    
060                            if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_FORM) ||
061                                    language.equals(TemplateConstants.LANG_TYPE_XSL)) {
062    
063                                    contentType = ContentTypes.TEXT_XML_UTF8;
064                            }
065                            else {
066                                    contentType = ContentTypes.TEXT_PLAIN_UTF8;
067                            }
068    
069                            ServletResponseUtil.sendFile(
070                                    request, response, null, script.getBytes(), contentType);
071    
072                            return null;
073                    }
074                    catch (Exception e) {
075                            PortalUtil.sendError(e, request, response);
076    
077                            return null;
078                    }
079            }
080    
081    }