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.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.PortalUtil;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.journal.model.JournalTemplate;
25  import com.liferay.portlet.journal.model.JournalTemplateConstants;
26  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
27  import com.liferay.portlet.journal.util.JournalUtil;
28  import com.liferay.util.servlet.ServletResponseUtil;
29  
30  import java.util.Map;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.struts.action.Action;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Raymond Augé
45   */
46  public class GetTemplateAction extends Action {
47  
48      public ActionForward execute(
49              ActionMapping mapping, ActionForm form, HttpServletRequest request,
50              HttpServletResponse response)
51          throws Exception {
52  
53          try {
54              long groupId = ParamUtil.getLong(request, "groupId");
55              String templateId = getTemplateId(request);
56  
57              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
58                  WebKeys.THEME_DISPLAY);
59  
60              Map<String, String> tokens = JournalUtil.getTokens(
61                  groupId, themeDisplay);
62  
63              tokens.put("template_id", templateId);
64  
65              String languageId = LanguageUtil.getLanguageId(request);
66  
67              boolean transform = ParamUtil.get(request, "transform", true);
68  
69              JournalTemplate template =
70                  JournalTemplateLocalServiceUtil.getTemplate(
71                      groupId, templateId);
72  
73              String script = JournalUtil.getTemplateScript(
74                  template, tokens, languageId, transform);
75  
76              String extension = JournalTemplateConstants.LANG_TYPE_VM;
77  
78              if (template.getLangType() != null) {
79                  extension = template.getLangType();
80              }
81  
82              String fileName = null;
83              byte[] bytes = script.getBytes();
84  
85              String contentType = ContentTypes.TEXT_PLAIN_UTF8;
86  
87              if (Validator.equals(
88                      extension, JournalTemplateConstants.LANG_TYPE_CSS)) {
89  
90                  contentType = ContentTypes.TEXT_CSS_UTF8;
91              }
92              else if (Validator.equals(
93                      extension, JournalTemplateConstants.LANG_TYPE_XSL)) {
94  
95                  contentType = ContentTypes.TEXT_XML_UTF8;
96              }
97  
98              ServletResponseUtil.sendFile(
99                  response, fileName, bytes, contentType);
100 
101             return null;
102         }
103         catch (Exception e) {
104             PortalUtil.sendError(e, request, response);
105 
106             return null;
107         }
108     }
109 
110     protected String getTemplateId(HttpServletRequest request) {
111         String templateId = ParamUtil.getString(request, "templateId");
112 
113         // Backwards compatibility
114 
115         if (Validator.isNull(templateId)) {
116             templateId = ParamUtil.getString(request, "template_id");
117         }
118 
119         return templateId;
120     }
121 
122 }