1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.util.ContentTypes;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.util.PortalUtil;
20  import com.liferay.portlet.journal.model.JournalTemplateConstants;
21  import com.liferay.portlet.journal.util.JournalUtil;
22  import com.liferay.util.JS;
23  import com.liferay.util.servlet.ServletResponseUtil;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.struts.action.Action;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  
33  /**
34   * <a href="GetTemplateContentAction.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class GetTemplateContentAction extends Action {
39  
40      public ActionForward execute(
41              ActionMapping mapping, ActionForm form, HttpServletRequest request,
42              HttpServletResponse response)
43          throws Exception {
44  
45          try {
46              String xslContent = JS.decodeURIComponent(
47                  ParamUtil.getString(request, "xslContent"));
48              boolean formatXsl = ParamUtil.getBoolean(request, "formatXsl");
49              String langType = ParamUtil.getString(
50                  request, "langType", JournalTemplateConstants.LANG_TYPE_XSL);
51  
52              if (formatXsl) {
53                  if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
54                      xslContent = JournalUtil.formatVM(xslContent);
55                  }
56                  else {
57                      xslContent = JournalUtil.formatXML(xslContent);
58                  }
59              }
60  
61              String fileName = "template." + langType;
62              byte[] bytes = xslContent.getBytes();
63  
64              ServletResponseUtil.sendFile(
65                  response, fileName, bytes, ContentTypes.TEXT_XML_UTF8);
66  
67              return null;
68          }
69          catch (Exception e) {
70              PortalUtil.sendError(e, request, response);
71  
72              return null;
73          }
74      }
75  
76  }