1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.journal.model.JournalTemplate;
33  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
34  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
35  import com.liferay.portlet.journal.util.JournalUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.jsp.PageContext;
43  
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Raymond Augé
54   *
55   */
56  public class GetTemplateAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest req,
60              HttpServletResponse res)
61          throws Exception {
62  
63          try {
64              long groupId = ParamUtil.getLong(req, "groupId");
65              String templateId = getTemplateId(req);
66  
67              ThemeDisplay themeDisplay =
68                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
69  
70              Map tokens = JournalUtil.getTokens(groupId, themeDisplay);
71  
72              tokens.put("template_id", templateId);
73  
74              String languageId = LanguageUtil.getLanguageId(req);
75  
76              boolean transform = ParamUtil.get(req, "transform", true);
77  
78              JournalTemplate template =
79                  JournalTemplateLocalServiceUtil.getTemplate(
80                      groupId, templateId);
81  
82              String script = JournalUtil.getTemplateScript(
83                  template, tokens, languageId, transform);
84  
85              String extension = JournalTemplateImpl.LANG_TYPE_VM;
86  
87              if (template.getLangType() != null) {
88                  extension = template.getLangType();
89              }
90  
91              String fileName = null;
92              byte[] byteArray = script.getBytes();
93  
94              String contentType = ContentTypes.TEXT_PLAIN_UTF8;
95  
96              if (Validator.equals(
97                      extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
98  
99                  contentType = ContentTypes.TEXT_CSS_UTF8;
100             }
101             else if (Validator.equals(
102                     extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
103 
104                 contentType = ContentTypes.TEXT_XML_UTF8;
105             }
106 
107             ServletResponseUtil.sendFile(res, fileName, byteArray, contentType);
108 
109             return null;
110         }
111         catch (Exception e) {
112             req.setAttribute(PageContext.EXCEPTION, e);
113 
114             return mapping.findForward(ActionConstants.COMMON_ERROR);
115         }
116     }
117 
118     protected String getTemplateId(HttpServletRequest req) {
119         String templateId = ParamUtil.getString(req, "templateId");
120 
121         // Backwards compatibility
122 
123         if (Validator.isNull(templateId)) {
124             templateId = ParamUtil.getString(req, "template_id");
125         }
126 
127         return templateId;
128     }
129 
130 }