001    /**
002     * Copyright (c) 2000-2012 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.util.ContentTypes;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
024    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
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 Juan Fernández
036     */
037    public class GetTemplateAction extends Action {
038    
039            @Override
040            public ActionForward execute(
041                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
042                            HttpServletResponse response)
043                    throws Exception {
044    
045                    try {
046                            long templateId = ParamUtil.getLong(request, "templateId");
047    
048                            DDMTemplate template = DDMTemplateServiceUtil.getTemplate(
049                                    templateId);
050    
051                            String extension = GetterUtil.getString(
052                                    template.getLanguage(), DDMTemplateConstants.LANG_TYPE_VM);
053    
054                            String script = template.getScript();
055    
056                            String contentType = null;
057    
058                            if (extension.equals(DDMTemplateConstants.LANG_TYPE_XSD)) {
059                                    contentType = ContentTypes.TEXT_XML_UTF8;
060                            }
061                            else {
062                                    contentType = ContentTypes.TEXT_PLAIN_UTF8;
063                            }
064    
065                            ServletResponseUtil.sendFile(
066                                    request, response, null, script.getBytes(), contentType);
067    
068                            return null;
069                    }
070                    catch (Exception e) {
071                            PortalUtil.sendError(e, request, response);
072    
073                            return null;
074                    }
075            }
076    
077    }