1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class GetTemplateAction extends Action {
55  
56      public ActionForward execute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              long groupId = ParamUtil.getLong(request, "groupId");
63              String templateId = getTemplateId(request);
64  
65              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66                  WebKeys.THEME_DISPLAY);
67  
68              Map<String, String> tokens = JournalUtil.getTokens(
69                  groupId, themeDisplay);
70  
71              tokens.put("template_id", templateId);
72  
73              String languageId = LanguageUtil.getLanguageId(request);
74  
75              boolean transform = ParamUtil.get(request, "transform", true);
76  
77              JournalTemplate template =
78                  JournalTemplateLocalServiceUtil.getTemplate(
79                      groupId, templateId);
80  
81              String script = JournalUtil.getTemplateScript(
82                  template, tokens, languageId, transform);
83  
84              String extension = JournalTemplateImpl.LANG_TYPE_VM;
85  
86              if (template.getLangType() != null) {
87                  extension = template.getLangType();
88              }
89  
90              String fileName = null;
91              byte[] bytes = script.getBytes();
92  
93              String contentType = ContentTypes.TEXT_PLAIN_UTF8;
94  
95              if (Validator.equals(
96                      extension, JournalTemplateImpl.LANG_TYPE_CSS)) {
97  
98                  contentType = ContentTypes.TEXT_CSS_UTF8;
99              }
100             else if (Validator.equals(
101                     extension, JournalTemplateImpl.LANG_TYPE_XSL)) {
102 
103                 contentType = ContentTypes.TEXT_XML_UTF8;
104             }
105 
106             ServletResponseUtil.sendFile(
107                 response, fileName, bytes, contentType);
108 
109             return null;
110         }
111         catch (Exception e) {
112             PortalUtil.sendError(e, request, response);
113 
114             return null;
115         }
116     }
117 
118     protected String getTemplateId(HttpServletRequest request) {
119         String templateId = ParamUtil.getString(request, "templateId");
120 
121         // Backwards compatibility
122 
123         if (Validator.isNull(templateId)) {
124             templateId = ParamUtil.getString(request, "template_id");
125         }
126 
127         return templateId;
128     }
129 
130 }