1   /**
2    * Copyright (c) 2000-2009 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.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
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  
43  import org.apache.struts.action.Action;
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Raymond Augé
53   *
54   */
55  public class GetTemplateAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              long groupId = ParamUtil.getLong(request, "groupId");
64              String templateId = getTemplateId(request);
65  
66              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67                  WebKeys.THEME_DISPLAY);
68  
69              Map<String, String> tokens = JournalUtil.getTokens(
70                  groupId, themeDisplay);
71  
72              tokens.put("template_id", templateId);
73  
74              String languageId = LanguageUtil.getLanguageId(request);
75  
76              boolean transform = ParamUtil.get(request, "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[] bytes = 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(
108                 response, fileName, bytes, contentType);
109 
110             return null;
111         }
112         catch (Exception e) {
113             PortalUtil.sendError(e, request, response);
114 
115             return null;
116         }
117     }
118 
119     protected String getTemplateId(HttpServletRequest request) {
120         String templateId = ParamUtil.getString(request, "templateId");
121 
122         // Backwards compatibility
123 
124         if (Validator.isNull(templateId)) {
125             templateId = ParamUtil.getString(request, "template_id");
126         }
127 
128         return templateId;
129     }
130 
131 }