1   /**
2    * Copyright (c) 2000-2008 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.PortalException;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.journal.NoSuchTemplateException;
36  import com.liferay.portlet.journal.model.JournalTemplate;
37  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
38  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
39  import com.liferay.portlet.journal.util.JournalUtil;
40  import com.liferay.util.servlet.ServletResponseUtil;
41  
42  import java.util.Map;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.jsp.PageContext;
47  
48  import org.apache.struts.action.Action;
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="GetTemplateAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Raymond Augé
58   *
59   */
60  public class GetTemplateAction extends Action {
61  
62      public ActionForward execute(
63              ActionMapping mapping, ActionForm form, HttpServletRequest req,
64              HttpServletResponse res)
65          throws Exception {
66  
67          try {
68              long groupId = ParamUtil.getLong(req, "groupId");
69              String templateId = getTemplateId(req);
70  
71              ThemeDisplay themeDisplay =
72                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
73  
74              Map<String, String> tokens = JournalUtil.getTokens(
75                  groupId, themeDisplay);
76  
77              tokens.put("template_id", templateId);
78  
79              String languageId = LanguageUtil.getLanguageId(req);
80  
81              boolean transform = ParamUtil.get(req, "transform", true);
82  
83              JournalTemplate template =
84                  JournalTemplateLocalServiceUtil.getTemplate(
85                      groupId, templateId);
86  
87              String script = JournalUtil.getTemplateScript(
88                  template, tokens, languageId, transform);
89  
90              String extension = JournalTemplateImpl.LANG_TYPE_VM;
91  
92              if (template.getLangType() != null) {
93                  extension = template.getLangType();
94              }
95  
96              String fileName = null;
97              byte[] byteArray = script.getBytes();
98  
99              String contentType = ContentTypes.TEXT_PLAIN_UTF8;
100 
101             if (Validator.equals(
102                     extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
103 
104                 contentType = ContentTypes.TEXT_CSS_UTF8;
105             }
106             else if (Validator.equals(
107                     extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
108 
109                 contentType = ContentTypes.TEXT_XML_UTF8;
110             }
111 
112             ServletResponseUtil.sendFile(res, fileName, byteArray, contentType);
113 
114             return null;
115         }
116         catch (PortalException pe) {
117             if (pe instanceof PrincipalException) {
118                 PortalUtil.sendError(
119                     HttpServletResponse.SC_FORBIDDEN, pe, req, res);
120             }
121             else if (pe instanceof NoSuchTemplateException) {
122                 PortalUtil.sendError(
123                     HttpServletResponse.SC_NOT_FOUND, pe, req, res);
124             }
125 
126             return null;
127         }
128         catch (Exception e) {
129             req.setAttribute(PageContext.EXCEPTION, e);
130 
131             return mapping.findForward(ActionConstants.COMMON_ERROR);
132         }
133     }
134 
135     protected String getTemplateId(HttpServletRequest req) {
136         String templateId = ParamUtil.getString(req, "templateId");
137 
138         // Backwards compatibility
139 
140         if (Validator.isNull(templateId)) {
141             templateId = ParamUtil.getString(req, "template_id");
142         }
143 
144         return templateId;
145     }
146 
147 }