1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.impl.JournalTemplateImpl;
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", JournalTemplateImpl.LANG_TYPE_XSL);
51  
52              if (formatXsl) {
53                  if (langType.equals(JournalTemplateImpl.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                  request, 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  }